@heybejs/ui 0.0.0-stage → 1.0.2

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 (101) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +32 -2
  3. package/dist/hooks/mergeProps.d.ts +1 -0
  4. package/dist/hooks/mergeProps.js +19 -0
  5. package/dist/hooks/useDismiss.d.ts +6 -0
  6. package/dist/hooks/useDismiss.js +34 -0
  7. package/dist/hooks/useListKeyboard.d.ts +9 -0
  8. package/dist/hooks/useListKeyboard.js +56 -0
  9. package/dist/hooks/useMergeRefs.d.ts +2 -0
  10. package/dist/hooks/useMergeRefs.js +18 -0
  11. package/dist/hooks/usePosition.d.ts +13 -0
  12. package/dist/hooks/usePosition.js +76 -0
  13. package/dist/hooks/useTooltipDelay.d.ts +5 -0
  14. package/dist/hooks/useTooltipDelay.js +28 -0
  15. package/dist/hooks/useTopLayer.d.ts +2 -0
  16. package/dist/hooks/useTopLayer.js +12 -0
  17. package/dist/index.d.ts +25 -0
  18. package/dist/index.js +25 -0
  19. package/dist/primitives/Combobox.d.ts +18 -0
  20. package/dist/primitives/Combobox.js +98 -0
  21. package/dist/primitives/Dialog.d.ts +10 -0
  22. package/dist/primitives/Dialog.js +48 -0
  23. package/dist/primitives/Menu.d.ts +22 -0
  24. package/dist/primitives/Menu.js +97 -0
  25. package/dist/primitives/Popover.d.ts +13 -0
  26. package/dist/primitives/Popover.js +47 -0
  27. package/dist/primitives/RadioGroup.d.ts +14 -0
  28. package/dist/primitives/RadioGroup.js +38 -0
  29. package/dist/primitives/Switch.d.ts +10 -0
  30. package/dist/primitives/Switch.js +6 -0
  31. package/dist/primitives/Tabs.d.ts +16 -0
  32. package/dist/primitives/Tabs.js +44 -0
  33. package/dist/primitives/Tooltip.d.ts +6 -0
  34. package/dist/primitives/Tooltip.js +59 -0
  35. package/dist/primitives/index.d.ts +8 -0
  36. package/dist/primitives/index.js +8 -0
  37. package/dist/state/useControlledState.d.ts +1 -0
  38. package/dist/state/useControlledState.js +12 -0
  39. package/dist/state/useListState.d.ts +12 -0
  40. package/dist/state/useListState.js +30 -0
  41. package/dist/state/useOverlayState.d.ts +10 -0
  42. package/dist/state/useOverlayState.js +8 -0
  43. package/dist/style.css +2 -0
  44. package/dist/styled/Badge.d.ts +12 -0
  45. package/dist/styled/Badge.js +10 -0
  46. package/dist/styled/Button.d.ts +7 -0
  47. package/dist/styled/Button.js +21 -0
  48. package/dist/styled/Checkbox.d.ts +5 -0
  49. package/dist/styled/Checkbox.js +7 -0
  50. package/dist/styled/CodeArea.d.ts +32 -0
  51. package/dist/styled/CodeArea.js +291 -0
  52. package/dist/styled/Combobox.d.ts +4 -0
  53. package/dist/styled/Combobox.js +18 -0
  54. package/dist/styled/Confirm.d.ts +15 -0
  55. package/dist/styled/Confirm.js +29 -0
  56. package/dist/styled/CopyButton.d.ts +5 -0
  57. package/dist/styled/CopyButton.js +26 -0
  58. package/dist/styled/Dialog.d.ts +6 -0
  59. package/dist/styled/Dialog.js +27 -0
  60. package/dist/styled/Disclosure.d.ts +10 -0
  61. package/dist/styled/Disclosure.js +13 -0
  62. package/dist/styled/EmptyState.d.ts +6 -0
  63. package/dist/styled/EmptyState.js +4 -0
  64. package/dist/styled/FieldShell.d.ts +13 -0
  65. package/dist/styled/FieldShell.js +7 -0
  66. package/dist/styled/FileField.d.ts +9 -0
  67. package/dist/styled/FileField.js +32 -0
  68. package/dist/styled/Menu.d.ts +4 -0
  69. package/dist/styled/Menu.js +19 -0
  70. package/dist/styled/NumberField.d.ts +3 -0
  71. package/dist/styled/NumberField.js +10 -0
  72. package/dist/styled/Popover.d.ts +3 -0
  73. package/dist/styled/Popover.js +10 -0
  74. package/dist/styled/RadioGroup.d.ts +4 -0
  75. package/dist/styled/RadioGroup.js +9 -0
  76. package/dist/styled/Section.d.ts +5 -0
  77. package/dist/styled/Section.js +6 -0
  78. package/dist/styled/Select.d.ts +3 -0
  79. package/dist/styled/Select.js +8 -0
  80. package/dist/styled/Switch.d.ts +4 -0
  81. package/dist/styled/Switch.js +8 -0
  82. package/dist/styled/Table.d.ts +19 -0
  83. package/dist/styled/Table.js +15 -0
  84. package/dist/styled/Tabs.d.ts +11 -0
  85. package/dist/styled/Tabs.js +5 -0
  86. package/dist/styled/TextField.d.ts +3 -0
  87. package/dist/styled/TextField.js +7 -0
  88. package/dist/styled/Toasts.d.ts +10 -0
  89. package/dist/styled/Toasts.js +29 -0
  90. package/dist/styled/Tooltip.d.ts +3 -0
  91. package/dist/styled/Tooltip.js +10 -0
  92. package/dist/styled/field.d.ts +16 -0
  93. package/dist/styled/field.js +19 -0
  94. package/dist/styled/glass.d.ts +1 -0
  95. package/dist/styled/glass.js +1 -0
  96. package/dist/styled/tv.d.ts +1 -0
  97. package/dist/styled/tv.js +2 -0
  98. package/dist/test-clock.d.ts +1 -0
  99. package/dist/test-clock.js +2 -0
  100. package/package.json +68 -3
  101. package/src/theme.css +173 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 mr.san
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,33 @@
1
- # Temporary Holding Version
1
+ # @heybejs/ui
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ React components of the Heybe dashboard, for building dashboard extension pages.
4
+
5
+ ```sh
6
+ npm install @heybejs/ui react react-dom
7
+ ```
8
+
9
+ ```tsx
10
+ import { createRoot } from "react-dom/client";
11
+ import { Button } from "@heybejs/ui";
12
+ import "@heybejs/ui/style.css";
13
+
14
+ window.heybe.dashboard.addPage({
15
+ path: "demo",
16
+ title: "Demo",
17
+ mount(el) {
18
+ const root = createRoot(el);
19
+ root.render(<Button>Hello</Button>);
20
+ return () => root.unmount();
21
+ },
22
+ });
23
+ ```
24
+
25
+ Use `@heybejs/ui` for styled dashboard components and `@heybejs/ui/primitives` for Popover, Menu, MenuItem, Tooltip, Combobox, Tabs, tabPanelIds, Switch, RadioGroup, Dialog and Placement. `ConfirmProvider` and `ToastProvider` must wrap the extension root to use `useConfirm` and `useToast`.
26
+
27
+ `style.css` is compiled Tailwind without a reset. Light is the default theme; set `data-theme="dark"` on the `html` element for dark mode.
28
+
29
+ Projects that use Tailwind v4 can import `@heybejs/ui/theme.css` instead, which registers the dark variant, the code editor colors and the package sources.
30
+
31
+ ## License
32
+
33
+ [MIT](LICENSE)
@@ -0,0 +1 @@
1
+ export declare function mergeProps(...props: Record<string, unknown>[]): Record<string, unknown>;
@@ -0,0 +1,19 @@
1
+ export function mergeProps(...props) {
2
+ const result = {};
3
+ for (const source of props)
4
+ for (const [key, value] of Object.entries(source)) {
5
+ const previous = result[key];
6
+ if (key === "className")
7
+ result[key] = [previous, value].filter(Boolean).join(" ");
8
+ else if (/^on[A-Z]/.test(key) &&
9
+ typeof previous === "function" &&
10
+ typeof value === "function")
11
+ result[key] = (...args) => {
12
+ previous(...args);
13
+ value(...args);
14
+ };
15
+ else
16
+ result[key] = value;
17
+ }
18
+ return result;
19
+ }
@@ -0,0 +1,6 @@
1
+ import { type RefObject } from "react";
2
+ export declare function useDismiss(ref: RefObject<HTMLElement | null>, opts: {
3
+ open: boolean;
4
+ onDismiss(): void;
5
+ triggerRef?: RefObject<HTMLElement | null>;
6
+ }): void;
@@ -0,0 +1,34 @@
1
+ import { useEffect, useRef } from "react";
2
+ const layers = [];
3
+ export function useDismiss(ref, opts) {
4
+ const latest = useRef(opts);
5
+ latest.current = opts;
6
+ useEffect(() => {
7
+ if (!opts.open)
8
+ return;
9
+ const layer = Symbol();
10
+ layers.push(layer);
11
+ const keydown = (event) => {
12
+ if (event.key !== "Escape" || layers[layers.length - 1] !== layer)
13
+ return;
14
+ event.preventDefault();
15
+ event.stopImmediatePropagation();
16
+ latest.current.onDismiss();
17
+ };
18
+ const pointerdown = (event) => {
19
+ const target = event.target;
20
+ if (!(target instanceof Node) ||
21
+ ref.current?.contains(target) ||
22
+ latest.current.triggerRef?.current?.contains(target))
23
+ return;
24
+ latest.current.onDismiss();
25
+ };
26
+ document.addEventListener("keydown", keydown, true);
27
+ document.addEventListener("pointerdown", pointerdown);
28
+ return () => {
29
+ layers.splice(layers.indexOf(layer), 1);
30
+ document.removeEventListener("keydown", keydown, true);
31
+ document.removeEventListener("pointerdown", pointerdown);
32
+ };
33
+ }, [ref, opts.open]);
34
+ }
@@ -0,0 +1,9 @@
1
+ import { type KeyboardEventHandler } from "react";
2
+ import type { useListState } from "../state/useListState.js";
3
+ export declare function useListKeyboard<K extends string>(list: ReturnType<typeof useListState<K>>, opts: {
4
+ orientation: "vertical" | "horizontal";
5
+ labels: Record<string, string>;
6
+ onSelect?(key: string): void;
7
+ }): {
8
+ onKeyDown: KeyboardEventHandler;
9
+ };
@@ -0,0 +1,56 @@
1
+ import { useCallback, useEffect, useRef, } from "react";
2
+ export function useListKeyboard(list, opts) {
3
+ const buffer = useRef("");
4
+ const timer = useRef(undefined);
5
+ const onKeyDown = useCallback((event) => {
6
+ const horizontal = opts.orientation === "horizontal";
7
+ if ((!horizontal && event.key === "ArrowDown") ||
8
+ (horizontal && event.key === "ArrowRight")) {
9
+ event.preventDefault();
10
+ list.next();
11
+ return;
12
+ }
13
+ if ((!horizontal && event.key === "ArrowUp") ||
14
+ (horizontal && event.key === "ArrowLeft")) {
15
+ event.preventDefault();
16
+ list.previous();
17
+ return;
18
+ }
19
+ if (event.key === "Home") {
20
+ event.preventDefault();
21
+ list.first();
22
+ return;
23
+ }
24
+ if (event.key === "End") {
25
+ event.preventDefault();
26
+ list.last();
27
+ return;
28
+ }
29
+ if ((event.key === "Enter" || event.key === " ") && list.activeKey) {
30
+ event.preventDefault();
31
+ opts.onSelect?.(list.activeKey);
32
+ return;
33
+ }
34
+ if (event.key.length !== 1 ||
35
+ event.ctrlKey ||
36
+ event.metaKey ||
37
+ event.altKey)
38
+ return;
39
+ buffer.current += event.key.toLocaleLowerCase();
40
+ clearTimeout(timer.current);
41
+ timer.current = setTimeout(() => {
42
+ buffer.current = "";
43
+ }, 500);
44
+ const keys = list.enabledKeys;
45
+ const current = list.activeKey ? keys.indexOf(list.activeKey) : -1;
46
+ for (let i = 1; i <= keys.length; i++) {
47
+ const key = keys[(current + i) % keys.length];
48
+ if (opts.labels[key]?.toLocaleLowerCase().startsWith(buffer.current)) {
49
+ list.setActiveKey(key);
50
+ break;
51
+ }
52
+ }
53
+ }, [list, opts]);
54
+ useEffect(() => () => clearTimeout(timer.current), []);
55
+ return { onKeyDown };
56
+ }
@@ -0,0 +1,2 @@
1
+ import { type Ref, type RefCallback } from "react";
2
+ export declare function useMergeRefs<T>(...refs: (Ref<T> | undefined)[]): RefCallback<T>;
@@ -0,0 +1,18 @@
1
+ import { useCallback } from "react";
2
+ export function useMergeRefs(...refs) {
3
+ return useCallback((node) => {
4
+ const cleanups = refs.map((ref) => {
5
+ if (typeof ref === "function") {
6
+ const cleanup = ref(node);
7
+ return typeof cleanup === "function" ? cleanup : () => void ref(null);
8
+ }
9
+ if (ref)
10
+ ref.current = node;
11
+ return () => {
12
+ if (ref)
13
+ ref.current = null;
14
+ };
15
+ });
16
+ return () => cleanups.forEach((cleanup) => cleanup());
17
+ }, refs);
18
+ }
@@ -0,0 +1,13 @@
1
+ import { type CSSProperties, type RefObject } from "react";
2
+ type Placement = "bottom-start" | "bottom-end" | "top-start" | "top" | "bottom" | "right-start";
3
+ type Options = {
4
+ open: boolean;
5
+ placement?: Placement;
6
+ offset?: number;
7
+ };
8
+ type Position = {
9
+ style: CSSProperties;
10
+ placement: string;
11
+ };
12
+ export declare function usePosition(triggerRef: RefObject<HTMLElement | null>, overlayRef: RefObject<HTMLElement | null>, opts: Options): Position;
13
+ export {};
@@ -0,0 +1,76 @@
1
+ import { useLayoutEffect, useState, } from "react";
2
+ export function usePosition(triggerRef, overlayRef, opts) {
3
+ const placement = opts.placement ?? "bottom";
4
+ const [position, setPosition] = useState({
5
+ style: { position: "fixed" },
6
+ placement,
7
+ });
8
+ useLayoutEffect(() => {
9
+ if (!opts.open || !triggerRef.current || !overlayRef.current)
10
+ return;
11
+ const update = () => {
12
+ const trigger = triggerRef.current.getBoundingClientRect();
13
+ const overlay = overlayRef.current.getBoundingClientRect();
14
+ const gap = opts.offset ?? 4;
15
+ let side = placement.startsWith("top")
16
+ ? "top"
17
+ : placement === "right-start"
18
+ ? "right"
19
+ : "bottom";
20
+ if (side === "bottom" &&
21
+ trigger.bottom + gap + overlay.height > window.innerHeight - 8 &&
22
+ trigger.top - gap - overlay.height >= 8)
23
+ side = "top";
24
+ if (side === "top" &&
25
+ trigger.top - gap - overlay.height < 8 &&
26
+ trigger.bottom + gap + overlay.height <= window.innerHeight - 8)
27
+ side = "bottom";
28
+ const align = placement.endsWith("-end")
29
+ ? "end"
30
+ : placement.endsWith("-start")
31
+ ? "start"
32
+ : "center";
33
+ let left = align === "end"
34
+ ? trigger.right - overlay.width
35
+ : align === "center"
36
+ ? trigger.left + trigger.width / 2 - overlay.width / 2
37
+ : trigger.left;
38
+ let top = side === "top"
39
+ ? trigger.top - overlay.height - gap
40
+ : side === "right"
41
+ ? trigger.top
42
+ : trigger.bottom + gap;
43
+ if (side === "right")
44
+ left = trigger.right + gap;
45
+ left = Math.max(8, Math.min(left, window.innerWidth - overlay.width - 8));
46
+ top = Math.max(8, Math.min(top, window.innerHeight - overlay.height - 8));
47
+ const actual = side === "right"
48
+ ? "right-start"
49
+ : align === "center"
50
+ ? side
51
+ : `${side}-${align}`;
52
+ setPosition({
53
+ style: {
54
+ position: "fixed",
55
+ margin: 0,
56
+ right: "auto",
57
+ bottom: "auto",
58
+ left,
59
+ top,
60
+ },
61
+ placement: actual,
62
+ });
63
+ };
64
+ update();
65
+ const observer = new ResizeObserver(update);
66
+ observer.observe(overlayRef.current);
67
+ window.addEventListener("resize", update);
68
+ window.addEventListener("scroll", update, true);
69
+ return () => {
70
+ observer.disconnect();
71
+ window.removeEventListener("resize", update);
72
+ window.removeEventListener("scroll", update, true);
73
+ };
74
+ }, [opts.open, opts.offset, placement, triggerRef, overlayRef]);
75
+ return position;
76
+ }
@@ -0,0 +1,5 @@
1
+ export declare function useTooltipDelay(): {
2
+ open: boolean;
3
+ onEnter(): void;
4
+ onLeave(): void;
5
+ };
@@ -0,0 +1,28 @@
1
+ import { useCallback, useEffect, useRef, useState } from "react";
2
+ let warmUntil = 0;
3
+ export function useTooltipDelay() {
4
+ const [open, setOpen] = useState(false);
5
+ const openRef = useRef(false);
6
+ openRef.current = open;
7
+ const timer = useRef(undefined);
8
+ const onEnter = useCallback(() => {
9
+ clearTimeout(timer.current);
10
+ if (Date.now() < warmUntil) {
11
+ setOpen(true);
12
+ return;
13
+ }
14
+ timer.current = setTimeout(() => setOpen(true), 700);
15
+ }, []);
16
+ const onLeave = useCallback(() => {
17
+ clearTimeout(timer.current);
18
+ if (openRef.current)
19
+ warmUntil = Date.now() + 300;
20
+ setOpen(false);
21
+ }, []);
22
+ useEffect(() => () => {
23
+ clearTimeout(timer.current);
24
+ if (openRef.current)
25
+ warmUntil = Date.now() + 300;
26
+ }, []);
27
+ return { open, onEnter, onLeave };
28
+ }
@@ -0,0 +1,2 @@
1
+ import { type RefObject } from "react";
2
+ export declare function useTopLayer(triggerRef: RefObject<HTMLElement | null>, overlayRef: RefObject<HTMLElement | null>, open: boolean): Element | null;
@@ -0,0 +1,12 @@
1
+ import { useLayoutEffect } from "react";
2
+ // A modal dialog makes everything outside it inert, so overlays render inside the open dialog.
3
+ // showPopover puts them in the top layer, out of the dialog's clipping and backdrop-filter containing block.
4
+ export function useTopLayer(triggerRef, overlayRef, open) {
5
+ useLayoutEffect(() => {
6
+ if (open)
7
+ overlayRef.current?.showPopover?.();
8
+ }, [open, overlayRef]);
9
+ return open
10
+ ? (triggerRef.current?.closest("dialog[open]") ?? document.body)
11
+ : null;
12
+ }
@@ -0,0 +1,25 @@
1
+ export { Badge } from "./styled/Badge.js";
2
+ export { Button } from "./styled/Button.js";
3
+ export { Checkbox } from "./styled/Checkbox.js";
4
+ export { CodeArea, type CodeAreaHandle, type CodeSuggest, type CodeSuggestion, } from "./styled/CodeArea.js";
5
+ export { ConfirmProvider, useConfirm, type ConfirmOptions, } from "./styled/Confirm.js";
6
+ export { CopyButton } from "./styled/CopyButton.js";
7
+ export { Dialog } from "./styled/Dialog.js";
8
+ export { Disclosure } from "./styled/Disclosure.js";
9
+ export { EmptyState } from "./styled/EmptyState.js";
10
+ export { FileField, type FileFieldProps } from "./styled/FileField.js";
11
+ export { FieldShell } from "./styled/FieldShell.js";
12
+ export { inputClass, useFieldIds, type FieldProps } from "./styled/field.js";
13
+ export { NumberField } from "./styled/NumberField.js";
14
+ export { Section } from "./styled/Section.js";
15
+ export { Select } from "./styled/Select.js";
16
+ export { Table, Td, Th, type SortDirection } from "./styled/Table.js";
17
+ export { Tabs, type TabItem } from "./styled/Tabs.js";
18
+ export { TextField } from "./styled/TextField.js";
19
+ export { ToastProvider, useToast } from "./styled/Toasts.js";
20
+ export { Switch } from "./styled/Switch.js";
21
+ export { RadioGroup } from "./styled/RadioGroup.js";
22
+ export { Popover } from "./styled/Popover.js";
23
+ export { Menu, type MenuItem } from "./styled/Menu.js";
24
+ export { Tooltip } from "./styled/Tooltip.js";
25
+ export { Combobox, type ComboboxOption } from "./styled/Combobox.js";
package/dist/index.js ADDED
@@ -0,0 +1,25 @@
1
+ export { Badge } from "./styled/Badge.js";
2
+ export { Button } from "./styled/Button.js";
3
+ export { Checkbox } from "./styled/Checkbox.js";
4
+ export { CodeArea, } from "./styled/CodeArea.js";
5
+ export { ConfirmProvider, useConfirm, } from "./styled/Confirm.js";
6
+ export { CopyButton } from "./styled/CopyButton.js";
7
+ export { Dialog } from "./styled/Dialog.js";
8
+ export { Disclosure } from "./styled/Disclosure.js";
9
+ export { EmptyState } from "./styled/EmptyState.js";
10
+ export { FileField } from "./styled/FileField.js";
11
+ export { FieldShell } from "./styled/FieldShell.js";
12
+ export { inputClass, useFieldIds } from "./styled/field.js";
13
+ export { NumberField } from "./styled/NumberField.js";
14
+ export { Section } from "./styled/Section.js";
15
+ export { Select } from "./styled/Select.js";
16
+ export { Table, Td, Th } from "./styled/Table.js";
17
+ export { Tabs } from "./styled/Tabs.js";
18
+ export { TextField } from "./styled/TextField.js";
19
+ export { ToastProvider, useToast } from "./styled/Toasts.js";
20
+ export { Switch } from "./styled/Switch.js";
21
+ export { RadioGroup } from "./styled/RadioGroup.js";
22
+ export { Popover } from "./styled/Popover.js";
23
+ export { Menu } from "./styled/Menu.js";
24
+ export { Tooltip } from "./styled/Tooltip.js";
25
+ export { Combobox } from "./styled/Combobox.js";
@@ -0,0 +1,18 @@
1
+ export type ComboboxOption = {
2
+ key: string;
3
+ label: string;
4
+ disabled?: boolean;
5
+ };
6
+ export declare function Combobox(props: {
7
+ label: string;
8
+ hideLabel?: boolean;
9
+ options: ComboboxOption[];
10
+ value: string | null;
11
+ onChange(key: string | null): void;
12
+ inputValue?: string;
13
+ onInputChange?(text: string): void;
14
+ placeholder?: string;
15
+ emptyText?: string;
16
+ className?: string;
17
+ listClassName?: string;
18
+ }): import("react").JSX.Element;
@@ -0,0 +1,98 @@
1
+ import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { createPortal } from "react-dom";
3
+ import { useEffect, useId, useRef, useState, } from "react";
4
+ import { useDismiss } from "../hooks/useDismiss.js";
5
+ import { usePosition } from "../hooks/usePosition.js";
6
+ import { useTopLayer } from "../hooks/useTopLayer.js";
7
+ import { useListState } from "../state/useListState.js";
8
+ export function Combobox(props) {
9
+ const id = useId();
10
+ const inputRef = useRef(null);
11
+ const listRef = useRef(null);
12
+ const [open, setOpen] = useState(false);
13
+ const [typed, setTyped] = useState(null);
14
+ const [seenValue, setSeenValue] = useState(props.value);
15
+ if (seenValue !== props.value) {
16
+ setSeenValue(props.value);
17
+ setTyped(null);
18
+ }
19
+ const selectedLabel = props.options.find((option) => option.key === props.value)?.label ?? "";
20
+ const text = props.inputValue ?? typed ?? selectedLabel;
21
+ const filterText = text === selectedLabel ? "" : text.toLocaleLowerCase();
22
+ const list = props.onInputChange
23
+ ? props.options
24
+ : props.options.filter((option) => option.label.toLocaleLowerCase().includes(filterText));
25
+ const listState = useListState({
26
+ keys: list.map((option) => option.key),
27
+ disabledKeys: list
28
+ .filter((option) => option.disabled)
29
+ .map((option) => option.key),
30
+ });
31
+ const container = useTopLayer(inputRef, listRef, open);
32
+ const position = usePosition(inputRef, listRef, {
33
+ open,
34
+ placement: "bottom-start",
35
+ });
36
+ useDismiss(listRef, {
37
+ open,
38
+ onDismiss: () => setOpen(false),
39
+ triggerRef: inputRef,
40
+ });
41
+ const setText = (next) => {
42
+ props.onInputChange?.(next);
43
+ setTyped(next);
44
+ if (next === "" && props.value !== null)
45
+ props.onChange(null);
46
+ };
47
+ const select = (key) => {
48
+ props.onChange(key);
49
+ const label = props.options.find((option) => option.key === key)?.label ?? "";
50
+ props.onInputChange?.(label);
51
+ setTyped(label);
52
+ setOpen(false);
53
+ };
54
+ useEffect(() => {
55
+ if (!open || !listState.activeKey)
56
+ return;
57
+ document
58
+ .getElementById(`${id}-option-${encodeURIComponent(listState.activeKey)}`)
59
+ ?.scrollIntoView?.({ block: "nearest" });
60
+ }, [id, listState.activeKey, open]);
61
+ const onChange = (event) => {
62
+ setText(event.currentTarget.value);
63
+ setOpen(true);
64
+ listState.setActiveKey(null);
65
+ };
66
+ const onKeyDown = (event) => {
67
+ if (event.key === "ArrowDown") {
68
+ event.preventDefault();
69
+ if (!open) {
70
+ setOpen(true);
71
+ listState.first();
72
+ }
73
+ else
74
+ listState.next();
75
+ }
76
+ else if (event.key === "ArrowUp" && open) {
77
+ event.preventDefault();
78
+ listState.previous();
79
+ }
80
+ else if (event.key === "Enter" && open && listState.activeKey) {
81
+ event.preventDefault();
82
+ select(listState.activeKey);
83
+ }
84
+ else if (event.key === "Escape" && !open && text) {
85
+ event.preventDefault();
86
+ setText("");
87
+ }
88
+ else if (event.key === "Tab")
89
+ setOpen(false);
90
+ };
91
+ const activeOption = listState.activeKey
92
+ ? list.find((option) => option.key === listState.activeKey)
93
+ : undefined;
94
+ return (_jsxs(_Fragment, { children: [_jsx("label", { id: `${id}-label`, htmlFor: `${id}-input`, className: props.hideLabel ? "sr-only" : undefined, children: props.label }), _jsx("input", { ref: inputRef, id: `${id}-input`, role: "combobox", "aria-autocomplete": "list", "aria-expanded": open, "aria-controls": open ? `${id}-listbox` : undefined, "aria-activedescendant": open && activeOption
95
+ ? `${id}-option-${encodeURIComponent(activeOption.key)}`
96
+ : undefined, value: text, placeholder: props.placeholder, className: props.className, "data-open": open ? "" : undefined, onChange: onChange, onKeyDown: onKeyDown, onFocus: () => setOpen(true), onClick: () => setOpen(true) }), container &&
97
+ createPortal(_jsxs("div", { ref: listRef, popover: "manual", className: props.listClassName, style: { ...position.style, maxHeight: 240, overflowY: "auto" }, "data-open": "", "data-placement": position.placement, children: [_jsx("div", { id: `${id}-listbox`, role: "listbox", "aria-labelledby": `${id}-label`, children: list.map((option) => (_jsx("div", { id: `${id}-option-${encodeURIComponent(option.key)}`, role: "option", "data-key": option.key, "data-active": listState.activeKey === option.key ? "" : undefined, "aria-selected": props.value === option.key, "aria-disabled": option.disabled || undefined, onPointerMove: () => !option.disabled && listState.setActiveKey(option.key), onMouseDown: (event) => event.preventDefault(), onClick: () => !option.disabled && select(option.key), children: option.label }, option.key))) }), !list.length && (_jsx("div", { "data-empty": "", children: props.emptyText ?? "No options" }))] }), container)] }));
98
+ }
@@ -0,0 +1,10 @@
1
+ import { type ReactNode } from "react";
2
+ export declare function Dialog(props: {
3
+ open: boolean;
4
+ onClose(): void;
5
+ title: string;
6
+ children: ReactNode;
7
+ footer?: ReactNode;
8
+ closeIcon?: ReactNode;
9
+ className?: string;
10
+ }): import("react").ReactPortal;
@@ -0,0 +1,48 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { useEffect, useId, useRef } from "react";
3
+ import { createPortal } from "react-dom";
4
+ const FIRST_FOCUS = ["body", "footer"]
5
+ .flatMap((part) => ["input", "select", "textarea", "button"].map((tag) => `[data-part=${part}] ${tag}:not([disabled])`))
6
+ .join(", ");
7
+ export function Dialog(props) {
8
+ const ref = useRef(null);
9
+ const opener = useRef(null);
10
+ const titleId = useId();
11
+ const restoreFocus = () => {
12
+ opener.current?.focus?.();
13
+ opener.current = null;
14
+ };
15
+ useEffect(() => {
16
+ const dialog = ref.current;
17
+ if (!dialog)
18
+ return;
19
+ if (props.open && !dialog.open) {
20
+ opener.current = document.activeElement;
21
+ dialog.showModal();
22
+ if (!dialog.contains(document.activeElement)) {
23
+ const first = [
24
+ ...dialog.querySelectorAll(FIRST_FOCUS),
25
+ ].find((el) => !el.closest("[hidden]"));
26
+ (first ?? dialog.querySelector("[data-part=close]"))?.focus();
27
+ }
28
+ }
29
+ if (!props.open && dialog.open) {
30
+ dialog.close();
31
+ restoreFocus();
32
+ }
33
+ }, [props.open]);
34
+ useEffect(() => restoreFocus, []);
35
+ return createPortal(_jsx("dialog", { ref: ref, "aria-labelledby": titleId, className: props.className, onSubmit: (event) => event.stopPropagation(), onKeyDown: (event) => {
36
+ if (event.key !== "Escape")
37
+ return;
38
+ event.preventDefault();
39
+ event.stopPropagation();
40
+ props.onClose();
41
+ }, onClose: (event) => {
42
+ if (event.target !== event.currentTarget)
43
+ return;
44
+ restoreFocus();
45
+ if (props.open)
46
+ props.onClose();
47
+ }, children: props.open && (_jsxs("div", { "data-part": "content", children: [_jsxs("div", { "data-part": "header", children: [_jsx("h2", { id: titleId, "data-part": "title", children: props.title }), _jsx("button", { type: "button", "data-part": "close", "aria-label": "Close", onClick: props.onClose, children: props.closeIcon ?? "\u00d7" })] }), _jsx("div", { "data-part": "body", children: props.children }), props.footer && _jsx("div", { "data-part": "footer", children: props.footer })] })) }), document.body);
48
+ }
@@ -0,0 +1,22 @@
1
+ import { type ReactElement, type ReactNode } from "react";
2
+ import type { Placement } from "./Popover.js";
3
+ export type MenuItem = {
4
+ key: string;
5
+ label: string;
6
+ icon?: ReactNode;
7
+ shortcut?: string;
8
+ danger?: boolean;
9
+ disabled?: boolean;
10
+ checked?: boolean;
11
+ } | {
12
+ heading: string;
13
+ } | {
14
+ separator: true;
15
+ };
16
+ export declare function Menu(props: {
17
+ trigger: ReactElement<Record<string, unknown>>;
18
+ items: MenuItem[];
19
+ onAction(key: string): void;
20
+ placement?: Placement;
21
+ className?: string;
22
+ }): import("react").JSX.Element;