diff --git a/superset-frontend/src/components/Accessibility/SkipLink.test.tsx b/superset-frontend/src/components/Accessibility/SkipLink.test.tsx new file mode 100644 index 000000000000..b10c7703a487 --- /dev/null +++ b/superset-frontend/src/components/Accessibility/SkipLink.test.tsx @@ -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(); + expect( + screen.getByRole('link', { name: 'Skip to main content' }), + ).toHaveAttribute('href', '#main-content'); +}); + +test('renders custom children and targetId', () => { + render(Jump to content); + 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(); + 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(Skip); + + 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(Skip); + + 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(Skip); + + await userEvent.click(screen.getByRole('link', { name: 'Skip' })); + + expect(window.location.hash).toBe('#missing-target'); +}); diff --git a/superset-frontend/src/components/Accessibility/SkipLink.tsx b/superset-frontend/src/components/Accessibility/SkipLink.tsx new file mode 100644 index 000000000000..935d37d76cdf --- /dev/null +++ b/superset-frontend/src/components/Accessibility/SkipLink.tsx @@ -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 = ({ + targetId = 'main-content', + children = t('Skip to main content'), +}) => { + const handleClick = (e: MouseEvent) => { + 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 ( + + {children} + + ); +}; + +export default SkipLink; diff --git a/superset-frontend/src/components/Accessibility/index.ts b/superset-frontend/src/components/Accessibility/index.ts new file mode 100644 index 000000000000..9fb07415c66e --- /dev/null +++ b/superset-frontend/src/components/Accessibility/index.ts @@ -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'; diff --git a/superset-frontend/src/views/App.test.tsx b/superset-frontend/src/views/App.test.tsx index 461fc0a88af3..fac8ff3d0fd6 100644 --- a/superset-frontend/src/views/App.test.tsx +++ b/superset-frontend/src/views/App.test.tsx @@ -24,7 +24,7 @@ import { LOG_ACTIONS_SPA_NAVIGATION } from 'src/logger/LogUtils'; import { logEvent } from 'src/logger/actions'; import type { User } from 'src/types/bootstrapTypes'; import getBootstrapData from 'src/utils/getBootstrapData'; -import App, { pageScrollShellCss } from './App'; +import App, { mainContentFocusCss, pageScrollShellCss } from './App'; jest.mock('src/setup/setupApp', () => jest.fn()); jest.mock('src/setup/setupPlugins', () => jest.fn()); @@ -157,6 +157,31 @@ test('does not log spa_navigation for an anonymous user', () => { } }); +// WCAG 2.4.1 (Bypass Blocks): keyboard users need a link ahead of the +// navigation that moves focus straight to the page content. +test('renders a skip link before the content that focuses the main landmark', () => { + const { unmount } = render(); + const skipLink = screen.getByRole('link', { name: 'Skip to main content' }); + const main = screen.getByRole('main'); + + expect(main).toHaveAttribute('id', 'main-content'); + expect(skipLink).toHaveAttribute('href', '#main-content'); + expect( + skipLink.compareDocumentPosition(main) & Node.DOCUMENT_POSITION_FOLLOWING, + ).toBeTruthy(); + + fireEvent.click(skipLink); + expect(main).toHaveFocus(); + unmount(); +}); + +// Programmatic focus on the main landmark should not flash an outline around +// the whole page content. +test('the main landmark hides the outline of its programmatic focus', () => { + expect(mainContentFocusCss.styles).toMatch(/\[tabindex='-1'\]:focus/); + expect(mainContentFocusCss.styles).toMatch(/outline:\s*none/); +}); + // Locks in the page-scroll shell's CSS contract (superset#44867). #app is a // growable flex item, so the shell must take the height #app grants it. A 100vh // shell overflows #app whenever a host page injects content above it, and diff --git a/superset-frontend/src/views/App.tsx b/superset-frontend/src/views/App.tsx index 890ebec0f614..ff58435d81cb 100644 --- a/superset-frontend/src/views/App.tsx +++ b/superset-frontend/src/views/App.tsx @@ -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 // sits in a Splitter beside the chat panel; otherwise the page scrolls normally. const AppContent = ({ @@ -201,7 +212,13 @@ const AppContent = ({ const layoutContent = ( - + @@ -254,6 +271,7 @@ const AppContent = ({ return ( +