@robert-stackflow/oneui 0.0.0-stage → 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 (380) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/README.md +72 -2
  3. package/assets/LUCIDE-LICENSE.txt +43 -0
  4. package/assets/character-metadata.json +1 -0
  5. package/assets/emoji.json +1 -0
  6. package/assets/icon-catalog.json +1 -0
  7. package/assets/symbols.json +1 -0
  8. package/components/Accessibility.module.css +8 -0
  9. package/components/Accordion.d.ts +7 -0
  10. package/components/Accordion.js +41 -0
  11. package/components/Accordion.module.css +38 -0
  12. package/components/Alert.d.ts +9 -0
  13. package/components/Alert.js +42 -0
  14. package/components/Alert.module.css +56 -0
  15. package/components/AppShell.d.ts +30 -0
  16. package/components/AppShell.js +152 -0
  17. package/components/AppShell.module.css +239 -0
  18. package/components/AppearanceSettings.d.ts +7 -0
  19. package/components/AppearanceSettings.js +201 -0
  20. package/components/AppearanceSettings.module.css +32 -0
  21. package/components/Avatar.d.ts +14 -0
  22. package/components/Avatar.js +69 -0
  23. package/components/Avatar.module.css +70 -0
  24. package/components/AvatarGroup.d.ts +15 -0
  25. package/components/AvatarGroup.js +115 -0
  26. package/components/AvatarGroup.module.css +54 -0
  27. package/components/Badge.d.ts +5 -0
  28. package/components/Badge.js +11 -0
  29. package/components/Badge.module.css +30 -0
  30. package/components/BlockedTab.d.ts +4 -0
  31. package/components/BlockedTab.js +10 -0
  32. package/components/Breadcrumb.d.ts +16 -0
  33. package/components/Breadcrumb.js +157 -0
  34. package/components/Breadcrumb.module.css +97 -0
  35. package/components/Button.d.ts +13 -0
  36. package/components/Button.js +52 -0
  37. package/components/Button.module.css +88 -0
  38. package/components/Calendar.d.ts +10 -0
  39. package/components/Calendar.js +260 -0
  40. package/components/Calendar.module.css +106 -0
  41. package/components/Card.d.ts +8 -0
  42. package/components/Card.js +29 -0
  43. package/components/Card.module.css +47 -0
  44. package/components/Cascader.d.ts +14 -0
  45. package/components/Cascader.js +296 -0
  46. package/components/CatalogIcon.d.ts +3 -0
  47. package/components/CatalogIcon.js +11 -0
  48. package/components/CharacterPicker.d.ts +15 -0
  49. package/components/CharacterPicker.js +200 -0
  50. package/components/Checkbox.d.ts +12 -0
  51. package/components/Checkbox.js +82 -0
  52. package/components/Checkbox.module.css +95 -0
  53. package/components/ColorField.d.ts +6 -0
  54. package/components/ColorField.js +61 -0
  55. package/components/ColorField.module.css +15 -0
  56. package/components/ColorPicker.d.ts +5 -0
  57. package/components/ColorPicker.js +188 -0
  58. package/components/ColorPicker.module.css +90 -0
  59. package/components/Combobox.d.ts +13 -0
  60. package/components/Combobox.js +338 -0
  61. package/components/Combobox.module.css +24 -0
  62. package/components/CommandPalette.d.ts +25 -0
  63. package/components/CommandPalette.js +269 -0
  64. package/components/CommandPalette.module.css +49 -0
  65. package/components/ContextMenu.d.ts +9 -0
  66. package/components/ContextMenu.js +363 -0
  67. package/components/ContextMenu.module.css +42 -0
  68. package/components/ContextMenuPanel.d.ts +22 -0
  69. package/components/ContextMenuPanel.js +155 -0
  70. package/components/ContributionCalendar.d.ts +18 -0
  71. package/components/ContributionCalendar.js +201 -0
  72. package/components/ContributionCalendar.module.css +114 -0
  73. package/components/ContributionCell.d.ts +11 -0
  74. package/components/ContributionCell.js +41 -0
  75. package/components/Control.module.css +86 -0
  76. package/components/DataTable.d.ts +49 -0
  77. package/components/DataTable.js +220 -0
  78. package/components/DataTable.module.css +112 -0
  79. package/components/DatePicker.d.ts +15 -0
  80. package/components/DatePicker.js +186 -0
  81. package/components/DatePicker.module.css +85 -0
  82. package/components/DateRangePicker.d.ts +9 -0
  83. package/components/DateRangePicker.js +168 -0
  84. package/components/Descriptions.d.ts +21 -0
  85. package/components/Descriptions.js +162 -0
  86. package/components/Descriptions.module.css +117 -0
  87. package/components/Dialog.d.ts +14 -0
  88. package/components/Dialog.js +125 -0
  89. package/components/Dialog.module.css +88 -0
  90. package/components/DialogVariants.d.ts +27 -0
  91. package/components/DialogVariants.js +161 -0
  92. package/components/DialogVariants.module.css +29 -0
  93. package/components/DisclosureChevron.d.ts +3 -0
  94. package/components/DisclosureChevron.js +17 -0
  95. package/components/DisclosureChevron.module.css +17 -0
  96. package/components/Divider.d.ts +5 -0
  97. package/components/Divider.js +24 -0
  98. package/components/Divider.module.css +19 -0
  99. package/components/Drawer.d.ts +18 -0
  100. package/components/Drawer.js +145 -0
  101. package/components/Drawer.module.css +109 -0
  102. package/components/EmojiPicker.d.ts +6 -0
  103. package/components/EmojiPicker.js +19 -0
  104. package/components/EmptyState.d.ts +8 -0
  105. package/components/EmptyState.js +20 -0
  106. package/components/EmptyState.module.css +30 -0
  107. package/components/FieldLabel.d.ts +8 -0
  108. package/components/FieldLabel.js +20 -0
  109. package/components/FieldMessages.d.ts +5 -0
  110. package/components/FieldMessages.js +15 -0
  111. package/components/FileDropOverlay.d.ts +5 -0
  112. package/components/FileDropOverlay.js +26 -0
  113. package/components/FileDropOverlay.module.css +55 -0
  114. package/components/FileDropZone.d.ts +15 -0
  115. package/components/FileDropZone.js +113 -0
  116. package/components/FileDropZone.module.css +6 -0
  117. package/components/FilePicker.d.ts +10 -0
  118. package/components/FilePicker.js +59 -0
  119. package/components/FileSelectionFeedback.d.ts +5 -0
  120. package/components/FileSelectionFeedback.js +23 -0
  121. package/components/FileSelectionFeedback.module.css +14 -0
  122. package/components/FileTree.d.ts +13 -0
  123. package/components/FileTree.js +125 -0
  124. package/components/FileTree.module.css +66 -0
  125. package/components/Form.d.ts +7 -0
  126. package/components/Form.js +26 -0
  127. package/components/Form.module.css +7 -0
  128. package/components/FormErrorSummary.d.ts +6 -0
  129. package/components/FormErrorSummary.js +27 -0
  130. package/components/FormErrorSummary.module.css +27 -0
  131. package/components/FormField.d.ts +9 -0
  132. package/components/FormField.js +59 -0
  133. package/components/FormField.module.css +85 -0
  134. package/components/FormGroup.d.ts +9 -0
  135. package/components/FormGroup.js +32 -0
  136. package/components/HierarchyPicker.module.css +181 -0
  137. package/components/IconButton.d.ts +7 -0
  138. package/components/IconButton.js +30 -0
  139. package/components/IconButton.module.css +42 -0
  140. package/components/IconPicker.d.ts +6 -0
  141. package/components/IconPicker.js +111 -0
  142. package/components/Indicator.d.ts +18 -0
  143. package/components/Indicator.js +97 -0
  144. package/components/Indicator.module.css +83 -0
  145. package/components/Input.d.ts +8 -0
  146. package/components/Input.js +37 -0
  147. package/components/Input.module.css +40 -0
  148. package/components/LoadingOverlay.d.ts +6 -0
  149. package/components/LoadingOverlay.js +16 -0
  150. package/components/LoadingOverlay.module.css +22 -0
  151. package/components/LoadingState.d.ts +7 -0
  152. package/components/LoadingState.js +40 -0
  153. package/components/LoadingState.module.css +17 -0
  154. package/components/Menu.d.ts +11 -0
  155. package/components/Menu.js +82 -0
  156. package/components/MenuItem.d.ts +15 -0
  157. package/components/MenuItem.js +51 -0
  158. package/components/MenuItem.module.css +172 -0
  159. package/components/MenuItemContent.d.ts +8 -0
  160. package/components/MenuItemContent.js +19 -0
  161. package/components/MultiSelect.d.ts +13 -0
  162. package/components/MultiSelect.js +225 -0
  163. package/components/MultiSelect.module.css +99 -0
  164. package/components/NumberInput.d.ts +12 -0
  165. package/components/NumberInput.js +114 -0
  166. package/components/NumberInput.module.css +33 -0
  167. package/components/OptionList.module.css +12 -0
  168. package/components/Page.d.ts +7 -0
  169. package/components/Page.js +22 -0
  170. package/components/Page.module.css +44 -0
  171. package/components/Pagination.d.ts +11 -0
  172. package/components/Pagination.js +160 -0
  173. package/components/Pagination.module.css +70 -0
  174. package/components/PasswordField.d.ts +9 -0
  175. package/components/PasswordField.js +102 -0
  176. package/components/PasswordField.module.css +64 -0
  177. package/components/Picker.module.css +52 -0
  178. package/components/PinInput.d.ts +13 -0
  179. package/components/PinInput.js +173 -0
  180. package/components/PinInput.module.css +72 -0
  181. package/components/Placeholder.d.ts +31 -0
  182. package/components/Placeholder.js +36 -0
  183. package/components/Popconfirm.d.ts +17 -0
  184. package/components/Popconfirm.js +159 -0
  185. package/components/Popconfirm.module.css +49 -0
  186. package/components/Popover.d.ts +16 -0
  187. package/components/Popover.js +156 -0
  188. package/components/Popover.module.css +19 -0
  189. package/components/Progress.d.ts +5 -0
  190. package/components/Progress.js +31 -0
  191. package/components/Progress.module.css +33 -0
  192. package/components/RadioGroup.d.ts +15 -0
  193. package/components/RadioGroup.js +73 -0
  194. package/components/RadioGroup.module.css +87 -0
  195. package/components/RangeSlider.d.ts +22 -0
  196. package/components/RangeSlider.js +278 -0
  197. package/components/RangeSlider.module.css +141 -0
  198. package/components/ResizablePanel.d.ts +21 -0
  199. package/components/ResizablePanel.js +219 -0
  200. package/components/ResizablePanel.module.css +101 -0
  201. package/components/SearchField.d.ts +11 -0
  202. package/components/SearchField.js +67 -0
  203. package/components/SearchField.module.css +37 -0
  204. package/components/Section.d.ts +6 -0
  205. package/components/Section.js +18 -0
  206. package/components/Section.module.css +14 -0
  207. package/components/Segmented.d.ts +11 -0
  208. package/components/Segmented.js +77 -0
  209. package/components/Segmented.module.css +82 -0
  210. package/components/Select.d.ts +11 -0
  211. package/components/Select.js +172 -0
  212. package/components/Select.module.css +24 -0
  213. package/components/SelectOptionContent.d.ts +5 -0
  214. package/components/SelectOptionContent.js +20 -0
  215. package/components/SelectOptionContent.module.css +7 -0
  216. package/components/SelectTrigger.d.ts +5 -0
  217. package/components/SelectTrigger.js +32 -0
  218. package/components/SelectTrigger.module.css +24 -0
  219. package/components/SelectionTag.d.ts +8 -0
  220. package/components/SelectionTag.js +39 -0
  221. package/components/SettingRow.d.ts +7 -0
  222. package/components/SettingRow.js +19 -0
  223. package/components/SettingRow.module.css +44 -0
  224. package/components/SettingsLayout.d.ts +11 -0
  225. package/components/SettingsLayout.js +61 -0
  226. package/components/SettingsLayout.module.css +68 -0
  227. package/components/ShortcutInput.d.ts +8 -0
  228. package/components/ShortcutInput.js +131 -0
  229. package/components/ShortcutInput.module.css +39 -0
  230. package/components/Skeleton.d.ts +5 -0
  231. package/components/Skeleton.js +19 -0
  232. package/components/Skeleton.module.css +27 -0
  233. package/components/SkeletonRows.d.ts +4 -0
  234. package/components/SkeletonRows.js +19 -0
  235. package/components/SkeletonRows.module.css +19 -0
  236. package/components/SkinTonePicker.d.ts +4 -0
  237. package/components/SkinTonePicker.js +43 -0
  238. package/components/SkinTonePicker.module.css +50 -0
  239. package/components/Slider.d.ts +10 -0
  240. package/components/Slider.js +40 -0
  241. package/components/Slider.module.css +19 -0
  242. package/components/Spinner.d.ts +6 -0
  243. package/components/Spinner.js +31 -0
  244. package/components/Spinner.module.css +57 -0
  245. package/components/SplitButton.d.ts +17 -0
  246. package/components/SplitButton.js +102 -0
  247. package/components/SplitButton.module.css +56 -0
  248. package/components/Statistic.d.ts +22 -0
  249. package/components/Statistic.js +102 -0
  250. package/components/Statistic.module.css +55 -0
  251. package/components/Steps.d.ts +19 -0
  252. package/components/Steps.js +102 -0
  253. package/components/Steps.module.css +176 -0
  254. package/components/Switch.d.ts +6 -0
  255. package/components/Switch.js +25 -0
  256. package/components/Switch.module.css +59 -0
  257. package/components/SymbolPicker.d.ts +6 -0
  258. package/components/SymbolPicker.js +18 -0
  259. package/components/Tab.module.css +145 -0
  260. package/components/TabList.d.ts +11 -0
  261. package/components/TabList.js +51 -0
  262. package/components/Table.d.ts +16 -0
  263. package/components/Table.js +30 -0
  264. package/components/Table.module.css +48 -0
  265. package/components/TableColumnFilter.d.ts +6 -0
  266. package/components/TableColumnFilter.js +76 -0
  267. package/components/TableColumnVisibility.d.ts +11 -0
  268. package/components/TableColumnVisibility.js +76 -0
  269. package/components/Tabs.d.ts +13 -0
  270. package/components/Tabs.js +49 -0
  271. package/components/Tabs.module.css +3 -0
  272. package/components/TagInput.d.ts +11 -0
  273. package/components/TagInput.js +142 -0
  274. package/components/TagInput.module.css +64 -0
  275. package/components/Textarea.d.ts +6 -0
  276. package/components/Textarea.js +24 -0
  277. package/components/Textarea.module.css +9 -0
  278. package/components/ThemePicker.d.ts +6 -0
  279. package/components/ThemePicker.js +78 -0
  280. package/components/ThemePicker.module.css +178 -0
  281. package/components/ThemeProvider.d.ts +11 -0
  282. package/components/ThemeProvider.js +94 -0
  283. package/components/ThemeStylePicker.d.ts +5 -0
  284. package/components/ThemeStylePicker.js +22 -0
  285. package/components/TimeColumns.d.ts +6 -0
  286. package/components/TimeColumns.js +55 -0
  287. package/components/TimeColumns.module.css +47 -0
  288. package/components/Timeline.d.ts +26 -0
  289. package/components/Timeline.js +131 -0
  290. package/components/Timeline.module.css +202 -0
  291. package/components/Toast.d.ts +6 -0
  292. package/components/Toast.js +181 -0
  293. package/components/Toast.module.css +175 -0
  294. package/components/ToastProvider.d.ts +35 -0
  295. package/components/ToastProvider.js +47 -0
  296. package/components/Tooltip.d.ts +10 -0
  297. package/components/Tooltip.js +169 -0
  298. package/components/Tooltip.module.css +53 -0
  299. package/components/TreeGuides.d.ts +9 -0
  300. package/components/TreeGuides.js +39 -0
  301. package/components/TreeGuides.module.css +45 -0
  302. package/components/TreeSelect.d.ts +26 -0
  303. package/components/TreeSelect.js +337 -0
  304. package/components/TreeTable.d.ts +15 -0
  305. package/components/TreeTable.js +151 -0
  306. package/components/TreeTable.module.css +53 -0
  307. package/components/UIProvider.d.ts +10 -0
  308. package/components/UIProvider.js +20 -0
  309. package/components/UnderlinedTab.d.ts +2 -0
  310. package/components/UnderlinedTab.js +8 -0
  311. package/components/Upload.d.ts +22 -0
  312. package/components/Upload.js +165 -0
  313. package/components/Upload.module.css +36 -0
  314. package/components/UploadList.d.ts +9 -0
  315. package/components/UploadList.js +14 -0
  316. package/components/UploadList.module.css +130 -0
  317. package/components/UploadListItem.d.ts +13 -0
  318. package/components/UploadListItem.js +122 -0
  319. package/components/VirtualPickerList.d.ts +25 -0
  320. package/components/VirtualPickerList.js +269 -0
  321. package/components/VirtualPickerList.module.css +93 -0
  322. package/components/appearance.d.ts +20 -0
  323. package/components/appearance.js +69 -0
  324. package/components/characterCatalog.d.ts +9 -0
  325. package/components/characterCatalog.js +55 -0
  326. package/components/color.d.ts +7 -0
  327. package/components/color.js +18 -0
  328. package/components/contribution.d.ts +57 -0
  329. package/components/contribution.js +86 -0
  330. package/components/dateConstraints.d.ts +13 -0
  331. package/components/dateConstraints.js +44 -0
  332. package/components/field.d.ts +58 -0
  333. package/components/field.js +57 -0
  334. package/components/fileSelection.d.ts +21 -0
  335. package/components/fileSelection.js +43 -0
  336. package/components/hierarchy.d.ts +23 -0
  337. package/components/hierarchy.js +33 -0
  338. package/components/iconCatalog.d.ts +13 -0
  339. package/components/iconCatalog.js +80 -0
  340. package/components/index.d.ts +152 -0
  341. package/components/index.js +225 -0
  342. package/components/menuFocus.d.ts +7 -0
  343. package/components/menuFocus.js +21 -0
  344. package/components/menuPointer.d.ts +6 -0
  345. package/components/menuPointer.js +13 -0
  346. package/components/menuTypes.d.ts +24 -0
  347. package/components/menuTypes.js +0 -0
  348. package/components/motion.d.ts +9 -0
  349. package/components/motion.js +82 -0
  350. package/components/rangeMath.d.ts +12 -0
  351. package/components/rangeMath.js +30 -0
  352. package/components/surfacePortal.d.ts +13 -0
  353. package/components/surfacePortal.js +22 -0
  354. package/components/tooltipState.d.ts +5 -0
  355. package/components/tooltipState.js +48 -0
  356. package/components/types.d.ts +9 -0
  357. package/components/types.js +0 -0
  358. package/components/uploadTypes.d.ts +21 -0
  359. package/components/uploadTypes.js +0 -0
  360. package/components/useFormState.d.ts +51 -0
  361. package/components/useFormState.js +218 -0
  362. package/components/useHierarchy.d.ts +8 -0
  363. package/components/useHierarchy.js +80 -0
  364. package/components/useMenuLayers.d.ts +17 -0
  365. package/components/useMenuLayers.js +23 -0
  366. package/components/useModalScrollLock.d.ts +1 -0
  367. package/components/useModalScrollLock.js +18 -0
  368. package/components/usePopover.d.ts +8 -0
  369. package/components/usePopover.js +22 -0
  370. package/components/useTabIndicator.d.ts +12 -0
  371. package/components/useTabIndicator.js +63 -0
  372. package/components/useUploadQueue.d.ts +20 -0
  373. package/components/useUploadQueue.js +175 -0
  374. package/package.json +810 -3
  375. package/styles/base.css +206 -0
  376. package/styles/blue.css +268 -0
  377. package/styles/desktop.css +2 -0
  378. package/styles/minimal.css +337 -0
  379. package/styles/themes.css +2 -0
  380. package/styles/web.css +2 -0
@@ -0,0 +1,269 @@
1
+ import { jsx, jsxs } from "react/jsx-runtime";
2
+ import {
3
+ useEffect,
4
+ useId,
5
+ useLayoutEffect,
6
+ useMemo,
7
+ useRef,
8
+ useState
9
+ } from "react";
10
+ import { Dialog } from "./Dialog.js";
11
+ import { SearchField } from "./SearchField.js";
12
+ import { MenuItem } from "./MenuItem.js";
13
+ import { Spinner } from "./Spinner.js";
14
+ import { Alert } from "./Alert.js";
15
+ import { useSurfacePortal } from "./surfacePortal.js";
16
+ import s from "./CommandPalette.module.css";
17
+ const normalize = (value) => value.normalize("NFKD").replace(/\p{Diacritic}/gu, "").toLocaleLowerCase();
18
+ function CommandPalette({
19
+ open,
20
+ onClose,
21
+ onOpen,
22
+ items,
23
+ onSelect,
24
+ title = "\u547D\u4EE4\u9762\u677F",
25
+ placeholder = "\u641C\u7D22\u547D\u4EE4\u2026",
26
+ hotkey = true,
27
+ closeOnSelect = true,
28
+ emptyText = "\u6CA1\u6709\u5339\u914D\u7684\u547D\u4EE4"
29
+ }) {
30
+ const [query, setQuery] = useState(""), [active, setActive] = useState(null), [busy, setBusy] = useState(), [error, setError] = useState("");
31
+ const root = useRef(null), revision = useRef(0), controller = useRef(null), id = useId(), owner = useSurfacePortal();
32
+ const results = useMemo(() => {
33
+ const tokens = normalize(query).trim().split(/\s+/).filter(Boolean);
34
+ return items.filter((item) => {
35
+ const text = normalize(
36
+ [
37
+ item.label,
38
+ item.description,
39
+ item.group,
40
+ item.shortcut,
41
+ ...item.keywords || []
42
+ ].filter(Boolean).join(" ")
43
+ );
44
+ return tokens.every((token) => text.includes(token));
45
+ });
46
+ }, [items, query]);
47
+ const actionable = (item) => !item.disabled && !!(item.onSelect || onSelect);
48
+ const grouped = /* @__PURE__ */ new Map();
49
+ for (const item of results) {
50
+ const group = item.group || "";
51
+ if (!grouped.has(group)) grouped.set(group, []);
52
+ grouped.get(group).push(item);
53
+ }
54
+ const enabled = [...grouped.values()].flat().filter(actionable), selected = enabled.find((item) => item.value === active) || enabled[0];
55
+ const reserveIcon = results.some((item) => !!item.icon);
56
+ const optionId = (value) => `${id}-${encodeURIComponent(value)}`;
57
+ const cancel = () => {
58
+ revision.current++;
59
+ controller.current?.abort();
60
+ controller.current = null;
61
+ };
62
+ const close = () => {
63
+ cancel();
64
+ onClose();
65
+ };
66
+ useLayoutEffect(() => {
67
+ cancel();
68
+ if (open) {
69
+ setQuery("");
70
+ setActive(null);
71
+ setBusy(void 0);
72
+ setError("");
73
+ }
74
+ }, [open]);
75
+ useEffect(
76
+ () => () => {
77
+ revision.current++;
78
+ controller.current?.abort();
79
+ },
80
+ []
81
+ );
82
+ const focus = () => root.current?.querySelector("input")?.focus({ preventScroll: true });
83
+ useLayoutEffect(() => {
84
+ if (open && !busy) focus();
85
+ }, [open, busy]);
86
+ useLayoutEffect(() => {
87
+ if (open && selected)
88
+ document.getElementById(optionId(selected.value))?.scrollIntoView({ block: "nearest" });
89
+ }, [open, selected?.value]);
90
+ const latest = useRef({
91
+ open,
92
+ onOpen,
93
+ close,
94
+ available: owner?.active !== false
95
+ });
96
+ latest.current = { open, onOpen, close, available: owner?.active !== false };
97
+ useEffect(() => {
98
+ if (!hotkey || !onOpen) return;
99
+ const key = (event) => {
100
+ if (event.defaultPrevented || event.repeat || event.isComposing || event.altKey || event.shiftKey || !(event.ctrlKey || event.metaKey) || event.key.toLowerCase() !== "k" || !latest.current.available)
101
+ return;
102
+ const target = event.target;
103
+ if (!latest.current.open && target?.closest('input,textarea,select,[contenteditable="true"]'))
104
+ return;
105
+ event.preventDefault();
106
+ if (latest.current.open) latest.current.close();
107
+ else latest.current.onOpen?.();
108
+ };
109
+ window.addEventListener("keydown", key);
110
+ return () => window.removeEventListener("keydown", key);
111
+ }, [hotkey, !!onOpen]);
112
+ const execute = async (item) => {
113
+ if (controller.current || busy || !actionable(item)) return;
114
+ cancel();
115
+ const token = revision.current, abort = new AbortController();
116
+ controller.current = abort;
117
+ setBusy(item.value);
118
+ setError("");
119
+ setActive(item.value);
120
+ try {
121
+ if (item.onSelect) await item.onSelect(abort.signal);
122
+ else await onSelect?.(item, abort.signal);
123
+ if (token !== revision.current || abort.signal.aborted) return;
124
+ setBusy(void 0);
125
+ if (closeOnSelect) close();
126
+ } catch {
127
+ if (token !== revision.current || abort.signal.aborted) return;
128
+ setBusy(void 0);
129
+ setError("\u64CD\u4F5C\u672A\u5B8C\u6210\uFF0C\u8BF7\u91CD\u8BD5\u3002");
130
+ } finally {
131
+ if (controller.current === abort) controller.current = null;
132
+ }
133
+ };
134
+ return /* @__PURE__ */ jsx(
135
+ Dialog,
136
+ {
137
+ open,
138
+ onClose: close,
139
+ title,
140
+ wide: true,
141
+ footer: /* @__PURE__ */ jsxs("div", { className: s.footer, children: [
142
+ /* @__PURE__ */ jsxs("span", { children: [
143
+ /* @__PURE__ */ jsx("kbd", { children: "\u2191 \u2193" }),
144
+ " \u9009\u62E9 ",
145
+ /* @__PURE__ */ jsx("kbd", { children: "\u21B5" }),
146
+ " \u6267\u884C ",
147
+ /* @__PURE__ */ jsx("kbd", { children: "Esc" }),
148
+ " \u5173\u95ED"
149
+ ] }),
150
+ /* @__PURE__ */ jsxs("span", { role: "status", "aria-live": "polite", children: [
151
+ results.length,
152
+ " \u9879"
153
+ ] })
154
+ ] }),
155
+ children: /* @__PURE__ */ jsxs("div", { ref: root, className: s.palette, children: [
156
+ /* @__PURE__ */ jsx(
157
+ SearchField,
158
+ {
159
+ label: "\u641C\u7D22\u547D\u4EE4",
160
+ placeholder,
161
+ value: query,
162
+ onChange: (value) => {
163
+ setQuery(value);
164
+ setActive(null);
165
+ setError("");
166
+ },
167
+ disabled: !!busy,
168
+ loading: !!busy,
169
+ role: "combobox",
170
+ "aria-autocomplete": "list",
171
+ "aria-expanded": open,
172
+ "aria-controls": id,
173
+ "aria-activedescendant": selected ? optionId(selected.value) : void 0,
174
+ onKeyDown: (event) => {
175
+ if (event.nativeEvent.isComposing || event.keyCode === 229) return;
176
+ if (event.key === "Escape") {
177
+ event.preventDefault();
178
+ event.stopPropagation();
179
+ close();
180
+ return;
181
+ }
182
+ if (busy) return;
183
+ if (event.key === "Enter") {
184
+ event.preventDefault();
185
+ if (selected) void execute(selected);
186
+ return;
187
+ }
188
+ if (!enabled.length) return;
189
+ const index = enabled.findIndex(
190
+ (item) => item.value === selected?.value
191
+ );
192
+ const next = event.key === "Home" ? 0 : event.key === "End" ? enabled.length - 1 : event.key === "ArrowDown" ? (index + 1) % enabled.length : event.key === "ArrowUp" ? (index - 1 + enabled.length) % enabled.length : -1;
193
+ if (next >= 0) {
194
+ event.preventDefault();
195
+ setActive(enabled[next].value);
196
+ }
197
+ }
198
+ }
199
+ ),
200
+ error && /* @__PURE__ */ jsx(
201
+ Alert,
202
+ {
203
+ tone: "danger",
204
+ title: "\u547D\u4EE4\u6267\u884C\u5931\u8D25",
205
+ onClose: () => {
206
+ setError("");
207
+ focus();
208
+ },
209
+ children: error
210
+ }
211
+ ),
212
+ /* @__PURE__ */ jsxs(
213
+ "div",
214
+ {
215
+ className: s.results,
216
+ role: "listbox",
217
+ "aria-label": "\u547D\u4EE4\u7ED3\u679C",
218
+ id,
219
+ "aria-busy": !!busy,
220
+ children: [
221
+ [...grouped].map(([group, commands]) => /* @__PURE__ */ jsxs(
222
+ "div",
223
+ {
224
+ role: "group",
225
+ "aria-label": group || "\u547D\u4EE4",
226
+ className: s.group,
227
+ children: [
228
+ group && /* @__PURE__ */ jsx("div", { className: s.groupLabel, "aria-hidden": "true", children: group }),
229
+ commands.map((item) => /* @__PURE__ */ jsx(
230
+ MenuItem,
231
+ {
232
+ id: optionId(item.value),
233
+ as: "div",
234
+ role: "option",
235
+ tabIndex: -1,
236
+ label: item.label,
237
+ description: item.description,
238
+ icon: item.icon,
239
+ reserveIcon,
240
+ shortcut: item.shortcut,
241
+ active: selected?.value === item.value,
242
+ "aria-selected": selected?.value === item.value,
243
+ disabled: !!busy || !actionable(item),
244
+ trailing: busy === item.value ? /* @__PURE__ */ jsx(Spinner, { size: 14, decorative: true }) : void 0,
245
+ onMouseDown: (event) => event.preventDefault(),
246
+ onPointerMove: () => {
247
+ if (!busy && actionable(item)) setActive(item.value);
248
+ },
249
+ onClick: () => {
250
+ void execute(item);
251
+ }
252
+ },
253
+ item.value
254
+ ))
255
+ ]
256
+ },
257
+ group
258
+ )),
259
+ !results.length && /* @__PURE__ */ jsx("div", { className: s.empty, children: emptyText })
260
+ ]
261
+ }
262
+ )
263
+ ] })
264
+ }
265
+ );
266
+ }
267
+ export {
268
+ CommandPalette
269
+ };
@@ -0,0 +1,49 @@
1
+ .palette {
2
+ display: flex;
3
+ flex-direction: column;
4
+ gap: 12px;
5
+ min-width: 0;
6
+ }
7
+ .results {
8
+ overflow: auto;
9
+ max-height: min(360px, 42vh);
10
+ min-height: 90px;
11
+ overscroll-behavior: contain;
12
+ }
13
+ .group {
14
+ display: flex;
15
+ flex-direction: column;
16
+ gap: var(--menu-item-spacing);
17
+ }
18
+ .group + .group {
19
+ margin-top: 10px;
20
+ padding-top: 8px;
21
+ border-top: var(--separator-width) var(--separator-style)
22
+ var(--separator-color);
23
+ }
24
+ .groupLabel {
25
+ padding: 3px var(--menu-item-padding-x) 5px;
26
+ font-size: 11px;
27
+ line-height: 18px;
28
+ color: var(--muted);
29
+ }
30
+ .empty {
31
+ padding: 30px 12px;
32
+ text-align: center;
33
+ color: var(--muted);
34
+ font-size: 12px;
35
+ }
36
+ .footer {
37
+ width: 100%;
38
+ display: flex;
39
+ align-items: center;
40
+ justify-content: space-between;
41
+ gap: 12px;
42
+ color: var(--muted);
43
+ font-size: 11px;
44
+ flex-wrap: wrap;
45
+ }
46
+ .footer kbd {
47
+ font: inherit;
48
+ color: var(--fg);
49
+ }
@@ -0,0 +1,9 @@
1
+ import { type ReactNode } from "react";
2
+ import type { ContextMenuItem } from "./menuTypes";
3
+ export declare function ContextMenu({ items, children, label, disabled: requestedDisabled, onOpenChange, }: {
4
+ items: ContextMenuItem[];
5
+ children: ReactNode;
6
+ label?: string;
7
+ disabled?: boolean;
8
+ onOpenChange?(open: boolean): void;
9
+ }): import("react").JSX.Element;
@@ -0,0 +1,363 @@
1
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
2
+ import {
3
+ useCallback,
4
+ useEffect,
5
+ useLayoutEffect,
6
+ useMemo,
7
+ useRef,
8
+ useState
9
+ } from "react";
10
+ import { createPortal } from "react-dom";
11
+ import { ContextMenuPanel } from "./ContextMenuPanel.js";
12
+ import { cancelMotion } from "./motion.js";
13
+ import { useMenuLayers } from "./useMenuLayers.js";
14
+ import { movingTowardSubmenu } from "./menuPointer.js";
15
+ import s from "./ContextMenu.module.css";
16
+ import { useSurfacePortal } from "./surfacePortal.js";
17
+ function ContextMenu({
18
+ items,
19
+ children,
20
+ label = "\u53F3\u952E\u83DC\u5355\u533A\u57DF",
21
+ disabled: requestedDisabled = false,
22
+ onOpenChange
23
+ }) {
24
+ const owner = useSurfacePortal(), portalHost = owner?.host || document.body, disabled = requestedDisabled || owner?.active === false;
25
+ const host = useRef(null), root = useRef(null), restore = useRef(null), hover = useRef(void 0), searchTimer = useRef(void 0), search = useRef(""), pointerHistory = useRef([]), hoverCandidate = useRef(void 0), revision = useRef(0), desired = useRef(false), callback = useRef(onOpenChange);
26
+ callback.current = onOpenChange;
27
+ const [open, setOpen] = useState(false), [point, setPoint] = useState({ x: 0, y: 0 }), [path, setPath] = useState([]), [focusLevel, setFocusLevel] = useState(null), [busy, setBusy] = useState(), [error, setError] = useState("");
28
+ const cancelHover = useCallback(() => {
29
+ clearTimeout(hover.current);
30
+ hoverCandidate.current = void 0;
31
+ }, []);
32
+ const close = useCallback(
33
+ (returnFocus = true) => {
34
+ revision.current++;
35
+ desired.current = false;
36
+ cancelHover();
37
+ setOpen(false);
38
+ setBusy(void 0);
39
+ callback.current?.(false);
40
+ if (returnFocus && restore.current?.isConnected)
41
+ restore.current.focus({ preventScroll: true });
42
+ },
43
+ [cancelHover]
44
+ );
45
+ useEffect(() => {
46
+ if (disabled && desired.current) close(false);
47
+ }, [disabled, close]);
48
+ const show = (x, y) => {
49
+ if (disabled) return;
50
+ revision.current++;
51
+ cancelHover();
52
+ pointerHistory.current = [];
53
+ desired.current = true;
54
+ restore.current = document.activeElement instanceof HTMLElement ? document.activeElement : host.current;
55
+ setPoint({ x, y });
56
+ setPath([]);
57
+ setBusy(void 0);
58
+ setError("");
59
+ setOpen(true);
60
+ setFocusLevel(0);
61
+ callback.current?.(true);
62
+ };
63
+ useLayoutEffect(() => {
64
+ const node = root.current;
65
+ cancelMotion(node);
66
+ node.style.opacity = "1";
67
+ node.style.translate = "0 0";
68
+ node.inert = !open;
69
+ if (open && !node.matches(":popover-open")) node.showPopover();
70
+ }, [open, point, portalHost]);
71
+ useEffect(() => {
72
+ if (!open) return;
73
+ const pointer = (e) => {
74
+ if (!root.current?.contains(e.target)) close(false);
75
+ };
76
+ const key2 = (e) => {
77
+ if (e.key === "Escape") {
78
+ e.preventDefault();
79
+ e.stopPropagation();
80
+ close();
81
+ }
82
+ };
83
+ const track = (event) => {
84
+ const now = performance.now();
85
+ pointerHistory.current = [
86
+ ...pointerHistory.current.filter((p) => now - p.time < 300).slice(-4),
87
+ { x: event.clientX, y: event.clientY, time: now }
88
+ ];
89
+ };
90
+ const blur = () => close(false);
91
+ document.addEventListener("pointerdown", pointer, true);
92
+ document.addEventListener("pointermove", track, true);
93
+ document.addEventListener("keydown", key2, true);
94
+ window.addEventListener("blur", blur);
95
+ return () => {
96
+ document.removeEventListener("pointerdown", pointer, true);
97
+ document.removeEventListener("pointermove", track, true);
98
+ document.removeEventListener("keydown", key2, true);
99
+ window.removeEventListener("blur", blur);
100
+ };
101
+ }, [open, close]);
102
+ useEffect(
103
+ () => () => {
104
+ revision.current++;
105
+ clearTimeout(hover.current);
106
+ clearTimeout(searchTimer.current);
107
+ if (root.current) cancelMotion(root.current);
108
+ },
109
+ []
110
+ );
111
+ useLayoutEffect(() => {
112
+ if (!open || focusLevel === null) return;
113
+ let frame = 0;
114
+ const started = performance.now();
115
+ const focus = () => {
116
+ const button = root.current?.querySelector(
117
+ '[data-menu-state="open"][data-menu-level="' + focusLevel + '"] button:not(:disabled)'
118
+ );
119
+ if (button && !button.closest("[inert]") && getComputedStyle(button).visibility === "visible" && button.getClientRects().length) {
120
+ button.focus({ preventScroll: true });
121
+ setFocusLevel(null);
122
+ } else if (performance.now() - started < 500)
123
+ frame = requestAnimationFrame(focus);
124
+ };
125
+ frame = requestAnimationFrame(focus);
126
+ return () => cancelAnimationFrame(frame);
127
+ }, [open, focusLevel, path, point]);
128
+ const levels = useMemo(() => {
129
+ if (!open) return [];
130
+ const result = [
131
+ { key: "root", level: 0, items, point, activeId: path[0]?.id }
132
+ ];
133
+ const ids = [];
134
+ for (const entry of path) {
135
+ const parent = result.at(-1).items.find((item) => item.id === entry.id);
136
+ if (!parent || parent.type === "separator" || !parent.children?.length)
137
+ break;
138
+ ids.push(entry.id);
139
+ result.push({
140
+ key: JSON.stringify(ids),
141
+ level: result.length,
142
+ items: parent.children,
143
+ point,
144
+ anchor: entry.anchor,
145
+ activeId: path[result.length]?.id
146
+ });
147
+ }
148
+ return result;
149
+ }, [open, path, items, point]);
150
+ const { layers, exited } = useMenuLayers(levels);
151
+ useLayoutEffect(() => {
152
+ const node = root.current;
153
+ if (!open && !layers.length && node.matches(":popover-open"))
154
+ node.hidePopover();
155
+ }, [open, layers.length]);
156
+ const expand = (item, level, index, focus = false) => {
157
+ cancelHover();
158
+ const anchor = root.current?.querySelector(
159
+ '[data-menu-state="open"][data-menu-level="' + level + '"] [data-menu-index="' + index + '"]'
160
+ );
161
+ if (!anchor) return;
162
+ if (item.children?.length && path[level]?.id === item.id) {
163
+ if (focus) setFocusLevel(level + 1);
164
+ return;
165
+ }
166
+ setPath(
167
+ (old) => item.children?.length ? [...old.slice(0, level), { id: item.id, anchor }] : old.slice(0, level)
168
+ );
169
+ if (focus && item.children?.length) setFocusLevel(level + 1);
170
+ };
171
+ const queueHover = (item, level, index) => {
172
+ if (item.disabled || path[level]?.id === item.id) {
173
+ cancelHover();
174
+ return;
175
+ }
176
+ const anchor = root.current?.querySelector(
177
+ '[data-menu-state="open"][data-menu-level="' + level + '"] [data-menu-index="' + index + '"]'
178
+ );
179
+ if (!anchor) return;
180
+ if (hoverCandidate.current?.id === item.id && hoverCandidate.current.level === level)
181
+ return;
182
+ cancelHover();
183
+ const candidate = {
184
+ id: item.id,
185
+ level,
186
+ anchor,
187
+ started: performance.now()
188
+ };
189
+ hoverCandidate.current = candidate;
190
+ const check = () => {
191
+ if (hoverCandidate.current !== candidate) return;
192
+ const points = pointerHistory.current, point2 = points.at(-1);
193
+ if (!point2) {
194
+ cancelHover();
195
+ return;
196
+ }
197
+ const target = document.elementFromPoint(point2.x, point2.y);
198
+ const panel = target?.closest(
199
+ '[data-menu-state="open"][data-menu-level]'
200
+ );
201
+ if (panel && Number(panel.dataset.menuLevel) > level) {
202
+ cancelHover();
203
+ return;
204
+ }
205
+ const child = root.current?.querySelector(
206
+ '[data-menu-state="open"][data-menu-level="' + (level + 1) + '"]'
207
+ );
208
+ if (child && performance.now() - candidate.started < 500 && movingTowardSubmenu(points[0], point2, child.getBoundingClientRect())) {
209
+ hover.current = setTimeout(check, 40);
210
+ return;
211
+ }
212
+ if (anchor.contains(target)) expand(item, level, index);
213
+ else cancelHover();
214
+ };
215
+ hover.current = setTimeout(check, 120);
216
+ };
217
+ const choose = async (item, level, index) => {
218
+ if (item.disabled || busy) return;
219
+ if (item.children?.length) {
220
+ expand(item, level, index, true);
221
+ return;
222
+ }
223
+ const token = revision.current;
224
+ setError("");
225
+ try {
226
+ const result = item.onSelect?.();
227
+ if (result && typeof result.then === "function") {
228
+ setBusy(item.id);
229
+ await result;
230
+ }
231
+ if (token !== revision.current) return;
232
+ setBusy(void 0);
233
+ if (item.closeOnSelect !== false) close();
234
+ } catch {
235
+ if (token === revision.current) {
236
+ setBusy(void 0);
237
+ setError("\u64CD\u4F5C\u672A\u5B8C\u6210\uFF0C\u8BF7\u91CD\u8BD5\u3002");
238
+ }
239
+ }
240
+ };
241
+ const key = (event, level) => {
242
+ cancelHover();
243
+ pointerHistory.current = [];
244
+ const target = event.target.closest(
245
+ "button[data-menu-index]"
246
+ );
247
+ const options = [
248
+ ...event.currentTarget.querySelectorAll(
249
+ "button:not(:disabled)"
250
+ )
251
+ ];
252
+ const index = target ? options.indexOf(target) : -1;
253
+ let next;
254
+ if (event.key === "ArrowDown") next = (index + 1) % options.length;
255
+ else if (event.key === "ArrowUp")
256
+ next = (index - 1 + options.length) % options.length;
257
+ else if (event.key === "Home") next = 0;
258
+ else if (event.key === "End") next = options.length - 1;
259
+ if (next !== void 0) {
260
+ event.preventDefault();
261
+ options[next]?.focus();
262
+ return;
263
+ }
264
+ if (event.key === "Tab") {
265
+ event.preventDefault();
266
+ close();
267
+ return;
268
+ }
269
+ if (event.key === "ArrowRight" && target) {
270
+ event.preventDefault();
271
+ const i = Number(target.dataset.menuIndex), item = levels[level].items[i];
272
+ if (item.type !== "separator") expand(item, level, i, true);
273
+ } else if (event.key === "ArrowLeft" && level > 0) {
274
+ event.preventDefault();
275
+ const anchor = path[level - 1]?.anchor;
276
+ setPath((old) => old.slice(0, level - 1));
277
+ requestAnimationFrame(() => anchor?.focus());
278
+ } else if (event.key === "Enter" || event.key === " ") {
279
+ event.preventDefault();
280
+ target?.click();
281
+ } else if (event.key.length === 1 && !event.ctrlKey && !event.metaKey && !event.altKey) {
282
+ search.current += event.key.toLowerCase();
283
+ clearTimeout(searchTimer.current);
284
+ searchTimer.current = setTimeout(() => search.current = "", 600);
285
+ options.find(
286
+ (button) => button.querySelector("[data-menu-label]")?.textContent?.toLowerCase().startsWith(search.current)
287
+ )?.focus();
288
+ }
289
+ };
290
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
291
+ /* @__PURE__ */ jsx(
292
+ "div",
293
+ {
294
+ ref: host,
295
+ className: s.host,
296
+ role: "group",
297
+ "aria-label": label,
298
+ tabIndex: disabled ? void 0 : 0,
299
+ onContextMenu: (e) => {
300
+ if (disabled) return;
301
+ e.preventDefault();
302
+ e.stopPropagation();
303
+ host.current?.focus({ preventScroll: true });
304
+ show(e.clientX, e.clientY);
305
+ },
306
+ onKeyDown: (e) => {
307
+ if (e.key === "ContextMenu" || e.shiftKey && e.key === "F10") {
308
+ e.preventDefault();
309
+ const rect = e.currentTarget.getBoundingClientRect();
310
+ show(rect.left + 16, rect.top + 16);
311
+ }
312
+ },
313
+ children
314
+ }
315
+ ),
316
+ createPortal(
317
+ /* @__PURE__ */ jsx(
318
+ "div",
319
+ {
320
+ ref: root,
321
+ popover: "manual",
322
+ className: s.root,
323
+ onContextMenu: (e) => e.preventDefault(),
324
+ children: layers.map((menu) => /* @__PURE__ */ jsx(
325
+ ContextMenuPanel,
326
+ {
327
+ open: menu.open,
328
+ onExited: () => exited(menu.key),
329
+ onReady: (node) => {
330
+ if (menu.open && focusLevel === menu.level && !node.inert && getComputedStyle(node).visibility === "visible") {
331
+ node.querySelector("button:not(:disabled)")?.focus({ preventScroll: true });
332
+ setFocusLevel(null);
333
+ }
334
+ },
335
+ onPanelEnter: () => {
336
+ if (hoverCandidate.current && menu.level > hoverCandidate.current.level)
337
+ cancelHover();
338
+ },
339
+ items: menu.items,
340
+ level: menu.level,
341
+ point: menu.point,
342
+ anchor: menu.anchor,
343
+ activeId: menu.activeId,
344
+ busy,
345
+ error,
346
+ onHover: (item, index) => queueHover(item, menu.level, index),
347
+ onChoose: (item, index) => void choose(item, menu.level, index),
348
+ onKey: key,
349
+ onScroll: () => {
350
+ if (menu.open) setPath((old) => old.slice(0, menu.level));
351
+ }
352
+ },
353
+ menu.key
354
+ ))
355
+ }
356
+ ),
357
+ portalHost
358
+ )
359
+ ] });
360
+ }
361
+ export {
362
+ ContextMenu
363
+ };