@glimstone/react 0.0.0-stage → 4.1.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 (86) hide show
  1. package/README.md +125 -2
  2. package/dist/AboutCard.d.ts +152 -0
  3. package/dist/AboutCard.js +82 -0
  4. package/dist/Badge.d.ts +47 -0
  5. package/dist/Badge.js +35 -0
  6. package/dist/Button.d.ts +54 -0
  7. package/dist/Button.js +52 -0
  8. package/dist/Card.d.ts +29 -0
  9. package/dist/Card.js +20 -0
  10. package/dist/Checkbox.d.ts +14 -0
  11. package/dist/Checkbox.js +7 -0
  12. package/dist/CoffeeDialog.d.ts +17 -0
  13. package/dist/CoffeeDialog.js +9 -0
  14. package/dist/ColorPicker.d.ts +21 -0
  15. package/dist/ColorPicker.js +68 -0
  16. package/dist/Combobox.d.ts +16 -0
  17. package/dist/Combobox.js +32 -0
  18. package/dist/ConfirmDialog.d.ts +29 -0
  19. package/dist/ConfirmDialog.js +15 -0
  20. package/dist/ContextMenu.d.ts +23 -0
  21. package/dist/ContextMenu.js +137 -0
  22. package/dist/CopyField.d.ts +15 -0
  23. package/dist/CopyField.js +24 -0
  24. package/dist/CryptoDonateDialog.d.ts +61 -0
  25. package/dist/CryptoDonateDialog.js +23 -0
  26. package/dist/DropdownListbox.d.ts +22 -0
  27. package/dist/DropdownListbox.js +182 -0
  28. package/dist/EmptyState.d.ts +19 -0
  29. package/dist/EmptyState.js +10 -0
  30. package/dist/IconTipButton.d.ts +18 -0
  31. package/dist/IconTipButton.js +13 -0
  32. package/dist/InfoBubble.d.ts +4 -0
  33. package/dist/InfoBubble.js +19 -0
  34. package/dist/Kbd.d.ts +5 -0
  35. package/dist/Kbd.js +6 -0
  36. package/dist/MenuBar.d.ts +18 -0
  37. package/dist/MenuBar.js +40 -0
  38. package/dist/Meter.d.ts +18 -0
  39. package/dist/Meter.js +9 -0
  40. package/dist/NumberField.d.ts +16 -0
  41. package/dist/NumberField.js +18 -0
  42. package/dist/PasswordField.d.ts +12 -0
  43. package/dist/PasswordField.js +8 -0
  44. package/dist/PaypalDialog.d.ts +51 -0
  45. package/dist/PaypalDialog.js +20 -0
  46. package/dist/Pill.d.ts +12 -0
  47. package/dist/Pill.js +5 -0
  48. package/dist/ProgressBar.d.ts +14 -0
  49. package/dist/ProgressBar.js +6 -0
  50. package/dist/ReadmeButton.d.ts +29 -0
  51. package/dist/ReadmeButton.js +27 -0
  52. package/dist/Select.d.ts +41 -0
  53. package/dist/Select.js +192 -0
  54. package/dist/Selector.d.ts +67 -0
  55. package/dist/Selector.js +128 -0
  56. package/dist/SidebarRow.d.ts +22 -0
  57. package/dist/SidebarRow.js +10 -0
  58. package/dist/Slider.d.ts +12 -0
  59. package/dist/Slider.js +18 -0
  60. package/dist/SubCard.d.ts +15 -0
  61. package/dist/SubCard.js +14 -0
  62. package/dist/Toast.d.ts +29 -0
  63. package/dist/Toast.js +24 -0
  64. package/dist/Toggle.d.ts +16 -0
  65. package/dist/Toggle.js +4 -0
  66. package/dist/UnavailableNotice.d.ts +15 -0
  67. package/dist/UnavailableNotice.js +12 -0
  68. package/dist/Window.d.ts +37 -0
  69. package/dist/Window.js +43 -0
  70. package/dist/WindowBar.d.ts +24 -0
  71. package/dist/WindowBar.js +31 -0
  72. package/dist/glyphs.d.ts +26 -0
  73. package/dist/glyphs.js +34 -0
  74. package/dist/index.d.ts +39 -0
  75. package/dist/index.js +39 -0
  76. package/dist/portalHue.d.ts +10 -0
  77. package/dist/portalHue.js +15 -0
  78. package/dist/useConfirm.d.ts +26 -0
  79. package/dist/useConfirm.js +39 -0
  80. package/dist/useLabelMode.d.ts +6 -0
  81. package/dist/useLabelMode.js +11 -0
  82. package/dist/usePaypalButtons.d.ts +15 -0
  83. package/dist/usePaypalButtons.js +42 -0
  84. package/dist/useTipBubble.d.ts +34 -0
  85. package/dist/useTipBubble.js +63 -0
  86. package/package.json +28 -4
@@ -0,0 +1,17 @@
1
+ import type { Ref } from "react";
2
+ export interface CoffeeDialogProps {
3
+ /** `https://buymeacoffee.com/widget/page/<handle>` with its query. */
4
+ widgetUrl: string;
5
+ text: {
6
+ title: string;
7
+ /** The app's appeal, the same in every giving window. */
8
+ appeal: string;
9
+ /** One sentence: the payment happens at BMAC, and no account is needed. */
10
+ intro: string;
11
+ closeLabel: string;
12
+ };
13
+ onClose: () => void;
14
+ /** The window's own card, for the app's Escape handling and focus trap. */
15
+ ref?: Ref<HTMLDivElement>;
16
+ }
17
+ export declare function CoffeeDialog({ widgetUrl, text, onClose, ref }: CoffeeDialogProps): import("react").JSX.Element;
@@ -0,0 +1,9 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { Badge } from "./Badge.js";
3
+ import { Button } from "./Button.js";
4
+ export function CoffeeDialog({ widgetUrl, text, onClose, ref }) {
5
+ return (_jsx("div", { className: "glim-modal-backdrop", onClick: (e) => {
6
+ if (e.target === e.currentTarget)
7
+ onClose();
8
+ }, children: _jsxs("div", { ref: ref, tabIndex: -1, role: "dialog", "aria-modal": "true", "aria-labelledby": "coffee-title", className: "glim-modal-card glim-modal-card--wide glim-modal-card--tall", children: [_jsx("div", { className: "glim-modal-head", children: _jsx("h2", { id: "coffee-title", className: "glim-modal-title", children: _jsx(Badge, { tone: "heading", size: "heading", wrap: true, children: text.title }) }) }), _jsxs("div", { className: "glim-give-body", children: [_jsx("p", { className: "glim-give-appeal", children: text.appeal }), _jsx("p", { className: "glim-give-intro", children: text.intro }), _jsx("div", { className: "glim-give-frame", children: _jsx("iframe", { src: widgetUrl, title: text.title, allow: "payment", className: "glim-give-iframe" }) })] }), _jsx("div", { className: "glim-give-foot", children: _jsx(Button, { label: text.closeLabel, labelKey: "common.close", tone: "neutral", onClick: onClose }) })] }) }));
9
+ }
@@ -0,0 +1,21 @@
1
+ import { type ColorName, type ColorPickerPanelText } from "@glimstone/core";
2
+ export type { ColorName, ColorPickerPanelText } from "@glimstone/core";
3
+ export interface ColorPickerProps {
4
+ /** The colour the picker starts from, as `#rrggbb`. */
5
+ value: string;
6
+ /** Apply or Enter, with the colour and the name it proposes. */
7
+ onApply: (hex: string, name: string | null) => void;
8
+ /** Every step of a drag, a key or a valid hex, for a live preview. */
9
+ onInput?: (hex: string) => void;
10
+ /** The control that opened the picker. With it the picker floats beside it
11
+ * and closes on Escape, on a click beside it and after Apply. */
12
+ anchor?: HTMLElement | null;
13
+ /** Called when a floating picker closes, however it closes. */
14
+ onClose?: () => void;
15
+ /** The names a colour is matched against, nearest in OKLab. `false` shows none. */
16
+ names?: readonly ColorName[] | false;
17
+ /** The words, read when the picker opens. */
18
+ text?: Partial<ColorPickerPanelText>;
19
+ className?: string;
20
+ }
21
+ export declare function ColorPicker({ value, onApply, onInput, anchor, onClose, names, text, className }: ColorPickerProps): import("react").JSX.Element;
@@ -0,0 +1,68 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { useEffect, useLayoutEffect, useRef, useState } from "react";
3
+ import { createPortal } from "react-dom";
4
+ import { COLOR_NAMES_DE, COLOR_PICKER_TEXT, colorPicker, floatColorPicker, nearestColorName, normalizeHex, } from "@glimstone/core";
5
+ import { Button } from "./Button.js";
6
+ import { IconCheck } from "./glyphs.js";
7
+ export function ColorPicker({ value, onApply, onInput, anchor, onClose, names = COLOR_NAMES_DE, text, className }) {
8
+ const words = { ...COLOR_PICKER_TEXT, ...text };
9
+ const panel = useRef(null);
10
+ const host = useRef(null);
11
+ const widget = useRef(null);
12
+ const [draft, setDraft] = useState(() => normalizeHex(value) ?? "#000000");
13
+ const [typed, setTyped] = useState(draft);
14
+ const latest = useRef({ onInput, onClose });
15
+ useLayoutEffect(() => {
16
+ latest.current = { onInput, onClose };
17
+ });
18
+ useLayoutEffect(() => {
19
+ const picker = colorPicker(draft, (hex) => {
20
+ setDraft(hex);
21
+ setTyped(hex);
22
+ latest.current.onInput?.(hex);
23
+ }, { text: words });
24
+ host.current.appendChild(picker.el);
25
+ widget.current = picker;
26
+ return () => picker.el.remove();
27
+ // Built once; a new value reaches it through setValue below.
28
+ }, []);
29
+ useEffect(() => {
30
+ const hex = normalizeHex(value);
31
+ if (!hex)
32
+ return;
33
+ setDraft(hex);
34
+ setTyped(hex);
35
+ widget.current?.setValue(hex);
36
+ }, [value]);
37
+ useLayoutEffect(() => {
38
+ if (!anchor || !panel.current)
39
+ return;
40
+ const done = floatColorPicker(panel.current, anchor, () => latest.current.onClose?.());
41
+ widget.current?.focus();
42
+ return done;
43
+ }, [anchor]);
44
+ const name = names === false ? null : nearestColorName(draft, names);
45
+ function apply() {
46
+ onApply(draft, name);
47
+ if (anchor) {
48
+ anchor.focus();
49
+ onClose?.();
50
+ }
51
+ }
52
+ function onKeyDown(e) {
53
+ if (e.key === "Enter" && !(e.target instanceof HTMLButtonElement)) {
54
+ e.preventDefault();
55
+ apply();
56
+ }
57
+ }
58
+ const body = (_jsxs("div", { ref: panel, className: ["glim-picker-panel", anchor && "glim-picker-panel--float", className].filter(Boolean).join(" "), style: { "--glim-picker-colour": draft }, onKeyDown: onKeyDown, children: [_jsx("div", { ref: host, style: { display: "contents" } }), _jsxs("div", { className: "glim-picker-foot", children: [_jsx("span", { className: "glim-picker-swatch", "aria-hidden": "true" }), name !== null && _jsx("span", { className: "glim-picker-name", children: name }), _jsx("input", { className: "glim-picker-hex", value: typed, maxLength: 7, spellCheck: false, "aria-label": words.hex, onChange: (e) => {
59
+ setTyped(e.target.value);
60
+ const hex = normalizeHex(e.target.value);
61
+ if (!hex)
62
+ return;
63
+ setDraft(hex);
64
+ widget.current?.setValue(hex);
65
+ onInput?.(hex);
66
+ } })] }), _jsx("div", { className: "glim-picker-actions", children: _jsx(Button, { label: words.apply, labelKey: "apply", glyph: _jsx(IconCheck, {}), tone: "accent", onClick: apply }) })] }));
67
+ return anchor ? createPortal(body, document.body) : body;
68
+ }
@@ -0,0 +1,16 @@
1
+ import type { SelectOption } from "./Select.tsx";
2
+ export interface ComboboxProps {
3
+ label: string;
4
+ hideLabel?: boolean;
5
+ options: SelectOption[];
6
+ /** The chosen value, or "" while nothing is chosen. */
7
+ value: string;
8
+ onChange: (value: string) => void;
9
+ placeholder?: string;
10
+ /** The line the list shows when nothing matches. */
11
+ empty: string;
12
+ /** Accessible name of the button that opens the full list. */
13
+ toggleLabel: string;
14
+ disabled?: boolean;
15
+ }
16
+ export declare function Combobox({ label, hideLabel, options, value, onChange, placeholder, empty, toggleLabel, disabled }: ComboboxProps): import("react").JSX.Element;
@@ -0,0 +1,32 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { useId, useState } from "react";
3
+ import * as combobox from "@zag-js/combobox";
4
+ import { normalizeProps, Portal, useMachine } from "@zag-js/react";
5
+ import { IconCheck, IconExpand } from "./glyphs.js";
6
+ export function Combobox({ label, hideLabel, options, value, onChange, placeholder, empty, toggleLabel, disabled }) {
7
+ const [shown, setShown] = useState(options);
8
+ const collection = combobox.collection({
9
+ items: shown,
10
+ itemToValue: (o) => o.value,
11
+ itemToString: (o) => o.label,
12
+ isItemDisabled: (o) => !!o.disabled,
13
+ });
14
+ const service = useMachine(combobox.machine, {
15
+ id: useId(),
16
+ collection,
17
+ value: value ? [value] : [],
18
+ placeholder,
19
+ disabled,
20
+ positioning: { sameWidth: true, gutter: 6 },
21
+ // Typing opens the list after the filter has run, so only a close may put
22
+ // the full list back.
23
+ onOpenChange: ({ open }) => !open && setShown(options),
24
+ onInputValueChange: ({ inputValue }) => {
25
+ const q = inputValue.toLocaleLowerCase();
26
+ setShown(options.filter((o) => o.label.toLocaleLowerCase().includes(q)));
27
+ },
28
+ onValueChange: (d) => onChange(d.value[0] ?? ""),
29
+ });
30
+ const api = combobox.connect(service, normalizeProps);
31
+ return (_jsxs("div", { ...api.getRootProps(), className: "glim-combobox", children: [_jsx("label", { ...api.getLabelProps(), className: hideLabel ? "glim-sr-only" : "glim-field-label", children: label }), _jsxs("div", { ...api.getControlProps(), className: "glim-combobox-control", children: [_jsx("input", { ...api.getInputProps(), className: "glim-combobox-input" }), _jsx("button", { ...api.getTriggerProps(), "aria-label": toggleLabel, className: "glim-combobox-toggle", children: _jsx("span", { className: "glim-select-indicator", "data-state": api.open ? "open" : "closed", children: _jsx(IconExpand, {}) }) })] }), _jsx(Portal, { children: _jsx("div", { ...api.getPositionerProps(), className: "glim-select-positioner", children: _jsxs("ul", { ...api.getContentProps(), className: "glim-select-content", children: [shown.length === 0 && _jsx("li", { className: "glim-select-empty", children: empty }), shown.map((item) => (_jsxs("li", { ...api.getItemProps({ item }), className: "glim-select-item", children: [_jsx("span", { ...api.getItemTextProps({ item }), children: item.label }), _jsx("span", { ...api.getItemIndicatorProps({ item }), className: "glim-select-check", children: _jsx(IconCheck, {}) })] }, item.value)))] }) }) })] }));
32
+ }
@@ -0,0 +1,29 @@
1
+ import type { ReactNode, Ref } from "react";
2
+ export interface ConfirmDialogProps {
3
+ /** A generic title such as "Confirm"; the call site's own text is `message`. */
4
+ title: string;
5
+ /** The question or explanation, also the dialog's accessible description. */
6
+ message: string;
7
+ confirmLabel: string;
8
+ /** Translation key behind `confirmLabel`, so the confirm button can pick
9
+ * a glyph. Optional: a caller passing a composed or data label has none. */
10
+ confirmLabelKey?: string;
11
+ cancelLabel: string;
12
+ /** A control the confirming action needs an answer to, such as "also remove
13
+ * X?", shown under the message so the question is asked in one window.
14
+ * Keep it to a switch or two. */
15
+ extra?: ReactNode;
16
+ /** Glyph for the confirm button. The action changes per call site, so no
17
+ * fixed key can name it; without one the button beside a glyphed Cancel
18
+ * looks unfinished. */
19
+ confirmGlyph?: ReactNode;
20
+ /** Accessible name for the header close button, separate from cancelLabel so
21
+ * the two controls are not announced alike. Leave it out and the button is
22
+ * not drawn, since a corner close beside Cancel reads as a second choice. */
23
+ closeLabel?: string;
24
+ onConfirm: () => void;
25
+ onCancel: () => void;
26
+ /** The dialog card's root DOM node, for useConfirm.tsx's focus trap. */
27
+ ref?: Ref<HTMLDivElement>;
28
+ }
29
+ export declare function ConfirmDialog({ title, message, confirmLabel, confirmLabelKey, confirmGlyph, extra, cancelLabel, closeLabel, onConfirm, onCancel, ref, }: ConfirmDialogProps): import("react").JSX.Element;
@@ -0,0 +1,15 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { Badge } from "./Badge.js";
3
+ import { Button } from "./Button.js";
4
+ import { IconCancel, IconClose } from "./glyphs.js";
5
+ export function ConfirmDialog({ title, message, confirmLabel, confirmLabelKey, confirmGlyph, extra, cancelLabel, closeLabel, onConfirm, onCancel, ref, }) {
6
+ return (_jsx("div", { className: "glim-modal-backdrop", onClick: (e) => {
7
+ if (e.target === e.currentTarget)
8
+ onCancel();
9
+ }, children: _jsxs("div", { ref: ref, role: "dialog", "aria-modal": "true", "aria-labelledby": "confirmdialog-title", "aria-describedby": "confirmdialog-message", className: "glim-modal-card", children: [_jsxs("div", { className: "glim-modal-head", children: [_jsx("h2", { id: "confirmdialog-title", className: "glim-modal-title", children: _jsx(Badge, { tone: "heading", size: "heading", wrap: true, children: title }) }), closeLabel !== undefined && (_jsx(Button, { label: closeLabel, labelKey: "common.close", glyph: _jsx(IconClose, {}), tone: "neutral", onClick: onCancel, className: "glim-modal-close" }))] }), _jsxs("div", { className: "glim-modal-body", children: [_jsx("p", { id: "confirmdialog-message", className: "glim-modal-text", children: message }), extra !== undefined && _jsx("div", { className: "glim-modal-extra", children: extra })] }), _jsxs("div", { className: "glim-modal-foot", children: [_jsx(Button, { label: cancelLabel, labelKey: "common.cancel", glyph: _jsx(IconCancel, {}), tone: "neutral", autoFocus: true, onClick: onCancel }), _jsx(Button, { label: confirmLabel,
10
+ // `null` means no key: the action being confirmed has no fixed one.
11
+ labelKey: confirmLabelKey ?? null, glyph: confirmGlyph,
12
+ // Not red ("Destructive and confirmable actions"): the dialog's
13
+ // sentence is the warning, and neither button is recommended.
14
+ tone: "neutral", onClick: onConfirm })] })] }) }));
15
+ }
@@ -0,0 +1,23 @@
1
+ import { type ReactNode } from "react";
2
+ import { type MenuEntry as CoreMenuEntry, type MenuHead as CoreMenuHead, type MenuItem as CoreMenuItem } from "@glimstone/core";
3
+ export type MenuEntry = CoreMenuEntry<ReactNode>;
4
+ export type MenuItem = CoreMenuItem<ReactNode>;
5
+ export type MenuHead = CoreMenuHead<ReactNode>;
6
+ export interface ContextMenuProps {
7
+ /** The point the menu hangs from, in viewport pixels, such as the pointer's. */
8
+ x: number;
9
+ y: number;
10
+ items: MenuEntry[];
11
+ /** Stand the menu on the point instead, for a menu opened from a bar at the bottom. */
12
+ above?: boolean;
13
+ /** The menu's accessible name. */
14
+ label?: string;
15
+ /** The control that opened the menu. A click on it is not a click outside,
16
+ * and focus goes back to it; without one, focus goes back to wherever it was. */
17
+ opener?: HTMLElement | null;
18
+ /** Called on every way out: a choice, Escape, Tab or a click outside. */
19
+ onClose: () => void;
20
+ /** Left or right on a row without a submenu, for a bar that steps to the next menu. */
21
+ onStep?: (direction: 1 | -1) => void;
22
+ }
23
+ export declare function ContextMenu({ x, y, items, above, label, opener, onClose, onStep }: ContextMenuProps): import("react").JSX.Element;
@@ -0,0 +1,137 @@
1
+ import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ // A menu at a point: a right-click on a row, the More button of a list, or a
3
+ // menu of the bar. Mount it to open it and unmount it on onClose. The model,
4
+ // the placing and the keys come from menu.ts in the core.
5
+ //
6
+ // A submenu opens beside its row as a second menu on the body, one level deep.
7
+ // A checkbox item leaves the menu open, so several can be ticked in a row; the
8
+ // caller flips `checked` and the row follows.
9
+ import { useLayoutEffect, useRef, useState } from "react";
10
+ import { createPortal } from "react-dom";
11
+ import { claimMenu, closeMenuOutside, focusedAsker, focusFirstMenuRow, focusMenuRow, isMenuHead, menuMove, menuRowView, placeMenu, placeSubmenu, returnFocus, tidyMenu, } from "@glimstone/core";
12
+ import { IconCheck, IconExpand, IconPencil } from "./glyphs.js";
13
+ import { Kbd } from "./Kbd.js";
14
+ import { useTipBubble } from "./useTipBubble.js";
15
+ const viewport = () => ({ width: innerWidth, height: innerHeight });
16
+ function HeadEdit({ tip, glyph, row, onPress }) {
17
+ const tooltip = useTipBubble(tip);
18
+ return (_jsxs(_Fragment, { children: [_jsx("button", { ref: tooltip.ref, type: "button", role: "menuitem", tabIndex: -1, "aria-label": tip, "aria-describedby": tooltip.describedBy, ...tooltip.handlers, className: "glim-menu-head-edit", "data-row": row, onClick: onPress, children: glyph ?? _jsx(IconPencil, {}) }), tooltip.bubble] }));
19
+ }
20
+ function Rows({ entries, openAt, onPick, }) {
21
+ return entries.map((entry, i) => {
22
+ if (entry === "-")
23
+ return _jsx("div", { className: "glim-menu-sep", role: "separator" }, i);
24
+ if (isMenuHead(entry)) {
25
+ return (_jsxs("div", { className: "glim-menu-head", role: "presentation", children: [_jsx("span", { children: entry.head }), entry.edit && _jsx(HeadEdit, { tip: entry.edit.tip, glyph: entry.edit.glyph, row: i, onPress: () => onPick(i) })] }, i));
26
+ }
27
+ const view = menuRowView(entry);
28
+ return (_jsxs("button", { type: "button", role: view.role, tabIndex: -1, "aria-checked": view.role === "menuitem" ? undefined : entry.checked, "aria-haspopup": entry.sub ? "menu" : undefined, "aria-expanded": entry.sub ? openAt === i : undefined, disabled: entry.disabled, className: view.current ? "glim-menu-item glim-menu-item--current" : "glim-menu-item", "data-row": i, onClick: () => onPick(i), children: [view.lead === "box" ? (_jsx("span", { className: "glim-checkbox-box", "aria-hidden": "true", children: _jsx(IconCheck, {}) })) : view.lead === "dot" ? (_jsx("span", { className: "glim-menu-dot", style: { backgroundColor: entry.color }, "aria-hidden": "true" })) : (_jsx("span", { className: "glim-menu-glyph", "aria-hidden": "true", children: view.lead === "glyph" && entry.glyph })), _jsx("span", { className: "glim-menu-label", children: entry.label }), view.end === "submenu" && (_jsx("span", { className: "glim-menu-end glim-menu-caret", "aria-hidden": "true", children: _jsx(IconExpand, {}) })), view.end === "tick" && (_jsx("span", { className: "glim-menu-end glim-menu-tick", "aria-hidden": "true", children: _jsx(IconCheck, {}) })), view.end === "shortcut" && _jsx(Kbd, { className: "glim-menu-end", children: entry.shortcut }), view.end === "count" && _jsx("span", { className: "glim-menu-end glim-menu-count glim-num", children: entry.count })] }, i));
29
+ });
30
+ }
31
+ export function ContextMenu({ x, y, items, above = false, label, opener, onClose, onStep }) {
32
+ const menu = useRef(null);
33
+ const subMenu = useRef(null);
34
+ const [asker] = useState(focusedAsker);
35
+ const [pos, setPos] = useState({ left: x, top: y });
36
+ const [sub, setSub] = useState(null);
37
+ const [subPos, setSubPos] = useState(null);
38
+ const latest = useRef({ onClose, onStep, opener });
39
+ useLayoutEffect(() => {
40
+ latest.current = { onClose, onStep, opener };
41
+ });
42
+ const rows = tidyMenu(items);
43
+ const host = sub ? rows[sub.at] : undefined;
44
+ const subRows = host && !isMenuHead(host) && host !== "-" && host.sub ? tidyMenu(host.sub) : null;
45
+ // Focus goes back only on a way out somebody chose; a click elsewhere keeps
46
+ // the focus it gave. It reads only refs, so the first render's copy serves
47
+ // the listeners too.
48
+ const finish = (handBack) => {
49
+ latest.current.onClose();
50
+ if (handBack)
51
+ returnFocus(latest.current.opener ?? asker);
52
+ };
53
+ useLayoutEffect(() => {
54
+ const el = menu.current;
55
+ setPos(placeMenu(x, y, { width: el.offsetWidth, height: el.offsetHeight }, viewport(), above));
56
+ }, [x, y, above]);
57
+ useLayoutEffect(() => {
58
+ const release = claimMenu(finish);
59
+ const outside = closeMenuOutside(() => [menu.current, subMenu.current, latest.current.opener], () => finish(false));
60
+ focusFirstMenuRow(menu.current);
61
+ return () => {
62
+ release();
63
+ outside();
64
+ };
65
+ // eslint-disable-next-line react-hooks/exhaustive-deps
66
+ }, []);
67
+ useLayoutEffect(() => {
68
+ const row = sub && menu.current?.querySelector(`[data-row="${sub.at}"]`);
69
+ if (!sub || !row || !subMenu.current) {
70
+ setSubPos(null);
71
+ return;
72
+ }
73
+ const rtl = getComputedStyle(menu.current).direction === "rtl";
74
+ const el = subMenu.current;
75
+ setSubPos(placeSubmenu(row.getBoundingClientRect(), { width: el.offsetWidth, height: el.offsetHeight }, viewport(), rtl));
76
+ if (sub.focus)
77
+ focusFirstMenuRow(el);
78
+ }, [sub]);
79
+ function pick(list, at) {
80
+ const entry = list[at];
81
+ if (entry === "-")
82
+ return;
83
+ if (isMenuHead(entry)) {
84
+ finish(true);
85
+ entry.edit?.onSelect();
86
+ return;
87
+ }
88
+ if (entry.sub && list === rows) {
89
+ setSub({ at, focus: true });
90
+ return;
91
+ }
92
+ if (menuRowView(entry).staysOpen) {
93
+ entry.onSelect?.();
94
+ return;
95
+ }
96
+ finish(true);
97
+ entry.onSelect?.();
98
+ }
99
+ function keys(e, inSub) {
100
+ if (e.key === "Tab")
101
+ return finish(false);
102
+ const move = menuMove(e.key, getComputedStyle(e.currentTarget).direction === "rtl");
103
+ if (!move)
104
+ return;
105
+ e.preventDefault();
106
+ if (move !== "in" && move !== "out")
107
+ return focusMenuRow(e.currentTarget, move);
108
+ if (inSub) {
109
+ if (move === "out" && sub) {
110
+ menu.current?.querySelector(`[data-row="${sub.at}"]`)?.focus();
111
+ setSub(null);
112
+ }
113
+ return;
114
+ }
115
+ const at = Number(document.activeElement?.dataset.row);
116
+ const entry = rows[at];
117
+ if (move === "in" && entry && entry !== "-" && !isMenuHead(entry) && entry.sub && !entry.disabled)
118
+ setSub({ at, focus: true });
119
+ else
120
+ latest.current.onStep?.(move === "in" ? 1 : -1);
121
+ }
122
+ // The pointer opens a submenu by resting on its row and closes it on any other row.
123
+ function hover(e) {
124
+ const row = e.target.closest("[data-row]");
125
+ if (!row)
126
+ return;
127
+ const at = Number(row.dataset.row);
128
+ const entry = rows[at];
129
+ const opens = entry !== "-" && !isMenuHead(entry) && !!entry.sub && !entry.disabled;
130
+ if (opens && sub?.at !== at)
131
+ setSub({ at, focus: false });
132
+ else if (!opens && sub)
133
+ setSub(null);
134
+ }
135
+ return (_jsxs(_Fragment, { children: [createPortal(_jsx("div", { ref: menu, role: "menu", "aria-label": label, className: "glim-menu", style: pos, onKeyDown: (e) => keys(e, false), onMouseOver: hover, onContextMenu: (e) => e.preventDefault(), children: _jsx(Rows, { entries: rows, openAt: sub?.at, onPick: (i) => pick(rows, i) }) }), document.body), subRows &&
136
+ createPortal(_jsx("div", { ref: subMenu, role: "menu", "aria-label": host.label, className: "glim-menu", style: subPos ?? { left: 0, top: 0 }, onKeyDown: (e) => keys(e, true), onContextMenu: (e) => e.preventDefault(), children: _jsx(Rows, { entries: subRows, onPick: (i) => pick(subRows, i) }) }), document.body)] }));
137
+ }
@@ -0,0 +1,15 @@
1
+ export interface CopyFieldProps {
2
+ value: string;
3
+ /** The field's accessible name, such as "Token". */
4
+ label: string;
5
+ /** The button's words, before and for a moment after it copied. */
6
+ text?: {
7
+ copy: string;
8
+ copied: string;
9
+ };
10
+ /** Called after every press, with whether the value reached the clipboard. */
11
+ onCopy?: (done: boolean) => void;
12
+ /** Classes for the row, which carries the width. */
13
+ className?: string;
14
+ }
15
+ export declare function CopyField({ value, label, text, onCopy, className }: CopyFieldProps): import("react").JSX.Element;
@@ -0,0 +1,24 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { useEffect, useRef, useState } from "react";
3
+ import { COPIED_MS, copyText } from "@glimstone/core";
4
+ import { Button } from "./Button.js";
5
+ import { IconCheck, IconCopy } from "./glyphs.js";
6
+ export function CopyField({ value, label, text = { copy: "Copy", copied: "Copied" }, onCopy, className }) {
7
+ const [copied, setCopied] = useState(false);
8
+ const field = useRef(null);
9
+ const timer = useRef(undefined);
10
+ useEffect(() => () => clearTimeout(timer.current), []);
11
+ async function copy() {
12
+ const done = await copyText(value);
13
+ onCopy?.(done);
14
+ if (!done) {
15
+ field.current?.focus();
16
+ field.current?.select();
17
+ return;
18
+ }
19
+ setCopied(true);
20
+ clearTimeout(timer.current);
21
+ timer.current = window.setTimeout(() => setCopied(false), COPIED_MS);
22
+ }
23
+ return (_jsxs("span", { className: className ? `glim-copy ${className}` : "glim-copy", children: [_jsx("input", { ref: field, readOnly: true, value: value, "aria-label": label, spellCheck: false, className: "glim-input glim-input--mono" }), _jsx(Button, { label: copied ? text.copied : text.copy, labelKey: copied ? "copied" : "copy", glyph: copied ? _jsx(IconCheck, {}) : _jsx(IconCopy, {}), variant: "icon", onClick: copy }), _jsx("span", { role: "status", className: "glim-sr-only", children: copied ? text.copied : "" })] }));
24
+ }
@@ -0,0 +1,61 @@
1
+ import type { ReactNode, Ref } from "react";
2
+ /** One address, and the chain it lives on. */
3
+ export interface CryptoNetwork {
4
+ /** Stable id, for the copy toast and for the app's own tests. */
5
+ id: string;
6
+ /** The chain as a donor's wallet names it. */
7
+ name: string;
8
+ address: string;
9
+ /** One short line shown with the address, for what a donor has to know
10
+ * before sending. Already translated. */
11
+ note?: string;
12
+ }
13
+ /** One coin, and every chain it can be sent on. */
14
+ export interface CryptoCoin {
15
+ /** Stable id, and the key `renderMark` draws by. */
16
+ id: string;
17
+ /** The ticker, on the tile under the mark. */
18
+ symbol: string;
19
+ /** The full name, for the accessible label. */
20
+ name: string;
21
+ /** Never empty, and every entry carries an address. */
22
+ networks: CryptoNetwork[];
23
+ /** The coin's own colour and the ink that holds on it, which its tile lights
24
+ * up in under the pointer ("Brand tiles"). The picked coin keeps the accent. */
25
+ tile?: {
26
+ color: string;
27
+ ink: string;
28
+ };
29
+ }
30
+ export interface CryptoDonateDialogProps {
31
+ /** The coins on offer. See the rule at the top of this file. */
32
+ coins: CryptoCoin[];
33
+ /** What is shown right now. The app owns this, so it can remember it. */
34
+ coin: CryptoCoin;
35
+ network: CryptoNetwork;
36
+ /** Picking a coin lands on that coin's first network rather than keeping the
37
+ * previous chain, which the donor never checked against the new coin. */
38
+ onPick: (coin: CryptoCoin, network: CryptoNetwork) => void;
39
+ text: {
40
+ title: string;
41
+ /** The app's appeal, the same in every giving window. */
42
+ appeal: string;
43
+ /** One sentence saying what to do: pick a currency and a network, then scan
44
+ * or copy. */
45
+ intro: string;
46
+ /** The label over the chain row ("Networks"). */
47
+ networks: string;
48
+ copyLabel: string;
49
+ closeLabel: string;
50
+ };
51
+ /** The address as a scannable square. */
52
+ renderQr?: (value: string) => ReactNode;
53
+ /** A coin's own mark. */
54
+ renderMark?: (coin: CryptoCoin) => ReactNode;
55
+ /** Copy the address and say so in a toast, in the app's own words. */
56
+ onCopy: (coin: CryptoCoin, network: CryptoNetwork) => void;
57
+ onClose: () => void;
58
+ /** The window's own card, for the app's Escape handling and focus trap. */
59
+ ref?: Ref<HTMLDivElement>;
60
+ }
61
+ export declare function CryptoDonateDialog({ coins, coin, network, onPick, text, renderQr, renderMark, onCopy, onClose, ref, }: CryptoDonateDialogProps): import("react").JSX.Element;
@@ -0,0 +1,23 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { hueVars } from "@glimstone/core";
3
+ import { Badge } from "./Badge.js";
4
+ import { Button } from "./Button.js";
5
+ import { useLabelMode } from "./useLabelMode.js";
6
+ export function CryptoDonateDialog({ coins, coin, network, onPick, text, renderQr, renderMark, onCopy, onClose, ref, }) {
7
+ // `reactive` shows both mark and ticker here: somebody searching a grid of
8
+ // coins should not have to hover every tile to find USDT.
9
+ const mode = useLabelMode("buttons");
10
+ const showMark = mode !== "text";
11
+ const showTicker = mode !== "glyph";
12
+ return (_jsx("div", { className: "glim-modal-backdrop", onClick: (e) => {
13
+ if (e.target === e.currentTarget)
14
+ onClose();
15
+ }, children: _jsxs("div", { ref: ref, tabIndex: -1, role: "dialog", "aria-modal": "true", "aria-labelledby": "cryptodonate-title", className: "glim-modal-card glim-modal-card--wide", children: [_jsx("div", { className: "glim-modal-head", children: _jsx("h2", { id: "cryptodonate-title", className: "glim-modal-title", children: _jsx(Badge, { tone: "heading", size: "heading", wrap: true, children: text.title }) }) }), _jsxs("div", { className: "glim-give-body glim-give-body--scroll", children: [_jsx("p", { className: "glim-give-appeal", children: text.appeal }), _jsx("p", { className: "glim-give-intro", children: text.intro }), _jsxs("div", { className: "glim-give-well", children: [renderQr?.(network.address), _jsx("p", { dir: "ltr", className: "glim-give-address", children: network.address }), _jsx("div", { className: "glim-give-networks", role: "listbox", "aria-label": text.networks, children: coin.networks.map((n, i) => (_jsx("button", { type: "button", role: "option", "aria-selected": n.id === network.id, onClick: () => onPick(coin, n), style: hueVars(i), className: `glim-give-network glim-hue${n.id === network.id ? " glim-active" : ""}`, children: n.name }, n.id))) }), network.note && _jsx("p", { className: "glim-give-note", children: network.note }), _jsx(Button, { label: text.copyLabel, labelKey: "common.copy", tone: "accent", hueIndex: coins.findIndex((c) => c.id === coin.id), onClick: () => onCopy(coin, network) })] }), _jsx("div", { className: "glim-give-coins", role: "listbox", "aria-label": text.title, children: coins.map((c, i) => {
16
+ const picked = c.id === coin.id;
17
+ const lit = !picked && c.tile;
18
+ return (_jsxs("button", { type: "button", role: "option", "aria-selected": picked, "aria-label": `${c.name} (${c.symbol})`, title: c.name, onClick: () => onPick(c, c.networks[0]), style: {
19
+ ...hueVars(i),
20
+ ...(lit && { "--tile": c.tile.color, "--tile-ink": c.tile.ink }),
21
+ }, className: `glim-give-coin ${showTicker ? "glim-hue glim-hue-icon" : "glim-hue"} ${picked ? "glim-active" : lit ? "glim-brand-tile" : "glim-give-coin--plain"}`, children: [showMark && renderMark?.(c), showTicker && _jsx("span", { className: "glim-give-ticker", children: c.symbol })] }, c.id));
22
+ }) })] }), _jsx("div", { className: "glim-give-foot", children: _jsx(Button, { label: text.closeLabel, labelKey: "common.close", tone: "neutral", onClick: onClose }) })] }) }));
23
+ }
@@ -0,0 +1,22 @@
1
+ import { type ReactNode, type RefObject } from "react";
2
+ export interface DropdownListboxProps {
3
+ /** Whether the panel is mounted. The caller owns the trigger; only the
4
+ * panel is shared. */
5
+ open: boolean;
6
+ /** Called when the panel dismisses itself (outside click, Escape, scroll,
7
+ * resize). Picking an option does not call it: a single-select list closes
8
+ * itself at the call site, a multi-select one stays open. */
9
+ onClose: () => void;
10
+ /** The trigger, which anchors the panel and is exempt from the outside-click
11
+ * dismissal so clicking it does not close and reopen the list. */
12
+ triggerRef: RefObject<HTMLElement | null>;
13
+ /** Accessible name for the listbox, normally the same label the trigger
14
+ * carries. */
15
+ label: string;
16
+ /** `aria-multiselectable`, for a checkbox multi-select list. */
17
+ multiselectable?: boolean;
18
+ /** The `role="option"` buttons. */
19
+ children: ReactNode;
20
+ }
21
+ /** @deprecated Use Select, which keeps focus on its trigger and adds typeahead. */
22
+ export declare function DropdownListbox({ open, onClose, triggerRef, label, multiselectable, children, }: DropdownListboxProps): import("react").ReactPortal | null;