<script lang="ts">
import { TanStackDevtools } from '@tanstack/svelte-devtools'
import {
createHotkey,
createHotkeyAttachment,
getFormatForAria,
getFormatForDisplay,
getHotkeyHint,
getHotkeyScopesState,
setHotkeysContext,
} from '@tanstack/svelte-hotkeys'
import { hotkeysDevtoolsPlugin } from '@tanstack/svelte-hotkeys-devtools'
let singleKeyShortcuts = $state(true)
setHotkeysContext({
get suspendCharacterKeys() {
return !singleKeyShortcuts
},
})
const formatForAria = getFormatForAria()
const formatForDisplay = getFormatForDisplay()
let lastAction = $state('none')
createHotkey('Mod+S', () => (lastAction = 'Save'))
createHotkey(['G', 'I'], () => (lastAction = 'Go to inbox'))
const scopes = getHotkeyScopesState()
let likes = $state(0)
let bookmarks = $state(0)
createHotkey('L', () => likes++)
createHotkey('Alt+[KeyB]', () => bookmarks++)
let selected = $state(1)
const nextMessage = createHotkeyAttachment(
'J',
() => (selected = Math.min(selected + 1, 3)),
)
const previousMessage = createHotkeyAttachment(
'K',
() => (selected = Math.max(selected - 1, 1)),
)
let archived = $state(0)
createHotkey('Alt+[KeyA]', () => archived++)
const archiveHint = getHotkeyHint('Alt+[KeyA]')
let previousPage = $state(0)
createHotkey('Alt+[BracketLeft]', () => previousPage++)
let dialog = $state<HTMLDialogElement>()
let dialogOpen = $state(false)
let escapes = $state(0)
createHotkey('Escape', () => escapes++)
const devtoolsPlugins = [hotkeysDevtoolsPlugin()]
</script>
<div class="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>
<section class="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 class="hotkey-list">
<div class="hotkey-row">
<button
onclick={() => (lastAction = 'Save')}
aria-keyshortcuts={formatForAria('Mod+S')}
>
Save <kbd>{formatForDisplay('Mod+S')}</kbd>
</button>
<code>aria-keyshortcuts: {formatForAria('Mod+S') ?? 'none'}</code>
</div>
<div class="hotkey-row">
<button
onclick={() => (lastAction = '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>
<section class="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" bind:checked={singleKeyShortcuts} />
Single-key shortcuts
</label>
<div class="hotkey-list">
<div class="hotkey-row">
<button
onclick={() => likes++}
aria-keyshortcuts={formatForAria('L')}
>
Like <kbd>{formatForDisplay('L')}</kbd>
</button>
<code>aria-keyshortcuts: {formatForAria('L') ?? 'none'}</code>
</div>
<div class="hotkey-row">
<button
onclick={() => bookmarks++}
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 ${scopes.state.characterKeysSuspended ? 'off' : 'on'}. Likes: ${likes}. Bookmarks: ${bookmarks}`}
</p>
</section>
<section class="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
tabindex="0"
aria-label="Messages"
class="list"
{@attach nextMessage}
{@attach previousMessage}
>
<li class={selected === 1 ? 'selected' : undefined}>Message 1</li>
<li class={selected === 2 ? 'selected' : undefined}>Message 2</li>
<li class={selected === 3 ? 'selected' : undefined}>Message 3</li>
</ul>
<p role="status">Selected: Message {selected}</p>
</section>
<section class="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={() => archived++}
aria-keyshortcuts={formatForAria('Alt+[KeyA]')}
>
Archive {#if archiveHint.visible}<kbd
>{formatForDisplay('Alt+[KeyA]')}</kbd
>{/if}
</button>
<p role="status">Archived: {archived}</p>
</section>
<section class="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 class="hotkey-list">
<div class="hotkey-row">
<span class="caption">kbd without dir</span>
<span dir="rtl" lang="he"
>הדף הקודם <kbd>{formatForDisplay('Alt+[BracketLeft]')}</kbd></span
>
</div>
<div class="hotkey-row">
<span class="caption">kbd with dir="ltr"</span>
<span dir="rtl" lang="he"
>הדף הקודם <kbd dir="ltr"
>{formatForDisplay('Alt+[BracketLeft]')}</kbd
></span
>
</div>
<div class="hotkey-row">
<span class="caption">String without isolate</span>
<span dir="rtl" lang="he"
>{`הדף הקודם ${formatForDisplay('Alt+[BracketLeft]')}`}</span
>
</div>
<div class="hotkey-row">
<span class="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>
<section class="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={() => {
dialog?.showModal()
dialogOpen = true
}}
>
Open dialog
</button>
<dialog
bind:this={dialog}
aria-labelledby="dialog-title"
onclose={() => (dialogOpen = false)}
>
<h3 id="dialog-title">Dialog</h3>
<p>Press Escape to close this dialog.</p>
<button onclick={() => dialog?.close()}>Close</button>
</dialog>
<p role="status">
Dialog {dialogOpen ? 'open' : 'closed'}. Page Escape: {escapes}
</p>
</section>
</main>
</div>
{#if import.meta.env.DEV}
<TanStackDevtools plugins={devtoolsPlugins} />
{/if}