import {
FormatForDisplayController,
HeldKeyCodesController,
HeldKeysController,
HotkeyController,
HotkeyHintController,
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'
@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),
]
@state() private history: Array<string> = []
@state() private lastAction = 'none'
@state() private boldEnabled = true
private formatForDisplay = new FormatForDisplayController(this)
private heldKeys = new HeldKeysController(this)
private heldCodes = new HeldKeyCodesController(this)
private saveHint = new HotkeyHintController(this, 'Mod+S')
private boldHint = new HotkeyHintController(this, 'Mod+B')
private exportHint = new HotkeyHintController(this, 'Mod+Shift+E')
private archiveHint = new HotkeyHintController(this, 'Mod+Shift+A', {
exact: true,
})
private mostHeld: Array<string> = []
constructor() {
super()
new HotkeyController(
this,
'Mod+B',
() => {
this.lastAction = 'Bold'
},
() => ({ enabled: this.boldEnabled }),
)
}
@hotkey('Mod+S')
save() {
this.lastAction = 'Save'
}
@hotkey('Mod+Shift+E')
export() {
this.lastAction = 'Export'
}
@hotkey('Mod+Shift+A')
archive() {
this.lastAction = 'Archive'
}
override willUpdate() {
const heldKeys = this.heldKeys.value
if (heldKeys.length > this.mostHeld.length) this.mostHeld = heldKeys
if (heldKeys.length > 0 || this.mostHeld.length === 0) return
const combination = this.mostHeld.join(' + ')
this.mostHeld = []
this.history = [...this.history.slice(-9), combination]
}
render() {
const formatForDisplay = this.formatForDisplay.format
const heldKeys = this.heldKeys.value
const heldCodes = this.heldCodes.value
const shownHints = [
this.saveHint.value && 'Save',
this.boldHint.value && 'Bold',
this.exportHint.value && 'Export',
this.archiveHint.value && 'Archive',
].filter(Boolean)
return html`
<div class="app">
<header>
<h1>HeldKeysController</h1>
<p>
Track which keys the user is holding, and show a hotkey's label only
while its modifiers are held.
</p>
</header>
<main>
<section class="demo-section">
<h2>Held keys</h2>
<p>
Hold <kbd>Shift</kbd> + <kbd>Control</kbd> + <kbd>A</kbd>, or
press several letters at once, then release them one by one.
</p>
<div class="key-display" aria-hidden="true">
${heldKeys.map(
(key) =>
html`<kbd class="large">${formatForDisplay({ key })}</kbd>`,
)}
</div>
<p role="status">
${
heldKeys.length > 0
? `Held: ${heldKeys.join(' + ')}`
: 'No keys held'
}
</p>
</section>
<section class="demo-section">
<h2>Held key codes</h2>
<p>
The physical key behind each held key. Left and right
<kbd>Shift</kbd> both hold <code>Shift</code>, with different
codes.
</p>
<p role="status">
${
heldKeys.length > 0
? heldKeys
.map((key) => `${key}: ${heldCodes[key]}`)
.join(', ')
: 'No keys held'
}
</p>
</section>
<section class="demo-section">
<h2>Hotkey hints</h2>
<p>
Hold <kbd>${formatForDisplay('Mod')}</kbd>, <kbd>Shift</kbd>, or
both to see which shortcuts they lead to.
</p>
<div class="toolbar">
<button @click=${() => (this.lastAction = 'Save')}>
Save
${
this.saveHint.value
? html`<kbd>${formatForDisplay('Mod+S')}</kbd>`
: null
}
</button>
<button @click=${() => (this.lastAction = 'Bold')}>
Bold
${
this.boldHint.value
? html`<kbd>${formatForDisplay('Mod+B')}</kbd>`
: null
}
</button>
<button @click=${() => (this.lastAction = 'Export')}>
Export
${
this.exportHint.value
? html`<kbd>${formatForDisplay('Mod+Shift+E')}</kbd>`
: null
}
</button>
<button @click=${() => (this.lastAction = 'Archive')}>
Archive
${
this.archiveHint.value
? html`<kbd>${formatForDisplay('Mod+Shift+A')}</kbd>`
: null
}
</button>
</div>
<label>
<input
type="checkbox"
.checked=${this.boldEnabled}
@change=${(e: Event) =>
(this.boldEnabled = (e.target as HTMLInputElement).checked)}
/>
Enable Bold
</label>
<p>Last action: ${this.lastAction}</p>
<p role="status">
${
shownHints.length > 0
? `Hints shown: ${shownHints.join(', ')}`
: 'Hints shown: none'
}
</p>
</section>
<section class="demo-section">
<h2>Recent combinations</h2>
<p>
Each entry is the most keys you held at once, added when you
release them all.
</p>
<p role="status">
Recorded ${this.history.length}
${this.history.length === 1 ? 'combination' : 'combinations'}
</p>
<ol class="history-list">
${this.history.map((combination) => html`<li>${combination}</li>`)}
</ol>
<button @click=${() => (this.history = [])}>Clear history</button>
</section>
</main>
</div>
`
}
}
declare global {
interface HTMLElementTagNameMap {
'my-app': MyApp
}
}