@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.
Files changed (127) hide show
  1. package/dist/actions/button/button.d.ts +2 -2
  2. package/dist/actions/icon-button/icon-button.d.ts +1 -1
  3. package/dist/components/animated-number-machine.d.ts +95 -0
  4. package/dist/components/file-icon-machine.d.ts +40 -0
  5. package/dist/components/file-icon.d.ts +0 -2
  6. package/dist/components/file-icons/choose-icon-name.d.ts +2 -0
  7. package/dist/components/image-flight-machine.d.ts +135 -0
  8. package/dist/components/image-zoom-machine.d.ts +95 -0
  9. package/dist/components/image-zoom.d.ts +6 -3
  10. package/dist/components/list.d.ts +2 -6
  11. package/dist/components/menu-scroll.d.ts +12 -0
  12. package/dist/components/popover-machine.d.ts +96 -0
  13. package/dist/components/popover.d.ts +2 -2
  14. package/dist/components/resize-handle-machine.d.ts +71 -0
  15. package/dist/components/resize-handle.d.ts +2 -0
  16. package/dist/components/scroll-view-machine.d.ts +68 -0
  17. package/dist/components/scroll-view.d.ts +2 -2
  18. package/dist/components/spinner.d.ts +11 -3
  19. package/dist/components/text-field-machine.d.ts +62 -0
  20. package/dist/components/text-reveal-machine.d.ts +90 -0
  21. package/dist/components/typewriter-machine.d.ts +62 -0
  22. package/dist/context/dialog-machine.d.ts +125 -0
  23. package/dist/context/dialog.d.ts +17 -16
  24. package/dist/data-display/line-comment/line-comment.d.ts +9 -0
  25. package/dist/data-display/line-comment/machine.d.ts +157 -0
  26. package/dist/data-display/project-avatar/animation-machine.d.ts +62 -0
  27. package/dist/data-display/project-avatar/animation.d.ts +1 -1
  28. package/dist/feedback/activity-ring/activity-ring.d.ts +2 -1
  29. package/dist/feedback/toast/machine.d.ts +57 -0
  30. package/dist/feedback/toast/toast.d.ts +1 -1
  31. package/dist/forms/checkbox/checkbox.d.ts +6 -1
  32. package/dist/forms/select/machine.d.ts +45 -0
  33. package/dist/forms/select/select.d.ts +4 -0
  34. package/dist/forms/text-input/text-input.d.ts +4 -2
  35. package/dist/forms/textarea/textarea.d.ts +1 -0
  36. package/dist/forms/typeahead/machine.d.ts +85 -0
  37. package/dist/hooks/auto-scroll-machine.d.ts +89 -0
  38. package/dist/hooks/create-auto-scroll.d.ts +1 -1
  39. package/dist/hooks/list-machine.d.ts +150 -0
  40. package/dist/hooks/use-filtered-list.d.ts +21 -35
  41. package/dist/i18n/en.d.ts +5 -0
  42. package/dist/icons/catalog.d.ts +8 -0
  43. package/dist/navigation/menu/machine.d.ts +49 -0
  44. package/dist/navigation/menu/menu.d.ts +5 -3
  45. package/dist/navigation/segmented-control/machine.d.ts +46 -0
  46. package/dist/overlays/mcp-sign-in/machine.d.ts +184 -0
  47. package/dist/overlays/mcp-sign-in/mcp-sign-in.d.ts +40 -0
  48. package/dist/overlays/tooltip/machine.d.ts +146 -0
  49. package/dist/theme/context.d.ts +38 -14
  50. package/dist/theme/machine.d.ts +174 -0
  51. package/dist/typography/text-shimmer/machine.d.ts +48 -0
  52. package/package.json +19 -3
  53. package/src/actions/button/button.tsx +11 -6
  54. package/src/actions/icon-button/icon-button.tsx +17 -4
  55. package/src/components/animated-number-machine.ts +214 -0
  56. package/src/components/animated-number.tsx +134 -83
  57. package/src/components/file-icon-machine.ts +78 -0
  58. package/src/components/file-icon.tsx +96 -579
  59. package/src/components/file-icons/choose-icon-name.ts +574 -0
  60. package/src/components/image-flight-machine.ts +247 -0
  61. package/src/components/image-preview.css +5 -2
  62. package/src/components/image-preview.tsx +390 -252
  63. package/src/components/image-zoom-machine.ts +191 -0
  64. package/src/components/image-zoom.ts +22 -14
  65. package/src/components/list.tsx +46 -137
  66. package/src/components/menu-scroll.css +70 -0
  67. package/src/components/menu-scroll.tsx +161 -0
  68. package/src/components/popover-machine.ts +198 -0
  69. package/src/components/popover.tsx +157 -89
  70. package/src/components/resize-handle-machine.ts +174 -0
  71. package/src/components/resize-handle.tsx +116 -67
  72. package/src/components/scroll-view-machine.ts +244 -0
  73. package/src/components/scroll-view.tsx +163 -116
  74. package/src/components/spinner.css +18 -5
  75. package/src/components/spinner.tsx +28 -14
  76. package/src/components/text-field-machine.ts +102 -0
  77. package/src/components/text-field.tsx +78 -24
  78. package/src/components/text-reveal-machine.ts +184 -0
  79. package/src/components/text-reveal.tsx +96 -93
  80. package/src/components/typewriter-machine.ts +134 -0
  81. package/src/components/typewriter.tsx +50 -39
  82. package/src/context/dialog-machine.ts +280 -0
  83. package/src/context/dialog.tsx +168 -177
  84. package/src/data-display/line-comment/line-comment.tsx +196 -132
  85. package/src/data-display/line-comment/machine.ts +391 -0
  86. package/src/data-display/project-avatar/animation-machine.ts +182 -0
  87. package/src/data-display/project-avatar/animation.ts +67 -38
  88. package/src/feedback/activity-ring/activity-ring.tsx +1 -1
  89. package/src/feedback/toast/machine.ts +115 -0
  90. package/src/feedback/toast/toast.tsx +55 -59
  91. package/src/forms/checkbox/checkbox-compact.css +169 -0
  92. package/src/forms/checkbox/checkbox.tsx +93 -31
  93. package/src/forms/search-list/search-list.css +5 -1
  94. package/src/forms/search-list/search-list.tsx +4 -4
  95. package/src/forms/select/machine.ts +81 -0
  96. package/src/forms/select/select.css +59 -17
  97. package/src/forms/select/select.tsx +67 -31
  98. package/src/forms/text-input/text-input.css +30 -16
  99. package/src/forms/text-input/text-input.tsx +6 -2
  100. package/src/forms/textarea/textarea.css +13 -8
  101. package/src/forms/textarea/textarea.tsx +3 -1
  102. package/src/forms/typeahead/machine.ts +253 -0
  103. package/src/forms/typeahead/typeahead.tsx +75 -66
  104. package/src/hooks/auto-scroll-machine.ts +261 -0
  105. package/src/hooks/create-auto-scroll.tsx +110 -205
  106. package/src/hooks/list-machine.ts +359 -0
  107. package/src/hooks/use-filtered-list.tsx +101 -112
  108. package/src/i18n/en.ts +6 -1
  109. package/src/icons/catalog.ts +8 -0
  110. package/src/navigation/menu/machine.ts +109 -0
  111. package/src/navigation/menu/menu.tsx +130 -22
  112. package/src/navigation/segmented-control/machine.ts +72 -0
  113. package/src/navigation/segmented-control/segmented-control.tsx +46 -46
  114. package/src/overlays/dialog/dialog.tsx +7 -1
  115. package/src/overlays/mcp-sign-in/machine.ts +343 -0
  116. package/src/overlays/mcp-sign-in/mcp-sign-in.ts +159 -0
  117. package/src/overlays/tooltip/machine.ts +303 -0
  118. package/src/overlays/tooltip/tooltip.tsx +157 -92
  119. package/src/styles/theme.css +10 -2
  120. package/src/styles/tokens/theme.css +13 -0
  121. package/src/theme/context.tsx +192 -269
  122. package/src/theme/machine.ts +508 -0
  123. package/src/theme/themes/oc-2.json +2 -0
  124. package/src/theme/v2/mapping.ts +12 -8
  125. package/src/typography/text-shimmer/machine.ts +90 -0
  126. package/src/typography/text-shimmer/text-shimmer.css +71 -39
  127. package/src/typography/text-shimmer/text-shimmer.tsx +41 -24
@@ -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 Active = {
24
- id: string
40
+ type Layer = {
41
+ attempt: number
25
42
  node: JSX.Element
26
- dispose: () => void
27
43
  owner: Owner
28
44
  onClose?: () => void
29
- setClosing: (closing: boolean) => void
30
- /** How long the dialog stays mounted once it starts closing, so its exit animation can finish. */
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 [stack, setStack] = createSignal<Active[]>([])
53
- // A dialog is closing from the moment its close starts until its exit animation ends and it is disposed.
54
- const closing = new Map<string, ReturnType<typeof setTimeout> | undefined>()
55
- // The same ids, reactive, so the top dialog that stays open owns the focus trap during an exit animation.
56
- const [exiting, setExiting] = createSignal<ReadonlySet<string>>(new Set())
57
- // The top dialog that stays open. A dialog without an exit animation leaves the DOM as soon as it starts closing, so
58
- // it stops being the active dialog then, not when it is disposed.
59
- const top = () => stack().findLast((item) => !exiting().has(item.id))
60
- const lock = { value: false }
61
- const state = { disposed: false }
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
- onCleanup(() => {
79
- state.disposed = true
80
- disposeAll(false)
81
- })
81
+ const drain = () => {
82
+ const event = queue[0]
82
83
 
83
- const finish = (current: Active) => {
84
- // Scheduled first, so the dialog still goes away when a callback throws.
85
- closing.set(
86
- current.id,
87
- setTimeout(() => {
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
- // Only the top open dialog holds focus. It returns before close() does, as a native dialog's does, so an
101
- // action that runs after closing, such as a palette command that opens the terminal, can move it on.
102
- if (stack().findLast((item) => item === current || !closing.has(item.id)) === current) current.returnFocus()
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
- /** Programmatic close. Without an id it closes the top dialog, one at a time; with an id it never waits. */
106
- const close = (id?: string) => {
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 (!current || closing.has(current.id) || (!id && lock.value)) return
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
- /** Escape, a backdrop click, or Kobalte dismissing: only the top dialog, and one per exit animation. */
116
- const dismiss = (id?: string) => {
117
- const current = stack().at(-1)
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
- if (!current || (id && current.id !== id) || closing.has(current.id) || lock.value) return
120
- closing.set(current.id, undefined)
121
- lock.value = true
122
- finish(current)
123
- }
112
+ return
113
+ }
124
114
 
125
- createEffect(() => {
126
- if (stack().length === 0) return
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
- const onKeyDown = (event: KeyboardEvent) => {
129
- if (event.key !== "Escape") return
124
+ if (effect.type === "notify.close") return layer?.onClose?.()
130
125
 
131
- // Escape in the page around a contained stack belongs to the page.
132
- const within = container()
126
+ if (effect.type === "focus.return") {
127
+ const ref = layer?.ref()
133
128
 
134
- if (within && event.target instanceof Node && !within.contains(event.target)) return
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
- // A control that cancels its own edit on Escape, such as an inline rename, keeps the dialog open.
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
- element: DialogElement,
148
- owner: Owner,
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 = () => !container() && top()?.id === id
168
- let dispose: (() => void) | undefined
169
- let setClosing: ((closing: boolean) => void) | undefined
170
- let layerRef: HTMLDivElement | undefined
171
- let exit = 100
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 [closing, setClosingSignal] = createSignal(false)
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(id)
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={layerRef}
199
- data-dialog-layer={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={{ setBackdropDismiss, setExitDuration: (ms) => (exit = ms), container: container() }}
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
- if (!dispose || !setClosing) return
223
-
224
- const active: Active = {
225
- id,
226
- node,
227
- dispose,
228
- owner,
229
- onClose,
230
- setClosing,
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
- stack,
258
- top,
259
- close,
260
- show,
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.stack()}>{(item) => item.node}</For>
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.top()
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.stack().at(-1)?.owner ?? owner
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.stack().at(-1)?.owner ?? owner
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(id)
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
  }