diff --git a/.changeset/keyboard-screen-reader-support.md b/.changeset/keyboard-screen-reader-support.md new file mode 100644 index 000000000..93bcc10b1 --- /dev/null +++ b/.changeset/keyboard-screen-reader-support.md @@ -0,0 +1,6 @@ +--- +"gitbook": patch +"@gitbook/icons": patch +--- + +Add a skip link, keyboard navigation between content tabs, focusable table of contents toggles, and hide decorative icons from assistive technologies. diff --git a/packages/gitbook/src/components/DocumentView/CodeBlock/CopyCodeButton.tsx b/packages/gitbook/src/components/DocumentView/CodeBlock/CopyCodeButton.tsx index 7f4971749..1b66b2282 100644 --- a/packages/gitbook/src/components/DocumentView/CodeBlock/CopyCodeButton.tsx +++ b/packages/gitbook/src/components/DocumentView/CodeBlock/CopyCodeButton.tsx @@ -1,8 +1,7 @@ 'use client'; -import React from 'react'; - import { getCodeTextFromId } from './utils'; +import { useCopyToClipboard } from '@/components/hooks'; import { Button } from '@/components/primitives'; import { t, useLanguage } from '@/intl/client'; import { type ClassValue, tcls } from '@/lib/tailwind'; @@ -15,41 +14,30 @@ export function CopyCodeButton(props: { codeId: string; style: ClassValue }) { const { codeId, style } = props; const language = useLanguage(); - const [copied, setCopied] = React.useState(false); - - React.useEffect(() => { - if (!copied) { - return; - } - - const timeout = setTimeout(() => { - setCopied(false); - }, 1000); - - return () => { - clearTimeout(timeout); - }; - }, [copied]); + const { copied, copy } = useCopyToClipboard(); const onClick = () => { const codeText = getCodeTextFromId(codeId); if (codeText === null) { return; } - - navigator.clipboard.writeText(codeText); - - setCopied(true); + copy(codeText); }; return ( - + <> + + {/* A label swap on the focused button isn't announced, the live region is. */} + + {copied ? t(language, 'code_copied') : null} + + ); } diff --git a/packages/gitbook/src/components/DocumentView/Prompt/PromptActions.tsx b/packages/gitbook/src/components/DocumentView/Prompt/PromptActions.tsx index f37e01b49..b60296893 100644 --- a/packages/gitbook/src/components/DocumentView/Prompt/PromptActions.tsx +++ b/packages/gitbook/src/components/DocumentView/Prompt/PromptActions.tsx @@ -5,6 +5,7 @@ import React from 'react'; import { Icon } from '@gitbook/icons'; import { type PromptActionId, setPromptAction, usePromptAction } from './promptAction'; +import { useCopyToClipboard } from '@/components/hooks'; import { Button, ButtonGroup, @@ -27,25 +28,10 @@ export function PromptActions(props: { prompt: string; openInAIProviders: boolea const { prompt, openInAIProviders } = props; const language = useLanguage(); const selectedAction = usePromptAction(); - const [copied, setCopied] = React.useState(false); - - React.useEffect(() => { - if (!copied) { - return; - } - - const timeout = setTimeout(() => { - setCopied(false); - }, COPIED_MESSAGE_DURATION); - - return () => { - clearTimeout(timeout); - }; - }, [copied]); + const { copied, copy } = useCopyToClipboard(COPIED_MESSAGE_DURATION); const copyPrompt = () => { - navigator.clipboard.writeText(prompt); - setCopied(true); + copy(prompt); }; // The pick follows the visitor from site to site, so fall back to copying wherever the agent diff --git a/packages/gitbook/src/components/DocumentView/Tabs/DynamicTabs.test.ts b/packages/gitbook/src/components/DocumentView/Tabs/DynamicTabs.test.ts new file mode 100644 index 000000000..50b91c090 --- /dev/null +++ b/packages/gitbook/src/components/DocumentView/Tabs/DynamicTabs.test.ts @@ -0,0 +1,26 @@ +import { describe, expect, it } from 'bun:test'; + +import { getNextTabIndex } from './DynamicTabs'; + +describe('getNextTabIndex', () => { + it('moves with the arrow keys and wraps around', () => { + expect(getNextTabIndex('ArrowRight', 0, 3)).toBe(1); + expect(getNextTabIndex('ArrowRight', 2, 3)).toBe(0); + expect(getNextTabIndex('ArrowLeft', 0, 3)).toBe(2); + }); + + it('jumps to the first and last tab', () => { + expect(getNextTabIndex('Home', 2, 3)).toBe(0); + expect(getNextTabIndex('End', 0, 3)).toBe(2); + }); + + it('mirrors the arrows in right-to-left', () => { + expect(getNextTabIndex('ArrowLeft', 0, 3, true)).toBe(1); + expect(getNextTabIndex('ArrowRight', 0, 3, true)).toBe(2); + }); + + it('ignores other keys and an unknown tab', () => { + expect(getNextTabIndex('Enter', 0, 3)).toBeNull(); + expect(getNextTabIndex('ArrowRight', -1, 3)).toBeNull(); + }); +}); diff --git a/packages/gitbook/src/components/DocumentView/Tabs/DynamicTabs.tsx b/packages/gitbook/src/components/DocumentView/Tabs/DynamicTabs.tsx index 4aef68fe0..e898f0f54 100644 --- a/packages/gitbook/src/components/DocumentView/Tabs/DynamicTabs.tsx +++ b/packages/gitbook/src/components/DocumentView/Tabs/DynamicTabs.tsx @@ -151,10 +151,15 @@ const TabItemList = memo(function TabItemList(props: { }).filter((x) => x !== undefined), [overflowing, tabs] ); + // Roving tabindex: one tab stop for the strip, the active tab when it isn't in the overflow menu. + const visibleTabs = tabs.filter((tab) => !overflowing.has(getTabButtonId(tab.id))); + const focusableTabId = + visibleTabs.find((tab) => tab.id === activeTabId)?.id ?? visibleTabs[0]?.id ?? null; return (
; isActive: boolean; + isFocusable: boolean; isMeasuring: boolean; tab: TabsItem; onSelect: (tabId: string) => void; }) { - const { ref, tab, isActive, isMeasuring, onSelect } = props; + const { ref, tab, isActive, isFocusable, isMeasuring, onSelect } = props; return ( ) { + const current = event.target instanceof HTMLElement ? event.target : null; + if (current?.getAttribute('role') !== 'tab') { + return; + } + const tabs = Array.from( + event.currentTarget.querySelectorAll('[role="tab"]:not([disabled])') + ); + const index = tabs.indexOf(current); + const isRTL = getComputedStyle(event.currentTarget).direction === 'rtl'; + const next = getNextTabIndex(event.key, index, tabs.length, isRTL); + if (next === null) { + return; + } + event.preventDefault(); + tabs[next]?.focus(); +} + +/** + * Index of the tab to focus for a key press, or null when the key doesn't move focus. + */ +export function getNextTabIndex( + key: string, + index: number, + count: number, + isRTL = false +): number | null { + if (index < 0 || count === 0) { + return null; + } + const forward = isRTL ? 'ArrowLeft' : 'ArrowRight'; + const backward = isRTL ? 'ArrowRight' : 'ArrowLeft'; + switch (key) { + case forward: + return (index + 1) % count; + case backward: + return (index - 1 + count) % count; + case 'Home': + return 0; + case 'End': + return count - 1; + default: + return null; + } +} + /** * Get the ID for a tab button. */ diff --git a/packages/gitbook/src/components/Header/HeaderLinks.tsx b/packages/gitbook/src/components/Header/HeaderLinks.tsx index 6ef92fbaf..6da478d05 100644 --- a/packages/gitbook/src/components/Header/HeaderLinks.tsx +++ b/packages/gitbook/src/components/Header/HeaderLinks.tsx @@ -9,7 +9,7 @@ interface HeaderLinksProps { export async function HeaderLinks({ children, style }: HeaderLinksProps) { return ( -
.button+.button]:-ml-2 z-20 ml-auto flex min-w-9 shrink grow @7xl:grow-0 items-center justify-end @4xl:gap-x-6 gap-x-4', @@ -17,6 +17,6 @@ export async function HeaderLinks({ children, style }: HeaderLinksProps) { )} > {children} -
+ ); } diff --git a/packages/gitbook/src/components/PageBody/PageBody.tsx b/packages/gitbook/src/components/PageBody/PageBody.tsx index 8d4e62d2f..795ba6c85 100644 --- a/packages/gitbook/src/components/PageBody/PageBody.tsx +++ b/packages/gitbook/src/components/PageBody/PageBody.tsx @@ -16,6 +16,7 @@ import { PageCover } from './PageCover'; import { PageFooterNavigation } from './PageFooterNavigation'; import { PageHeader } from './PageHeader'; import { PreservePageLayout } from './PreservePageLayout'; +import { MAIN_CONTENT_ID } from '@/components/layout'; import { getSpaceLanguage } from '@/intl/server'; import { t } from '@/intl/translate'; import type { GitBookSiteContext } from '@/lib/context'; @@ -103,6 +104,7 @@ export async function PageBody(props: { /> ) : null}
+
diff --git a/packages/gitbook/src/components/TableOfContents/ToggleableLinkItem.tsx b/packages/gitbook/src/components/TableOfContents/ToggleableLinkItem.tsx index 4946b320e..697f9c355 100644 --- a/packages/gitbook/src/components/TableOfContents/ToggleableLinkItem.tsx +++ b/packages/gitbook/src/components/TableOfContents/ToggleableLinkItem.tsx @@ -26,6 +26,7 @@ export function ToggleableLinkItem( isActive || pathnames.some((pathname) => currentPagePath.startsWith(`${pathname}/`)); const [isOpen, setIsOpen] = React.useState(defaultIsOpen); const hasBeenToggled = useRef(false); + const linkId = React.useId(); // Update the visibility of the children if one of the descendants becomes active. React.useEffect(() => { @@ -60,10 +61,16 @@ export function ToggleableLinkItem( } return ( - + {({ descendants, toggler }) => ( <> & { + props: Pick & { isActive: boolean; onActiveClick?: () => void; } ) { - const { isActive, href, insights, children, onActiveClick } = props; + const { id, isActive, href, insights, children, onActiveClick } = props; const handleClick = (event: React.MouseEvent) => { if (isActive && onActiveClick) { @@ -104,6 +111,7 @@ function LinkItem( return ( >; + /** Id of the link the toggle belongs to, used as its accessible name. */ + labelledBy: string; children: (renderProps: { descendants: React.ReactNode; toggler: React.ReactNode; }) => React.ReactNode; }) { - const { descendants, isOpen, setIsOpen } = props; + const { descendants, isOpen, setIsOpen, labelledBy } = props; return props.children({ toggler: ( { setIsOpen((prev) => !prev); }} @@ -144,8 +155,13 @@ function DescendantsRenderer(props: { }); } -function Toggler(props: { isLinkActive: boolean; isOpen: boolean; onToggle: () => void }) { - const { isOpen, onToggle } = props; +function Toggler(props: { + isLinkActive: boolean; + isOpen: boolean; + labelledBy: string; + onToggle: () => void; +}) { + const { isOpen, labelledBy, onToggle } = props; return (