|
| 1 | +/* ! |
| 2 | + * (c) Copyright 2026 Palantir Technologies Inc. All rights reserved. |
| 3 | + */ |
| 4 | + |
| 5 | +import type { Meta, StoryObj } from "@storybook/react-vite"; |
| 6 | +import { storybookLayoutDecorator } from "@storybook-common"; |
| 7 | +import { useMemo, useState } from "react"; |
| 8 | + |
| 9 | +import { Flex } from "@blueprintjs/labs"; |
| 10 | + |
| 11 | +import { type HotkeyConfig, useHotkeys, type UseHotkeysOptions } from "../../hooks"; |
| 12 | +import { Code } from "../html/html"; |
| 13 | + |
| 14 | +import { KeyComboTag } from "./keyComboTag"; |
| 15 | + |
| 16 | +interface UseHotkeysStoryProps { |
| 17 | + showDialogKeyCombo?: UseHotkeysOptions["showDialogKeyCombo"]; |
| 18 | +} |
| 19 | + |
| 20 | +function UseHotkeysStory({ showDialogKeyCombo }: UseHotkeysStoryProps) { |
| 21 | + const [lastFired, setLastFired] = useState<string>(); |
| 22 | + |
| 23 | + const hotkeys = useMemo<readonly HotkeyConfig[]>( |
| 24 | + () => [ |
| 25 | + { |
| 26 | + combo: "mod + c", |
| 27 | + global: true, |
| 28 | + group: "Edit", |
| 29 | + label: "Copy", |
| 30 | + onKeyDown: () => setLastFired("Copy"), |
| 31 | + }, |
| 32 | + { |
| 33 | + combo: "mod + v", |
| 34 | + global: true, |
| 35 | + group: "Edit", |
| 36 | + label: "Paste", |
| 37 | + onKeyDown: () => setLastFired("Paste"), |
| 38 | + }, |
| 39 | + { |
| 40 | + combo: "mod + z", |
| 41 | + global: true, |
| 42 | + group: "Edit", |
| 43 | + label: "Undo", |
| 44 | + onKeyDown: () => setLastFired("Undo"), |
| 45 | + }, |
| 46 | + { |
| 47 | + combo: "mod + shift + z", |
| 48 | + global: true, |
| 49 | + group: "Edit", |
| 50 | + label: "Redo", |
| 51 | + onKeyDown: () => setLastFired("Redo"), |
| 52 | + }, |
| 53 | + ], |
| 54 | + [], |
| 55 | + ); |
| 56 | + |
| 57 | + useHotkeys(hotkeys, { showDialogKeyCombo }); |
| 58 | + |
| 59 | + return ( |
| 60 | + <Flex flexDirection="column" gap={4} style={{ width: 320 }}> |
| 61 | + <Flex alignItems="center" gap={3}> |
| 62 | + <span>Dialog trigger:</span> |
| 63 | + {showDialogKeyCombo === false ? ( |
| 64 | + <KeyComboTag combo="(disabled)" /> |
| 65 | + ) : ( |
| 66 | + <KeyComboTag combo={showDialogKeyCombo ?? "?"} /> |
| 67 | + )} |
| 68 | + </Flex> |
| 69 | + <Flex flexDirection="column" gap={2}> |
| 70 | + {hotkeys.map(({ combo, label }) => ( |
| 71 | + <Flex key={combo} alignItems="center" gap={3}> |
| 72 | + <div style={{ minWidth: 140 }}> |
| 73 | + <KeyComboTag combo={combo} /> |
| 74 | + </div> |
| 75 | + <span>{label}</span> |
| 76 | + </Flex> |
| 77 | + ))} |
| 78 | + </Flex> |
| 79 | + <Flex alignItems="center" gap={3}> |
| 80 | + <span>Last fired:</span> |
| 81 | + <Code>{lastFired ?? "(none)"}</Code> |
| 82 | + </Flex> |
| 83 | + </Flex> |
| 84 | + ); |
| 85 | +} |
| 86 | + |
| 87 | +const meta: Meta<typeof UseHotkeysStory> = { |
| 88 | + title: "Core/Hotkeys/useHotkeys", |
| 89 | + component: UseHotkeysStory, |
| 90 | + decorators: [storybookLayoutDecorator], |
| 91 | + args: { |
| 92 | + showDialogKeyCombo: "?", |
| 93 | + }, |
| 94 | + argTypes: { |
| 95 | + showDialogKeyCombo: { |
| 96 | + description: |
| 97 | + "Key combo that opens the built-in help dialog. Pass `false` to disable the trigger entirely; the dialog can still be opened programmatically via `HotkeysContext`.", |
| 98 | + control: { type: "radio" }, |
| 99 | + options: ["?", "shift + h", "Disabled (false)"], |
| 100 | + mapping: { |
| 101 | + "?": "?", |
| 102 | + "shift + h": "shift + h", |
| 103 | + "Disabled (false)": false, |
| 104 | + }, |
| 105 | + }, |
| 106 | + }, |
| 107 | +} satisfies Meta<typeof UseHotkeysStory>; |
| 108 | + |
| 109 | +export default meta; |
| 110 | +type Story = StoryObj<typeof meta>; |
| 111 | + |
| 112 | +/** |
| 113 | + * Registers Copy, Paste, Undo, and Redo as global hotkeys. Use the |
| 114 | + * `showDialogKeyCombo` control to customize or disable the help dialog trigger. |
| 115 | + */ |
| 116 | +export const Default: Story = {}; |
0 commit comments