@glimstone/react 0.0.0-stage → 4.0.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 (64) hide show
  1. package/README.md +118 -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/CoffeeDialog.d.ts +17 -0
  11. package/dist/CoffeeDialog.js +9 -0
  12. package/dist/Combobox.d.ts +16 -0
  13. package/dist/Combobox.js +32 -0
  14. package/dist/ConfirmDialog.d.ts +29 -0
  15. package/dist/ConfirmDialog.js +15 -0
  16. package/dist/CryptoDonateDialog.d.ts +61 -0
  17. package/dist/CryptoDonateDialog.js +23 -0
  18. package/dist/DropdownListbox.d.ts +22 -0
  19. package/dist/DropdownListbox.js +182 -0
  20. package/dist/EmptyState.d.ts +19 -0
  21. package/dist/EmptyState.js +10 -0
  22. package/dist/IconTipButton.d.ts +18 -0
  23. package/dist/IconTipButton.js +13 -0
  24. package/dist/InfoBubble.d.ts +4 -0
  25. package/dist/InfoBubble.js +19 -0
  26. package/dist/NumberField.d.ts +16 -0
  27. package/dist/NumberField.js +18 -0
  28. package/dist/PaypalDialog.d.ts +51 -0
  29. package/dist/PaypalDialog.js +20 -0
  30. package/dist/ReadmeButton.d.ts +29 -0
  31. package/dist/ReadmeButton.js +27 -0
  32. package/dist/Select.d.ts +41 -0
  33. package/dist/Select.js +192 -0
  34. package/dist/Selector.d.ts +67 -0
  35. package/dist/Selector.js +128 -0
  36. package/dist/SidebarRow.d.ts +22 -0
  37. package/dist/SidebarRow.js +10 -0
  38. package/dist/Slider.d.ts +12 -0
  39. package/dist/Slider.js +18 -0
  40. package/dist/SubCard.d.ts +15 -0
  41. package/dist/SubCard.js +14 -0
  42. package/dist/Toast.d.ts +29 -0
  43. package/dist/Toast.js +24 -0
  44. package/dist/Toggle.d.ts +16 -0
  45. package/dist/Toggle.js +4 -0
  46. package/dist/UnavailableNotice.d.ts +15 -0
  47. package/dist/UnavailableNotice.js +12 -0
  48. package/dist/WindowBar.d.ts +24 -0
  49. package/dist/WindowBar.js +31 -0
  50. package/dist/glyphs.d.ts +22 -0
  51. package/dist/glyphs.js +30 -0
  52. package/dist/index.d.ts +28 -0
  53. package/dist/index.js +28 -0
  54. package/dist/portalHue.d.ts +10 -0
  55. package/dist/portalHue.js +15 -0
  56. package/dist/useConfirm.d.ts +26 -0
  57. package/dist/useConfirm.js +39 -0
  58. package/dist/useLabelMode.d.ts +6 -0
  59. package/dist/useLabelMode.js +11 -0
  60. package/dist/usePaypalButtons.d.ts +15 -0
  61. package/dist/usePaypalButtons.js +42 -0
  62. package/dist/useTipBubble.d.ts +34 -0
  63. package/dist/useTipBubble.js +63 -0
  64. package/package.json +28 -4
package/dist/Toggle.js ADDED
@@ -0,0 +1,4 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ export function Toggle({ checked, onChange, label, hideLabel = false, disabled, className }) {
3
+ return (_jsxs("span", { className: `glim-toggle${className ? ` ${className}` : ""}`, children: [!hideLabel && _jsx("span", { className: "glim-toggle-label", children: label }), _jsx("button", { type: "button", role: "switch", "aria-checked": checked, "aria-label": label, disabled: disabled, onClick: () => onChange(!checked), className: "glim-toggle-track", children: _jsx("span", { className: "glim-toggle-thumb" }) })] }));
4
+ }
@@ -0,0 +1,15 @@
1
+ import type { ReactNode } from "react";
2
+ export declare function UnavailableNotice({ title, reason, action, }: {
3
+ /** One line saying what is unavailable. Already translated. */
4
+ title: string;
5
+ /**
6
+ * Why, and ideally what would change it. Already translated. A node lets a
7
+ * caller mark up a hostname or a path inside it.
8
+ */
9
+ reason: ReactNode;
10
+ /**
11
+ * A control for what somebody can do from here, such as opening the setting
12
+ * that fixes the cause. Never the control that was refused.
13
+ */
14
+ action?: ReactNode;
15
+ }): import("react").JSX.Element;
@@ -0,0 +1,12 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ // What a card shows in place of a control when the environment forbids what the
3
+ // control would do, such as a passkey on a bare IP address or an untrusted
4
+ // certificate, where WebAuthn refuses the exchange. See "The second way in
5
+ // (second factor and passkey)" in the design language.
6
+ //
7
+ // Every string is a prop and arrives translated; a server's verdict travels as a
8
+ // boolean, never as the sentence shown here. It is a paragraph rather than an
9
+ // info bubble because there is no control to hang a bubble off.
10
+ export function UnavailableNotice({ title, reason, action, }) {
11
+ return (_jsxs("div", { className: "glim-notice", children: [_jsx("p", { className: "glim-notice-title", children: title }), _jsx("p", { className: "glim-notice-reason", children: reason }), action ? _jsx("div", { className: "glim-notice-action", children: action }) : null] }));
12
+ }
@@ -0,0 +1,24 @@
1
+ export interface WindowBarProps {
2
+ /** The app's name. The bar shows none, so this is what a screen reader
3
+ * announces for it. */
4
+ appName: string;
5
+ /** Whether the window fills the screen. The middle button then restores it,
6
+ * and the app turns its edge resizing off. */
7
+ maximised: boolean;
8
+ /** The buttons' accessible names, which also show in their bubbles. */
9
+ text: {
10
+ minimise: string;
11
+ maximise: string;
12
+ restore: string;
13
+ close: string;
14
+ };
15
+ onMinimise: () => void;
16
+ onToggleMaximise: () => void;
17
+ /** Asks the way the native close button does, so an app that hides to the
18
+ * tray on close still does. */
19
+ onClose: () => void;
20
+ /** Called once a press on the bar starts to move. The app hands the press to
21
+ * the system, which moves the window from there. */
22
+ onDragStart: () => void;
23
+ }
24
+ export declare function WindowBar({ appName, maximised, text, onMinimise, onToggleMaximise, onClose, onDragStart, }: WindowBarProps): import("react").JSX.Element;
@@ -0,0 +1,31 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { useRef } from "react";
3
+ import { IconClose, IconWindowMaximise, IconWindowMinimise, IconWindowRestore } from "./glyphs.js";
4
+ import { IconTipButton } from "./IconTipButton.js";
5
+ const onButton = (e) => e.target.closest("button") !== null;
6
+ export function WindowBar({ appName, maximised, text, onMinimise, onToggleMaximise, onClose, onDragStart, }) {
7
+ // The drag starts on the first move, not on the press, so a click and a
8
+ // double click on the bar stay clicks. An edge the app's resize has taken
9
+ // arrives with its default prevented and is left to it.
10
+ const pressed = useRef(false);
11
+ const down = (e) => {
12
+ pressed.current = e.button === 0 && !e.defaultPrevented && !onButton(e);
13
+ };
14
+ const move = (e) => {
15
+ if (!pressed.current)
16
+ return;
17
+ pressed.current = false;
18
+ if (e.buttons === 1)
19
+ onDragStart();
20
+ };
21
+ const toggle = (e) => {
22
+ if (!onButton(e))
23
+ onToggleMaximise();
24
+ };
25
+ return (_jsxs("header", { "aria-label": appName, className: "glim-window-bar", onMouseDown: down, onMouseMove: move, onMouseUp: () => (pressed.current = false), onDoubleClick: toggle, children: [_jsx(WindowButton, { label: text.minimise, onPress: onMinimise, children: _jsx(IconWindowMinimise, {}) }), _jsx(WindowButton, { label: maximised ? text.restore : text.maximise, onPress: onToggleMaximise, children: maximised ? _jsx(IconWindowRestore, {}) : _jsx(IconWindowMaximise, {}) }), _jsx(WindowButton, { label: text.close, onPress: onClose, children: _jsx(IconClose, {}) })] }));
26
+ }
27
+ // The button takes the bar's full height and shows the house square inside, so
28
+ // a pointer thrown into the corner of a maximised window still lands on close.
29
+ function WindowButton({ label, onPress, children }) {
30
+ return (_jsx(IconTipButton, { tip: label, onClick: onPress, className: "glim-window-button", children: _jsx("span", { className: "glim-window-square", children: children }) }));
31
+ }
@@ -0,0 +1,22 @@
1
+ import type { ReactNode } from "react";
2
+ import { type Mark } from "@glimstone/core";
3
+ /**
4
+ * Register the app's label-key-to-glyph mapping, once, at boot. A button that is
5
+ * given no glyph cannot be shown in glyph mode, and one place to ask saves
6
+ * passing an icon at every call site.
7
+ */
8
+ export declare function setGlyphResolver(fn: (labelKey: string) => ReactNode | undefined): void;
9
+ /** The mark for a label key, or nothing when no app resolver knows it. */
10
+ export declare function glyphFor(labelKey: string): ReactNode | undefined;
11
+ /** A mark from the core, drawn in the current colour. */
12
+ export declare function MarkSvg({ mark, className }: {
13
+ mark: Mark;
14
+ className?: string;
15
+ }): import("react").JSX.Element;
16
+ export declare const IconClose: () => import("react").JSX.Element;
17
+ export declare const IconCancel: () => import("react").JSX.Element;
18
+ export declare const IconCheck: () => import("react").JSX.Element;
19
+ export declare const IconExpand: () => import("react").JSX.Element;
20
+ export declare const IconWindowMinimise: () => import("react").JSX.Element;
21
+ export declare const IconWindowMaximise: () => import("react").JSX.Element;
22
+ export declare const IconWindowRestore: () => import("react").JSX.Element;
package/dist/glyphs.js ADDED
@@ -0,0 +1,30 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { MARKS } from "@glimstone/core";
3
+ // The icon on a "Save" button or a nav item is the app's vocabulary, so GlimStone
4
+ // ships the assortment as a specification (`packages/core/glyphs.md`) and lets the app
5
+ // register a resolver. `Button` then picks a glyph by meaning. Without a resolver a
6
+ // button falls back to the glyph it was handed, and with neither, to text alone.
7
+ let resolver = null;
8
+ /**
9
+ * Register the app's label-key-to-glyph mapping, once, at boot. A button that is
10
+ * given no glyph cannot be shown in glyph mode, and one place to ask saves
11
+ * passing an icon at every call site.
12
+ */
13
+ export function setGlyphResolver(fn) {
14
+ resolver = fn;
15
+ }
16
+ /** The mark for a label key, or nothing when no app resolver knows it. */
17
+ export function glyphFor(labelKey) {
18
+ return resolver ? resolver(labelKey) : undefined;
19
+ }
20
+ /** A mark from the core, drawn in the current colour. */
21
+ export function MarkSvg({ mark, className }) {
22
+ return (_jsx("svg", { width: mark.size, height: mark.size, viewBox: mark.viewBox, fill: "currentColor", className: className, "aria-hidden": "true", dangerouslySetInnerHTML: { __html: mark.body } }));
23
+ }
24
+ export const IconClose = () => _jsx(MarkSvg, { mark: MARKS.close, className: "glim-mark" });
25
+ export const IconCancel = () => _jsx(MarkSvg, { mark: MARKS.cancel, className: "glim-mark" });
26
+ export const IconCheck = () => _jsx(MarkSvg, { mark: MARKS.check, className: "glim-mark" });
27
+ export const IconExpand = () => _jsx(MarkSvg, { mark: MARKS.expand, className: "glim-mark" });
28
+ export const IconWindowMinimise = () => _jsx(MarkSvg, { mark: MARKS.windowMinimise, className: "glim-mark" });
29
+ export const IconWindowMaximise = () => _jsx(MarkSvg, { mark: MARKS.windowMaximise, className: "glim-mark" });
30
+ export const IconWindowRestore = () => _jsx(MarkSvg, { mark: MARKS.windowRestore, className: "glim-mark" });
@@ -0,0 +1,28 @@
1
+ export * from "./AboutCard.tsx";
2
+ export * from "./Badge.tsx";
3
+ export * from "./Button.tsx";
4
+ export * from "./Card.tsx";
5
+ export * from "./CoffeeDialog.tsx";
6
+ export * from "./ConfirmDialog.tsx";
7
+ export * from "./CryptoDonateDialog.tsx";
8
+ export * from "./DropdownListbox.tsx";
9
+ export * from "./EmptyState.tsx";
10
+ export * from "./IconTipButton.tsx";
11
+ export * from "./InfoBubble.tsx";
12
+ export * from "./NumberField.tsx";
13
+ export * from "./PaypalDialog.tsx";
14
+ export * from "./ReadmeButton.tsx";
15
+ export * from "./Selector.tsx";
16
+ export * from "./SidebarRow.tsx";
17
+ export * from "./SubCard.tsx";
18
+ export * from "./Toast.tsx";
19
+ export * from "./Toggle.tsx";
20
+ export * from "./UnavailableNotice.tsx";
21
+ export * from "./WindowBar.tsx";
22
+ export * from "./glyphs.tsx";
23
+ export * from "./portalHue.ts";
24
+ export * from "./useConfirm.tsx";
25
+ export * from "./useLabelMode.ts";
26
+ export * from "./usePaypalButtons.ts";
27
+ export * from "./useTipBubble.tsx";
28
+ export { GLIMSTONE_VERSION } from "@glimstone/core";
package/dist/index.js ADDED
@@ -0,0 +1,28 @@
1
+ export * from "./AboutCard.js";
2
+ export * from "./Badge.js";
3
+ export * from "./Button.js";
4
+ export * from "./Card.js";
5
+ export * from "./CoffeeDialog.js";
6
+ export * from "./ConfirmDialog.js";
7
+ export * from "./CryptoDonateDialog.js";
8
+ export * from "./DropdownListbox.js";
9
+ export * from "./EmptyState.js";
10
+ export * from "./IconTipButton.js";
11
+ export * from "./InfoBubble.js";
12
+ export * from "./NumberField.js";
13
+ export * from "./PaypalDialog.js";
14
+ export * from "./ReadmeButton.js";
15
+ export * from "./Selector.js";
16
+ export * from "./SidebarRow.js";
17
+ export * from "./SubCard.js";
18
+ export * from "./Toast.js";
19
+ export * from "./Toggle.js";
20
+ export * from "./UnavailableNotice.js";
21
+ export * from "./WindowBar.js";
22
+ export * from "./glyphs.js";
23
+ export * from "./portalHue.js";
24
+ export * from "./useConfirm.js";
25
+ export * from "./useLabelMode.js";
26
+ export * from "./usePaypalButtons.js";
27
+ export * from "./useTipBubble.js";
28
+ export { GLIMSTONE_VERSION } from "@glimstone/core";
@@ -0,0 +1,10 @@
1
+ import { type RefObject } from "react";
2
+ /** What a portalled panel needs to stand in its trigger's palette position. */
3
+ export interface PortalHue {
4
+ /** Inline custom properties to spread onto the portalled element. */
5
+ style: Record<string, string> | undefined;
6
+ /** `"glim-hue"` when there is a hue to apply, otherwise `""`. */
7
+ className: string;
8
+ }
9
+ /** Copies the trigger's rainbow position onto a portalled panel while it is open. */
10
+ export declare function usePortalHue(open: boolean, triggerRef: RefObject<HTMLElement | null>): PortalHue;
@@ -0,0 +1,15 @@
1
+ import { useLayoutEffect, useState } from "react";
2
+ import { readPortalHue } from "@glimstone/core";
3
+ /** Copies the trigger's rainbow position onto a portalled panel while it is open. */
4
+ export function usePortalHue(open, triggerRef) {
5
+ const [hue, setHue] = useState(null);
6
+ useLayoutEffect(() => {
7
+ if (!open) {
8
+ setHue(null);
9
+ return;
10
+ }
11
+ if (triggerRef.current)
12
+ setHue(readPortalHue(triggerRef.current));
13
+ }, [open, triggerRef]);
14
+ return { style: hue ?? undefined, className: hue ? "glim-hue" : "" };
15
+ }
@@ -0,0 +1,26 @@
1
+ import { type ReactNode, type RefObject } from "react";
2
+ export interface ConfirmText {
3
+ /** The dialog's title, the same for every question, such as "Confirm". */
4
+ title: string;
5
+ cancel: string;
6
+ /** The confirm button's words where a question passes none of its own. */
7
+ confirm: string;
8
+ }
9
+ export interface ConfirmOptions {
10
+ /** The words of the action being confirmed, so the answer repeats "Delete"
11
+ * rather than a bare "Confirm". */
12
+ confirmLabel?: string;
13
+ /** Translation key behind confirmLabel, so the confirm button shows its glyph. */
14
+ confirmLabelKey?: string;
15
+ confirmGlyph?: ReactNode;
16
+ /** A switch or two the answer needs, shown under the question. */
17
+ extra?: ReactNode;
18
+ }
19
+ export declare function useConfirm(text: ConfirmText): {
20
+ confirm: (message: string, options?: ConfirmOptions) => Promise<boolean>;
21
+ confirmDialog: import("react").ReactPortal | null;
22
+ dismiss: () => void;
23
+ };
24
+ /** Escape from anywhere and a Tab trap over the dialog's own controls, so focus
25
+ * never reaches the page it covers. Exported for any other modal window. */
26
+ export declare function useDialogKeys(open: boolean, dialogRef: RefObject<HTMLDivElement | null>, onCancel: () => void): void;
@@ -0,0 +1,39 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { useCallback, useEffect, useRef, useState } from "react";
3
+ import { createPortal } from "react-dom";
4
+ import { focusedAsker, returnFocus, trapDialogKeys } from "@glimstone/core";
5
+ import { ConfirmDialog } from "./ConfirmDialog.js";
6
+ export function useConfirm(text) {
7
+ const [pending, setPending] = useState(null);
8
+ const resolveRef = useRef(null);
9
+ const dialogRef = useRef(null);
10
+ const askerRef = useRef(null);
11
+ const confirm = useCallback((message, options) => {
12
+ askerRef.current = focusedAsker();
13
+ return new Promise((resolve) => {
14
+ resolveRef.current = resolve;
15
+ setPending({ message, ...options });
16
+ });
17
+ }, []);
18
+ const settle = useCallback((result) => {
19
+ resolveRef.current?.(result);
20
+ resolveRef.current = null;
21
+ setPending(null);
22
+ returnFocus(askerRef.current);
23
+ askerRef.current = null;
24
+ }, []);
25
+ /** For a caller whose question stopped making sense while it was open. */
26
+ const dismiss = useCallback(() => settle(false), [settle]);
27
+ useDialogKeys(pending !== null, dialogRef, dismiss);
28
+ // An ancestor with a transform would confine a fixed backdrop to its own box,
29
+ // so the dialog goes to <body>.
30
+ const confirmDialog = pending
31
+ ? createPortal(_jsx(ConfirmDialog, { ref: dialogRef, title: text.title, message: pending.message, confirmLabel: pending.confirmLabel ?? text.confirm, confirmLabelKey: pending.confirmLabelKey, confirmGlyph: pending.confirmGlyph, cancelLabel: text.cancel, extra: pending.extra, onConfirm: () => settle(true), onCancel: () => settle(false) }), document.body)
32
+ : null;
33
+ return { confirm, confirmDialog, dismiss };
34
+ }
35
+ /** Escape from anywhere and a Tab trap over the dialog's own controls, so focus
36
+ * never reaches the page it covers. Exported for any other modal window. */
37
+ export function useDialogKeys(open, dialogRef, onCancel) {
38
+ useEffect(() => (open ? trapDialogKeys(() => dialogRef.current, onCancel) : undefined), [open, dialogRef, onCancel]);
39
+ }
@@ -0,0 +1,6 @@
1
+ import { type LabelAxis, type LabelMode } from "@glimstone/core";
2
+ /**
3
+ * The current label mode for one axis. The first value is read synchronously so
4
+ * the first paint is already right.
5
+ */
6
+ export declare function useLabelMode(axis: LabelAxis): LabelMode;
@@ -0,0 +1,11 @@
1
+ import { useEffect, useState } from "react";
2
+ import { getLabelMode, subscribeLabelMode } from "@glimstone/core";
3
+ /**
4
+ * The current label mode for one axis. The first value is read synchronously so
5
+ * the first paint is already right.
6
+ */
7
+ export function useLabelMode(axis) {
8
+ const [mode, setMode] = useState(() => getLabelMode(axis));
9
+ useEffect(() => subscribeLabelMode(() => setMode(getLabelMode(axis))), [axis]);
10
+ return mode;
11
+ }
@@ -0,0 +1,15 @@
1
+ import { type GiveFrequency, type PaypalConfig } from "@glimstone/core";
2
+ /**
3
+ * Renders PayPal's two buttons into the returned ref, and again whenever the
4
+ * frequency changes, since a subscription needs the other SDK namespace. The
5
+ * amount is read at click time, so picking another amount reloads nothing.
6
+ */
7
+ export declare function usePaypalButtons({ config, frequency, amount, description, onDone, onError, }: {
8
+ config: PaypalConfig;
9
+ frequency: GiveFrequency;
10
+ /** The amount to charge, or null while the typed amount is not valid. */
11
+ amount: string | null;
12
+ description: string;
13
+ onDone: () => void;
14
+ onError: () => void;
15
+ }): import("react").RefObject<HTMLDivElement | null>;
@@ -0,0 +1,42 @@
1
+ import { useEffect, useRef } from "react";
2
+ import { donationHandlers, loadPaypal } from "@glimstone/core";
3
+ /**
4
+ * Renders PayPal's two buttons into the returned ref, and again whenever the
5
+ * frequency changes, since a subscription needs the other SDK namespace. The
6
+ * amount is read at click time, so picking another amount reloads nothing.
7
+ */
8
+ export function usePaypalButtons({ config, frequency, amount, description, onDone, onError, }) {
9
+ const container = useRef(null);
10
+ const latest = useRef({ amount, onDone, onError });
11
+ latest.current = { amount, onDone, onError };
12
+ useEffect(() => {
13
+ const box = container.current;
14
+ if (!box)
15
+ return;
16
+ let buttons = null;
17
+ let cancelled = false;
18
+ loadPaypal(config, frequency !== "once").then((paypal) => {
19
+ if (cancelled)
20
+ return;
21
+ const instance = paypal.Buttons({
22
+ // Blue is one of the five colours PayPal allows, and reads on both themes.
23
+ style: { layout: "vertical", color: "blue", shape: "rect", borderRadius: 10, label: "donate", height: 40 },
24
+ // A half-typed amount keeps the buttons from opening PayPal at all.
25
+ onClick: (_, actions) => (latest.current.amount ? actions.resolve() : actions.reject()),
26
+ ...donationHandlers(config, frequency, () => latest.current.amount ?? "0", description, () => latest.current.onDone()),
27
+ onError: () => latest.current.onError(),
28
+ });
29
+ buttons = instance;
30
+ box.replaceChildren();
31
+ instance.render(box).catch(() => latest.current.onError());
32
+ }, () => {
33
+ if (!cancelled)
34
+ latest.current.onError();
35
+ });
36
+ return () => {
37
+ cancelled = true;
38
+ buttons?.close().catch(() => { });
39
+ };
40
+ }, [config, frequency, description]);
41
+ return container;
42
+ }
@@ -0,0 +1,34 @@
1
+ import { type ReactNode } from "react";
2
+ export interface TipBubble {
3
+ /** Ref callback for the trigger element the bubble is placed against. A
4
+ * callback, so a trigger that keeps its own ref can feed both from one
5
+ * attribute. */
6
+ ref: (el: HTMLElement | null) => void;
7
+ /** Spread on the trigger. Focus opens the bubble too, when it came from the
8
+ * keyboard. */
9
+ handlers: {
10
+ onMouseEnter: () => void;
11
+ onMouseLeave: () => void;
12
+ onFocus: () => void;
13
+ onBlur: () => void;
14
+ };
15
+ /** `aria-describedby` while open, so the bubble is announced as the
16
+ * trigger's description. */
17
+ describedBy: string | undefined;
18
+ /** The bubble, portalled to <body> so no card's `overflow: hidden` clips it,
19
+ * or null when there is nothing to show. Render it beside the trigger. */
20
+ bubble: ReactNode;
21
+ /** Wraps a disabled trigger with a tip in a box that still sees the pointer,
22
+ * since a disabled <button> emits no mouse events. Other triggers pass
23
+ * through unchanged. */
24
+ wrap: (node: ReactNode) => ReactNode;
25
+ /** Open or close by hand, for a hover wrapper of the call site's own. */
26
+ show: () => void;
27
+ hide: () => void;
28
+ }
29
+ /**
30
+ * @param tip What the bubble says. When it is empty nothing opens or renders
31
+ * and `wrap` passes its node through.
32
+ * @param disabled Whether the trigger is currently disabled; see `wrap`.
33
+ */
34
+ export declare function useTipBubble(tip?: string, disabled?: boolean): TipBubble;
@@ -0,0 +1,63 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { useEffect, useId, useLayoutEffect, useRef, useState } from "react";
3
+ import { createPortal } from "react-dom";
4
+ import { dismissBubbleOn, placeBubble, pointerWasLast, trackInputModality } from "@glimstone/core";
5
+ /**
6
+ * @param tip What the bubble says. When it is empty nothing opens or renders
7
+ * and `wrap` passes its node through.
8
+ * @param disabled Whether the trigger is currently disabled; see `wrap`.
9
+ */
10
+ export function useTipBubble(tip, disabled = false) {
11
+ const [open, setOpen] = useState(false);
12
+ const triggerRef = useRef(null);
13
+ const bubbleRef = useRef(null);
14
+ const tooltipId = useId();
15
+ // Flipping `disabled` replaces the trigger element: `wrap` boxes a disabled
16
+ // one, so React mounts a new <button> in its place. The old one takes its
17
+ // focus and hover with it without firing blur or mouseleave, so an open
18
+ // bubble would stay up for good. Closing during render keeps it from being
19
+ // painted even once; a pointer still over the control reopens it on its
20
+ // next move, through the wrapper's mouseenter.
21
+ const [seenDisabled, setSeenDisabled] = useState(disabled);
22
+ if (seenDisabled !== disabled) {
23
+ setSeenDisabled(disabled);
24
+ setOpen(false);
25
+ }
26
+ const shown = !!tip && open;
27
+ function show() {
28
+ if (!tip)
29
+ return;
30
+ setOpen(true);
31
+ }
32
+ function hide() {
33
+ setOpen(false);
34
+ }
35
+ useEffect(trackInputModality, []);
36
+ function showOnFocus() {
37
+ if (!pointerWasLast())
38
+ show();
39
+ }
40
+ useLayoutEffect(() => {
41
+ if (shown && triggerRef.current && bubbleRef.current)
42
+ placeBubble(triggerRef.current, bubbleRef.current);
43
+ }, [shown]);
44
+ useEffect(() => (shown ? dismissBubbleOn(hide) : undefined), [shown]);
45
+ return {
46
+ ref: (el) => {
47
+ triggerRef.current = el;
48
+ },
49
+ handlers: {
50
+ onMouseEnter: show,
51
+ onMouseLeave: hide,
52
+ onFocus: showOnFocus,
53
+ onBlur: hide,
54
+ },
55
+ describedBy: shown ? tooltipId : undefined,
56
+ bubble: shown
57
+ ? createPortal(_jsx("div", { ref: bubbleRef, role: "tooltip", id: tooltipId, className: "glim-bubble glim-fade", children: tip }), document.body)
58
+ : null,
59
+ wrap: (node) => disabled && tip ? (_jsx("span", { className: "glim-tip-wrap", onMouseEnter: show, onMouseLeave: hide, children: node })) : (node),
60
+ show,
61
+ hide,
62
+ };
63
+ }
package/package.json CHANGED
@@ -1,6 +1,30 @@
1
1
  {
2
2
  "name": "@glimstone/react",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "4.0.0",
4
+ "description": "GlimStone's components for React",
5
+ "license": "AGPL-3.0-only",
6
+ "repository": { "type": "git", "url": "git+https://github.com/junkerderprovinz/glimstone.git", "directory": "packages/react" },
7
+ "homepage": "https://junkerderprovinz.github.io/glimstone/",
8
+ "type": "module",
9
+ "sideEffects": false,
10
+ "exports": {
11
+ ".": { "types": "./dist/index.d.ts", "default": "./dist/index.js" },
12
+ "./*": { "types": "./dist/*.d.ts", "default": "./dist/*.js" }
13
+ },
14
+ "files": ["dist"],
15
+ "scripts": {
16
+ "build": "tsc -p tsconfig.build.json"
17
+ },
18
+ "dependencies": {
19
+ "@glimstone/core": "4.0.0"
20
+ },
21
+ "peerDependencies": {
22
+ "@zag-js/combobox": "^1.46.0",
23
+ "@zag-js/menu": "^1.46.0",
24
+ "@zag-js/react": "^1.46.0",
25
+ "@zag-js/select": "^1.46.0",
26
+ "@zag-js/slider": "^1.46.0",
27
+ "react": "^19.0.0",
28
+ "react-dom": "^19.0.0"
29
+ }
30
+ }