@opencode/ui 2.2.4 → 2.2.6
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/actions/button/button.d.ts +2 -2
- package/dist/actions/icon-button/icon-button.d.ts +1 -1
- package/dist/components/animated-number-machine.d.ts +95 -0
- package/dist/components/file-icon-machine.d.ts +40 -0
- package/dist/components/file-icon.d.ts +0 -2
- package/dist/components/file-icons/choose-icon-name.d.ts +2 -0
- package/dist/components/image-flight-machine.d.ts +135 -0
- package/dist/components/image-zoom-machine.d.ts +95 -0
- package/dist/components/image-zoom.d.ts +6 -3
- package/dist/components/list.d.ts +2 -6
- package/dist/components/menu-scroll.d.ts +12 -0
- package/dist/components/popover-machine.d.ts +96 -0
- package/dist/components/popover.d.ts +2 -2
- package/dist/components/resize-handle-machine.d.ts +71 -0
- package/dist/components/resize-handle.d.ts +2 -0
- package/dist/components/scroll-view-machine.d.ts +68 -0
- package/dist/components/scroll-view.d.ts +2 -2
- package/dist/components/spinner.d.ts +11 -3
- package/dist/components/text-field-machine.d.ts +62 -0
- package/dist/components/text-reveal-machine.d.ts +90 -0
- package/dist/components/typewriter-machine.d.ts +62 -0
- package/dist/context/dialog-machine.d.ts +125 -0
- package/dist/context/dialog.d.ts +17 -16
- package/dist/data-display/line-comment/line-comment.d.ts +9 -0
- package/dist/data-display/line-comment/machine.d.ts +157 -0
- package/dist/data-display/project-avatar/animation-machine.d.ts +62 -0
- package/dist/data-display/project-avatar/animation.d.ts +1 -1
- package/dist/feedback/activity-ring/activity-ring.d.ts +2 -1
- package/dist/feedback/toast/machine.d.ts +57 -0
- package/dist/feedback/toast/toast.d.ts +1 -1
- package/dist/forms/checkbox/checkbox.d.ts +6 -1
- package/dist/forms/select/machine.d.ts +45 -0
- package/dist/forms/select/select.d.ts +4 -0
- package/dist/forms/text-input/text-input.d.ts +4 -2
- package/dist/forms/textarea/textarea.d.ts +1 -0
- package/dist/forms/typeahead/machine.d.ts +85 -0
- package/dist/hooks/auto-scroll-machine.d.ts +89 -0
- package/dist/hooks/create-auto-scroll.d.ts +1 -1
- package/dist/hooks/list-machine.d.ts +150 -0
- package/dist/hooks/use-filtered-list.d.ts +21 -35
- package/dist/i18n/en.d.ts +5 -0
- package/dist/icons/catalog.d.ts +8 -0
- package/dist/navigation/menu/machine.d.ts +49 -0
- package/dist/navigation/menu/menu.d.ts +5 -3
- package/dist/navigation/segmented-control/machine.d.ts +46 -0
- package/dist/overlays/mcp-sign-in/machine.d.ts +184 -0
- package/dist/overlays/mcp-sign-in/mcp-sign-in.d.ts +40 -0
- package/dist/overlays/tooltip/machine.d.ts +146 -0
- package/dist/theme/context.d.ts +38 -14
- package/dist/theme/machine.d.ts +174 -0
- package/dist/typography/text-shimmer/machine.d.ts +48 -0
- package/package.json +19 -3
- package/src/actions/button/button.tsx +11 -6
- package/src/actions/icon-button/icon-button.tsx +17 -4
- package/src/components/animated-number-machine.ts +214 -0
- package/src/components/animated-number.tsx +134 -83
- package/src/components/file-icon-machine.ts +78 -0
- package/src/components/file-icon.tsx +96 -579
- package/src/components/file-icons/choose-icon-name.ts +574 -0
- package/src/components/image-flight-machine.ts +247 -0
- package/src/components/image-preview.css +5 -2
- package/src/components/image-preview.tsx +390 -252
- package/src/components/image-zoom-machine.ts +191 -0
- package/src/components/image-zoom.ts +22 -14
- package/src/components/list.tsx +46 -137
- package/src/components/menu-scroll.css +70 -0
- package/src/components/menu-scroll.tsx +161 -0
- package/src/components/popover-machine.ts +198 -0
- package/src/components/popover.tsx +157 -89
- package/src/components/resize-handle-machine.ts +174 -0
- package/src/components/resize-handle.tsx +116 -67
- package/src/components/scroll-view-machine.ts +244 -0
- package/src/components/scroll-view.tsx +163 -116
- package/src/components/spinner.css +18 -5
- package/src/components/spinner.tsx +28 -14
- package/src/components/text-field-machine.ts +102 -0
- package/src/components/text-field.tsx +78 -24
- package/src/components/text-reveal-machine.ts +184 -0
- package/src/components/text-reveal.tsx +96 -93
- package/src/components/typewriter-machine.ts +134 -0
- package/src/components/typewriter.tsx +50 -39
- package/src/context/dialog-machine.ts +280 -0
- package/src/context/dialog.tsx +168 -177
- package/src/data-display/line-comment/line-comment.tsx +196 -132
- package/src/data-display/line-comment/machine.ts +391 -0
- package/src/data-display/project-avatar/animation-machine.ts +182 -0
- package/src/data-display/project-avatar/animation.ts +67 -38
- package/src/feedback/activity-ring/activity-ring.tsx +1 -1
- package/src/feedback/toast/machine.ts +115 -0
- package/src/feedback/toast/toast.tsx +55 -59
- package/src/forms/checkbox/checkbox-compact.css +169 -0
- package/src/forms/checkbox/checkbox.tsx +93 -31
- package/src/forms/search-list/search-list.css +5 -1
- package/src/forms/search-list/search-list.tsx +4 -4
- package/src/forms/select/machine.ts +81 -0
- package/src/forms/select/select.css +59 -17
- package/src/forms/select/select.tsx +67 -31
- package/src/forms/text-input/text-input.css +30 -16
- package/src/forms/text-input/text-input.tsx +6 -2
- package/src/forms/textarea/textarea.css +13 -8
- package/src/forms/textarea/textarea.tsx +3 -1
- package/src/forms/typeahead/machine.ts +253 -0
- package/src/forms/typeahead/typeahead.tsx +75 -66
- package/src/hooks/auto-scroll-machine.ts +261 -0
- package/src/hooks/create-auto-scroll.tsx +110 -205
- package/src/hooks/list-machine.ts +359 -0
- package/src/hooks/use-filtered-list.tsx +101 -112
- package/src/i18n/en.ts +6 -1
- package/src/icons/catalog.ts +8 -0
- package/src/navigation/menu/machine.ts +109 -0
- package/src/navigation/menu/menu.tsx +130 -22
- package/src/navigation/segmented-control/machine.ts +72 -0
- package/src/navigation/segmented-control/segmented-control.tsx +46 -46
- package/src/overlays/dialog/dialog.tsx +7 -1
- package/src/overlays/mcp-sign-in/machine.ts +343 -0
- package/src/overlays/mcp-sign-in/mcp-sign-in.ts +159 -0
- package/src/overlays/tooltip/machine.ts +303 -0
- package/src/overlays/tooltip/tooltip.tsx +157 -92
- package/src/styles/theme.css +10 -2
- package/src/styles/tokens/theme.css +13 -0
- package/src/theme/context.tsx +192 -269
- package/src/theme/machine.ts +508 -0
- package/src/theme/themes/oc-2.json +2 -0
- package/src/theme/v2/mapping.ts +12 -8
- package/src/typography/text-shimmer/machine.ts +90 -0
- package/src/typography/text-shimmer/text-shimmer.css +71 -39
- package/src/typography/text-shimmer/text-shimmer.tsx +41 -24
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
|
}
|