import { TanStackDevtools } from '@tanstack/react-devtools'
import {
HotkeysProvider,
useFormatForDisplay,
useHotkey,
useKeyboardLayout,
} from '@tanstack/react-hotkeys'
import { hotkeysDevtoolsPlugin } from '@tanstack/react-hotkeys-devtools'
import React from 'react'
import ReactDOM from 'react-dom/client'
import type { GermanKeys, UsKeys } from '@tanstack/react-hotkeys'
import './index.css'
declare module '@tanstack/react-hotkeys' {
interface Register {
keys: UsKeys | GermanKeys
}
}
const devtoolsPlugins = [hotkeysDevtoolsPlugin()]
function App() {
const formatForDisplay = useFormatForDisplay()
const layoutMap = useKeyboardLayout()
const [germanFired, setGermanFired] = React.useState('none')
const [everyLayoutFired, setEveryLayoutFired] = React.useState('none')
const [physicalFired, setPhysicalFired] = React.useState('none')
useHotkey('Mod+Ö', () => setGermanFired('Toggle comment'))
useHotkey('Mod+Shift+Ä', () => setGermanFired('Add annotation'))
useHotkey('ß', () => setGermanFired('Toggle sidebar'))
useHotkey('§', () => setGermanFired('Show sections'))
useHotkey('Mod+K', () => setEveryLayoutFired('Open command palette'))
useHotkey('Mod+Enter', () => setEveryLayoutFired('Send'))
useHotkey('Escape', () => setEveryLayoutFired('Dismiss'))
useHotkey('[KeyW]', () => setPhysicalFired('Move up'))
useHotkey('[KeyA]', () => setPhysicalFired('Move left'))
useHotkey('[KeyS]', () => setPhysicalFired('Move down'))
useHotkey('[KeyD]', () => setPhysicalFired('Move right'))
useHotkey('Mod+[KeyZ]', () => setPhysicalFired('Undo'))
return (
<div className="app">
<header>
<h1>Keyboard Layouts</h1>
<p>
Type hotkeys with keys from other layouts, match keys by position, and
label them with the characters on the user's keyboard.
</p>
</header>
<main>
<section className="demo-section">
<h2>German keys</h2>
<p className="note">
Switch your keyboard layout to German to press these.
</p>
<div className="hotkey-list">
<div className="hotkey-row">
<kbd>{formatForDisplay('Mod+Ö')}</kbd> Toggle comment
</div>
<div className="hotkey-row">
<kbd>{formatForDisplay('Mod+Shift+Ä')}</kbd> Add annotation
</div>
<div className="hotkey-row">
<kbd>{formatForDisplay('ß')}</kbd> Toggle sidebar
</div>
<div className="hotkey-row">
<kbd>{formatForDisplay('§')}</kbd> Show sections
</div>
</div>
<p role="status">Last fired: {germanFired}</p>
</section>
<section className="demo-section">
<h2>Keys on every layout</h2>
<div className="hotkey-list">
<div className="hotkey-row">
<kbd>{formatForDisplay('Mod+K')}</kbd> Open command palette
</div>
<div className="hotkey-row">
<kbd>{formatForDisplay('Mod+Enter')}</kbd> Send
</div>
<div className="hotkey-row">
<kbd>{formatForDisplay('Escape')}</kbd> Dismiss
</div>
</div>
<p role="status">Last fired: {everyLayoutFired}</p>
</section>
<section className="demo-section">
<h2>Physical keys</h2>
<p className="note">
These labels show the characters on your keyboard. On French AZERTY,
the movement keys read Z Q S D.
</p>
<div className="hotkey-list">
<div className="hotkey-row">
<kbd>{formatForDisplay('[KeyW]')}</kbd> Move up
</div>
<div className="hotkey-row">
<kbd>{formatForDisplay('[KeyA]')}</kbd> Move left
</div>
<div className="hotkey-row">
<kbd>{formatForDisplay('[KeyS]')}</kbd> Move down
</div>
<div className="hotkey-row">
<kbd>{formatForDisplay('[KeyD]')}</kbd> Move right
</div>
<div className="hotkey-row">
<kbd>{formatForDisplay('Mod+[KeyZ]')}</kbd> Undo
</div>
</div>
<p role="status">Last fired: {physicalFired}</p>
</section>
<section className="demo-section">
<h2>On-screen keyboard</h2>
<p className="note">
{layoutMap
? 'The top letter row of your keyboard layout.'
: 'No layout from the browser, so this is the US top row.'}
</p>
<p role="status">
Top row:{' '}
{[
'KeyQ',
'KeyW',
'KeyE',
'KeyR',
'KeyT',
'KeyY',
'KeyU',
'KeyI',
'KeyO',
'KeyP',
].map((code) => (
<React.Fragment key={code}>
<kbd>
{(layoutMap?.get(code) ?? code.slice(3)).toUpperCase()}
</kbd>{' '}
</React.Fragment>
))}
</p>
</section>
{}
<HotkeysProvider defaultOptions={{ keyboardLayout: false }}>
<OwnLayout />
</HotkeysProvider>
</main>
<TanStackDevtools plugins={devtoolsPlugins} />
</div>
)
}
function OwnLayout() {
const formatForDisplay = useFormatForDisplay()
const [layout, setLayout] = React.useState('us')
const layoutMap =
layout === 'fr'
? new Map([
['KeyQ', 'a'],
['KeyA', 'q'],
['KeyW', 'z'],
['KeyZ', 'w'],
])
: layout === 'de'
? new Map([
['KeyY', 'z'],
['KeyZ', 'y'],
])
: undefined
return (
<section className="demo-section">
<h2>Supply your own layout</h2>
<p className="note">
When your app stores the user's layout, pass it as{' '}
<code>layoutMap</code>. These are the physical keys from above. This
section's provider sets <code>keyboardLayout: false</code>, so US
QWERTY shows US keys whatever your keyboard is.
</p>
<label>
Keyboard layout{' '}
<select value={layout} onChange={(e) => setLayout(e.target.value)}>
<option value="us">US QWERTY</option>
<option value="fr">French AZERTY</option>
<option value="de">German QWERTZ</option>
</select>
</label>
<p role="status">
Move: {formatForDisplay('[KeyW]', { layoutMap })}{' '}
{formatForDisplay('[KeyA]', { layoutMap })}{' '}
{formatForDisplay('[KeyS]', { layoutMap })}{' '}
{formatForDisplay('[KeyD]', { layoutMap })}. Undo:{' '}
{formatForDisplay('Mod+[KeyZ]', { layoutMap })}
</p>
</section>
)
}
ReactDOM.createRoot(document.getElementById('root')!).render(
<HotkeysProvider>
<App />
</HotkeysProvider>,
)