@opencode/ui 0.0.0-dev-686 → 0.0.0-dev-703
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.
- package/dist/context/dialog-machine.d.ts +125 -0
- package/dist/context/dialog.d.ts +17 -16
- package/package.json +2 -2
- package/src/context/dialog-machine.ts +280 -0
- package/src/context/dialog.tsx +168 -177
- package/src/overlays/dialog/dialog.tsx +7 -1
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/** How long a dialog stays mounted once it starts closing, unless its layer asks for longer. */
|
|
2
|
+
export declare const DIALOG_EXIT_MS = 100;
|
|
3
|
+
/**
|
|
4
|
+
* `attempt` identifies one mount, so a reused caller `id` never collides with a layer that is still closing. `focus` is
|
|
5
|
+
* where focus returns: where it was before this dialog opened, or before the dialogs it replaced opened. `replaced`
|
|
6
|
+
* holds the attempts of the open dialogs this one replaced through `show`, and theirs in turn, so a close from inside
|
|
7
|
+
* one of them closes this dialog.
|
|
8
|
+
*/
|
|
9
|
+
export type DialogOpenEntry<F = unknown> = {
|
|
10
|
+
readonly status: "open";
|
|
11
|
+
readonly attempt: number;
|
|
12
|
+
readonly id: string;
|
|
13
|
+
readonly exitMs: number;
|
|
14
|
+
readonly backdropDismiss: boolean;
|
|
15
|
+
readonly focus: F | undefined;
|
|
16
|
+
readonly replaced: readonly number[];
|
|
17
|
+
};
|
|
18
|
+
/** Waits for its exit animation. It keeps `focus` so a `show` that replaces it still returns focus to the origin. */
|
|
19
|
+
export type DialogClosingEntry<F = unknown> = {
|
|
20
|
+
readonly status: "closing";
|
|
21
|
+
readonly attempt: number;
|
|
22
|
+
readonly id: string;
|
|
23
|
+
readonly focus: F | undefined;
|
|
24
|
+
readonly replaced: readonly number[];
|
|
25
|
+
};
|
|
26
|
+
export type DialogEntry<F = unknown> = DialogOpenEntry<F> | DialogClosingEntry<F>;
|
|
27
|
+
export type DialogStackState<F = unknown> = {
|
|
28
|
+
readonly status: "active";
|
|
29
|
+
readonly issued: number;
|
|
30
|
+
readonly entries: readonly DialogEntry<F>[];
|
|
31
|
+
} | {
|
|
32
|
+
readonly status: "disposed";
|
|
33
|
+
readonly issued: number;
|
|
34
|
+
};
|
|
35
|
+
/** `request` is the driver's content for the open; the machine only hands it back in `mount` or `discard`. */
|
|
36
|
+
export type DialogStackEvent<F = unknown, R = unknown> = {
|
|
37
|
+
readonly type: "open";
|
|
38
|
+
readonly mode: "show" | "push";
|
|
39
|
+
readonly id: string;
|
|
40
|
+
readonly aborted: boolean;
|
|
41
|
+
readonly focus: F | undefined;
|
|
42
|
+
readonly request: R;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* With `id`, closes the newest open dialog with that id. Without one, `layer` is the attempt of the dialog that asked:
|
|
46
|
+
* that dialog closes itself, even under a dialog that is still exiting, or the dialog that replaced it through `show`.
|
|
47
|
+
* Once that dialog is gone, a late close (after an await) closes nothing. A close from outside every dialog closes the
|
|
48
|
+
* newest dialog. Both close one at a time: while the target plays its exit animation, a second close (a double click,
|
|
49
|
+
* a callback closing again) closes nothing below it.
|
|
50
|
+
*/
|
|
51
|
+
| {
|
|
52
|
+
readonly type: "close";
|
|
53
|
+
readonly id?: string;
|
|
54
|
+
readonly layer?: number;
|
|
55
|
+
}
|
|
56
|
+
/** Escape, or a dismissal that applies only while `id` is the top open dialog. */
|
|
57
|
+
| {
|
|
58
|
+
readonly type: "dismiss";
|
|
59
|
+
readonly id?: string;
|
|
60
|
+
}
|
|
61
|
+
/** A click on one layer's backdrop. */
|
|
62
|
+
| {
|
|
63
|
+
readonly type: "backdrop.click";
|
|
64
|
+
readonly attempt: number;
|
|
65
|
+
}
|
|
66
|
+
/** Kobalte asking one layer to close, as its close button does. */
|
|
67
|
+
| {
|
|
68
|
+
readonly type: "layer.dismiss";
|
|
69
|
+
readonly attempt: number;
|
|
70
|
+
} | {
|
|
71
|
+
readonly type: "layer.backdrop-dismiss";
|
|
72
|
+
readonly attempt: number;
|
|
73
|
+
readonly enabled: boolean;
|
|
74
|
+
} | {
|
|
75
|
+
readonly type: "layer.exit-duration";
|
|
76
|
+
readonly attempt: number;
|
|
77
|
+
readonly exitMs: number;
|
|
78
|
+
} | {
|
|
79
|
+
readonly type: "exit.completed";
|
|
80
|
+
readonly attempt: number;
|
|
81
|
+
} | {
|
|
82
|
+
readonly type: "disposed";
|
|
83
|
+
};
|
|
84
|
+
export type DialogStackEffect<F = unknown, R = unknown> = {
|
|
85
|
+
readonly type: "mount";
|
|
86
|
+
readonly attempt: number;
|
|
87
|
+
readonly id: string;
|
|
88
|
+
readonly request: R;
|
|
89
|
+
} | {
|
|
90
|
+
readonly type: "discard";
|
|
91
|
+
readonly request: R;
|
|
92
|
+
readonly notify: boolean;
|
|
93
|
+
} | {
|
|
94
|
+
readonly type: "timer.start";
|
|
95
|
+
readonly attempt: number;
|
|
96
|
+
readonly delayMs: number;
|
|
97
|
+
} | {
|
|
98
|
+
readonly type: "timer.stop";
|
|
99
|
+
readonly attempt: number;
|
|
100
|
+
} | {
|
|
101
|
+
readonly type: "notify.close";
|
|
102
|
+
readonly attempt: number;
|
|
103
|
+
} | {
|
|
104
|
+
readonly type: "focus.return";
|
|
105
|
+
readonly attempt: number;
|
|
106
|
+
readonly focus: F | undefined;
|
|
107
|
+
} | {
|
|
108
|
+
readonly type: "dispose";
|
|
109
|
+
readonly attempt: number;
|
|
110
|
+
readonly notify: boolean;
|
|
111
|
+
};
|
|
112
|
+
export type DialogStackTransition<F = unknown, R = unknown> = {
|
|
113
|
+
readonly state: DialogStackState<F>;
|
|
114
|
+
readonly effects: readonly DialogStackEffect<F, R>[];
|
|
115
|
+
};
|
|
116
|
+
export declare function createDialogStackState<F = unknown>(): DialogStackState<F>;
|
|
117
|
+
export declare function transitionDialogStack<F, R>(state: DialogStackState<F>, event: DialogStackEvent<F, R>): DialogStackTransition<F, R>;
|
|
118
|
+
/** The top dialog that is open. One that started closing no longer counts, though it waits to be disposed. */
|
|
119
|
+
export declare function selectTopDialog<F>(state: DialogStackState<F>): DialogOpenEntry<F> | undefined;
|
|
120
|
+
/** True from the moment a layer starts closing, through its exit animation, and once it is gone. */
|
|
121
|
+
export declare function selectLayerClosing<F>(state: DialogStackState<F>, attempt: number): boolean;
|
|
122
|
+
/** Stacked dialogs render as sibling portals, so only the top open layer may own the focus trap. */
|
|
123
|
+
export declare function selectLayerModal<F>(state: DialogStackState<F>, attempt: number, contained: boolean): boolean;
|
|
124
|
+
/** The layer's place in the stack, so a newer dialog always renders above. */
|
|
125
|
+
export declare function selectLayerIndex<F>(state: DialogStackState<F>, attempt: number): number;
|
package/dist/context/dialog.d.ts
CHANGED
|
@@ -1,24 +1,16 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type Accessor, type ParentProps, type JSX } from "solid-js";
|
|
2
2
|
type DialogElement = () => JSX.Element;
|
|
3
|
-
type FocusReturn = {
|
|
3
|
+
export type FocusReturn = {
|
|
4
4
|
element: HTMLElement;
|
|
5
5
|
visible: boolean;
|
|
6
6
|
range?: Range;
|
|
7
7
|
};
|
|
8
|
-
type Active = {
|
|
9
|
-
id: string;
|
|
10
|
-
node: JSX.Element;
|
|
11
|
-
dispose: () => void;
|
|
12
|
-
owner: Owner;
|
|
13
|
-
onClose?: () => void;
|
|
14
|
-
setClosing: (closing: boolean) => void;
|
|
15
|
-
/** How long the dialog stays mounted once it starts closing, so its exit animation can finish. */
|
|
16
|
-
exit: () => number;
|
|
17
|
-
/** Where focus was before this dialog opened, or before the dialogs it replaced opened. */
|
|
18
|
-
focus?: FocusReturn;
|
|
19
|
-
returnFocus: () => void;
|
|
20
|
-
};
|
|
21
8
|
export declare function useDialogLayer(): {
|
|
9
|
+
readonly id: string;
|
|
10
|
+
/** This layer's mount, which a `close()` from inside it targets. */
|
|
11
|
+
readonly attempt: number;
|
|
12
|
+
/** True from the moment this dialog starts closing, through its exit animation. */
|
|
13
|
+
readonly closing: Accessor<boolean>;
|
|
22
14
|
setBackdropDismiss: (value: boolean) => void;
|
|
23
15
|
setExitDuration: (ms: number) => void;
|
|
24
16
|
container?: HTMLElement;
|
|
@@ -32,13 +24,22 @@ export declare function DialogProvider(props: ParentProps<{
|
|
|
32
24
|
}>): JSX.Element;
|
|
33
25
|
export declare function useDialog(): {
|
|
34
26
|
/** The top dialog that is open. One that has started closing no longer counts, though it waits to be disposed. */
|
|
35
|
-
readonly active:
|
|
27
|
+
readonly active: import("./dialog-machine").DialogOpenEntry<FocusReturn> | undefined;
|
|
28
|
+
/** Inside a dialog, true from the moment that dialog starts closing; outside every dialog, false. */
|
|
29
|
+
readonly closing: boolean;
|
|
36
30
|
/**
|
|
37
31
|
* id lets the caller close this dialog later rather than whichever is on top. Opening is deferred; once
|
|
38
32
|
* signal aborts, the open neither mounts nor replaces anything and onClose runs as for a replaced dialog.
|
|
39
33
|
*/
|
|
40
34
|
show(element: DialogElement, onClose?: () => void, id?: string, signal?: AbortSignal): Promise<void>;
|
|
41
35
|
push(element: DialogElement, onClose?: () => void, id?: string, signal?: AbortSignal): Promise<void>;
|
|
36
|
+
/**
|
|
37
|
+
* Programmatic close. With an id it closes that dialog at once. Without one, called inside a dialog, it closes that
|
|
38
|
+
* dialog, or the dialog that replaced it through `show`, and nothing once it is gone; outside every dialog it closes
|
|
39
|
+
* the top one. Either way one at a time, so a double click closes one dialog.
|
|
40
|
+
*/
|
|
42
41
|
close(id?: string): void;
|
|
42
|
+
/** Dismisses the top open dialog, as Escape does; with an id, only while that dialog is on top. */
|
|
43
|
+
dismiss(id?: string): void;
|
|
43
44
|
};
|
|
44
45
|
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opencode/ui",
|
|
3
|
-
"version": "0.0.0-dev-
|
|
3
|
+
"version": "0.0.0-dev-703",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -288,7 +288,7 @@
|
|
|
288
288
|
"dependencies": {
|
|
289
289
|
"@kitlangton/solid-motion": "0.2.2",
|
|
290
290
|
"@kobalte/core": "0.13.13",
|
|
291
|
-
"@opencode/util": "0.0.0-dev-
|
|
291
|
+
"@opencode/util": "0.0.0-dev-703",
|
|
292
292
|
"@pierre/diffs": "1.6.1",
|
|
293
293
|
"@pierre/highlights": "0.2.0",
|
|
294
294
|
"@solid-primitives/event-listener": "2.4.6",
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
/** How long a dialog stays mounted once it starts closing, unless its layer asks for longer. */
|
|
2
|
+
export const DIALOG_EXIT_MS = 100
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* `attempt` identifies one mount, so a reused caller `id` never collides with a layer that is still closing. `focus` is
|
|
6
|
+
* where focus returns: where it was before this dialog opened, or before the dialogs it replaced opened. `replaced`
|
|
7
|
+
* holds the attempts of the open dialogs this one replaced through `show`, and theirs in turn, so a close from inside
|
|
8
|
+
* one of them closes this dialog.
|
|
9
|
+
*/
|
|
10
|
+
export type DialogOpenEntry<F = unknown> = {
|
|
11
|
+
readonly status: "open"
|
|
12
|
+
readonly attempt: number
|
|
13
|
+
readonly id: string
|
|
14
|
+
readonly exitMs: number
|
|
15
|
+
readonly backdropDismiss: boolean
|
|
16
|
+
readonly focus: F | undefined
|
|
17
|
+
readonly replaced: readonly number[]
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Waits for its exit animation. It keeps `focus` so a `show` that replaces it still returns focus to the origin. */
|
|
21
|
+
export type DialogClosingEntry<F = unknown> = {
|
|
22
|
+
readonly status: "closing"
|
|
23
|
+
readonly attempt: number
|
|
24
|
+
readonly id: string
|
|
25
|
+
readonly focus: F | undefined
|
|
26
|
+
readonly replaced: readonly number[]
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export type DialogEntry<F = unknown> = DialogOpenEntry<F> | DialogClosingEntry<F>
|
|
30
|
+
|
|
31
|
+
export type DialogStackState<F = unknown> =
|
|
32
|
+
| { readonly status: "active"; readonly issued: number; readonly entries: readonly DialogEntry<F>[] }
|
|
33
|
+
| { readonly status: "disposed"; readonly issued: number }
|
|
34
|
+
|
|
35
|
+
/** `request` is the driver's content for the open; the machine only hands it back in `mount` or `discard`. */
|
|
36
|
+
export type DialogStackEvent<F = unknown, R = unknown> =
|
|
37
|
+
| {
|
|
38
|
+
readonly type: "open"
|
|
39
|
+
readonly mode: "show" | "push"
|
|
40
|
+
readonly id: string
|
|
41
|
+
readonly aborted: boolean
|
|
42
|
+
readonly focus: F | undefined
|
|
43
|
+
readonly request: R
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* With `id`, closes the newest open dialog with that id. Without one, `layer` is the attempt of the dialog that asked:
|
|
47
|
+
* that dialog closes itself, even under a dialog that is still exiting, or the dialog that replaced it through `show`.
|
|
48
|
+
* Once that dialog is gone, a late close (after an await) closes nothing. A close from outside every dialog closes the
|
|
49
|
+
* newest dialog. Both close one at a time: while the target plays its exit animation, a second close (a double click,
|
|
50
|
+
* a callback closing again) closes nothing below it.
|
|
51
|
+
*/
|
|
52
|
+
| { readonly type: "close"; readonly id?: string; readonly layer?: number }
|
|
53
|
+
/** Escape, or a dismissal that applies only while `id` is the top open dialog. */
|
|
54
|
+
| { readonly type: "dismiss"; readonly id?: string }
|
|
55
|
+
/** A click on one layer's backdrop. */
|
|
56
|
+
| { readonly type: "backdrop.click"; readonly attempt: number }
|
|
57
|
+
/** Kobalte asking one layer to close, as its close button does. */
|
|
58
|
+
| { readonly type: "layer.dismiss"; readonly attempt: number }
|
|
59
|
+
| { readonly type: "layer.backdrop-dismiss"; readonly attempt: number; readonly enabled: boolean }
|
|
60
|
+
| { readonly type: "layer.exit-duration"; readonly attempt: number; readonly exitMs: number }
|
|
61
|
+
| { readonly type: "exit.completed"; readonly attempt: number }
|
|
62
|
+
| { readonly type: "disposed" }
|
|
63
|
+
|
|
64
|
+
export type DialogStackEffect<F = unknown, R = unknown> =
|
|
65
|
+
| { readonly type: "mount"; readonly attempt: number; readonly id: string; readonly request: R }
|
|
66
|
+
| { readonly type: "discard"; readonly request: R; readonly notify: boolean }
|
|
67
|
+
| { readonly type: "timer.start"; readonly attempt: number; readonly delayMs: number }
|
|
68
|
+
| { readonly type: "timer.stop"; readonly attempt: number }
|
|
69
|
+
| { readonly type: "notify.close"; readonly attempt: number }
|
|
70
|
+
| { readonly type: "focus.return"; readonly attempt: number; readonly focus: F | undefined }
|
|
71
|
+
| { readonly type: "dispose"; readonly attempt: number; readonly notify: boolean }
|
|
72
|
+
|
|
73
|
+
export type DialogStackTransition<F = unknown, R = unknown> = {
|
|
74
|
+
readonly state: DialogStackState<F>
|
|
75
|
+
readonly effects: readonly DialogStackEffect<F, R>[]
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function createDialogStackState<F = unknown>(): DialogStackState<F> {
|
|
79
|
+
return { status: "active", issued: 0, entries: [] }
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function transitionDialogStack<F, R>(
|
|
83
|
+
state: DialogStackState<F>,
|
|
84
|
+
event: DialogStackEvent<F, R>,
|
|
85
|
+
): DialogStackTransition<F, R> {
|
|
86
|
+
if (event.type === "open") return open(state, event)
|
|
87
|
+
|
|
88
|
+
if (state.status === "disposed") return { state, effects: [] }
|
|
89
|
+
|
|
90
|
+
if (event.type === "disposed") {
|
|
91
|
+
return {
|
|
92
|
+
state: { status: "disposed", issued: state.issued },
|
|
93
|
+
effects: state.entries.flatMap((entry) => teardown<F, R>(entry, false)),
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (event.type === "exit.completed") {
|
|
98
|
+
const entry = state.entries.find((item) => item.attempt === event.attempt)
|
|
99
|
+
|
|
100
|
+
if (entry?.status !== "closing") return { state, effects: [] }
|
|
101
|
+
|
|
102
|
+
return {
|
|
103
|
+
state: { ...state, entries: state.entries.filter((item) => item !== entry) },
|
|
104
|
+
effects: [{ type: "dispose", attempt: entry.attempt, notify: false }],
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (event.type === "layer.backdrop-dismiss") {
|
|
109
|
+
return update(state, event.attempt, (entry) =>
|
|
110
|
+
entry.backdropDismiss === event.enabled ? entry : { ...entry, backdropDismiss: event.enabled },
|
|
111
|
+
)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (event.type === "layer.exit-duration") {
|
|
115
|
+
const exitMs = Math.max(0, event.exitMs)
|
|
116
|
+
|
|
117
|
+
return update(state, event.attempt, (entry) => (entry.exitMs === exitMs ? entry : { ...entry, exitMs }))
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const target = closeTarget(state, event)
|
|
121
|
+
|
|
122
|
+
if (!target) return { state, effects: [] }
|
|
123
|
+
|
|
124
|
+
return beginClose(state, target)
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** The top dialog that is open. One that started closing no longer counts, though it waits to be disposed. */
|
|
128
|
+
export function selectTopDialog<F>(state: DialogStackState<F>) {
|
|
129
|
+
if (state.status === "disposed") return undefined
|
|
130
|
+
|
|
131
|
+
return state.entries.findLast((entry): entry is DialogOpenEntry<F> => entry.status === "open")
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** True from the moment a layer starts closing, through its exit animation, and once it is gone. */
|
|
135
|
+
export function selectLayerClosing<F>(state: DialogStackState<F>, attempt: number) {
|
|
136
|
+
if (state.status === "disposed") return true
|
|
137
|
+
|
|
138
|
+
return state.entries.find((entry) => entry.attempt === attempt)?.status !== "open"
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** Stacked dialogs render as sibling portals, so only the top open layer may own the focus trap. */
|
|
142
|
+
export function selectLayerModal<F>(state: DialogStackState<F>, attempt: number, contained: boolean) {
|
|
143
|
+
return !contained && selectTopDialog(state)?.attempt === attempt
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** The layer's place in the stack, so a newer dialog always renders above. */
|
|
147
|
+
export function selectLayerIndex<F>(state: DialogStackState<F>, attempt: number) {
|
|
148
|
+
if (state.status === "disposed") return 0
|
|
149
|
+
|
|
150
|
+
return Math.max(
|
|
151
|
+
0,
|
|
152
|
+
state.entries.findIndex((entry) => entry.attempt === attempt),
|
|
153
|
+
)
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function open<F, R>(
|
|
157
|
+
state: DialogStackState<F>,
|
|
158
|
+
event: Extract<DialogStackEvent<F, R>, { type: "open" }>,
|
|
159
|
+
): DialogStackTransition<F, R> {
|
|
160
|
+
// An open whose signal aborted neither mounts nor replaces anything; its caller hears onClose as a replaced one would.
|
|
161
|
+
if (event.aborted) return { state, effects: [{ type: "discard", request: event.request, notify: true }] }
|
|
162
|
+
|
|
163
|
+
// A deferred open must not mount after the provider is gone.
|
|
164
|
+
if (state.status === "disposed") return { state, effects: [{ type: "discard", request: event.request, notify: false }] }
|
|
165
|
+
const attempt = state.issued + 1
|
|
166
|
+
const replace = event.mode === "show"
|
|
167
|
+
const first = state.entries[0]
|
|
168
|
+
|
|
169
|
+
const entry: DialogOpenEntry<F> = {
|
|
170
|
+
status: "open",
|
|
171
|
+
attempt,
|
|
172
|
+
id: event.id,
|
|
173
|
+
exitMs: DIALOG_EXIT_MS,
|
|
174
|
+
backdropDismiss: true,
|
|
175
|
+
// A replacement returns focus to where the dialogs it replaces would have, not to their now-removed content.
|
|
176
|
+
focus: replace && first ? first.focus : event.focus,
|
|
177
|
+
// A dialog that had already started closing was not replaced: a late close from inside it still closes nothing.
|
|
178
|
+
replaced: replace
|
|
179
|
+
? state.entries.flatMap((item) => (item.status === "open" ? [item.attempt, ...item.replaced] : []))
|
|
180
|
+
: [],
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const mount: DialogStackEffect<F, R> = { type: "mount", attempt, id: event.id, request: event.request }
|
|
184
|
+
|
|
185
|
+
if (!replace) return { state: { ...state, issued: attempt, entries: [...state.entries, entry] }, effects: [mount] }
|
|
186
|
+
|
|
187
|
+
return {
|
|
188
|
+
state: { ...state, issued: attempt, entries: [entry] },
|
|
189
|
+
effects: [...state.entries.flatMap((item) => teardown<F, R>(item, true)), mount],
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function closeTarget<F>(
|
|
194
|
+
state: Extract<DialogStackState<F>, { status: "active" }>,
|
|
195
|
+
event: Extract<
|
|
196
|
+
DialogStackEvent<F>,
|
|
197
|
+
{ type: "close" } | { type: "dismiss" } | { type: "backdrop.click" } | { type: "layer.dismiss" }
|
|
198
|
+
>,
|
|
199
|
+
) {
|
|
200
|
+
const top = selectTopDialog(state)
|
|
201
|
+
|
|
202
|
+
if (event.type === "close") {
|
|
203
|
+
if (event.id !== undefined) {
|
|
204
|
+
return state.entries.findLast(
|
|
205
|
+
(entry): entry is DialogOpenEntry<F> => entry.status === "open" && entry.id === event.id,
|
|
206
|
+
)
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const layer = event.layer
|
|
210
|
+
|
|
211
|
+
const target =
|
|
212
|
+
layer === undefined
|
|
213
|
+
? state.entries.at(-1)
|
|
214
|
+
: state.entries.find((entry) => entry.attempt === layer || entry.replaced.includes(layer))
|
|
215
|
+
|
|
216
|
+
return target?.status === "open" ? target : undefined
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
if (event.type === "dismiss") return event.id === undefined || top?.id === event.id ? top : undefined
|
|
220
|
+
|
|
221
|
+
if (top?.attempt !== event.attempt) return undefined
|
|
222
|
+
|
|
223
|
+
if (event.type === "backdrop.click" && !top.backdropDismiss) return undefined
|
|
224
|
+
|
|
225
|
+
return top
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/** The exit timer starts first, so the dialog still goes away when its onClose throws. */
|
|
229
|
+
function beginClose<F, R>(
|
|
230
|
+
state: Extract<DialogStackState<F>, { status: "active" }>,
|
|
231
|
+
target: DialogOpenEntry<F>,
|
|
232
|
+
): DialogStackTransition<F, R> {
|
|
233
|
+
// Only the top open dialog holds focus. It returns after onClose, so focus that onClose moved elsewhere stays there,
|
|
234
|
+
// and before close() returns, as a native dialog's does, so an action that runs after closing can move it on.
|
|
235
|
+
const top = selectTopDialog(state)?.attempt === target.attempt
|
|
236
|
+
|
|
237
|
+
const closing: DialogClosingEntry<F> = {
|
|
238
|
+
status: "closing",
|
|
239
|
+
attempt: target.attempt,
|
|
240
|
+
id: target.id,
|
|
241
|
+
focus: target.focus,
|
|
242
|
+
replaced: target.replaced,
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
return {
|
|
246
|
+
state: { ...state, entries: state.entries.map((entry) => (entry === target ? closing : entry)) },
|
|
247
|
+
effects: [
|
|
248
|
+
{ type: "timer.start", attempt: target.attempt, delayMs: target.exitMs },
|
|
249
|
+
{ type: "notify.close", attempt: target.attempt },
|
|
250
|
+
...(top ? [{ type: "focus.return" as const, attempt: target.attempt, focus: target.focus }] : []),
|
|
251
|
+
],
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/** A dialog that is already closing heard onClose when it started to; it does not hear it again. */
|
|
256
|
+
function teardown<F, R>(entry: DialogEntry<F>, notify: boolean): DialogStackEffect<F, R>[] {
|
|
257
|
+
if (entry.status === "open") return [{ type: "dispose", attempt: entry.attempt, notify }]
|
|
258
|
+
|
|
259
|
+
return [
|
|
260
|
+
{ type: "timer.stop", attempt: entry.attempt },
|
|
261
|
+
{ type: "dispose", attempt: entry.attempt, notify: false },
|
|
262
|
+
]
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function update<F, R>(
|
|
266
|
+
state: Extract<DialogStackState<F>, { status: "active" }>,
|
|
267
|
+
attempt: number,
|
|
268
|
+
change: (entry: DialogOpenEntry<F>) => DialogOpenEntry<F>,
|
|
269
|
+
): DialogStackTransition<F, R> {
|
|
270
|
+
const entry = state.entries.find(
|
|
271
|
+
(item): item is DialogOpenEntry<F> => item.attempt === attempt && item.status === "open",
|
|
272
|
+
)
|
|
273
|
+
|
|
274
|
+
if (!entry) return { state, effects: [] }
|
|
275
|
+
const next = change(entry)
|
|
276
|
+
|
|
277
|
+
if (next === entry) return { state, effects: [] }
|
|
278
|
+
|
|
279
|
+
return { state: { ...state, entries: state.entries.map((item) => (item === entry ? next : item)) }, effects: [] }
|
|
280
|
+
}
|
package/src/context/dialog.tsx
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import {
|
|
2
|
+
type Accessor,
|
|
2
3
|
createContext,
|
|
3
4
|
createEffect,
|
|
5
|
+
createMemo,
|
|
4
6
|
createRoot,
|
|
5
7
|
createSignal,
|
|
6
8
|
getOwner,
|
|
@@ -8,6 +10,7 @@ import {
|
|
|
8
10
|
type Owner,
|
|
9
11
|
type ParentProps,
|
|
10
12
|
runWithOwner,
|
|
13
|
+
untrack,
|
|
11
14
|
useContext,
|
|
12
15
|
type JSX,
|
|
13
16
|
startTransition,
|
|
@@ -15,23 +18,32 @@ import {
|
|
|
15
18
|
} from "solid-js"
|
|
16
19
|
import { Dialog as Kobalte } from "@kobalte/core/dialog"
|
|
17
20
|
import { makeEventListener } from "@solid-primitives/event-listener"
|
|
21
|
+
import { uuid } from "@opencode/util/uuid"
|
|
22
|
+
import {
|
|
23
|
+
createDialogStackState,
|
|
24
|
+
selectLayerClosing,
|
|
25
|
+
selectLayerIndex,
|
|
26
|
+
selectLayerModal,
|
|
27
|
+
selectTopDialog,
|
|
28
|
+
transitionDialogStack,
|
|
29
|
+
type DialogStackEffect,
|
|
30
|
+
type DialogStackEvent,
|
|
31
|
+
} from "./dialog-machine"
|
|
18
32
|
|
|
19
33
|
type DialogElement = () => JSX.Element
|
|
20
34
|
|
|
21
|
-
type FocusReturn = { element: HTMLElement; visible: boolean; range?: Range }
|
|
35
|
+
export type FocusReturn = { element: HTMLElement; visible: boolean; range?: Range }
|
|
36
|
+
|
|
37
|
+
/** What an open mounts: its content, the owner it renders under, and who hears that it closed. */
|
|
38
|
+
type DialogRequest = { element: DialogElement; owner: Owner; onClose?: () => void }
|
|
22
39
|
|
|
23
|
-
type
|
|
24
|
-
|
|
40
|
+
type Layer = {
|
|
41
|
+
attempt: number
|
|
25
42
|
node: JSX.Element
|
|
26
|
-
dispose: () => void
|
|
27
43
|
owner: Owner
|
|
28
44
|
onClose?: () => void
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
exit: () => number
|
|
32
|
-
/** Where focus was before this dialog opened, or before the dialogs it replaced opened. */
|
|
33
|
-
focus?: FocusReturn
|
|
34
|
-
returnFocus: () => void
|
|
45
|
+
dispose: () => void
|
|
46
|
+
ref: () => HTMLDivElement | undefined
|
|
35
47
|
}
|
|
36
48
|
|
|
37
49
|
const Context = createContext<ReturnType<typeof init>>()
|
|
@@ -39,6 +51,11 @@ const Context = createContext<ReturnType<typeof init>>()
|
|
|
39
51
|
// Lets the dialog rendered in a layer opt out of closing on a backdrop click, or ask for a longer exit animation. A
|
|
40
52
|
// contained layer also says which element holds it, so presses outside that element leave the dialog open.
|
|
41
53
|
const LayerContext = createContext<{
|
|
54
|
+
readonly id: string
|
|
55
|
+
/** This layer's mount, which a `close()` from inside it targets. */
|
|
56
|
+
readonly attempt: number
|
|
57
|
+
/** True from the moment this dialog starts closing, through its exit animation. */
|
|
58
|
+
readonly closing: Accessor<boolean>
|
|
42
59
|
setBackdropDismiss: (value: boolean) => void
|
|
43
60
|
setExitDuration: (ms: number) => void
|
|
44
61
|
container?: HTMLElement
|
|
@@ -49,154 +66,136 @@ export function useDialogLayer() {
|
|
|
49
66
|
}
|
|
50
67
|
|
|
51
68
|
function init(container: () => HTMLElement | undefined) {
|
|
52
|
-
const [
|
|
53
|
-
|
|
54
|
-
const
|
|
55
|
-
//
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
// Detach the stack before disposing, so cleanups that close dialogs find nothing to admit. Every dialog is
|
|
64
|
-
// drained even when one throws. A replaced dialog that was not already closing still hears onClose.
|
|
65
|
-
const disposeAll = (notify: boolean) => {
|
|
66
|
-
const items = stack()
|
|
67
|
-
const exited = new Set(closing.keys())
|
|
68
|
-
setStack([])
|
|
69
|
-
closing.forEach((timer) => clearTimeout(timer))
|
|
70
|
-
closing.clear()
|
|
71
|
-
setExiting(new Set<string>())
|
|
72
|
-
items.forEach((item) => {
|
|
73
|
-
if (notify && !exited.has(item.id)) isolate(() => item.onClose?.())
|
|
74
|
-
isolate(item.dispose)
|
|
75
|
-
})
|
|
69
|
+
const [machine, setMachine] = createSignal(createDialogStackState<FocusReturn>())
|
|
70
|
+
const [layers, setLayers] = createSignal<Layer[]>([])
|
|
71
|
+
const timers = new Map<number, ReturnType<typeof setTimeout>>()
|
|
72
|
+
// Events an effect raises synchronously (a dialog closing as it renders or disposes) wait behind it.
|
|
73
|
+
const queue: DialogStackEvent<FocusReturn, DialogRequest>[] = []
|
|
74
|
+
|
|
75
|
+
const dispatch = (event: DialogStackEvent<FocusReturn, DialogRequest>) => {
|
|
76
|
+
queue.push(event)
|
|
77
|
+
|
|
78
|
+
if (queue.length === 1) untrack(drain)
|
|
76
79
|
}
|
|
77
80
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
disposeAll(false)
|
|
81
|
-
})
|
|
81
|
+
const drain = () => {
|
|
82
|
+
const event = queue[0]
|
|
82
83
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
closing.delete(current.id)
|
|
89
|
-
|
|
90
|
-
if (closing.size === 0) lock.value = false
|
|
91
|
-
setExiting((ids) => new Set([...ids].filter((id) => id !== current.id)))
|
|
92
|
-
setStack((items) => items.filter((item) => item.id !== current.id))
|
|
93
|
-
current.dispose()
|
|
94
|
-
}, current.exit()),
|
|
95
|
-
)
|
|
96
|
-
setExiting((ids) => new Set([...ids, current.id]))
|
|
97
|
-
isolate(() => current.onClose?.())
|
|
98
|
-
current.setClosing(true)
|
|
84
|
+
if (!event) return
|
|
85
|
+
|
|
86
|
+
try {
|
|
87
|
+
const prev = machine()
|
|
88
|
+
const next = transitionDialogStack(prev, event)
|
|
99
89
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
90
|
+
// Committed before the effects, so a closing layer's Kobalte dialog has closed by the time focus returns. A reader
|
|
91
|
+
// of the state or an effect that throws must not stop the queue or skip the other effects.
|
|
92
|
+
if (next.state !== prev) isolate(() => setMachine(next.state))
|
|
93
|
+
next.effects.forEach((effect) => isolate(() => run(effect)))
|
|
94
|
+
} finally {
|
|
95
|
+
queue.shift()
|
|
96
|
+
drain()
|
|
97
|
+
}
|
|
103
98
|
}
|
|
104
99
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
const current = id ? stack().find((item) => item.id === id) : stack().at(-1)
|
|
100
|
+
const run = (effect: DialogStackEffect<FocusReturn, DialogRequest>) => {
|
|
101
|
+
if (effect.type === "mount") return mount(effect)
|
|
108
102
|
|
|
109
|
-
if (
|
|
110
|
-
closing.set(current.id, undefined)
|
|
111
|
-
lock.value = true
|
|
112
|
-
finish(current)
|
|
113
|
-
}
|
|
103
|
+
if (effect.type === "discard") return void (effect.notify && effect.request.onClose?.())
|
|
114
104
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
105
|
+
if (effect.type === "timer.start") {
|
|
106
|
+
clearTimeout(timers.get(effect.attempt))
|
|
107
|
+
timers.set(
|
|
108
|
+
effect.attempt,
|
|
109
|
+
setTimeout(() => dispatch({ type: "exit.completed", attempt: effect.attempt }), effect.delayMs),
|
|
110
|
+
)
|
|
118
111
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
lock.value = true
|
|
122
|
-
finish(current)
|
|
123
|
-
}
|
|
112
|
+
return
|
|
113
|
+
}
|
|
124
114
|
|
|
125
|
-
|
|
126
|
-
|
|
115
|
+
if (effect.type === "timer.stop") {
|
|
116
|
+
clearTimeout(timers.get(effect.attempt))
|
|
117
|
+
timers.delete(effect.attempt)
|
|
118
|
+
|
|
119
|
+
return
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const layer = layers().find((item) => item.attempt === effect.attempt)
|
|
127
123
|
|
|
128
|
-
|
|
129
|
-
if (event.key !== "Escape") return
|
|
124
|
+
if (effect.type === "notify.close") return layer?.onClose?.()
|
|
130
125
|
|
|
131
|
-
|
|
132
|
-
const
|
|
126
|
+
if (effect.type === "focus.return") {
|
|
127
|
+
const ref = layer?.ref()
|
|
133
128
|
|
|
134
|
-
|
|
129
|
+
// A close inside a reactive update unmounts the dialog, and its focus trap, only when the update ends.
|
|
130
|
+
if (ref?.contains(document.activeElement)) return queueMicrotask(() => returnFocus(effect.focus, ref))
|
|
135
131
|
|
|
136
|
-
|
|
137
|
-
if (event.target instanceof Element && event.target.closest("[data-owns-escape]")) return
|
|
138
|
-
dismiss()
|
|
139
|
-
event.preventDefault()
|
|
140
|
-
event.stopPropagation()
|
|
132
|
+
return returnFocus(effect.focus, ref)
|
|
141
133
|
}
|
|
142
134
|
|
|
135
|
+
timers.delete(effect.attempt)
|
|
136
|
+
setLayers((items) => items.filter((item) => item.attempt !== effect.attempt))
|
|
137
|
+
|
|
138
|
+
if (effect.notify) isolate(() => layer?.onClose?.())
|
|
139
|
+
layer?.dispose()
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
onCleanup(() => dispatch({ type: "disposed" }))
|
|
143
|
+
|
|
144
|
+
// Escape is captured only while a dialog is open, not while the last one plays its exit animation.
|
|
145
|
+
const open = createMemo(() => !!selectTopDialog(machine()))
|
|
146
|
+
|
|
147
|
+
const onKeyDown = (event: KeyboardEvent) => {
|
|
148
|
+
if (event.key !== "Escape") return
|
|
149
|
+
|
|
150
|
+
// Escape in the page around a contained stack belongs to the page.
|
|
151
|
+
const within = container()
|
|
152
|
+
|
|
153
|
+
if (within && event.target instanceof Node && !within.contains(event.target)) return
|
|
154
|
+
|
|
155
|
+
// A control that cancels its own edit on Escape, such as an inline rename, keeps the dialog open.
|
|
156
|
+
if (event.target instanceof Element && event.target.closest("[data-owns-escape]")) return
|
|
157
|
+
dispatch({ type: "dismiss" })
|
|
158
|
+
event.preventDefault()
|
|
159
|
+
event.stopPropagation()
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
createEffect(() => {
|
|
163
|
+
if (!open()) return
|
|
164
|
+
|
|
143
165
|
makeEventListener(window, "keydown", onKeyDown, { capture: true })
|
|
144
166
|
})
|
|
145
167
|
|
|
146
|
-
const mount = (
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
onClose: (() => void) | undefined,
|
|
150
|
-
key: string | undefined,
|
|
151
|
-
focus: FocusReturn | undefined,
|
|
152
|
-
) => {
|
|
153
|
-
// A deferred open (e.g. from a focus callback) must not mount after the provider is gone.
|
|
154
|
-
if (state.disposed) return
|
|
155
|
-
const id = key ?? Math.random().toString(36).slice(2)
|
|
156
|
-
|
|
157
|
-
// The layer follows the dialog's current place in the stack, so a new top dialog always renders above.
|
|
158
|
-
const layer = () =>
|
|
159
|
-
Math.max(
|
|
160
|
-
0,
|
|
161
|
-
stack().findIndex((item) => item.id === id),
|
|
162
|
-
)
|
|
163
|
-
|
|
164
|
-
const zIndex = () => String(50 + layer() * 10)
|
|
168
|
+
const mount = (effect: Extract<DialogStackEffect<FocusReturn, DialogRequest>, { type: "mount" }>) => {
|
|
169
|
+
const attempt = effect.attempt
|
|
170
|
+
const zIndex = () => String(50 + selectLayerIndex(machine(), attempt) * 10)
|
|
165
171
|
// A function, not an inline `&&`: the compiler wraps an inline condition in a memo on every read of the prop, and
|
|
166
172
|
// Kobalte reads it outside any owner, from outside-pointer handlers and while the closing dialog unmounts.
|
|
167
|
-
const modal = () =>
|
|
168
|
-
let
|
|
169
|
-
let
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
// Stacked dialogs render as sibling portals, so only the top layer may own the focus trap.
|
|
174
|
-
const node = runWithOwner(owner, () =>
|
|
173
|
+
const modal = () => selectLayerModal(machine(), attempt, !!container())
|
|
174
|
+
let ref: HTMLDivElement | undefined
|
|
175
|
+
let dispose = () => {}
|
|
176
|
+
|
|
177
|
+
const node = runWithOwner(effect.request.owner, () =>
|
|
175
178
|
createRoot((d: () => void) => {
|
|
176
179
|
dispose = d
|
|
177
|
-
const
|
|
178
|
-
const [backdropDismiss, setBackdropDismiss] = createSignal(true)
|
|
179
|
-
setClosing = setClosingSignal
|
|
180
|
+
const closing = createMemo(() => selectLayerClosing(machine(), attempt))
|
|
180
181
|
|
|
181
182
|
return (
|
|
182
183
|
<Kobalte
|
|
183
184
|
modal={modal()}
|
|
184
185
|
open={!closing()}
|
|
185
186
|
onOpenChange={(open: boolean) => {
|
|
186
|
-
if (!open) dismiss
|
|
187
|
+
if (!open) dispatch({ type: "layer.dismiss", attempt })
|
|
187
188
|
}}
|
|
188
189
|
>
|
|
189
190
|
<Kobalte.Portal mount={container()}>
|
|
190
191
|
<Kobalte.Overlay
|
|
191
192
|
data-component="dialog-overlay"
|
|
192
193
|
style={{ "z-index": zIndex() }}
|
|
193
|
-
onClick={() => {
|
|
194
|
-
if (backdropDismiss()) dismiss(id)
|
|
195
|
-
}}
|
|
194
|
+
onClick={() => dispatch({ type: "backdrop.click", attempt })}
|
|
196
195
|
/>
|
|
197
196
|
<div
|
|
198
|
-
ref={
|
|
199
|
-
data-dialog-layer={
|
|
197
|
+
ref={ref}
|
|
198
|
+
data-dialog-layer={selectLayerIndex(machine(), attempt)}
|
|
200
199
|
style={{
|
|
201
200
|
position: "fixed",
|
|
202
201
|
inset: "0",
|
|
@@ -208,9 +207,16 @@ function init(container: () => HTMLElement | undefined) {
|
|
|
208
207
|
}}
|
|
209
208
|
>
|
|
210
209
|
<LayerContext.Provider
|
|
211
|
-
value={{
|
|
210
|
+
value={{
|
|
211
|
+
id: effect.id,
|
|
212
|
+
attempt,
|
|
213
|
+
closing,
|
|
214
|
+
setBackdropDismiss: (enabled) => dispatch({ type: "layer.backdrop-dismiss", attempt, enabled }),
|
|
215
|
+
setExitDuration: (exitMs) => dispatch({ type: "layer.exit-duration", attempt, exitMs }),
|
|
216
|
+
container: container(),
|
|
217
|
+
}}
|
|
212
218
|
>
|
|
213
|
-
{element()}
|
|
219
|
+
{effect.request.element()}
|
|
214
220
|
</LayerContext.Provider>
|
|
215
221
|
</div>
|
|
216
222
|
</Kobalte.Portal>
|
|
@@ -219,56 +225,33 @@ function init(container: () => HTMLElement | undefined) {
|
|
|
219
225
|
}),
|
|
220
226
|
)
|
|
221
227
|
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
exit: () => exit,
|
|
232
|
-
focus,
|
|
233
|
-
returnFocus: () => {
|
|
234
|
-
// A close inside a reactive update unmounts the dialog, and its focus trap, only when the update ends.
|
|
235
|
-
if (layerRef?.contains(document.activeElement)) return queueMicrotask(() => returnFocus(focus, layerRef))
|
|
236
|
-
returnFocus(focus, layerRef)
|
|
228
|
+
setLayers((items) => [
|
|
229
|
+
...items,
|
|
230
|
+
{
|
|
231
|
+
attempt,
|
|
232
|
+
node,
|
|
233
|
+
owner: effect.request.owner,
|
|
234
|
+
onClose: effect.request.onClose,
|
|
235
|
+
dispose,
|
|
236
|
+
ref: () => ref,
|
|
237
237
|
},
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
setStack((items) => [...items, active])
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
const push = (element: DialogElement, owner: Owner, onClose?: () => void, id?: string) => {
|
|
244
|
-
lock.value = false
|
|
245
|
-
mount(element, owner, onClose, id, captureFocus())
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
const show = (element: DialogElement, owner: Owner, onClose?: () => void, id?: string) => {
|
|
249
|
-
// A replacement returns focus to where the dialogs it replaces would have, not to their now-removed content.
|
|
250
|
-
const focus = stack().length > 0 ? stack()[0].focus : captureFocus()
|
|
251
|
-
disposeAll(true)
|
|
252
|
-
lock.value = false
|
|
253
|
-
mount(element, owner, onClose, id, focus)
|
|
238
|
+
])
|
|
254
239
|
}
|
|
255
240
|
|
|
256
241
|
return {
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
push,
|
|
242
|
+
dispatch,
|
|
243
|
+
active: () => selectTopDialog(machine()),
|
|
244
|
+
/** Stacked dialogs render under the owner of the dialog below, so they see the same context. */
|
|
245
|
+
base: () => layers().at(-1)?.owner,
|
|
246
|
+
open: (mode: "show" | "push", request: DialogRequest, id: string | undefined, aborted: boolean) =>
|
|
247
|
+
dispatch({ type: "open", mode, id: id ?? uuid(), aborted, focus: captureFocus(), request }),
|
|
248
|
+
layers,
|
|
262
249
|
}
|
|
263
250
|
}
|
|
264
251
|
|
|
265
252
|
/** Runs a dialog callback so its throw is reported without stopping the caller's cleanup. */
|
|
266
253
|
function isolate(fn: () => void) {
|
|
267
|
-
try
|
|
268
|
-
fn()
|
|
269
|
-
} catch (error) {
|
|
270
|
-
console.error("[dialog]", error)
|
|
271
|
-
}
|
|
254
|
+
void Promise.try(fn).catch((cause: unknown) => console.error("[dialog]", cause))
|
|
272
255
|
}
|
|
273
256
|
|
|
274
257
|
/** Remembers the focused element and, in editable content, the caret, which focusing alone does not restore. */
|
|
@@ -314,7 +297,7 @@ export function DialogProvider(props: ParentProps<{ container?: HTMLElement }>)
|
|
|
314
297
|
<Context.Provider value={ctx}>
|
|
315
298
|
{props.children}
|
|
316
299
|
<div data-component="dialog-stack">
|
|
317
|
-
<For each={ctx.
|
|
300
|
+
<For each={ctx.layers()}>{(item) => item.node}</For>
|
|
318
301
|
</div>
|
|
319
302
|
</Context.Provider>
|
|
320
303
|
)
|
|
@@ -322,6 +305,7 @@ export function DialogProvider(props: ParentProps<{ container?: HTMLElement }>)
|
|
|
322
305
|
|
|
323
306
|
export function useDialog() {
|
|
324
307
|
const ctx = useContext(Context)
|
|
308
|
+
const layer = useContext(LayerContext)
|
|
325
309
|
const owner = getOwner()
|
|
326
310
|
|
|
327
311
|
if (!owner) {
|
|
@@ -335,30 +319,37 @@ export function useDialog() {
|
|
|
335
319
|
return {
|
|
336
320
|
/** The top dialog that is open. One that has started closing no longer counts, though it waits to be disposed. */
|
|
337
321
|
get active() {
|
|
338
|
-
return ctx.
|
|
322
|
+
return ctx.active()
|
|
323
|
+
},
|
|
324
|
+
/** Inside a dialog, true from the moment that dialog starts closing; outside every dialog, false. */
|
|
325
|
+
get closing() {
|
|
326
|
+
return layer?.closing() ?? false
|
|
339
327
|
},
|
|
340
328
|
/**
|
|
341
329
|
* id lets the caller close this dialog later rather than whichever is on top. Opening is deferred; once
|
|
342
330
|
* signal aborts, the open neither mounts nor replaces anything and onClose runs as for a replaced dialog.
|
|
343
331
|
*/
|
|
344
332
|
show(element: DialogElement, onClose?: () => void, id?: string, signal?: AbortSignal) {
|
|
345
|
-
const base = ctx.
|
|
333
|
+
const base = ctx.base() ?? owner
|
|
346
334
|
|
|
347
|
-
return startTransition(() => {
|
|
348
|
-
if (signal?.aborted) return isolate(() => onClose?.())
|
|
349
|
-
ctx.show(element, base, onClose, id)
|
|
350
|
-
})
|
|
335
|
+
return startTransition(() => ctx.open("show", { element, owner: base, onClose }, id, !!signal?.aborted))
|
|
351
336
|
},
|
|
352
337
|
push(element: DialogElement, onClose?: () => void, id?: string, signal?: AbortSignal) {
|
|
353
|
-
const base = ctx.
|
|
338
|
+
const base = ctx.base() ?? owner
|
|
354
339
|
|
|
355
|
-
return startTransition(() => {
|
|
356
|
-
if (signal?.aborted) return isolate(() => onClose?.())
|
|
357
|
-
ctx.push(element, base, onClose, id)
|
|
358
|
-
})
|
|
340
|
+
return startTransition(() => ctx.open("push", { element, owner: base, onClose }, id, !!signal?.aborted))
|
|
359
341
|
},
|
|
342
|
+
/**
|
|
343
|
+
* Programmatic close. With an id it closes that dialog at once. Without one, called inside a dialog, it closes that
|
|
344
|
+
* dialog, or the dialog that replaced it through `show`, and nothing once it is gone; outside every dialog it closes
|
|
345
|
+
* the top one. Either way one at a time, so a double click closes one dialog.
|
|
346
|
+
*/
|
|
360
347
|
close(id?: string) {
|
|
361
|
-
ctx.close
|
|
348
|
+
ctx.dispatch({ type: "close", id, layer: layer?.attempt })
|
|
349
|
+
},
|
|
350
|
+
/** Dismisses the top open dialog, as Escape does; with an id, only while that dialog is on top. */
|
|
351
|
+
dismiss(id?: string) {
|
|
352
|
+
ctx.dispatch({ type: "dismiss", id })
|
|
362
353
|
},
|
|
363
354
|
}
|
|
364
355
|
}
|
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
Show,
|
|
7
7
|
children,
|
|
8
8
|
createEffect,
|
|
9
|
+
onCleanup,
|
|
9
10
|
splitProps,
|
|
10
11
|
} from "solid-js"
|
|
11
12
|
import { useDialogLayer } from "../../context/dialog"
|
|
@@ -113,7 +114,9 @@ export function Dialog(props: DialogProps) {
|
|
|
113
114
|
])
|
|
114
115
|
|
|
115
116
|
const layer = useDialogLayer()
|
|
117
|
+
let focusTimer: ReturnType<typeof setTimeout> | undefined
|
|
116
118
|
createEffect(() => layer?.setBackdropDismiss(!local.preventBackdropDismiss))
|
|
119
|
+
onCleanup(() => clearTimeout(focusTimer))
|
|
117
120
|
|
|
118
121
|
return (
|
|
119
122
|
<div
|
|
@@ -147,7 +150,10 @@ export function Dialog(props: DialogProps) {
|
|
|
147
150
|
// Kobalte attaches its focus trap after this event, so focusing now would leave the trap with no element
|
|
148
151
|
// to return to. Deferring, as Kobalte's own autofocus does, also lets children that focus themselves on
|
|
149
152
|
// mount keep that focus.
|
|
150
|
-
|
|
153
|
+
clearTimeout(focusTimer)
|
|
154
|
+
focusTimer = setTimeout(() => {
|
|
155
|
+
// A dialog that closed in the meantime must not pull focus back from where its close returned it.
|
|
156
|
+
if (layer?.closing() || !content.isConnected) return
|
|
151
157
|
const autofocus = content.querySelector<HTMLElement>("[autofocus]")
|
|
152
158
|
const active = document.activeElement
|
|
153
159
|
|