Skip to Content

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-ui

Usage

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

PropTypeDefaultDescription
helpKeystring | false"?"Key that opens the overlay. false leaves opening to open.
openboolean-Controlled open state.
onOpenChange(open: boolean) => void-Called when the overlay opens or closes.
titlestring"Keyboard shortcuts"Dialog title.

useListNavigation

PropTypeDefaultDescription
idsreadonly string[]-Item ids in display order.
activeIdstring-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.
enabledbooleantrueTurn the keys off.
arrowsbooleanfalseAlso bind the down and up arrows.
groupstring"Navigation"Group name in the overlay.

ShortcutCommands

PropTypeDefaultDescription
(none)--Renders every described single-key shortcut as command palette items. Put it inside a CommandList.

useShortcutList

PropTypeDefaultDescription
returns[group, entries][]-The registrations the overlay lists, for a custom overlay.