-
Notifications
You must be signed in to change notification settings - Fork 59
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
80 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,16 @@ | ||
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 = { | ||
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 /> | ||
<main id="#main-content"> | ||
<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-content"); | ||
|
||
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,14 @@ | ||
import React from "react"; | ||
|
||
/** | ||
* 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 = (): JSX.Element => { | ||
return ( | ||
<a className="p-link--skip" href="#main-content"> | ||
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 @@ | ||
export { default } 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