@alulore/kumiko 0.18.3 → 0.19.1

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 (147) hide show
  1. package/dist/dialog/ask.d.ts +7 -0
  2. package/dist/dialog/ask.js +8 -0
  3. package/dist/dialog/dialog-content.d.ts +23 -0
  4. package/dist/dialog/dialog-content.js +15 -0
  5. package/dist/dialog/dialog-entry.d.ts +14 -0
  6. package/dist/dialog/dialog-entry.js +111 -0
  7. package/dist/dialog/dialog-labels.d.ts +23 -0
  8. package/dist/dialog/dialog-labels.js +16 -0
  9. package/dist/dialog/dialog-store.d.ts +139 -0
  10. package/dist/dialog/dialog-store.js +296 -0
  11. package/dist/dialog/dialog.css +36 -0
  12. package/dist/dialog/dialog.d.ts +0 -3
  13. package/dist/dialog/dialog.js +0 -6
  14. package/dist/dialog/index.d.ts +8 -2
  15. package/dist/dialog/index.js +7 -2
  16. package/dist/dialog/leave-guard.d.ts +24 -0
  17. package/dist/dialog/leave-guard.js +98 -0
  18. package/dist/dialog/nest.d.ts +14 -0
  19. package/dist/dialog/nest.js +26 -0
  20. package/dist/dialog/open-dialog.d.ts +16 -0
  21. package/dist/dialog/open-dialog.js +13 -0
  22. package/dist/dialog/overlay-host.d.ts +27 -0
  23. package/dist/dialog/overlay-host.js +56 -0
  24. package/dist/dialog/question-dialog.d.ts +21 -0
  25. package/dist/dialog/question-dialog.js +78 -0
  26. package/dist/drawer/drawer.css +18 -16
  27. package/dist/menu/menu.css +21 -17
  28. package/dist/otp-input/otp-input.css +38 -37
  29. package/dist/page/index.d.ts +8 -0
  30. package/dist/page/index.js +8 -0
  31. package/dist/page/page-action.d.ts +26 -0
  32. package/dist/page/page-action.js +23 -0
  33. package/dist/page/page-actions.d.ts +10 -0
  34. package/dist/page/page-actions.js +66 -0
  35. package/dist/page/page-back.d.ts +11 -0
  36. package/dist/page/page-back.js +17 -0
  37. package/dist/page/page-breadcrumbs.d.ts +21 -0
  38. package/dist/page/page-breadcrumbs.js +60 -0
  39. package/dist/page/page-context.d.ts +5 -0
  40. package/dist/page/page-context.js +13 -0
  41. package/dist/page/page-fit.d.ts +26 -0
  42. package/dist/page/page-fit.js +36 -0
  43. package/dist/page/page-header.d.ts +26 -0
  44. package/dist/page/page-header.js +16 -0
  45. package/dist/page/page-icons.d.ts +4 -0
  46. package/dist/page/page-icons.js +13 -0
  47. package/dist/page/page-labels.d.ts +12 -0
  48. package/dist/page/page-labels.js +6 -0
  49. package/dist/page/page-section.d.ts +14 -0
  50. package/dist/page/page-section.js +9 -0
  51. package/dist/page/page.css +422 -0
  52. package/dist/page/page.d.ts +16 -0
  53. package/dist/page/page.js +12 -0
  54. package/dist/page/use-measured-fit.d.ts +22 -0
  55. package/dist/page/use-measured-fit.js +75 -0
  56. package/dist/page/use-stuck.d.ts +7 -0
  57. package/dist/page/use-stuck.js +31 -0
  58. package/dist/pagination/index.d.ts +3 -0
  59. package/dist/pagination/index.js +3 -0
  60. package/dist/pagination/pagination-extras.d.ts +28 -0
  61. package/dist/pagination/pagination-extras.js +42 -0
  62. package/dist/pagination/pagination-fit.d.ts +15 -0
  63. package/dist/pagination/pagination-fit.js +52 -0
  64. package/dist/pagination/pagination-icons.d.ts +4 -0
  65. package/dist/pagination/pagination-icons.js +16 -0
  66. package/dist/pagination/pagination-labels.d.ts +23 -0
  67. package/dist/pagination/pagination-labels.js +14 -0
  68. package/dist/pagination/pagination-math.d.ts +7 -0
  69. package/dist/pagination/pagination-math.js +24 -0
  70. package/dist/pagination/pagination-motion.d.ts +6 -0
  71. package/dist/pagination/pagination-motion.js +71 -0
  72. package/dist/pagination/pagination.css +455 -0
  73. package/dist/pagination/pagination.d.ts +59 -0
  74. package/dist/pagination/pagination.js +77 -0
  75. package/dist/select/select.css +6 -10
  76. package/dist/server-render.test.js +49 -0
  77. package/dist/sidebar/app-shell.d.ts +54 -0
  78. package/dist/sidebar/app-shell.js +28 -0
  79. package/dist/sidebar/index.d.ts +9 -0
  80. package/dist/sidebar/index.js +8 -0
  81. package/dist/sidebar/shell-context.d.ts +30 -0
  82. package/dist/sidebar/shell-context.js +20 -0
  83. package/dist/sidebar/shell-sheet.d.ts +7 -0
  84. package/dist/sidebar/shell-sheet.js +12 -0
  85. package/dist/sidebar/shell-top-bar.d.ts +8 -0
  86. package/dist/sidebar/shell-top-bar.js +25 -0
  87. package/dist/sidebar/sidebar-body.d.ts +5 -0
  88. package/dist/sidebar/sidebar-body.js +37 -0
  89. package/dist/sidebar/sidebar-icons.d.ts +4 -0
  90. package/dist/sidebar/sidebar-icons.js +13 -0
  91. package/dist/sidebar/sidebar-item.d.ts +26 -0
  92. package/dist/sidebar/sidebar-item.js +68 -0
  93. package/dist/sidebar/sidebar-labels.d.ts +14 -0
  94. package/dist/sidebar/sidebar-labels.js +7 -0
  95. package/dist/sidebar/sidebar-marker.d.ts +12 -0
  96. package/dist/sidebar/sidebar-marker.js +77 -0
  97. package/dist/sidebar/sidebar-storage.d.ts +18 -0
  98. package/dist/sidebar/sidebar-storage.js +27 -0
  99. package/dist/sidebar/sidebar-toggle.d.ts +7 -0
  100. package/dist/sidebar/sidebar-toggle.js +18 -0
  101. package/dist/sidebar/sidebar.css +587 -0
  102. package/dist/sidebar/sidebar.d.ts +19 -0
  103. package/dist/sidebar/sidebar.js +31 -0
  104. package/dist/sidebar/use-collapsed.d.ts +13 -0
  105. package/dist/sidebar/use-collapsed.js +20 -0
  106. package/dist/sidebar/use-narrow.d.ts +4 -0
  107. package/dist/sidebar/use-narrow.js +18 -0
  108. package/dist/sidebar/use-shell-focus.d.ts +8 -0
  109. package/dist/sidebar/use-shell-focus.js +30 -0
  110. package/dist/sidebar/use-shell-shortcut.d.ts +4 -0
  111. package/dist/sidebar/use-shell-shortcut.js +32 -0
  112. package/dist/sidebar/use-shell-state.d.ts +18 -0
  113. package/dist/sidebar/use-shell-state.js +47 -0
  114. package/dist/slider/slider.css +9 -6
  115. package/dist/smooth-caret/caret-geometry.js +10 -4
  116. package/dist/table/data-table-props.d.ts +96 -0
  117. package/dist/table/data-table-props.js +1 -0
  118. package/dist/table/data-table.d.ts +7 -0
  119. package/dist/table/data-table.js +59 -0
  120. package/dist/table/index.d.ts +7 -0
  121. package/dist/table/index.js +5 -0
  122. package/dist/table/table-actions.d.ts +9 -0
  123. package/dist/table/table-actions.js +24 -0
  124. package/dist/table/table-builder.d.ts +60 -0
  125. package/dist/table/table-builder.js +99 -0
  126. package/dist/table/table-cells.d.ts +19 -0
  127. package/dist/table/table-cells.js +67 -0
  128. package/dist/table/table-columns.d.ts +38 -0
  129. package/dist/table/table-columns.js +119 -0
  130. package/dist/table/table-icons.d.ts +3 -0
  131. package/dist/table/table-icons.js +8 -0
  132. package/dist/table/table-labels.d.ts +24 -0
  133. package/dist/table/table-labels.js +17 -0
  134. package/dist/table/table-model.d.ts +28 -0
  135. package/dist/table/table-model.js +69 -0
  136. package/dist/table/table-sections.d.ts +25 -0
  137. package/dist/table/table-sections.js +79 -0
  138. package/dist/table/table-state.d.ts +42 -0
  139. package/dist/table/table-state.js +81 -0
  140. package/dist/table/table-types.d.ts +86 -0
  141. package/dist/table/table-types.js +1 -0
  142. package/dist/table/table.css +919 -0
  143. package/dist/table/use-table-view.d.ts +30 -0
  144. package/dist/table/use-table-view.js +147 -0
  145. package/dist/toast/index.d.ts +1 -2
  146. package/dist/toast/index.js +1 -2
  147. package/package.json +44 -1
@@ -0,0 +1,7 @@
1
+ import { type QuestionOptions } from "./dialog-store";
2
+ export type AskOptions = QuestionOptions;
3
+ /**
4
+ * Asks a yes or no question from anywhere. It resolves `true` once confirmed (after `action` succeeds) and
5
+ * `false` on Cancel or Escape. Questions queue, one at a time. An `OverlayHost` must be mounted, or this throws.
6
+ */
7
+ export declare function ask(options: AskOptions): Promise<boolean>;
@@ -0,0 +1,8 @@
1
+ import { addQuestion } from "./dialog-store";
2
+ /**
3
+ * Asks a yes or no question from anywhere. It resolves `true` once confirmed (after `action` succeeds) and
4
+ * `false` on Cancel or Escape. Questions queue, one at a time. An `OverlayHost` must be mounted, or this throws.
5
+ */
6
+ export function ask(options) {
7
+ return addQuestion(options).promise;
8
+ }
@@ -0,0 +1,23 @@
1
+ import type { DismissGuard, EntryUpdate } from "./dialog-store";
2
+ /** What the content of an opened dialog can do to the dialog around it. */
3
+ export interface DialogContentApi<Result = unknown> {
4
+ /** Closes the dialog and settles its `result` with `value`. It closes even while `pending`. */
5
+ close: (value?: Result) => void;
6
+ /** While pending, Escape, an outside press and the "×" can't close the dialog. */
7
+ setPending: (pending: boolean) => void;
8
+ /** While dirty, a dismiss asks "Discard your changes?" before it closes. */
9
+ setDirty: (dirty: boolean) => void;
10
+ pending: boolean;
11
+ /** Changes the dialog's title or description, which the host draws, while it is open. */
12
+ update: (update: Omit<EntryUpdate, "content">) => void;
13
+ /**
14
+ * Adds a guard that can turn down Escape, an outside press, the "×" or a swipe, by reason. It returns
15
+ * its removal, so it fits an effect: `useEffect(() => guardDismiss(guard), [guard])`.
16
+ */
17
+ guardDismiss: (guard: DismissGuard) => () => void;
18
+ }
19
+ export declare const DialogContentContext: import("react").Context<DialogContentApi<unknown> | null>;
20
+ /** Reads the dialog the content is shown in. It throws outside a dialog `openDialog()` opened. */
21
+ export declare function useDialogContent<Result = unknown>(): DialogContentApi<Result>;
22
+ /** The row of buttons at the end of a form in an opened dialog. */
23
+ export declare function DialogActions(props: React.ComponentProps<"div">): React.JSX.Element;
@@ -0,0 +1,15 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { createContext, useContext } from "react";
3
+ export const DialogContentContext = createContext(null);
4
+ /** Reads the dialog the content is shown in. It throws outside a dialog `openDialog()` opened. */
5
+ export function useDialogContent() {
6
+ const api = useContext(DialogContentContext);
7
+ if (!api) {
8
+ throw new Error("useDialogContent() works only inside the content of an openDialog() dialog.");
9
+ }
10
+ return api;
11
+ }
12
+ /** The row of buttons at the end of a form in an opened dialog. */
13
+ export function DialogActions(props) {
14
+ return _jsx("div", { ...props, "data-kumiko-actions": "" });
15
+ }
@@ -0,0 +1,14 @@
1
+ import { type ReactNode } from "react";
2
+ import { type HostState } from "./dialog-store";
3
+ /**
4
+ * How modal the host's opened dialogs are. `"trap-focus"` keeps Tab inside without making the rest of the
5
+ * page inert, which on a big app restyles every element on each open.
6
+ */
7
+ export declare const DialogModalContext: import("react").Context<"trap-focus" | boolean | undefined>;
8
+ /** What the host's corner close button shows. */
9
+ export declare const DialogCloseIconContext: import("react").Context<ReactNode>;
10
+ /** The dialogs opened directly above `parent` (the host root when `null`), and the question that shows there. */
11
+ export declare function Stack({ parent, state }: {
12
+ parent: number | null;
13
+ state: HostState;
14
+ }): React.JSX.Element;
@@ -0,0 +1,111 @@
1
+ import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { DialogBackdrop, DialogClose, DialogContent, DialogDescription, DialogTitle } from "@ui-sdk/react/dialog";
3
+ import { createContext, useContext, useMemo, useRef } from "react";
4
+ import { Dialog } from "./dialog";
5
+ import { DialogContentContext } from "./dialog-content";
6
+ import { addDismissGuard, allowsDismiss, answerQuestion, childrenOf, closeEntry, discardEntry, dismissEntry, finishEntry, finishQuestion, keepEditing, setEntryFlag, updateEntry } from "./dialog-store";
7
+ import { NestAnchor, useOpener } from "./nest";
8
+ import { QuestionDialog } from "./question-dialog";
9
+ const contentSelector = '[data-slot$="dialog-content"]';
10
+ /**
11
+ * How modal the host's opened dialogs are. `"trap-focus"` keeps Tab inside without making the rest of the
12
+ * page inert, which on a big app restyles every element on each open.
13
+ */
14
+ export const DialogModalContext = createContext(true);
15
+ /** What the host's corner close button shows. */
16
+ export const DialogCloseIconContext = createContext("×");
17
+ /** The reasons ui-sdk closes a dialog for that the store already knows about. */
18
+ const settledByStore = new Set(["parent-close", "imperative"]);
19
+ function QuestionView({ state }) {
20
+ const { question } = state;
21
+ if (!question) {
22
+ return null;
23
+ }
24
+ return (_jsx(QuestionDialog, { open: question.phase === "open", options: question.options, labels: state.labels, nested: question.anchor !== null, onAnswer: (yes) => {
25
+ answerQuestion(question.id, yes);
26
+ }, onComplete: () => {
27
+ finishQuestion(question.id);
28
+ } }, question.id));
29
+ }
30
+ /** The dialogs opened directly above `parent` (the host root when `null`), and the question that shows there. */
31
+ export function Stack({ parent, state }) {
32
+ const showsQuestion = state.question?.anchor === parent;
33
+ return (_jsxs(_Fragment, { children: [childrenOf(state.entries, parent).map((entry) => (_jsx(EntryView, { entry: entry, state: state }, entry.id))), showsQuestion ? _jsx(QuestionView, { state: state }) : null] }));
34
+ }
35
+ function discardQuestion(state) {
36
+ const { labels } = state;
37
+ return {
38
+ title: labels.discardTitle,
39
+ description: labels.discardDescription,
40
+ tone: "warning",
41
+ confirmLabel: labels.discard,
42
+ cancelLabel: labels.keepEditing
43
+ };
44
+ }
45
+ function useContentApi(entry) {
46
+ const { id, pending } = entry;
47
+ return useMemo(() => ({
48
+ close: (value) => {
49
+ closeEntry(id, value);
50
+ },
51
+ setPending: (next) => {
52
+ setEntryFlag(id, "pending", next);
53
+ },
54
+ setDirty: (next) => {
55
+ setEntryFlag(id, "dirty", next);
56
+ },
57
+ pending,
58
+ update: (update) => {
59
+ updateEntry(id, update);
60
+ },
61
+ guardDismiss: (guard) => addDismissGuard(id, guard)
62
+ }), [id, pending]);
63
+ }
64
+ /** The question that stands between a dirty dialog and its closing. */
65
+ function DiscardQuestion({ entry, state, focusBack }) {
66
+ return (_jsx(QuestionDialog, { open: entry.phase === "discarding", options: discardQuestion(state), labels: state.labels, nested: true, onAnswer: (yes) => {
67
+ if (yes) {
68
+ discardEntry(entry.id);
69
+ }
70
+ else {
71
+ keepEditing(entry.id);
72
+ }
73
+ }, onComplete: () => undefined, finalFocus: focusBack }));
74
+ }
75
+ /** The panel: the host's title, description and "×", the app's content, and what opened above it. */
76
+ function EntryPanel({ entry, state }) {
77
+ const api = useContentApi(entry);
78
+ const modal = useContext(DialogModalContext);
79
+ const closeIcon = useContext(DialogCloseIconContext);
80
+ const opener = useOpener();
81
+ const lastFocus = useRef(null);
82
+ const nested = entry.parent !== null;
83
+ const focusBack = () => lastFocus.current?.isConnected ? lastFocus.current : undefined;
84
+ return (_jsxs(DialogContent, { "aria-modal": modal === false ? undefined : "true", "data-size": entry.size, className: entry.className, finalFocus: nested ? opener : undefined, onFocus: (event) => {
85
+ const { target } = event;
86
+ if (target instanceof HTMLElement &&
87
+ target.closest(contentSelector) === event.currentTarget) {
88
+ lastFocus.current = target;
89
+ }
90
+ }, children: [entry.chrome === "full" ? _jsx(DialogTitle, { children: entry.title }) : null, entry.chrome === "full" && entry.description ? (_jsx(DialogDescription, { children: entry.description })) : null, _jsx(DialogContentContext.Provider, { value: api, children: entry.content }), entry.chrome === "full" ? (_jsx(DialogClose, { "aria-label": state.labels.close, disabled: entry.pending, children: closeIcon })) : null, _jsx(Stack, { parent: entry.id, state: state }), _jsx(DiscardQuestion, { entry: entry, state: state, focusBack: focusBack })] }));
91
+ }
92
+ /**
93
+ * One opened dialog. The store owns its phase, so every close ui-sdk proposes is cancelled and handed to
94
+ * the store as a dismiss, which closes, or asks first when the content is dirty.
95
+ */
96
+ function EntryView({ entry, state }) {
97
+ const modal = useContext(DialogModalContext);
98
+ return (_jsxs(Dialog, { open: entry.phase !== "closed", modal: modal, presentation: entry.presentation, dismissible: !entry.pending, onOpenChange: (open, details) => {
99
+ if (open || settledByStore.has(details.reason)) {
100
+ return;
101
+ }
102
+ details.cancel();
103
+ if (allowsDismiss(entry.id, details.reason)) {
104
+ dismissEntry(entry.id);
105
+ }
106
+ }, onOpenChangeComplete: (open) => {
107
+ if (!open) {
108
+ finishEntry(entry.id);
109
+ }
110
+ }, children: [entry.parent === null ? null : _jsx(NestAnchor, {}), _jsx(DialogBackdrop, {}), _jsx(EntryPanel, { entry: entry, state: state })] }));
111
+ }
@@ -0,0 +1,23 @@
1
+ /** The words the dialog host and the leave guard speak. */
2
+ export interface DialogLabels {
3
+ /** The corner "×" of an opened dialog. */
4
+ close: string;
5
+ /** The confirm button of a question that gives no label of its own. */
6
+ confirm: string;
7
+ /** The cancel button of a question that gives no label of its own. */
8
+ cancel: string;
9
+ /** The confirm button after its action failed. */
10
+ retry: string;
11
+ /** The error line when a failed action has no message. */
12
+ failed: string;
13
+ discardTitle: string;
14
+ discardDescription: string;
15
+ discard: string;
16
+ keepEditing: string;
17
+ leaveTitle: string;
18
+ leaveDescription: string;
19
+ leave: string;
20
+ stay: string;
21
+ }
22
+ /** The English words. An `OverlayHost` `labels` prop overrides any of them. */
23
+ export declare const defaultDialogLabels: DialogLabels;
@@ -0,0 +1,16 @@
1
+ /** The English words. An `OverlayHost` `labels` prop overrides any of them. */
2
+ export const defaultDialogLabels = {
3
+ close: "Close",
4
+ confirm: "Confirm",
5
+ cancel: "Cancel",
6
+ retry: "Try again",
7
+ failed: "That didn't work.",
8
+ discardTitle: "Discard your changes?",
9
+ discardDescription: "What you changed in this dialog will be lost.",
10
+ discard: "Discard",
11
+ keepEditing: "Keep editing",
12
+ leaveTitle: "Leave this page?",
13
+ leaveDescription: "Your changes haven't been saved.",
14
+ leave: "Leave",
15
+ stay: "Stay"
16
+ };
@@ -0,0 +1,139 @@
1
+ import type { DialogOpenChangeReason, DialogProps } from "@ui-sdk/react/dialog";
2
+ import type { ReactNode } from "react";
3
+ import { type DialogLabels } from "./dialog-labels";
4
+ /**
5
+ * One opened dialog.
6
+ *
7
+ * - `open`: showing, and a dismiss closes it (or asks, when dirty).
8
+ * - `discarding`: the "Discard your changes?" question is up above it.
9
+ * - `closed`: its result is settled; it stays until its exit has played.
10
+ */
11
+ export type EntryPhase = "open" | "discarding" | "closed";
12
+ export type DialogSize = "sm" | "md" | "lg";
13
+ /**
14
+ * - `full`: the host draws the title, the description and the corner "×" around the content.
15
+ * - `bare`: the content draws its own, and must hold a `DialogTitle`. The host keeps the rest: the stack,
16
+ * the dim, focus, dismissing, dirty and pending.
17
+ */
18
+ export type DialogChrome = "full" | "bare";
19
+ /** Says whether a dismiss for `reason` may go ahead. Returning `false` keeps the dialog open. */
20
+ export type DismissGuard = (reason: DialogOpenChangeReason) => boolean;
21
+ export interface DialogEntry {
22
+ readonly id: number;
23
+ /** The entry this one opened above, which renders it inside its own content. */
24
+ readonly parent: number | null;
25
+ readonly title: ReactNode;
26
+ readonly description: ReactNode;
27
+ readonly content: ReactNode;
28
+ readonly size: DialogSize;
29
+ readonly chrome: DialogChrome;
30
+ readonly presentation: NonNullable<DialogProps["presentation"]>;
31
+ readonly className: string | undefined;
32
+ readonly phase: EntryPhase;
33
+ readonly dirty: boolean;
34
+ readonly pending: boolean;
35
+ }
36
+ export interface QuestionOptions {
37
+ title: string;
38
+ description?: ReactNode;
39
+ /** `"danger"` makes the confirm button destructive, `"warning"` amber. */
40
+ tone?: "danger" | "warning";
41
+ confirmLabel?: string;
42
+ cancelLabel?: string;
43
+ /** Runs when confirmed. The question stays open while it runs, and again after it throws. */
44
+ action?: () => unknown;
45
+ /**
46
+ * Draws the confirm control in place of the confirm button, such as a hold-to-confirm button. Call
47
+ * `confirm()` from it to answer yes, which runs `action` as the button would.
48
+ */
49
+ renderConfirm?: (control: QuestionConfirmControl) => ReactNode;
50
+ }
51
+ /** What a question gives a confirm control of the app's own. */
52
+ export interface QuestionConfirmControl {
53
+ /** `confirmLabel`, or Try again after `action` threw. */
54
+ label: string;
55
+ tone: QuestionOptions["tone"];
56
+ /** Whether `action` is running. */
57
+ pending: boolean;
58
+ confirm: () => void;
59
+ }
60
+ /** The question on screen: `open` until answered, then `closed` while it animates out. */
61
+ export interface QuestionEntry {
62
+ readonly id: number;
63
+ readonly options: QuestionOptions;
64
+ readonly phase: "open" | "closed";
65
+ /** The entry whose content shows it, or `null` for the host root. */
66
+ readonly anchor: number | null;
67
+ }
68
+ export interface HostState {
69
+ readonly entries: readonly DialogEntry[];
70
+ readonly question: QuestionEntry | null;
71
+ readonly labels: DialogLabels;
72
+ /** The host that renders the stack: the first one mounted. */
73
+ readonly activeHost: symbol | null;
74
+ }
75
+ interface EntryLook {
76
+ content: ReactNode;
77
+ size?: DialogSize;
78
+ /** The presentation ui-sdk gives it: `"auto"` (a sheet on small touch screens), `"dialog"` or `"sheet"`. */
79
+ presentation?: DialogProps["presentation"];
80
+ /** A class for the panel, for a look of the app's own. */
81
+ className?: string;
82
+ }
83
+ export type EntryOptions = EntryLook & ({
84
+ chrome?: "full";
85
+ title: ReactNode;
86
+ description?: ReactNode;
87
+ } | {
88
+ chrome: "bare";
89
+ title?: undefined;
90
+ description?: undefined;
91
+ });
92
+ /** What can change on an open dialog. */
93
+ export interface EntryUpdate {
94
+ title?: ReactNode;
95
+ description?: ReactNode;
96
+ content?: ReactNode;
97
+ }
98
+ export declare function subscribe(listener: () => void): () => void;
99
+ export declare function getSnapshot(): HostState;
100
+ /** The words the host speaks now: the defaults, then the first host's `labels`. */
101
+ export declare function currentLabels(): DialogLabels;
102
+ /**
103
+ * Registers a mounted `OverlayHost` and returns its way out. The first host renders the stack, and a later
104
+ * one takes over only when the first leaves. When the last host leaves, everything open resolves.
105
+ */
106
+ export declare function registerHost(token: symbol, labels: Partial<DialogLabels> | undefined): () => void;
107
+ export declare function setHostLabels(token: symbol, labels: Partial<DialogLabels> | undefined): void;
108
+ /** Opens a dialog above the newest open one, and returns the id to close it by and its result. */
109
+ export declare function addEntry(options: EntryOptions): {
110
+ id: number;
111
+ result: Promise<unknown>;
112
+ };
113
+ /** Closes an entry and everything above it. Each resolves once: the entry with `value`, the rest with nothing. */
114
+ export declare function closeEntry(id: number, value?: unknown): void;
115
+ /** Escape, an outside press, the "×" or a swipe: closes, or asks first when there is something to lose. */
116
+ export declare function dismissEntry(id: number): void;
117
+ export declare function keepEditing(id: number): void;
118
+ export declare function discardEntry(id: number): void;
119
+ export declare function setEntryFlag(id: number, flag: "dirty" | "pending", value: boolean): void;
120
+ /** Adds a guard that can turn down a dismiss of the entry, and returns its removal. */
121
+ export declare function addDismissGuard(id: number, guard: DismissGuard): () => void;
122
+ /** Whether every guard of the entry lets a dismiss for `reason` through. */
123
+ export declare function allowsDismiss(id: number, reason: DialogOpenChangeReason): boolean;
124
+ /** Changes the title, description or content of an open entry. A closed one stays as it was. */
125
+ export declare function updateEntry(id: number, update: EntryUpdate): void;
126
+ /** Takes a closed entry, and any above it, off the stack once its exit has played. */
127
+ export declare function finishEntry(id: number): void;
128
+ /** Asks a question, now or after the ones before it. `cancel` answers it `false` wherever it is. */
129
+ export declare function addQuestion(options: QuestionOptions): {
130
+ promise: Promise<boolean>;
131
+ cancel: () => void;
132
+ };
133
+ /** Settles a question. One on screen starts closing, and one still waiting leaves the queue. */
134
+ export declare function answerQuestion(id: number, yes: boolean): void;
135
+ /** Takes an answered question off the screen once its exit has played, and shows the next one. */
136
+ export declare function finishQuestion(id: number): void;
137
+ /** The entries opened directly above `parent`, or the ones with no parent, in the order they opened. */
138
+ export declare function childrenOf(entries: readonly DialogEntry[], parent: number | null): DialogEntry[];
139
+ export {};
@@ -0,0 +1,296 @@
1
+ import { defaultDialogLabels } from "./dialog-labels";
2
+ const noHostMessage = "openDialog() and ask() need an OverlayHost: mount <OverlayHost /> once near the root of the app.";
3
+ let state = {
4
+ entries: [],
5
+ question: null,
6
+ labels: defaultDialogLabels,
7
+ activeHost: null
8
+ };
9
+ let nextId = 1;
10
+ const hosts = [];
11
+ const listeners = new Set();
12
+ const entryResolvers = new Map();
13
+ const answerResolvers = new Map();
14
+ const dismissGuards = new Map();
15
+ let waiting = [];
16
+ function commit(next) {
17
+ state = { ...state, ...next };
18
+ for (const listener of listeners) {
19
+ listener();
20
+ }
21
+ }
22
+ export function subscribe(listener) {
23
+ listeners.add(listener);
24
+ return () => {
25
+ listeners.delete(listener);
26
+ };
27
+ }
28
+ export function getSnapshot() {
29
+ return state;
30
+ }
31
+ function requireHost() {
32
+ if (hosts.length === 0) {
33
+ throw new Error(noHostMessage);
34
+ }
35
+ }
36
+ /** The words the host speaks now: the defaults, then the first host's `labels`. */
37
+ export function currentLabels() {
38
+ return state.labels;
39
+ }
40
+ function sameLabels(a, b) {
41
+ return JSON.stringify(a) === JSON.stringify(b);
42
+ }
43
+ /** The defaults, with the words a host gives over them. Only strings count, so an `undefined` keeps the default. */
44
+ function mergeLabels(labels) {
45
+ const given = Object.entries(labels ?? {}).filter(([, word]) => typeof word === "string");
46
+ return { ...defaultDialogLabels, ...Object.fromEntries(given) };
47
+ }
48
+ function syncHosts() {
49
+ const first = hosts[0];
50
+ const labels = first ? first.labels : defaultDialogLabels;
51
+ const next = sameLabels(labels, state.labels) ? state.labels : labels;
52
+ const activeHost = first?.token ?? null;
53
+ if (activeHost !== state.activeHost || next !== state.labels) {
54
+ commit({ activeHost, labels: next });
55
+ }
56
+ }
57
+ function settleEverything() {
58
+ for (const resolve of entryResolvers.values()) {
59
+ resolve(undefined);
60
+ }
61
+ entryResolvers.clear();
62
+ for (const resolve of answerResolvers.values()) {
63
+ resolve(false);
64
+ }
65
+ answerResolvers.clear();
66
+ dismissGuards.clear();
67
+ waiting = [];
68
+ commit({ entries: [], question: null });
69
+ }
70
+ /**
71
+ * Registers a mounted `OverlayHost` and returns its way out. The first host renders the stack, and a later
72
+ * one takes over only when the first leaves. When the last host leaves, everything open resolves.
73
+ */
74
+ export function registerHost(token, labels) {
75
+ hosts.push({ token, labels: mergeLabels(labels) });
76
+ syncHosts();
77
+ return () => {
78
+ const at = hosts.findIndex((host) => host.token === token);
79
+ if (at !== -1) {
80
+ hosts.splice(at, 1);
81
+ }
82
+ if (hosts.length === 0) {
83
+ settleEverything();
84
+ }
85
+ syncHosts();
86
+ };
87
+ }
88
+ export function setHostLabels(token, labels) {
89
+ const host = hosts.find((one) => one.token === token);
90
+ if (host) {
91
+ host.labels = mergeLabels(labels);
92
+ syncHosts();
93
+ }
94
+ }
95
+ /** The newest entry that has not been closed: where the next dialog or question opens above. */
96
+ function topOpenId(entries) {
97
+ for (let at = entries.length - 1; at >= 0; at -= 1) {
98
+ const entry = entries[at];
99
+ if (entry && entry.phase !== "closed") {
100
+ return entry.id;
101
+ }
102
+ }
103
+ return null;
104
+ }
105
+ /** An entry and every entry opened above it, at any depth. */
106
+ function subtreeIds(entries, id) {
107
+ const ids = new Set([id]);
108
+ for (const entry of entries) {
109
+ if (entry.parent !== null && ids.has(entry.parent)) {
110
+ ids.add(entry.id);
111
+ }
112
+ }
113
+ return ids;
114
+ }
115
+ function replaceEntry(id, change) {
116
+ commit({ entries: state.entries.map((entry) => (entry.id === id ? change(entry) : entry)) });
117
+ }
118
+ function newId() {
119
+ const id = nextId;
120
+ nextId += 1;
121
+ return id;
122
+ }
123
+ /** Opens a dialog above the newest open one, and returns the id to close it by and its result. */
124
+ export function addEntry(options) {
125
+ requireHost();
126
+ const id = newId();
127
+ const result = new Promise((resolve) => {
128
+ entryResolvers.set(id, resolve);
129
+ });
130
+ const entry = {
131
+ id,
132
+ parent: topOpenId(state.entries),
133
+ title: options.title,
134
+ description: options.description,
135
+ content: options.content,
136
+ size: options.size ?? "md",
137
+ chrome: options.chrome ?? "full",
138
+ presentation: options.presentation ?? "auto",
139
+ className: options.className,
140
+ phase: "open",
141
+ dirty: false,
142
+ pending: false
143
+ };
144
+ commit({ entries: [...state.entries, entry] });
145
+ return { id, result };
146
+ }
147
+ /** A question showing inside a dialog that has closed moves to the newest open one, or the root. */
148
+ function rehomeQuestion(closed) {
149
+ const { question } = state;
150
+ if (!question || question.anchor === null || !closed.has(question.anchor)) {
151
+ return;
152
+ }
153
+ if (question.phase === "closed") {
154
+ finishQuestion(question.id);
155
+ return;
156
+ }
157
+ commit({ question: { ...question, anchor: topOpenId(state.entries) } });
158
+ }
159
+ /** Closes an entry and everything above it. Each resolves once: the entry with `value`, the rest with nothing. */
160
+ export function closeEntry(id, value) {
161
+ const target = state.entries.find((entry) => entry.id === id);
162
+ if (!target || target.phase === "closed") {
163
+ return;
164
+ }
165
+ const ids = subtreeIds(state.entries, id);
166
+ commit({
167
+ entries: state.entries.map((entry) => ids.has(entry.id) && entry.phase !== "closed" ? { ...entry, phase: "closed" } : entry)
168
+ });
169
+ for (const closing of ids) {
170
+ entryResolvers.get(closing)?.(closing === id ? value : undefined);
171
+ entryResolvers.delete(closing);
172
+ dismissGuards.delete(closing);
173
+ }
174
+ rehomeQuestion(ids);
175
+ }
176
+ /** Escape, an outside press, the "×" or a swipe: closes, or asks first when there is something to lose. */
177
+ export function dismissEntry(id) {
178
+ const entry = state.entries.find((one) => one.id === id);
179
+ if (!entry || entry.phase !== "open" || entry.pending) {
180
+ return;
181
+ }
182
+ if (entry.dirty) {
183
+ replaceEntry(id, (one) => ({ ...one, phase: "discarding" }));
184
+ return;
185
+ }
186
+ closeEntry(id);
187
+ }
188
+ export function keepEditing(id) {
189
+ const entry = state.entries.find((one) => one.id === id);
190
+ if (entry?.phase === "discarding") {
191
+ replaceEntry(id, (one) => ({ ...one, phase: "open" }));
192
+ }
193
+ }
194
+ export function discardEntry(id) {
195
+ const entry = state.entries.find((one) => one.id === id);
196
+ if (entry?.phase === "discarding") {
197
+ closeEntry(id);
198
+ }
199
+ }
200
+ export function setEntryFlag(id, flag, value) {
201
+ const entry = state.entries.find((one) => one.id === id);
202
+ if (entry && entry.phase !== "closed" && entry[flag] !== value) {
203
+ replaceEntry(id, (one) => ({ ...one, [flag]: value }));
204
+ }
205
+ }
206
+ /** Adds a guard that can turn down a dismiss of the entry, and returns its removal. */
207
+ export function addDismissGuard(id, guard) {
208
+ const guards = dismissGuards.get(id) ?? new Set();
209
+ guards.add(guard);
210
+ dismissGuards.set(id, guards);
211
+ return () => {
212
+ dismissGuards.get(id)?.delete(guard);
213
+ };
214
+ }
215
+ /** Whether every guard of the entry lets a dismiss for `reason` through. */
216
+ export function allowsDismiss(id, reason) {
217
+ return [...(dismissGuards.get(id) ?? [])].every((guard) => guard(reason));
218
+ }
219
+ /** Changes the title, description or content of an open entry. A closed one stays as it was. */
220
+ export function updateEntry(id, update) {
221
+ const entry = state.entries.find((one) => one.id === id);
222
+ if (!entry || entry.phase === "closed") {
223
+ return;
224
+ }
225
+ replaceEntry(id, (one) => ({ ...one, ...update }));
226
+ }
227
+ /** Takes a closed entry, and any above it, off the stack once its exit has played. */
228
+ export function finishEntry(id) {
229
+ const entry = state.entries.find((one) => one.id === id);
230
+ if (entry?.phase !== "closed") {
231
+ return;
232
+ }
233
+ const ids = subtreeIds(state.entries, id);
234
+ commit({ entries: state.entries.filter((one) => !ids.has(one.id)) });
235
+ rehomeQuestion(ids);
236
+ }
237
+ function startQuestion(next) {
238
+ commit({
239
+ question: {
240
+ id: next.id,
241
+ options: next.options,
242
+ phase: "open",
243
+ anchor: topOpenId(state.entries)
244
+ }
245
+ });
246
+ }
247
+ /** Asks a question, now or after the ones before it. `cancel` answers it `false` wherever it is. */
248
+ export function addQuestion(options) {
249
+ requireHost();
250
+ const id = newId();
251
+ const promise = new Promise((resolve) => {
252
+ answerResolvers.set(id, resolve);
253
+ });
254
+ if (state.question) {
255
+ waiting.push({ id, options });
256
+ }
257
+ else {
258
+ startQuestion({ id, options });
259
+ }
260
+ return { promise, cancel: () => answerQuestion(id, false) };
261
+ }
262
+ /** Settles a question. One on screen starts closing, and one still waiting leaves the queue. */
263
+ export function answerQuestion(id, yes) {
264
+ const { question } = state;
265
+ if (question?.id === id) {
266
+ if (question.phase === "open") {
267
+ commit({ question: { ...question, phase: "closed" } });
268
+ answerResolvers.get(id)?.(yes);
269
+ answerResolvers.delete(id);
270
+ }
271
+ return;
272
+ }
273
+ if (waiting.some((one) => one.id === id)) {
274
+ waiting = waiting.filter((one) => one.id !== id);
275
+ answerResolvers.get(id)?.(false);
276
+ answerResolvers.delete(id);
277
+ }
278
+ }
279
+ /** Takes an answered question off the screen once its exit has played, and shows the next one. */
280
+ export function finishQuestion(id) {
281
+ const { question } = state;
282
+ if (question?.id !== id || question.phase !== "closed") {
283
+ return;
284
+ }
285
+ const next = waiting.shift();
286
+ if (next) {
287
+ startQuestion(next);
288
+ }
289
+ else {
290
+ commit({ question: null });
291
+ }
292
+ }
293
+ /** The entries opened directly above `parent`, or the ones with no parent, in the order they opened. */
294
+ export function childrenOf(entries, parent) {
295
+ return entries.filter((entry) => entry.parent === parent);
296
+ }