-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(skip-link): add SkipLink component WD-15080
- Loading branch information
Showing
6 changed files
with
105 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import React from "react"; | ||
import { Meta, StoryObj } from "@storybook/react"; | ||
|
||
import SkipLink from "./SkipLink"; | ||
|
||
const meta: Meta<typeof SkipLink> = { | ||
component: SkipLink, | ||
tags: ["autodocs"], | ||
}; | ||
|
||
export default meta; | ||
|
||
type Story = StoryObj<typeof SkipLink>; | ||
|
||
export const Default: Story = { | ||
render: () => ( | ||
<div> | ||
<SkipLink /> | ||
<p> | ||
Click inside this example box, then hit the "Tab" key to make the skip | ||
link focused and visible. | ||
</p> | ||
</div> | ||
), | ||
|
||
name: "Default", | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { render, screen } from "@testing-library/react"; | ||
import userEvent from "@testing-library/user-event"; | ||
import React from "react"; | ||
|
||
import SkipLink from "./SkipLink"; | ||
|
||
describe("<SkipLink />", () => { | ||
it("renders and is found in the DOM", () => { | ||
render(<SkipLink />); | ||
|
||
expect(screen.getByText("Skip to main content")).toBeInTheDocument(); | ||
}); | ||
|
||
it("gets focused only after a TAB press", async () => { | ||
render(<SkipLink />); | ||
|
||
const skipLink = screen.getByText("Skip to main content"); | ||
expect(skipLink).not.toHaveFocus(); | ||
|
||
await userEvent.tab(); | ||
expect(skipLink).toHaveFocus(); | ||
}); | ||
|
||
it("redirects focus to the main content", async () => { | ||
render( | ||
<div> | ||
<SkipLink mainId="main-element" /> | ||
<main id="#main-element"> | ||
<input /> | ||
</main> | ||
</div>, | ||
); | ||
|
||
const input = screen.getByRole("textbox"); | ||
expect(input).not.toHaveFocus(); | ||
|
||
await userEvent.click(screen.getByText("Skip to main content")); | ||
expect(window.location.hash).toBe("#main-element"); | ||
|
||
await userEvent.tab(); | ||
expect(input).toHaveFocus(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import React, { HTMLProps } from "react"; | ||
|
||
export type Props = { | ||
/** | ||
* Id of the main content area to skip to. | ||
*/ | ||
mainId?: string; | ||
} & HTMLProps<HTMLAnchorElement>; | ||
|
||
/** | ||
* This is a [React](https://reactjs.org/) component for the Vanilla [Skip link](https://vanillaframework.io/docs/patterns/links#skip-link) component. | ||
*/ | ||
export const SkipLink = ({ mainId = "main-content" }: Props): JSX.Element => { | ||
return ( | ||
<a className="p-link--skip" href={`#${mainId}`}> | ||
Skip to main content | ||
</a> | ||
); | ||
}; | ||
|
||
export default SkipLink; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { default } from "./SkipLink"; | ||
export type { Props as SkipLinkProps } from "./SkipLink"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters