Shortcuts
The ? overlay that lists keyboard shortcuts, plus j and k list navigation.
import { useState } from "react"
import { Button } from "@dapplelight/signet-ui/components/button"
import { ShortcutsHelp, useListNavigation } from "@dapplelight/signet-ui/components/shortcuts"
import { openOrder } from "./orders"
const orders = ["ORD-2041", "ORD-2042", "ORD-2043", "ORD-2044"]
export function Demo() {
const [open, setOpen] = useState(false)
useListNavigation({ ids: orders, onOpen: openOrder })
return (
<>
<Button variant="outline" onClick={() => setOpen(true)}>Keyboard shortcuts</Button>
<ShortcutsHelp open={open} onOpenChange={setOpen} />
</>
)
}Installation
npm i @dapplelight/signet-uiUsage
import {
ShortcutCommands,
ShortcutsHelp,
useListNavigation,
useShortcutList,
} from "@dapplelight/signet-ui/components/shortcuts"
import { useHotkey } from "@tanstack/react-hotkeys"
import { goInbox, openSearch } from "./navigation"
// Register keys with TanStack Hotkeys and describe them in `meta`.
useHotkey("Mod+K", openSearch, { meta: { name: "Search", group: "General" } })
useHotkey(["G", "I"], goInbox, { meta: { name: "Go to inbox" } })
// Mount the overlay once, near the root.
<ShortcutsHelp />Needs the optional peer @tanstack/react-hotkeys. Press ? to open the overlay when it is mounted. Shortcuts show up only when registered with a meta.name or meta.description.
Examples
List navigation
- ORD-2041
- ORD-2042
- ORD-2043
- ORD-2044
Press j and k to move, Enter to open.
import { useListNavigation } from "@dapplelight/signet-ui/components/shortcuts"
import { openOrder } from "./orders"
const orders = ["ORD-2041", "ORD-2042", "ORD-2043", "ORD-2044"]
const { activeId } = useListNavigation({
ids: orders,
onOpen: (id) => openOrder(id),
arrows: true,
})
<ul>
{orders.map((id) => (
<li key={id} aria-current={id === activeId}>{id}</li>
))}
</ul>Another help key
Press / to open the shortcuts list.
import { ShortcutsHelp } from "@dapplelight/signet-ui/components/shortcuts"
<ShortcutsHelp helpKey="/" title="Shortcuts" />API Reference
ShortcutsHelp
| Prop | Type | Default | Description |
|---|---|---|---|
| helpKey | string | false | "?" | Key that opens the overlay. false leaves opening to open. |
| open | boolean | - | Controlled open state. |
| onOpenChange | (open: boolean) => void | - | Called when the overlay opens or closes. |
| title | string | "Keyboard shortcuts" | Dialog title. |
useListNavigation
| Prop | Type | Default | Description |
|---|---|---|---|
| ids | readonly string[] | - | Item ids in display order. |
| activeId | string | - | Controlled active id. |
| onActiveChange | (id: string) => void | - | Called when j or k moves the active item. |
| onOpen | (id: string) => void | - | Called on Enter for the active item. |
| enabled | boolean | true | Turn the keys off. |
| arrows | boolean | false | Also bind the down and up arrows. |
| group | string | "Navigation" | Group name in the overlay. |
ShortcutCommands
| Prop | Type | Default | Description |
|---|---|---|---|
| (none) | - | - | Renders every described single-key shortcut as command palette items. Put it inside a CommandList. |
useShortcutList
| Prop | Type | Default | Description |
|---|---|---|---|
| returns | [group, entries][] | - | The registrations the overlay lists, for a custom overlay. |