@siiway/ui-react 0.0.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (258) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +19 -1
  3. package/dist/components/AppShell.d.ts +29 -0
  4. package/dist/components/AppShell.d.ts.map +1 -0
  5. package/dist/components/AppShell.js +220 -0
  6. package/dist/components/AppShell.js.map +1 -0
  7. package/dist/components/AuthShell.d.ts +21 -0
  8. package/dist/components/AuthShell.d.ts.map +1 -0
  9. package/dist/components/AuthShell.js +53 -0
  10. package/dist/components/AuthShell.js.map +1 -0
  11. package/dist/components/BrandMark.d.ts +11 -0
  12. package/dist/components/BrandMark.d.ts.map +1 -0
  13. package/dist/components/BrandMark.js +11 -0
  14. package/dist/components/BrandMark.js.map +1 -0
  15. package/dist/components/ButtonLink.d.ts +11 -0
  16. package/dist/components/ButtonLink.d.ts.map +1 -0
  17. package/dist/components/ButtonLink.js +15 -0
  18. package/dist/components/ButtonLink.js.map +1 -0
  19. package/dist/components/CheckboxField.d.ts +15 -0
  20. package/dist/components/CheckboxField.d.ts.map +1 -0
  21. package/dist/components/CheckboxField.js +23 -0
  22. package/dist/components/CheckboxField.js.map +1 -0
  23. package/dist/components/ColorPickerInput.d.ts +17 -0
  24. package/dist/components/ColorPickerInput.d.ts.map +1 -0
  25. package/dist/components/ColorPickerInput.js +97 -0
  26. package/dist/components/ColorPickerInput.js.map +1 -0
  27. package/dist/components/CommandPalette.d.ts +65 -0
  28. package/dist/components/CommandPalette.d.ts.map +1 -0
  29. package/dist/components/CommandPalette.js +269 -0
  30. package/dist/components/CommandPalette.js.map +1 -0
  31. package/dist/components/CopyButton.d.ts +27 -0
  32. package/dist/components/CopyButton.d.ts.map +1 -0
  33. package/dist/components/CopyButton.js +20 -0
  34. package/dist/components/CopyButton.js.map +1 -0
  35. package/dist/components/CopyIdTrigger.d.ts +31 -0
  36. package/dist/components/CopyIdTrigger.d.ts.map +1 -0
  37. package/dist/components/CopyIdTrigger.js +72 -0
  38. package/dist/components/CopyIdTrigger.js.map +1 -0
  39. package/dist/components/DataList.d.ts +78 -0
  40. package/dist/components/DataList.d.ts.map +1 -0
  41. package/dist/components/DataList.js +150 -0
  42. package/dist/components/DataList.js.map +1 -0
  43. package/dist/components/DetailHeader.d.ts +26 -0
  44. package/dist/components/DetailHeader.d.ts.map +1 -0
  45. package/dist/components/DetailHeader.js +49 -0
  46. package/dist/components/DetailHeader.js.map +1 -0
  47. package/dist/components/DurationInput.d.ts +25 -0
  48. package/dist/components/DurationInput.d.ts.map +1 -0
  49. package/dist/components/DurationInput.js +83 -0
  50. package/dist/components/DurationInput.js.map +1 -0
  51. package/dist/components/EmptyState.d.ts +14 -0
  52. package/dist/components/EmptyState.d.ts.map +1 -0
  53. package/dist/components/EmptyState.js +41 -0
  54. package/dist/components/EmptyState.js.map +1 -0
  55. package/dist/components/FormPage.d.ts +22 -0
  56. package/dist/components/FormPage.d.ts.map +1 -0
  57. package/dist/components/FormPage.js +65 -0
  58. package/dist/components/FormPage.js.map +1 -0
  59. package/dist/components/GetStartedPanel.d.ts +16 -0
  60. package/dist/components/GetStartedPanel.d.ts.map +1 -0
  61. package/dist/components/GetStartedPanel.js +96 -0
  62. package/dist/components/GetStartedPanel.js.map +1 -0
  63. package/dist/components/LabeledLine.d.ts +12 -0
  64. package/dist/components/LabeledLine.d.ts.map +1 -0
  65. package/dist/components/LabeledLine.js +6 -0
  66. package/dist/components/LabeledLine.js.map +1 -0
  67. package/dist/components/ListSearch.d.ts +15 -0
  68. package/dist/components/ListSearch.d.ts.map +1 -0
  69. package/dist/components/ListSearch.js +23 -0
  70. package/dist/components/ListSearch.js.map +1 -0
  71. package/dist/components/LoadingBar.d.ts +8 -0
  72. package/dist/components/LoadingBar.d.ts.map +1 -0
  73. package/dist/components/LoadingBar.js +55 -0
  74. package/dist/components/LoadingBar.js.map +1 -0
  75. package/dist/components/LoadingPage.d.ts +6 -0
  76. package/dist/components/LoadingPage.d.ts.map +1 -0
  77. package/dist/components/LoadingPage.js +25 -0
  78. package/dist/components/LoadingPage.js.map +1 -0
  79. package/dist/components/MenuPanel.d.ts +34 -0
  80. package/dist/components/MenuPanel.d.ts.map +1 -0
  81. package/dist/components/MenuPanel.js +98 -0
  82. package/dist/components/MenuPanel.js.map +1 -0
  83. package/dist/components/Outline.d.ts +23 -0
  84. package/dist/components/Outline.d.ts.map +1 -0
  85. package/dist/components/Outline.js +150 -0
  86. package/dist/components/Outline.js.map +1 -0
  87. package/dist/components/Overview.d.ts +103 -0
  88. package/dist/components/Overview.d.ts.map +1 -0
  89. package/dist/components/Overview.js +286 -0
  90. package/dist/components/Overview.js.map +1 -0
  91. package/dist/components/PageHeader.d.ts +19 -0
  92. package/dist/components/PageHeader.d.ts.map +1 -0
  93. package/dist/components/PageHeader.js +41 -0
  94. package/dist/components/PageHeader.js.map +1 -0
  95. package/dist/components/Pagination.d.ts +19 -0
  96. package/dist/components/Pagination.d.ts.map +1 -0
  97. package/dist/components/Pagination.js +47 -0
  98. package/dist/components/Pagination.js.map +1 -0
  99. package/dist/components/PasswordInput.d.ts +3 -0
  100. package/dist/components/PasswordInput.d.ts.map +1 -0
  101. package/dist/components/PasswordInput.js +29 -0
  102. package/dist/components/PasswordInput.js.map +1 -0
  103. package/dist/components/PrevNext.d.ts +16 -0
  104. package/dist/components/PrevNext.d.ts.map +1 -0
  105. package/dist/components/PrevNext.js +61 -0
  106. package/dist/components/PrevNext.js.map +1 -0
  107. package/dist/components/SearchButton.d.ts +16 -0
  108. package/dist/components/SearchButton.d.ts.map +1 -0
  109. package/dist/components/SearchButton.js +63 -0
  110. package/dist/components/SearchButton.js.map +1 -0
  111. package/dist/components/SearchField.d.ts +10 -0
  112. package/dist/components/SearchField.d.ts.map +1 -0
  113. package/dist/components/SearchField.js +67 -0
  114. package/dist/components/SearchField.js.map +1 -0
  115. package/dist/components/Segmented.d.ts +28 -0
  116. package/dist/components/Segmented.d.ts.map +1 -0
  117. package/dist/components/Segmented.js +74 -0
  118. package/dist/components/Segmented.js.map +1 -0
  119. package/dist/components/Select.d.ts +22 -0
  120. package/dist/components/Select.d.ts.map +1 -0
  121. package/dist/components/Select.js +50 -0
  122. package/dist/components/Select.js.map +1 -0
  123. package/dist/components/Settings.d.ts +103 -0
  124. package/dist/components/Settings.d.ts.map +1 -0
  125. package/dist/components/Settings.js +246 -0
  126. package/dist/components/Settings.js.map +1 -0
  127. package/dist/components/SideNav.d.ts +47 -0
  128. package/dist/components/SideNav.d.ts.map +1 -0
  129. package/dist/components/SideNav.js +125 -0
  130. package/dist/components/SideNav.js.map +1 -0
  131. package/dist/components/Skeletons.d.ts +37 -0
  132. package/dist/components/Skeletons.d.ts.map +1 -0
  133. package/dist/components/Skeletons.js +109 -0
  134. package/dist/components/Skeletons.js.map +1 -0
  135. package/dist/components/StatusPage.d.ts +49 -0
  136. package/dist/components/StatusPage.d.ts.map +1 -0
  137. package/dist/components/StatusPage.js +110 -0
  138. package/dist/components/StatusPage.js.map +1 -0
  139. package/dist/components/ThemeMenu.d.ts +13 -0
  140. package/dist/components/ThemeMenu.d.ts.map +1 -0
  141. package/dist/components/ThemeMenu.js +18 -0
  142. package/dist/components/ThemeMenu.js.map +1 -0
  143. package/dist/components/ThemeSwitch.d.ts +17 -0
  144. package/dist/components/ThemeSwitch.d.ts.map +1 -0
  145. package/dist/components/ThemeSwitch.js +25 -0
  146. package/dist/components/ThemeSwitch.js.map +1 -0
  147. package/dist/components/TopBar.d.ts +49 -0
  148. package/dist/components/TopBar.d.ts.map +1 -0
  149. package/dist/components/TopBar.js +146 -0
  150. package/dist/components/TopBar.js.map +1 -0
  151. package/dist/components/ViewBanner.d.ts +17 -0
  152. package/dist/components/ViewBanner.d.ts.map +1 -0
  153. package/dist/components/ViewBanner.js +14 -0
  154. package/dist/components/ViewBanner.js.map +1 -0
  155. package/dist/hooks/platform.d.ts +7 -0
  156. package/dist/hooks/platform.d.ts.map +1 -0
  157. package/dist/hooks/platform.js +19 -0
  158. package/dist/hooks/platform.js.map +1 -0
  159. package/dist/hooks/useConfirm.d.ts +23 -0
  160. package/dist/hooks/useConfirm.d.ts.map +1 -0
  161. package/dist/hooks/useConfirm.js +55 -0
  162. package/dist/hooks/useConfirm.js.map +1 -0
  163. package/dist/hooks/useCopy.d.ts +12 -0
  164. package/dist/hooks/useCopy.d.ts.map +1 -0
  165. package/dist/hooks/useCopy.js +39 -0
  166. package/dist/hooks/useCopy.js.map +1 -0
  167. package/dist/hooks/useDebouncedSearch.d.ts +8 -0
  168. package/dist/hooks/useDebouncedSearch.d.ts.map +1 -0
  169. package/dist/hooks/useDebouncedSearch.js +19 -0
  170. package/dist/hooks/useDebouncedSearch.js.map +1 -0
  171. package/dist/hooks/usePageClamp.d.ts +4 -0
  172. package/dist/hooks/usePageClamp.d.ts.map +1 -0
  173. package/dist/hooks/usePageClamp.js +17 -0
  174. package/dist/hooks/usePageClamp.js.map +1 -0
  175. package/dist/hooks/useSearchShortcut.d.ts +8 -0
  176. package/dist/hooks/useSearchShortcut.d.ts.map +1 -0
  177. package/dist/hooks/useSearchShortcut.js +38 -0
  178. package/dist/hooks/useSearchShortcut.js.map +1 -0
  179. package/dist/hooks/useThemeMode.d.ts +29 -0
  180. package/dist/hooks/useThemeMode.d.ts.map +1 -0
  181. package/dist/hooks/useThemeMode.js +111 -0
  182. package/dist/hooks/useThemeMode.js.map +1 -0
  183. package/dist/hooks/useToastMessage.d.ts +22 -0
  184. package/dist/hooks/useToastMessage.d.ts.map +1 -0
  185. package/dist/hooks/useToastMessage.js +26 -0
  186. package/dist/hooks/useToastMessage.js.map +1 -0
  187. package/dist/index.d.ts +50 -0
  188. package/dist/index.d.ts.map +1 -0
  189. package/dist/index.js +60 -0
  190. package/dist/index.js.map +1 -0
  191. package/dist/provider/UiProvider.d.ts +60 -0
  192. package/dist/provider/UiProvider.d.ts.map +1 -0
  193. package/dist/provider/UiProvider.js +144 -0
  194. package/dist/provider/UiProvider.js.map +1 -0
  195. package/dist/provider/canvas.d.ts +7 -0
  196. package/dist/provider/canvas.d.ts.map +1 -0
  197. package/dist/provider/canvas.js +30 -0
  198. package/dist/provider/canvas.js.map +1 -0
  199. package/dist/provider/context.d.ts +69 -0
  200. package/dist/provider/context.d.ts.map +1 -0
  201. package/dist/provider/context.js +84 -0
  202. package/dist/provider/context.js.map +1 -0
  203. package/dist/theme/createTheme.d.ts +13 -0
  204. package/dist/theme/createTheme.d.ts.map +1 -0
  205. package/dist/theme/createTheme.js +18 -0
  206. package/dist/theme/createTheme.js.map +1 -0
  207. package/package.json +42 -2
  208. package/src/components/AppShell.tsx +337 -0
  209. package/src/components/AuthShell.tsx +104 -0
  210. package/src/components/BrandMark.tsx +28 -0
  211. package/src/components/ButtonLink.tsx +40 -0
  212. package/src/components/CheckboxField.tsx +50 -0
  213. package/src/components/ColorPickerInput.tsx +190 -0
  214. package/src/components/CommandPalette.tsx +479 -0
  215. package/src/components/CopyButton.tsx +76 -0
  216. package/src/components/CopyIdTrigger.tsx +127 -0
  217. package/src/components/DataList.tsx +332 -0
  218. package/src/components/DetailHeader.tsx +108 -0
  219. package/src/components/DurationInput.tsx +133 -0
  220. package/src/components/EmptyState.tsx +67 -0
  221. package/src/components/FormPage.tsx +109 -0
  222. package/src/components/GetStartedPanel.tsx +132 -0
  223. package/src/components/LabeledLine.tsx +28 -0
  224. package/src/components/ListSearch.tsx +64 -0
  225. package/src/components/LoadingBar.tsx +81 -0
  226. package/src/components/LoadingPage.tsx +41 -0
  227. package/src/components/MenuPanel.tsx +163 -0
  228. package/src/components/Outline.tsx +208 -0
  229. package/src/components/Overview.tsx +448 -0
  230. package/src/components/PageHeader.tsx +69 -0
  231. package/src/components/Pagination.tsx +105 -0
  232. package/src/components/PasswordInput.tsx +50 -0
  233. package/src/components/PrevNext.tsx +98 -0
  234. package/src/components/SearchButton.tsx +90 -0
  235. package/src/components/SearchField.tsx +97 -0
  236. package/src/components/Segmented.tsx +136 -0
  237. package/src/components/Select.tsx +110 -0
  238. package/src/components/Settings.tsx +432 -0
  239. package/src/components/SideNav.tsx +258 -0
  240. package/src/components/Skeletons.tsx +233 -0
  241. package/src/components/StatusPage.tsx +212 -0
  242. package/src/components/ThemeMenu.tsx +51 -0
  243. package/src/components/ThemeSwitch.tsx +49 -0
  244. package/src/components/TopBar.tsx +293 -0
  245. package/src/components/ViewBanner.tsx +51 -0
  246. package/src/hooks/platform.ts +22 -0
  247. package/src/hooks/useConfirm.tsx +97 -0
  248. package/src/hooks/useCopy.ts +39 -0
  249. package/src/hooks/useDebouncedSearch.ts +19 -0
  250. package/src/hooks/usePageClamp.ts +20 -0
  251. package/src/hooks/useSearchShortcut.ts +49 -0
  252. package/src/hooks/useThemeMode.ts +168 -0
  253. package/src/hooks/useToastMessage.ts +38 -0
  254. package/src/index.ts +156 -0
  255. package/src/provider/UiProvider.tsx +256 -0
  256. package/src/provider/canvas.ts +31 -0
  257. package/src/provider/context.tsx +145 -0
  258. package/src/theme/createTheme.ts +29 -0
@@ -0,0 +1,190 @@
1
+ // A hex colour field: a swatch that opens the picker in a popover, beside a
2
+ // short text input for typing or pasting a hex value.
3
+
4
+ import {
5
+ Button,
6
+ ColorArea,
7
+ ColorPicker,
8
+ ColorSlider,
9
+ Input,
10
+ Popover,
11
+ PopoverSurface,
12
+ PopoverTrigger,
13
+ makeStyles,
14
+ mergeClasses,
15
+ tokens,
16
+ type ColorPickerProps,
17
+ } from "@fluentui/react-components";
18
+ import { useState } from "react";
19
+ import { useUiStrings } from "../provider/context.js";
20
+ import { hexToHsv, hsvToHex, normalizeHexColor } from "@siiway/ui-core";
21
+
22
+ const DEFAULT_COLOR = "#0078d4";
23
+
24
+ const useStyles = makeStyles({
25
+ root: {
26
+ display: "flex",
27
+ flexDirection: "column",
28
+ gap: "4px",
29
+ },
30
+ editor: {
31
+ display: "flex",
32
+ alignItems: "center",
33
+ gap: "8px",
34
+ },
35
+ input: { width: "180px" },
36
+ swatch: {
37
+ width: "20px",
38
+ height: "20px",
39
+ padding: 0,
40
+ flexShrink: 0,
41
+ cursor: "pointer",
42
+ borderRadius: tokens.borderRadiusSmall,
43
+ border: `1px solid ${tokens.colorNeutralStroke1}`,
44
+ ":focus-visible": {
45
+ outline: `2px solid ${tokens.colorStrokeFocus2}`,
46
+ outlineOffset: "1px",
47
+ },
48
+ },
49
+ // Empty value in a nullable field: a diagonal line through the swatch.
50
+ swatchEmpty: {
51
+ backgroundImage: `linear-gradient(to top right, transparent calc(50% - 1px), ${tokens.colorNeutralStroke1} 50%, transparent calc(50% + 1px))`,
52
+ },
53
+ picker: {
54
+ display: "flex",
55
+ flexDirection: "column",
56
+ gap: "12px",
57
+ width: "240px",
58
+ },
59
+ area: {
60
+ width: "100%",
61
+ minWidth: 0,
62
+ height: "160px",
63
+ },
64
+ error: {
65
+ fontSize: tokens.fontSizeBase200,
66
+ color: tokens.colorPaletteRedForeground1,
67
+ },
68
+ });
69
+
70
+ interface ColorPickerInputProps {
71
+ /** The colour as hex, e.g. `#0078d4`, or `null` when empty. */
72
+ value: string | null;
73
+ /** Called with the colour as lowercase `#rrggbb`: as it's picked, or when
74
+ * typed text is read (on blur or Enter). Gets `null` when a `nullable`
75
+ * field is emptied. Text that isn't a hex colour shows an error instead. */
76
+ onChange: (value: string | null) => void;
77
+ /** The field may be left empty, giving `null`. Adds a Clear button while
78
+ * it holds a colour. */
79
+ nullable?: boolean;
80
+ /** The colour the picker starts from while the field is empty. Default
81
+ * `#0078d4`. */
82
+ fallbackColor?: string;
83
+ }
84
+
85
+ export function ColorPickerInput({
86
+ value,
87
+ onChange,
88
+ nullable = false,
89
+ fallbackColor = DEFAULT_COLOR,
90
+ }: ColorPickerInputProps) {
91
+ const styles = useStyles();
92
+ const strings = useUiStrings();
93
+ const [draft, setDraft] = useState(value ?? "");
94
+ const [lastValue, setLastValue] = useState(value);
95
+ // The popover lines up with the whole field, not just the swatch.
96
+ const [anchor, setAnchor] = useState<HTMLSpanElement | null>(null);
97
+ if (value !== lastValue) {
98
+ setLastValue(value);
99
+ setDraft(value ?? "");
100
+ }
101
+ const normalized = normalizeHexColor(draft);
102
+ const pickerHex = normalized ?? normalizeHexColor(fallbackColor) ?? DEFAULT_COLOR;
103
+ const pickerColor = hexToHsv(pickerHex) ?? { h: 0, s: 0, v: 0 };
104
+ const invalid = draft !== "" && normalized === null;
105
+ const empty = nullable && normalized === null;
106
+
107
+ const handlePickerChange: ColorPickerProps["onColorChange"] = (_, data) => {
108
+ const hex = hsvToHex(data.color);
109
+ setDraft(hex);
110
+ onChange(hex);
111
+ };
112
+
113
+ const commitDraft = () => {
114
+ if (draft.trim() === "" && nullable) {
115
+ setDraft("");
116
+ onChange(null);
117
+ return;
118
+ }
119
+ const next = normalizeHexColor(draft);
120
+ if (next) {
121
+ setDraft(next);
122
+ onChange(next);
123
+ }
124
+ };
125
+
126
+ return (
127
+ <div className={styles.root}>
128
+ <div className={styles.editor}>
129
+ <Input
130
+ className={styles.input}
131
+ root={{ ref: setAnchor }}
132
+ value={draft}
133
+ aria-label={strings.hexColor}
134
+ aria-invalid={invalid}
135
+ placeholder={nullable ? strings.optionalHexColor : DEFAULT_COLOR}
136
+ contentBefore={
137
+ <Popover trapFocus positioning={{ target: anchor, position: "below", align: "end" }}>
138
+ <PopoverTrigger disableButtonEnhancement>
139
+ <button
140
+ type="button"
141
+ className={mergeClasses(styles.swatch, empty && styles.swatchEmpty)}
142
+ style={{
143
+ backgroundColor: empty ? "transparent" : (normalized ?? pickerHex),
144
+ }}
145
+ aria-label={strings.pickColor}
146
+ />
147
+ </PopoverTrigger>
148
+ <PopoverSurface>
149
+ <ColorPicker
150
+ className={styles.picker}
151
+ color={pickerColor}
152
+ onColorChange={handlePickerChange}
153
+ >
154
+ <ColorArea
155
+ className={styles.area}
156
+ aria-label={strings.colorArea}
157
+ inputX={{ "aria-label": strings.saturation }}
158
+ inputY={{ "aria-label": strings.brightness }}
159
+ />
160
+ <ColorSlider aria-label={strings.hue} input={{ "aria-label": strings.hue }} />
161
+ </ColorPicker>
162
+ </PopoverSurface>
163
+ </Popover>
164
+ }
165
+ onChange={(event) => setDraft(event.target.value)}
166
+ onBlur={commitDraft}
167
+ onKeyDown={(event) => {
168
+ if (event.key === "Enter") commitDraft();
169
+ }}
170
+ />
171
+ {nullable && normalized !== null && (
172
+ <Button
173
+ appearance="subtle"
174
+ onClick={() => {
175
+ setDraft("");
176
+ onChange(null);
177
+ }}
178
+ >
179
+ {strings.clear}
180
+ </Button>
181
+ )}
182
+ </div>
183
+ {invalid && (
184
+ <span role="alert" className={styles.error}>
185
+ {strings.invalidHexColor}
186
+ </span>
187
+ )}
188
+ </div>
189
+ );
190
+ }
@@ -0,0 +1,479 @@
1
+ // Ctrl K search, as Prism and Vellum both have it: a dialog pinned near the
2
+ // top of the screen (so the list grows downwards and the field stays put), a
3
+ // field, grouped results and the keys to use them. ↑ ↓ move, Enter opens,
4
+ // Esc closes. The app does the searching: it hears the query and gives the
5
+ // groups to show.
6
+
7
+ import {
8
+ Button,
9
+ Dialog,
10
+ DialogSurface,
11
+ Spinner,
12
+ makeStyles,
13
+ mergeClasses,
14
+ tokens,
15
+ useIsomorphicLayoutEffect,
16
+ } from "@fluentui/react-components";
17
+ import { ArrowEnterLeftRegular, DismissRegular, SearchRegular } from "@fluentui/react-icons";
18
+ import {
19
+ useEffect,
20
+ useId,
21
+ useMemo,
22
+ useRef,
23
+ useState,
24
+ type MouseEvent,
25
+ type ReactNode,
26
+ } from "react";
27
+ import { useUiStrings } from "../provider/context.js";
28
+
29
+ // Highlighted terms (<mark>) and code in titles and excerpts.
30
+ const inline = {
31
+ "& mark": {
32
+ backgroundColor: tokens.colorPaletteYellowBackground2,
33
+ color: tokens.colorPaletteYellowForeground1,
34
+ paddingInline: "3px",
35
+ borderRadius: "2px",
36
+ fontWeight: tokens.fontWeightSemibold,
37
+ },
38
+ "& code": {
39
+ fontFamily: tokens.fontFamilyMonospace,
40
+ fontSize: "0.92em",
41
+ paddingInline: "4px",
42
+ paddingBlock: "1px",
43
+ borderRadius: tokens.borderRadiusSmall,
44
+ backgroundColor: tokens.colorNeutralBackground3,
45
+ },
46
+ } as const;
47
+
48
+ const useStyles = makeStyles({
49
+ surface: {
50
+ width: "min(640px, calc(100vw - 32px))",
51
+ maxWidth: "640px",
52
+ padding: 0,
53
+ alignSelf: "flex-start",
54
+ marginTop: "12vh",
55
+ overflow: "hidden",
56
+ borderRadius: tokens.borderRadiusXLarge,
57
+ "@media (max-width: 640px)": { marginTop: "16px" },
58
+ },
59
+ inputRow: {
60
+ display: "flex",
61
+ alignItems: "center",
62
+ gap: "10px",
63
+ padding: "0 10px 0 16px",
64
+ borderBottom: `1px solid ${tokens.colorNeutralStroke2}`,
65
+ "& > svg": { fontSize: "18px", color: tokens.colorNeutralForeground3, flexShrink: 0 },
66
+ },
67
+ input: {
68
+ flex: 1,
69
+ minWidth: 0,
70
+ height: "52px",
71
+ padding: 0,
72
+ border: "none",
73
+ outline: "none",
74
+ background: "transparent",
75
+ color: tokens.colorNeutralForeground1,
76
+ fontFamily: "inherit",
77
+ fontSize: tokens.fontSizeBase400,
78
+ "::placeholder": { color: tokens.colorNeutralForeground4 },
79
+ },
80
+ list: {
81
+ maxHeight: "min(60vh, 480px)",
82
+ overflowY: "auto",
83
+ padding: "6px",
84
+ },
85
+ group: { marginBottom: "4px" },
86
+ groupHead: {
87
+ display: "flex",
88
+ alignItems: "center",
89
+ justifyContent: "space-between",
90
+ gap: "8px",
91
+ minHeight: "28px",
92
+ padding: "6px 4px 2px 10px",
93
+ fontSize: tokens.fontSizeBase200,
94
+ color: tokens.colorNeutralForeground3,
95
+ },
96
+ item: {
97
+ display: "flex",
98
+ alignItems: "center",
99
+ gap: "12px",
100
+ padding: "8px 10px",
101
+ borderRadius: tokens.borderRadiusMedium,
102
+ cursor: "pointer",
103
+ color: tokens.colorNeutralForeground1,
104
+ },
105
+ active: { backgroundColor: tokens.colorNeutralBackground1Hover },
106
+ itemIcon: {
107
+ display: "inline-flex",
108
+ flexShrink: 0,
109
+ fontSize: "18px",
110
+ color: tokens.colorNeutralForeground3,
111
+ },
112
+ itemText: {
113
+ display: "flex",
114
+ flexDirection: "column",
115
+ gap: "2px",
116
+ flex: 1,
117
+ minWidth: 0,
118
+ },
119
+ title: {
120
+ fontSize: tokens.fontSizeBase300,
121
+ overflow: "hidden",
122
+ textOverflow: "ellipsis",
123
+ whiteSpace: "nowrap",
124
+ ...inline,
125
+ },
126
+ subtitle: {
127
+ fontSize: tokens.fontSizeBase200,
128
+ color: tokens.colorNeutralForeground3,
129
+ overflow: "hidden",
130
+ textOverflow: "ellipsis",
131
+ whiteSpace: "nowrap",
132
+ },
133
+ excerpt: {
134
+ fontSize: tokens.fontSizeBase200,
135
+ lineHeight: 1.4,
136
+ color: tokens.colorNeutralForeground2,
137
+ overflow: "hidden",
138
+ display: "-webkit-box",
139
+ WebkitLineClamp: 2,
140
+ WebkitBoxOrient: "vertical",
141
+ ...inline,
142
+ },
143
+ action: { display: "inline-flex", flexShrink: 0 },
144
+ enter: {
145
+ flexShrink: 0,
146
+ fontSize: "14px",
147
+ color: tokens.colorNeutralForeground3,
148
+ visibility: "hidden",
149
+ },
150
+ enterVisible: { visibility: "visible" },
151
+ empty: {
152
+ display: "flex",
153
+ flexDirection: "column",
154
+ alignItems: "center",
155
+ gap: "8px",
156
+ padding: "28px 16px",
157
+ textAlign: "center",
158
+ color: tokens.colorNeutralForeground3,
159
+ },
160
+ footer: {
161
+ display: "flex",
162
+ alignItems: "center",
163
+ justifyContent: "space-between",
164
+ gap: "16px",
165
+ padding: "8px 16px",
166
+ borderTop: `1px solid ${tokens.colorNeutralStroke2}`,
167
+ fontSize: tokens.fontSizeBase200,
168
+ color: tokens.colorNeutralForeground3,
169
+ "@media (max-width: 640px)": { display: "none" },
170
+ },
171
+ hints: { display: "flex", flexWrap: "wrap", alignItems: "center", gap: "12px" },
172
+ hint: { display: "inline-flex", alignItems: "center", gap: "4px" },
173
+ kbd: {
174
+ display: "inline-flex",
175
+ alignItems: "center",
176
+ justifyContent: "center",
177
+ minWidth: "18px",
178
+ height: "18px",
179
+ paddingInline: "5px",
180
+ boxSizing: "border-box",
181
+ border: `1px solid ${tokens.colorNeutralStroke2}`,
182
+ borderRadius: tokens.borderRadiusSmall,
183
+ fontFamily: "inherit",
184
+ fontSize: "11px",
185
+ lineHeight: 1,
186
+ },
187
+ });
188
+
189
+ export interface CommandItem<T = unknown> {
190
+ /** Unique within the palette. */
191
+ id: string;
192
+ /** Anything of yours, handed back with the row to `onSelect`: what the row stands for. */
193
+ data?: T;
194
+ /** The row's text. Wrap matched words in `<mark>` to highlight them. */
195
+ title: ReactNode;
196
+ /** One line under the title: where the result is, what matched. */
197
+ subtitle?: ReactNode;
198
+ /** Up to two lines under that, such as a passage of the page. `<mark>` highlights here too. */
199
+ excerpt?: ReactNode;
200
+ /** Shown before the text. */
201
+ icon?: ReactNode;
202
+ /**
203
+ * A button at the row's end, such as "Remove" on a recent search. Clicking
204
+ * it doesn't open the row.
205
+ */
206
+ action?: ReactNode;
207
+ }
208
+
209
+ export interface CommandGroup<T = unknown> {
210
+ /** The group's heading. With none, the items follow the group before. */
211
+ label?: ReactNode;
212
+ /** A button beside the heading, such as "Clear" for recent searches. */
213
+ action?: ReactNode;
214
+ /** The group's rows. A group without any isn't shown. */
215
+ items: CommandItem<T>[];
216
+ }
217
+
218
+ export interface CommandPaletteProps<T = unknown> {
219
+ /** Whether the palette is open. */
220
+ open: boolean;
221
+ /** Called with the new state, as when Esc or the close button closes it. */
222
+ onOpenChange: (open: boolean) => void;
223
+ /**
224
+ * The field's text, if you hold it: clear it as you open the palette to
225
+ * start a new search. Leave it out and the palette holds it, emptying the
226
+ * field each time it opens.
227
+ */
228
+ query?: string;
229
+ /**
230
+ * Called with the field's text as it changes. Without `query`, that
231
+ * includes the empty field of each opening.
232
+ */
233
+ onQueryChange: (query: string) => void;
234
+ /**
235
+ * What to show for the query: `{ label?, action?, items }` each. `action`
236
+ * is a button beside the label, such as "Clear".
237
+ */
238
+ groups: CommandGroup<T>[];
239
+ /**
240
+ * Called on Enter, or a click on a row, with the row and its `data`. Close
241
+ * the palette here when the row leads away.
242
+ */
243
+ onSelect: (item: CommandItem<T>) => void;
244
+ /** The field's placeholder and accessible name. */
245
+ placeholder: string;
246
+ /** Shows a spinner in the field while results are on their way. */
247
+ loading?: boolean;
248
+ /** Shown when no group has items: a start screen, or "No results". */
249
+ empty?: ReactNode;
250
+ /** The footer's right side, such as a count of results. */
251
+ status?: ReactNode;
252
+ }
253
+
254
+ export function CommandPalette<T = unknown>({
255
+ open,
256
+ onOpenChange,
257
+ query,
258
+ onQueryChange,
259
+ groups,
260
+ onSelect,
261
+ placeholder,
262
+ loading,
263
+ empty,
264
+ status,
265
+ }: CommandPaletteProps<T>) {
266
+ const styles = useStyles();
267
+ const strings = useUiStrings();
268
+ const listId = useId();
269
+ const listRef = useRef<HTMLDivElement>(null);
270
+ const inputRef = useRef<HTMLInputElement>(null);
271
+
272
+ // The rows in the order they're drawn, for the arrow keys, and where each
273
+ // group's rows start among them.
274
+ const flat = useMemo(() => groups.flatMap((group) => group.items), [groups]);
275
+ const starts = useMemo(
276
+ () =>
277
+ groups.map((_, g) =>
278
+ groups.slice(0, g).reduce((sum, before) => sum + before.items.length, 0),
279
+ ),
280
+ [groups],
281
+ );
282
+ // The active row, by id: when the results change under it, the first row
283
+ // takes over, so Enter always opens what's at the top of fresh results.
284
+ const [activeId, setActiveId] = useState<string | null>(null);
285
+ const activeIndex = Math.max(
286
+ 0,
287
+ flat.findIndex((item) => item.id === activeId),
288
+ );
289
+ const active = flat[activeIndex];
290
+
291
+ // Each opening starts at the top row. Without `query`, the palette holds
292
+ // the text, and each opening starts a new search: the field empties as the
293
+ // palette opens, and the app hears of it before the palette paints, to give
294
+ // the groups for the empty text.
295
+ const controlled = query !== undefined;
296
+ const [ownQuery, setOwnQuery] = useState("");
297
+ const text = controlled ? query : ownQuery;
298
+ const [wasOpen, setWasOpen] = useState(false);
299
+ const [emptied, setEmptied] = useState(0);
300
+ if (open !== wasOpen) {
301
+ setWasOpen(open);
302
+ if (open) {
303
+ setActiveId(null);
304
+ if (!controlled) {
305
+ setOwnQuery("");
306
+ setEmptied(emptied + 1);
307
+ }
308
+ }
309
+ }
310
+ // Once per emptying: not again when Strict Mode reruns the effect, nor on
311
+ // every render, as onQueryChange may be a new function each time.
312
+ const reported = useRef(0);
313
+ useIsomorphicLayoutEffect(() => {
314
+ if (reported.current === emptied) return;
315
+ reported.current = emptied;
316
+ onQueryChange("");
317
+ }, [emptied]);
318
+
319
+ useEffect(() => {
320
+ if (!active) return;
321
+ listRef.current
322
+ ?.querySelector(`[data-index="${activeIndex}"]`)
323
+ ?.scrollIntoView({ block: "nearest" });
324
+ }, [active, activeIndex]);
325
+
326
+ const optionId = (index: number) => `${listId}-${index}`;
327
+
328
+ // The field keeps focus, as a combobox's does: a press on a row or its
329
+ // button doesn't take it, and when a button removes itself (a recent
330
+ // search), focus goes back to the field rather than to nowhere.
331
+ const keepFocus = (e: MouseEvent) => e.preventDefault();
332
+ const afterAction = (e: MouseEvent) => {
333
+ e.stopPropagation();
334
+ requestAnimationFrame(() => {
335
+ const now = document.activeElement;
336
+ if (!now || now === document.body || !now.isConnected) inputRef.current?.focus();
337
+ });
338
+ };
339
+
340
+ return (
341
+ <Dialog open={open} onOpenChange={(_, data) => onOpenChange(data.open)}>
342
+ <DialogSurface className={styles.surface} aria-label={placeholder}>
343
+ <div className={styles.inputRow}>
344
+ <SearchRegular />
345
+ <input
346
+ ref={inputRef}
347
+ autoFocus
348
+ className={styles.input}
349
+ role="combobox"
350
+ aria-expanded
351
+ aria-autocomplete="list"
352
+ aria-controls={listId}
353
+ aria-activedescendant={active ? optionId(activeIndex) : undefined}
354
+ aria-label={placeholder}
355
+ placeholder={placeholder}
356
+ autoComplete="off"
357
+ spellCheck={false}
358
+ value={text}
359
+ onChange={(e) => {
360
+ if (!controlled) setOwnQuery(e.target.value);
361
+ onQueryChange(e.target.value);
362
+ setActiveId(null);
363
+ }}
364
+ onKeyDown={(e) => {
365
+ // An input method's Enter commits its text, not a result.
366
+ if (e.nativeEvent.isComposing) return;
367
+ if (e.key === "ArrowDown") {
368
+ e.preventDefault();
369
+ const next = flat[Math.min(flat.length - 1, activeIndex + 1)];
370
+ if (next) setActiveId(next.id);
371
+ } else if (e.key === "ArrowUp") {
372
+ e.preventDefault();
373
+ const prev = flat[Math.max(0, activeIndex - 1)];
374
+ if (prev) setActiveId(prev.id);
375
+ } else if (e.key === "Enter") {
376
+ e.preventDefault();
377
+ if (active) onSelect(active);
378
+ }
379
+ }}
380
+ />
381
+ {loading && <Spinner size="extra-tiny" />}
382
+ <Button
383
+ appearance="subtle"
384
+ size="small"
385
+ icon={<DismissRegular />}
386
+ aria-label={strings.close}
387
+ onClick={() => onOpenChange(false)}
388
+ />
389
+ </div>
390
+
391
+ <div
392
+ ref={listRef}
393
+ id={listId}
394
+ role="listbox"
395
+ aria-label={placeholder}
396
+ className={styles.list}
397
+ >
398
+ {flat.length === 0
399
+ ? empty != null && <div className={styles.empty}>{empty}</div>
400
+ : groups.map((group, g) =>
401
+ group.items.length === 0 ? null : (
402
+ <div key={g} role="presentation" className={styles.group}>
403
+ {(group.label != null || group.action != null) && (
404
+ <div className={styles.groupHead} role="presentation">
405
+ <span>{group.label}</span>
406
+ {group.action != null && (
407
+ <span
408
+ className={styles.action}
409
+ onMouseDown={keepFocus}
410
+ onClick={afterAction}
411
+ >
412
+ {group.action}
413
+ </span>
414
+ )}
415
+ </div>
416
+ )}
417
+ {group.items.map((item, j) => {
418
+ const i = starts[g]! + j;
419
+ const isActive = i === activeIndex;
420
+ return (
421
+ <div
422
+ key={item.id}
423
+ id={optionId(i)}
424
+ data-index={i}
425
+ role="option"
426
+ aria-selected={isActive}
427
+ className={mergeClasses(styles.item, isActive && styles.active)}
428
+ // Movement, not hover: a pointer resting on the list
429
+ // mustn't take the row from fresh results.
430
+ onMouseMove={() => !isActive && setActiveId(item.id)}
431
+ onMouseDown={keepFocus}
432
+ onClick={() => onSelect(item)}
433
+ >
434
+ {item.icon && <span className={styles.itemIcon}>{item.icon}</span>}
435
+ <span className={styles.itemText}>
436
+ <span className={styles.title}>{item.title}</span>
437
+ {item.subtitle && (
438
+ <span className={styles.subtitle}>{item.subtitle}</span>
439
+ )}
440
+ {item.excerpt && <span className={styles.excerpt}>{item.excerpt}</span>}
441
+ </span>
442
+ {item.action && (
443
+ <span className={styles.action} onClick={afterAction}>
444
+ {item.action}
445
+ </span>
446
+ )}
447
+ <ArrowEnterLeftRegular
448
+ className={mergeClasses(styles.enter, isActive && styles.enterVisible)}
449
+ />
450
+ </div>
451
+ );
452
+ })}
453
+ </div>
454
+ ),
455
+ )}
456
+ </div>
457
+
458
+ <div className={styles.footer}>
459
+ <span className={styles.hints}>
460
+ <span className={styles.hint}>
461
+ <kbd className={styles.kbd}>↑</kbd>
462
+ <kbd className={styles.kbd}>↓</kbd>
463
+ {strings.commandMove}
464
+ </span>
465
+ <span className={styles.hint}>
466
+ <kbd className={styles.kbd}>Enter</kbd>
467
+ {strings.commandOpen}
468
+ </span>
469
+ <span className={styles.hint}>
470
+ <kbd className={styles.kbd}>Esc</kbd>
471
+ {strings.commandClose}
472
+ </span>
473
+ </span>
474
+ {status}
475
+ </div>
476
+ </DialogSurface>
477
+ </Dialog>
478
+ );
479
+ }