import {
FormatForDisplayController,
HotkeyController,
HotkeyRecorderController,
hotkey,
} from '@tanstack/lit-hotkeys'
import { LitElement, css, html, unsafeCSS } from 'lit'
import { customElement, state } from 'lit/decorators.js'
import appStyles from './index.css?raw'
import type { HotkeyChord } from '@tanstack/lit-hotkeys'
@customElement('my-app')
export class MyApp extends LitElement {
static styles = [
css`
:host {
display: block;
margin: 0;
font-family: system-ui, -apple-system, sans-serif;
background: #f5f5f5;
color: #333;
box-sizing: border-box;
}
:host *,
:host *::before,
:host *::after {
box-sizing: border-box;
}
`,
unsafeCSS(appStyles),
]
private formatForDisplay = new FormatForDisplayController(this)
@state() private listStatus = 'Not recording'
@state() private editing: string | null = null
@state() private shortcuts: Array<{
name: string
hotkey: HotkeyChord | null
}> = [
{ name: 'Save', hotkey: 'Mod+S' },
{ name: 'Open', hotkey: 'Mod+O' },
{ name: 'Toggle sidebar', hotkey: 'Mod+B' },
]
private listRecorder = new HotkeyRecorderController(this, {
onRecord: (hotkey) => {
this.shortcuts = this.shortcuts.map((s) =>
s.name === this.editing ? { ...s, hotkey } : s,
)
this.listStatus = `${this.editing} is now ${hotkey}`
this.editing = null
},
onCancel: () => {
this.listStatus = `${this.editing} unchanged`
this.editing = null
},
onClear: () => {
this.shortcuts = this.shortcuts.map((s) =>
s.name === this.editing ? { ...s, hotkey: null } : s,
)
this.listStatus = `${this.editing} cleared`
this.editing = null
},
})
constructor() {
super()
for (const { name } of this.shortcuts) {
new HotkeyController(
this,
() => this.shortcuts.find((s) => s.name === name)?.hotkey,
() => (this.listStatus = `Ran ${name}`),
{ meta: { name } },
)
}
}
@state() private validationStatus = 'Not recording'
private validationRecorder = new HotkeyRecorderController(this, {
validate: (_hotkey, { parsedChord }) =>
parsedChord.modifiers.length > 0 ||
'Include a modifier, such as Mod or Alt.',
onReject: ({ reason, message }) =>
(this.validationStatus = message
? `Rejected: ${message}`
: `Rejected (${reason})`),
onRecord: (hotkey) => (this.validationStatus = `Recorded ${hotkey}`),
onCancel: () => (this.validationStatus = 'Cancelled'),
})
@state() private conflictsStatus = 'Not recording'
@hotkey('Mod+K', { meta: { name: 'Command palette' } })
openCommandPalette() {
this.conflictsStatus = 'Ran Command palette'
}
private conflictsRecorder = new HotkeyRecorderController(this, {
detectConflicts: true,
onReject: ({ reason, hotkey, conflicts }) =>
(this.conflictsStatus =
reason === 'conflict'
? `Rejected: ${hotkey} is used by ${conflicts
?.map((c) => c.registration.options.meta?.name)
.join(', ')}`
: `Rejected (${reason})`),
onRecord: (hotkey) => (this.conflictsStatus = `Recorded ${hotkey}`),
onCancel: () => (this.conflictsStatus = 'Cancelled'),
})
@state() private recordByStatus = 'Not recording'
private byTypedKey = new HotkeyRecorderController(this, {
recordBy: 'auto',
onRecord: (hotkey) => (this.recordByStatus = `Recorded ${hotkey}`),
onCancel: () => (this.recordByStatus = 'Cancelled'),
})
private byPosition = new HotkeyRecorderController(this, {
recordBy: 'code',
onRecord: (hotkey) => (this.recordByStatus = `Recorded ${hotkey}`),
onCancel: () => (this.recordByStatus = 'Cancelled'),
})
@state() private blurStatus = 'Not recording'
private cancelsOnBlur = new HotkeyRecorderController(this, {
onRecord: (hotkey) => (this.blurStatus = `Recorded ${hotkey}`),
onCancel: () => (this.blurStatus = 'Cancelled'),
})
private keepsRecording = new HotkeyRecorderController(this, {
cancelOnBlur: false,
onRecord: (hotkey) => (this.blurStatus = `Recorded ${hotkey}`),
onCancel: () => (this.blurStatus = 'Cancelled'),
})
@state() private tabStatus = 'Not recording'
private tabLeaves = new HotkeyRecorderController(this, {
onRecord: (hotkey) => (this.tabStatus = `Recorded ${hotkey}`),
onCancel: () => (this.tabStatus = 'Cancelled'),
})
private tabRecords = new HotkeyRecorderController(this, {
recordTab: true,
onRecord: (hotkey) => (this.tabStatus = `Recorded ${hotkey}`),
onCancel: () => (this.tabStatus = 'Cancelled'),
})
render() {
const formatForDisplay = this.formatForDisplay.format
return html`
<div class="app">
<header>
<h1>Hotkey Recorder</h1>
<p>
<code>HotkeyRecorderController</code> records the next key
combination the user presses. Each section shows one recorder
option.
</p>
</header>
<main>
<section class="demo-section">
<h2>Shortcut list</h2>
<p>
Click Edit, then press a key combination. Escape cancels and keeps
the old shortcut. Backspace or Delete clears it.
</p>
<ul class="shortcut-list">
${this.shortcuts.map((s) => {
const recording =
this.editing === s.name && this.listRecorder.isRecording
return html`
<li>
<span>${s.name}</span>
${
recording
? html`<span>Press keys</span>`
: s.hotkey
? html`<kbd>${formatForDisplay(s.hotkey)}</kbd>`
: html`<span>No shortcut</span>`
}
${
recording
? html`<button
type="button"
@click=${() => this.listRecorder.cancelRecording()}
>
Cancel
</button>`
: html`<button
type="button"
@click=${() => {
this.listRecorder.startRecording()
this.editing = s.name
this.listStatus = `Recording ${s.name}`
}}
>
Edit
</button>`
}
</li>
`
})}
</ul>
<p role="status" class="status">${this.listStatus}</p>
</section>
<section class="demo-section">
<h2>Validation</h2>
<p>
This recorder rejects a key without a modifier. Press
<kbd>${formatForDisplay('K')}</kbd>, then
<kbd>${formatForDisplay('Mod+Shift+K')}</kbd>.
</p>
<div class="buttons">
<button
type="button"
@click=${() => {
this.validationRecorder.startRecording()
this.validationStatus = 'Recording'
}}
>
Record
</button>
</div>
<p role="status" class="status">${this.validationStatus}</p>
</section>
<section class="demo-section">
<h2>Conflicts</h2>
<p>
This section registers <kbd>${formatForDisplay('Mod+K')}</kbd> for
a command palette. Record it, or
<kbd>${formatForDisplay('Mod+S')}</kbd> from the list above, to
see the conflict. Then record
<kbd>${formatForDisplay('Mod+J')}</kbd>.
</p>
<div class="buttons">
<button
type="button"
@click=${() => {
this.conflictsRecorder.startRecording()
this.conflictsStatus = 'Recording'
}}
>
Record
</button>
</div>
<p role="status" class="status">${this.conflictsStatus}</p>
</section>
<section class="demo-section">
<h2>Record by</h2>
<p>
Press <kbd>${formatForDisplay('Shift+[Slash]')}</kbd> with each
button to compare what gets saved.
</p>
<div class="buttons">
<button
type="button"
@click=${() => {
this.byTypedKey.startRecording()
this.recordByStatus = 'Recording'
}}
>
Record what it types
</button>
<button
type="button"
@click=${() => {
this.byPosition.startRecording()
this.recordByStatus = 'Recording'
}}
>
Record key position
</button>
</div>
<p role="status" class="status">${this.recordByStatus}</p>
</section>
<section class="demo-section">
<h2>Cancel on blur</h2>
<p>
Start recording, switch to another app or tab, then come back and
press a key combination.
</p>
<div class="buttons">
<button
type="button"
@click=${() => {
this.cancelsOnBlur.startRecording()
this.blurStatus = 'Recording'
}}
>
Record (default)
</button>
<button
type="button"
@click=${() => {
this.keepsRecording.startRecording()
this.blurStatus = 'Recording'
}}
>
Record (cancelOnBlur: false)
</button>
</div>
<p role="status" class="status">${this.blurStatus}</p>
</section>
<section class="demo-section">
<h2>Record Tab</h2>
<p>
Start recording and press <kbd>${formatForDisplay('Tab')}</kbd> or
<kbd>${formatForDisplay('Shift+Tab')}</kbd>. With recordTab, press
<kbd>${formatForDisplay('Escape')}</kbd> to cancel.
</p>
<div class="buttons">
<button
type="button"
@click=${() => {
this.tabLeaves.startRecording()
this.tabStatus = 'Recording'
}}
>
Record (default)
</button>
<button
type="button"
@click=${() => {
this.tabRecords.startRecording()
this.tabStatus = 'Recording'
}}
>
Record (recordTab: true)
</button>
</div>
<p role="status" class="status">${this.tabStatus}</p>
</section>
</main>
</div>
`
}
}
declare global {
interface HTMLElementTagNameMap {
'my-app': MyApp
}
}