Components
Shortcut
NativeKeyboard shortcuts: a hook that listens and a label that shows the keys for this device.
Press one of the shortcuts.
Installation
npx nook-ui@latest add shortcutPrompt
Paste this into your AI coding assistant. It adds this exact component, the same way the CLI does, and tells the assistant how to use it.
Short. For assistants that can run commands in your project.
Add the Nook "Shortcut" component to this project, exactly as Nook ships it.
Keyboard shortcuts: a hook that listens and a label that shows the keys for this device.
Steps
1. Check for `nook.json` in the project root. If it is missing, run `npx nook-ui@latest init` first (React or Next.js with Tailwind CSS v4). It adds the Nook tokens, the theme and `cn()`.
2. Run `npx nook-ui@latest add shortcut`. It writes `components/ui/shortcut.tsx` together with everything the component needs. Do not write the component from memory.
3. If you cannot run commands, fetch http://localhost:3000/r/shortcut.json, write every entry of `files` to the path it names, install the packages in `dependencies`, and repeat for each name in `registryDependencies`.
4. Use it where I ask. Start from this example:
```tsx
"use client";
import * as React from "react";
import { Shortcut, useShortcut } from "@/components/ui/shortcut";
export default function ShortcutDefault() {
const [log, setLog] = React.useState("Press one of the shortcuts.");
const [dark, setDark] = React.useState(false);
useShortcut("alt+d", () => {
setDark((value) => !value);
setLog("Toggled the highlight.");
});
return (
<div className="grid w-full max-w-sm gap-3 text-sm">
<div className="flex items-center justify-between gap-4">
New task <Shortcut keys="n" onTrigger={() => setLog("New task")} />
</div>
<div className="flex items-center justify-between gap-4">
Search <Shortcut keys="mod+k" onTrigger={() => setLog("Search opened")} />
</div>
<div className="flex items-center justify-between gap-4">
Highlight <Shortcut keys="alt+d" />
</div>
<div className="flex items-center justify-between gap-4">
Shortcuts help <Shortcut keys="shift+/" onTrigger={() => setLog("Help opened")} />
</div>
<p role="status" className={dark ? "rounded-md bg-accent px-3 py-2 font-medium" : "rounded-md bg-muted px-3 py-2 text-muted-foreground"}>
{log}
</p>
</div>
);
}
```
API
- `useShortcut`: `useShortcut(keys, handler, options?)` runs the handler when the keys are pressed. Keys read like `mod+k`, `shift+/`, `alt+ArrowUp` or `Escape`; `mod` is ⌘ on Apple devices and Ctrl elsewhere.
- `enabled` (`boolean`, default true): Turn it off without removing the hook.
- `whileTyping` (`boolean`, default false): Also fire while someone is typing in a field.
- `preventDefault` (`boolean`, default true): Stop the browser's own action.
- `Shortcut`: The keys as a row of Kbd, written the way this device writes them. With `onTrigger` it also listens.
- `keys` (`string`): For example "mod+k".
- `onTrigger` (`(event) => void`): What to run. Leave out to only show the keys.
Rules
- Colors, radii, shadows and motion come from Nook tokens and classes such as `bg-primary`, `text-muted-foreground`, `border-border`, `rounded-lg` and `h-control`. Never hard-code them.
- Do not restyle or simplify the file. Change how it looks through `className`, its props and the theme.
- It must keep working with the keyboard, in light and dark mode, and in the sharp, soft and bold styles.
- Accessibility: Shortcuts without Ctrl, ⌘ or Alt are ignored while someone types in a field. Always offer the same action somewhere on screen: a shortcut is a faster way, never the only way.
Done when the component renders like the example in light and dark mode and the project type-checks.Usage
"use client";
import * as React from "react";
import { Shortcut, useShortcut } from "@/components/ui/shortcut";
export default function ShortcutDefault() {
const [log, setLog] = React.useState("Press one of the shortcuts.");
const [dark, setDark] = React.useState(false);
useShortcut("alt+d", () => {
setDark((value) => !value);
setLog("Toggled the highlight.");
});
return (
<div className="grid w-full max-w-sm gap-3 text-sm">
<div className="flex items-center justify-between gap-4">
New task <Shortcut keys="n" onTrigger={() => setLog("New task")} />
</div>
<div className="flex items-center justify-between gap-4">
Search <Shortcut keys="mod+k" onTrigger={() => setLog("Search opened")} />
</div>
<div className="flex items-center justify-between gap-4">
Highlight <Shortcut keys="alt+d" />
</div>
<div className="flex items-center justify-between gap-4">
Shortcuts help <Shortcut keys="shift+/" onTrigger={() => setLog("Help opened")} />
</div>
<p role="status" className={dark ? "rounded-md bg-accent px-3 py-2 font-medium" : "rounded-md bg-muted px-3 py-2 text-muted-foreground"}>
{log}
</p>
</div>
);
}Examples
One shortcut
Open the command menu with CtrlK
Editor shortcuts
- SaveCtrlS
- UndoCtrlZ
- RedoCtrlShiftZ
- BoldCtrlB
- Delete lineAltShiftD
Props
useShortcut
`useShortcut(keys, handler, options?)` runs the handler when the keys are pressed. Keys read like `mod+k`, `shift+/`, `alt+ArrowUp` or `Escape`; `mod` is ⌘ on Apple devices and Ctrl elsewhere.
| Prop | Type | Default | Description |
|---|---|---|---|
| enabled | boolean | true | Turn it off without removing the hook. |
| whileTyping | boolean | false | Also fire while someone is typing in a field. |
| preventDefault | boolean | true | Stop the browser's own action. |
Shortcut
The keys as a row of Kbd, written the way this device writes them. With `onTrigger` it also listens.
| Prop | Type | Default | Description |
|---|---|---|---|
| keys | string | – | For example "mod+k". |
| onTrigger | (event) => void | – | What to run. Leave out to only show the keys. |
Accessibility
Shortcuts without Ctrl, ⌘ or Alt are ignored while someone types in a field. Always offer the same action somewhere on screen: a shortcut is a faster way, never the only way.
Checked against WCAG 2.1 AA with automated axe tests and keyboard tests on every release.