Repository navigation
fix(a11y): add skip link to bypass the navigation #46452
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sadpandajoe
wants to merge
3
commits into
master
Choose a base branch
from
ultraset-skip-link-keyboard-bypass-blocks
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+249
−2
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
93 changes: 93 additions & 0 deletions
93
superset-frontend/src/components/Accessibility/SkipLink.test.tsx
This file contains hidden or 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,93 @@ | ||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| import userEvent from '@testing-library/user-event'; | ||
| import { render, screen } from 'spec/helpers/testing-library'; | ||
| import SkipLink from './SkipLink'; | ||
|
|
||
| const targets: HTMLElement[] = []; | ||
|
|
||
| const createTarget = (id: string) => { | ||
| const target = document.createElement('div'); | ||
| target.id = id; | ||
| document.body.appendChild(target); | ||
| targets.push(target); | ||
| return target; | ||
| }; | ||
|
|
||
| afterEach(() => { | ||
| targets.splice(0).forEach(target => target.remove()); | ||
| window.location.hash = ''; | ||
| }); | ||
|
|
||
| test('renders the default label pointing at #main-content', () => { | ||
| render(<SkipLink />); | ||
| expect( | ||
| screen.getByRole('link', { name: 'Skip to main content' }), | ||
| ).toHaveAttribute('href', '#main-content'); | ||
| }); | ||
|
|
||
| test('renders custom children and targetId', () => { | ||
| render(<SkipLink targetId="my-target">Jump to content</SkipLink>); | ||
| expect(screen.getByRole('link', { name: 'Jump to content' })).toHaveAttribute( | ||
| 'href', | ||
| '#my-target', | ||
| ); | ||
| }); | ||
|
|
||
| // A fixed negative offset leaves part of the link visible once larger text | ||
| // makes it taller than the offset; translating by its own height does not. | ||
| test('hides the link by its own height until it is focused', () => { | ||
| render(<SkipLink />); | ||
| const link = screen.getByRole('link', { name: 'Skip to main content' }); | ||
| expect(link).toHaveStyle({ transform: 'translateY(-100%)' }); | ||
| expect(link).not.toHaveStyle({ top: '-100px' }); | ||
| }); | ||
|
|
||
| test('moves focus to the target and removes the temporary tabindex on blur', async () => { | ||
| const target = createTarget('target-area'); | ||
| render(<SkipLink targetId="target-area">Skip</SkipLink>); | ||
|
|
||
| await userEvent.click(screen.getByRole('link', { name: 'Skip' })); | ||
|
|
||
| expect(target).toHaveFocus(); | ||
| expect(target).toHaveAttribute('tabindex', '-1'); | ||
|
|
||
| target.blur(); | ||
| expect(target).not.toHaveAttribute('tabindex'); | ||
| }); | ||
|
|
||
| test('keeps a tabindex the target already had', async () => { | ||
| const target = createTarget('target-area'); | ||
| target.setAttribute('tabindex', '0'); | ||
| render(<SkipLink targetId="target-area">Skip</SkipLink>); | ||
|
|
||
| await userEvent.click(screen.getByRole('link', { name: 'Skip' })); | ||
| expect(target).toHaveFocus(); | ||
|
|
||
| target.blur(); | ||
| expect(target).toHaveAttribute('tabindex', '0'); | ||
| }); | ||
|
|
||
| test('falls back to the URL hash when the target is missing', async () => { | ||
| render(<SkipLink targetId="missing-target">Skip</SkipLink>); | ||
|
|
||
| await userEvent.click(screen.getByRole('link', { name: 'Skip' })); | ||
|
|
||
| expect(window.location.hash).toBe('#missing-target'); | ||
| }); |
89 changes: 89 additions & 0 deletions
89
superset-frontend/src/components/Accessibility/SkipLink.tsx
This file contains hidden or 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,89 @@ | ||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| import { FC, MouseEvent, ReactNode } from 'react'; | ||
| import { styled } from '@apache-superset/core/theme'; | ||
| import { t } from '@apache-superset/core/translation'; | ||
|
|
||
| /** | ||
| * SkipLink - WCAG 2.4.1 Bypass Blocks. | ||
| * Lets keyboard users skip the navigation and jump to the main content. | ||
| * The link sits just above the viewport and moves into view when focused. | ||
| */ | ||
| const StyledSkipLink = styled.a` | ||
| position: absolute; | ||
| top: 0; | ||
| left: 0; | ||
| z-index: 10000; | ||
| /* Translating by the link's own height keeps it fully off-screen at any | ||
| text size, unlike a fixed negative offset. */ | ||
| transform: translateY(-100%); | ||
| padding: ${({ theme }) => theme.sizeUnit * 3}px | ||
| ${({ theme }) => theme.sizeUnit * 4}px; | ||
| background: ${({ theme }) => theme.colorPrimary}; | ||
| color: ${({ theme }) => theme.colorTextLightSolid}; | ||
| font-size: ${({ theme }) => theme.fontSizeSM}px; | ||
| font-weight: ${({ theme }) => theme.fontWeightStrong}; | ||
| text-decoration: none; | ||
| border-radius: 0 0 ${({ theme }) => theme.borderRadius}px | ||
| ${({ theme }) => theme.borderRadius}px; | ||
|
|
||
| &:focus, | ||
| &:focus-visible { | ||
| transform: none; | ||
| outline: 3px solid ${({ theme }) => theme.colorPrimaryBorderHover}; | ||
| outline-offset: 2px; | ||
| } | ||
| `; | ||
|
|
||
| interface SkipLinkProps { | ||
| targetId?: string; | ||
| children?: ReactNode; | ||
| } | ||
|
|
||
| const SkipLink: FC<SkipLinkProps> = ({ | ||
| targetId = 'main-content', | ||
| children = t('Skip to main content'), | ||
| }) => { | ||
| const handleClick = (e: MouseEvent<HTMLAnchorElement>) => { | ||
| e.preventDefault(); | ||
| const el = document.getElementById(targetId); | ||
| if (!el) { | ||
| window.location.hash = `#${targetId}`; | ||
| return; | ||
| } | ||
| // tabindex="-1" makes the target programmatically focusable without | ||
| // adding it to the tab order; it is removed on blur to restore the | ||
| // element's original markup. | ||
| if (!el.hasAttribute('tabindex')) { | ||
| el.setAttribute('tabindex', '-1'); | ||
| el.addEventListener('blur', () => el.removeAttribute('tabindex'), { | ||
| once: true, | ||
| }); | ||
| } | ||
| el.focus(); | ||
| }; | ||
|
|
||
| return ( | ||
| <StyledSkipLink href={`#${targetId}`} onClick={handleClick}> | ||
| {children} | ||
| </StyledSkipLink> | ||
| ); | ||
| }; | ||
|
|
||
| export default SkipLink; |
This file contains hidden or 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,19 @@ | ||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| export { default as SkipLink } from './SkipLink'; |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -35,6 +35,7 @@ import { css, useTheme } from '@apache-superset/core/theme'; | |
| import { Flex, Layout, Loading, Splitter } from '@superset-ui/core/components'; | ||
| import { setupAGGridModules } from '@superset-ui/core/components/ThemedAgGridReact'; | ||
| import { ErrorBoundary } from 'src/components'; | ||
| import { SkipLink } from 'src/components/Accessibility'; | ||
| import MobileRouteGuard from 'src/components/MobileRouteGuard'; | ||
| import MenuWrapper from 'src/features/home/Menu'; | ||
| import getBootstrapData, { applicationRoot } from 'src/utils/getBootstrapData'; | ||
|
|
@@ -170,6 +171,16 @@ const pageScrollContentCss = css` | |
| flex-direction: column; | ||
| `; | ||
|
|
||
| // The skip link focuses the main landmark programmatically; that focus has no | ||
| // visible control behind it, so it should not paint an outline. Exported for | ||
| // App.test.tsx, like pageScrollShellCss. | ||
| export const mainContentFocusCss = css` | ||
| &[tabindex='-1']:focus, | ||
| &[tabindex='-1']:focus-visible { | ||
| outline: none; | ||
| } | ||
| `; | ||
|
|
||
| // Renders the app shell and picks the scroll model: in chat panel mode <Layout> | ||
| // sits in a Splitter beside the chat panel; otherwise the page scrolls normally. | ||
| const AppContent = ({ | ||
|
|
@@ -201,7 +212,13 @@ const AppContent = ({ | |
|
|
||
| const layoutContent = ( | ||
| <Layout css={isPanelOpen ? layoutCss : undefined}> | ||
| <Layout.Content css={isPanelOpen ? contentCss : pageScrollContentCss}> | ||
| <Layout.Content | ||
| id="main-content" | ||
| css={[ | ||
| isPanelOpen ? contentCss : pageScrollContentCss, | ||
| mainContentFocusCss, | ||
| ]} | ||
| > | ||
| <OutPortal node={layoutPortalNode} /> | ||
| </Layout.Content> | ||
| </Layout> | ||
|
|
@@ -254,6 +271,7 @@ const AppContent = ({ | |
|
|
||
| return ( | ||
| <Flex vertical css={isPanelOpen ? lockedShellCss : pageScrollShellCss}> | ||
| <SkipLink /> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Question, not a blocker: |
||
| <MenuWrapper | ||
| data={bootstrapData.common.menu_data} | ||
| isFrontendRoute={isFrontendRoute} | ||
|
|
||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: When the main region is already visible, activating the link hides the focused link and leaves the focused landmark without a visible focus cue.
Assessment: 🟠
Major· 🔁Occurrence: Sometimes· 🏷️Possible bugPrompt for AI Agent 🤖