import { TanStackDevtools } from '@tanstack/preact-devtools'
import {
HotkeysProvider,
useFormatForAria,
useFormatForDisplay,
useHotkey,
useHotkeyHint,
useHotkeyScopesState,
} from '@tanstack/preact-hotkeys'
import { hotkeysDevtoolsPlugin } from '@tanstack/preact-hotkeys-devtools'
import { render } from 'preact'
import { useRef, useState } from 'preact/hooks'
import './index.css'
function App() {
const [singleKeyShortcuts, setSingleKeyShortcuts] = useState(true)
return (
<HotkeysProvider suspendCharacterKeys={!singleKeyShortcuts}>
<div className="app">
<header>
<h1>Accessibility</h1>
<p>
Announce shortcuts to screen readers, let users turn off single-key
shortcuts, and leave Escape to an open dialog.
</p>
</header>
<main>
<AnnounceSection />
<CharacterKeysSection
singleKeyShortcuts={singleKeyShortcuts}
onSingleKeyShortcutsChange={setSingleKeyShortcuts}
/>
<TargetSection />
<HintSection />
<RightToLeftSection />
<DialogSection />
</main>
</div>
</HotkeysProvider>
)
}
function AnnounceSection() {
const formatForAria = useFormatForAria()
const formatForDisplay = useFormatForDisplay()
const [lastAction, setLastAction] = useState('none')
useHotkey('Mod+S', () => setLastAction('Save'))
useHotkey(['G', 'I'], () => setLastAction('Go to inbox'))
return (
<section className="demo-section">
<h2>Announce shortcuts to screen readers</h2>
<p>
A screen reader reads a button's shortcut from its{' '}
<code>aria-keyshortcuts</code> attribute, not from the visible label.
ARIA has no syntax for sequences, so a sequence gets no attribute.
</p>
<div className="hotkey-list">
<div className="hotkey-row">
{}
<button
onClick={() => setLastAction('Save')}
aria-keyshortcuts={formatForAria('Mod+S')}
>
Save <kbd>{formatForDisplay('Mod+S')}</kbd>
</button>
<code>aria-keyshortcuts: {formatForAria('Mod+S') ?? 'none'}</code>
</div>
<div className="hotkey-row">
{}
<button
onClick={() => setLastAction('Go to inbox')}
aria-keyshortcuts={formatForAria(['G', 'I'])}
>
Go to inbox <kbd>{formatForDisplay(['G', 'I'])}</kbd>
</button>
<code>aria-keyshortcuts: {formatForAria(['G', 'I']) ?? 'none'}</code>
</div>
</div>
<p role="status">Last action: {lastAction}</p>
</section>
)
}
function CharacterKeysSection({
singleKeyShortcuts,
onSingleKeyShortcutsChange,
}: {
singleKeyShortcuts: boolean
onSingleKeyShortcutsChange: (enabled: boolean) => void
}) {
const formatForAria = useFormatForAria()
const formatForDisplay = useFormatForDisplay()
const { characterKeysSuspended } = useHotkeyScopesState()
const [likes, setLikes] = useState(0)
const [bookmarks, setBookmarks] = useState(0)
useHotkey('L', () => setLikes((n) => n + 1))
useHotkey('Alt+[KeyB]', () => setBookmarks((n) => n + 1))
return (
<section className="demo-section">
<h2>Turn off character key shortcuts</h2>
<p>
Speech input and accidental key presses can fire single-key shortcuts.
WCAG 2.1.4 asks for a way to turn them off. Clear the checkbox, then
press both shortcuts.
</p>
<label>
<input
type="checkbox"
checked={singleKeyShortcuts}
onChange={(e) => onSingleKeyShortcutsChange(e.currentTarget.checked)}
/>{' '}
Single-key shortcuts
</label>
<div className="hotkey-list">
<div className="hotkey-row">
{}
<button
onClick={() => setLikes((n) => n + 1)}
aria-keyshortcuts={formatForAria('L')}
>
Like <kbd>{formatForDisplay('L')}</kbd>
</button>
<code>aria-keyshortcuts: {formatForAria('L') ?? 'none'}</code>
</div>
<div className="hotkey-row">
<button
onClick={() => setBookmarks((n) => n + 1)}
aria-keyshortcuts={formatForAria('Alt+[KeyB]')}
>
Bookmark <kbd>{formatForDisplay('Alt+[KeyB]')}</kbd>
</button>
<code>
aria-keyshortcuts: {formatForAria('Alt+[KeyB]') ?? 'none'}
</code>
</div>
</div>
<p role="status">
Character keys {characterKeysSuspended ? 'off' : 'on'}. Likes: {likes}.
Bookmarks: {bookmarks}
</p>
</section>
)
}
function TargetSection() {
const formatForDisplay = useFormatForDisplay()
const listRef = useRef<HTMLUListElement>(null)
const [selected, setSelected] = useState(1)
useHotkey('J', () => setSelected((n) => Math.min(n + 1, 3)), {
target: listRef,
})
useHotkey('K', () => setSelected((n) => Math.max(n - 1, 1)), {
target: listRef,
})
return (
<section className="demo-section">
<h2>Scope character keys to an element</h2>
<p>
Focus the list, then press <kbd>{formatForDisplay('J')}</kbd> and{' '}
<kbd>{formatForDisplay('K')}</kbd> to move the selection.
</p>
{}
<ul ref={listRef} tabIndex={0} aria-label="Messages" className="list">
<li className={selected === 1 ? 'selected' : undefined}>Message 1</li>
<li className={selected === 2 ? 'selected' : undefined}>Message 2</li>
<li className={selected === 3 ? 'selected' : undefined}>Message 3</li>
</ul>
<p role="status">Selected: Message {selected}</p>
</section>
)
}
function HintSection() {
const formatForAria = useFormatForAria()
const formatForDisplay = useFormatForDisplay()
const [archived, setArchived] = useState(0)
useHotkey('Alt+[KeyA]', () => setArchived((n) => n + 1))
const showHint = useHotkeyHint('Alt+[KeyA]')
return (
<section className="demo-section">
<h2>Show hints while a modifier is held</h2>
<p>
Hold <kbd>{formatForDisplay('Alt')}</kbd> to show the shortcut on the
button. The <code>aria-keyshortcuts</code> attribute stays set either
way.
</p>
<button
onClick={() => setArchived((n) => n + 1)}
aria-keyshortcuts={formatForAria('Alt+[KeyA]')}
>
Archive {showHint && <kbd>{formatForDisplay('Alt+[KeyA]')}</kbd>}
</button>
<p role="status">Archived: {archived}</p>
</section>
)
}
function RightToLeftSection() {
const formatForDisplay = useFormatForDisplay()
const [previousPage, setPreviousPage] = useState(0)
useHotkey('Alt+[BracketLeft]', () => setPreviousPage((n) => n + 1))
return (
<section className="demo-section">
<h2>Keep labels in order on right-to-left pages</h2>
<p>
Each row shows "Previous page" in Hebrew with the shortcut{' '}
<kbd>{formatForDisplay('Alt+[BracketLeft]')}</kbd>. Without a fix, the
browser reverses the label and mirrors the bracket.
</p>
<div className="hotkey-list">
<div className="hotkey-row">
<span className="caption">kbd without dir</span>
<span dir="rtl" lang="he">
הדף הקודם <kbd>{formatForDisplay('Alt+[BracketLeft]')}</kbd>
</span>
</div>
<div className="hotkey-row">
{}
<span className="caption">kbd with dir="ltr"</span>
<span dir="rtl" lang="he">
הדף הקודם{' '}
<kbd dir="ltr">{formatForDisplay('Alt+[BracketLeft]')}</kbd>
</span>
</div>
<div className="hotkey-row">
<span className="caption">String without isolate</span>
<span dir="rtl" lang="he">
{`הדף הקודם ${formatForDisplay('Alt+[BracketLeft]')}`}
</span>
</div>
<div className="hotkey-row">
{}
<span className="caption">String with isolate</span>
<span dir="rtl" lang="he">
{`הדף הקודם ${formatForDisplay('Alt+[BracketLeft]', { isolate: true })}`}
</span>
</div>
</div>
<p role="status">Previous page: {previousPage}</p>
</section>
)
}
function DialogSection() {
const dialogRef = useRef<HTMLDialogElement>(null)
const [dialogOpen, setDialogOpen] = useState(false)
const [escapes, setEscapes] = useState(0)
useHotkey('Escape', () => setEscapes((n) => n + 1))
return (
<section className="demo-section">
<h2>Let an open dialog handle Escape</h2>
<p>
Press Escape on the page, then open the dialog and press Escape again.
The dialog closes and the page hotkey stays quiet.
</p>
<button
onClick={() => {
dialogRef.current?.showModal()
setDialogOpen(true)
}}
>
Open dialog
</button>
{}
<dialog
ref={dialogRef}
aria-labelledby="dialog-title"
onClose={() => setDialogOpen(false)}
>
<h3 id="dialog-title">Dialog</h3>
<p>Press Escape to close this dialog.</p>
<button onClick={() => dialogRef.current?.close()}>Close</button>
</dialog>
<p role="status">
Dialog {dialogOpen ? 'open' : 'closed'}. Page Escape: {escapes}
</p>
</section>
)
}
const devtoolsPlugins = [hotkeysDevtoolsPlugin()]
render(
<>
<App />
<TanStackDevtools plugins={devtoolsPlugins} />
</>,
document.getElementById('root')!,
)