@opencode/ui 2.2.5 → 2.2.7

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 (119) 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 +9 -3
  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/machine.d.ts +157 -0
  25. package/dist/data-display/project-avatar/animation-machine.d.ts +62 -0
  26. package/dist/data-display/project-avatar/animation.d.ts +1 -1
  27. package/dist/feedback/activity-ring/activity-ring.d.ts +2 -1
  28. package/dist/feedback/toast/machine.d.ts +57 -0
  29. package/dist/feedback/toast/toast.d.ts +1 -1
  30. package/dist/forms/checkbox/checkbox.d.ts +6 -1
  31. package/dist/forms/select/machine.d.ts +45 -0
  32. package/dist/forms/select/select.d.ts +4 -0
  33. package/dist/forms/typeahead/machine.d.ts +85 -0
  34. package/dist/hooks/auto-scroll-machine.d.ts +89 -0
  35. package/dist/hooks/create-auto-scroll.d.ts +1 -1
  36. package/dist/hooks/list-machine.d.ts +150 -0
  37. package/dist/hooks/use-filtered-list.d.ts +21 -35
  38. package/dist/i18n/en.d.ts +5 -0
  39. package/dist/icons/catalog.d.ts +8 -0
  40. package/dist/navigation/menu/machine.d.ts +49 -0
  41. package/dist/navigation/menu/menu.d.ts +5 -3
  42. package/dist/navigation/segmented-control/machine.d.ts +46 -0
  43. package/dist/overlays/mcp-sign-in/machine.d.ts +184 -0
  44. package/dist/overlays/mcp-sign-in/mcp-sign-in.d.ts +40 -0
  45. package/dist/overlays/tooltip/machine.d.ts +146 -0
  46. package/dist/theme/context.d.ts +38 -14
  47. package/dist/theme/machine.d.ts +174 -0
  48. package/dist/typography/text-shimmer/machine.d.ts +48 -0
  49. package/package.json +128 -111
  50. package/src/actions/button/button.tsx +11 -6
  51. package/src/actions/icon-button/icon-button.tsx +17 -4
  52. package/src/components/animated-number-machine.ts +214 -0
  53. package/src/components/animated-number.tsx +134 -83
  54. package/src/components/file-icon-machine.ts +78 -0
  55. package/src/components/file-icon.tsx +96 -579
  56. package/src/components/file-icons/choose-icon-name.ts +574 -0
  57. package/src/components/image-flight-machine.ts +247 -0
  58. package/src/components/image-preview.css +5 -2
  59. package/src/components/image-preview.tsx +390 -252
  60. package/src/components/image-zoom-machine.ts +191 -0
  61. package/src/components/image-zoom.ts +22 -14
  62. package/src/components/list.tsx +46 -137
  63. package/src/components/menu-scroll.css +19 -8
  64. package/src/components/menu-scroll.tsx +111 -34
  65. package/src/components/popover-machine.ts +198 -0
  66. package/src/components/popover.tsx +157 -89
  67. package/src/components/resize-handle-machine.ts +174 -0
  68. package/src/components/resize-handle.tsx +116 -67
  69. package/src/components/scroll-view-machine.ts +244 -0
  70. package/src/components/scroll-view.tsx +163 -116
  71. package/src/components/spinner.css +18 -5
  72. package/src/components/spinner.tsx +28 -14
  73. package/src/components/text-field-machine.ts +102 -0
  74. package/src/components/text-field.tsx +78 -24
  75. package/src/components/text-reveal-machine.ts +184 -0
  76. package/src/components/text-reveal.tsx +96 -93
  77. package/src/components/typewriter-machine.ts +134 -0
  78. package/src/components/typewriter.tsx +50 -39
  79. package/src/context/dialog-machine.ts +280 -0
  80. package/src/context/dialog.tsx +168 -177
  81. package/src/data-display/line-comment/line-comment.tsx +157 -178
  82. package/src/data-display/line-comment/machine.ts +391 -0
  83. package/src/data-display/project-avatar/animation-machine.ts +182 -0
  84. package/src/data-display/project-avatar/animation.ts +67 -38
  85. package/src/feedback/activity-ring/activity-ring.tsx +1 -1
  86. package/src/feedback/toast/machine.ts +115 -0
  87. package/src/feedback/toast/toast.tsx +59 -59
  88. package/src/forms/checkbox/checkbox-compact.css +169 -0
  89. package/src/forms/checkbox/checkbox.tsx +93 -31
  90. package/src/forms/inline-input/inline-input.css +0 -1
  91. package/src/forms/search-list/search-list.css +5 -1
  92. package/src/forms/search-list/search-list.tsx +18 -10
  93. package/src/forms/select/machine.ts +81 -0
  94. package/src/forms/select/select.css +59 -17
  95. package/src/forms/select/select.tsx +74 -32
  96. package/src/forms/typeahead/machine.ts +253 -0
  97. package/src/forms/typeahead/typeahead.tsx +70 -62
  98. package/src/hooks/auto-scroll-machine.ts +261 -0
  99. package/src/hooks/create-auto-scroll.tsx +110 -205
  100. package/src/hooks/list-machine.ts +359 -0
  101. package/src/hooks/use-filtered-list.tsx +101 -112
  102. package/src/i18n/en.ts +6 -1
  103. package/src/i18n/uk.ts +3 -3
  104. package/src/icons/catalog.ts +8 -0
  105. package/src/navigation/menu/machine.ts +109 -0
  106. package/src/navigation/menu/menu.tsx +133 -22
  107. package/src/navigation/segmented-control/machine.ts +72 -0
  108. package/src/navigation/segmented-control/segmented-control.tsx +46 -46
  109. package/src/overlays/dialog/dialog.tsx +7 -1
  110. package/src/overlays/mcp-sign-in/machine.ts +343 -0
  111. package/src/overlays/mcp-sign-in/mcp-sign-in.ts +159 -0
  112. package/src/overlays/tooltip/machine.ts +303 -0
  113. package/src/overlays/tooltip/tooltip.tsx +159 -92
  114. package/src/styles/theme.css +10 -2
  115. package/src/theme/context.tsx +192 -269
  116. package/src/theme/machine.ts +508 -0
  117. package/src/typography/text-shimmer/machine.ts +90 -0
  118. package/src/typography/text-shimmer/text-shimmer.css +71 -39
  119. package/src/typography/text-shimmer/text-shimmer.tsx +41 -24
@@ -0,0 +1,134 @@
1
+ export const TYPEWRITER_INITIAL_DELAY_MS = 200
2
+
3
+ export const TYPEWRITER_LINGER_MS = 2_000
4
+
5
+ export type TypewriterCursor = "none" | "solid" | "blinking"
6
+
7
+ /**
8
+ * `typing` shows the first `index` characters behind a solid cursor; `index` 0 is the pause before the first one.
9
+ * `lingering` shows the whole text behind a blinking cursor, then `done` hides the cursor.
10
+ */
11
+ export type TypewriterState =
12
+ | { readonly status: "empty"; readonly attempts: number }
13
+ | {
14
+ readonly status: "typing"
15
+ readonly attempt: number
16
+ readonly text: string
17
+ /** Code points, so a step never shows half of a surrogate pair. */
18
+ readonly chars: readonly string[]
19
+ readonly index: number
20
+ }
21
+ | { readonly status: "lingering"; readonly attempt: number; readonly text: string }
22
+ | { readonly status: "done"; readonly attempts: number; readonly text: string }
23
+ | { readonly status: "disposed" }
24
+
25
+ export type TypewriterEvent =
26
+ | { readonly type: "sync"; readonly text: string | undefined; readonly reducedMotion: boolean }
27
+ | { readonly type: "typing.step"; readonly attempt: number }
28
+ | { readonly type: "linger.ended"; readonly attempt: number }
29
+ | { readonly type: "disposed" }
30
+
31
+ export type TypewriterEffect =
32
+ | { readonly type: "none" }
33
+ | { readonly type: "clear" }
34
+ | { readonly type: "schedule"; readonly kind: "initial" | "step" | "linger"; readonly attempt: number }
35
+
36
+ export type TypewriterTransition = {
37
+ readonly state: TypewriterState
38
+ readonly effect: TypewriterEffect
39
+ }
40
+
41
+ export const INITIAL_EMPTY_TYPEWRITER_STATE: TypewriterState = Object.freeze({ status: "empty", attempts: 0 })
42
+
43
+ export const DISPOSED_TYPEWRITER_STATE: TypewriterState = Object.freeze({ status: "disposed" })
44
+
45
+ export const DISPOSED_TYPEWRITER_EVENT: TypewriterEvent = Object.freeze({ type: "disposed" })
46
+
47
+ const NONE: TypewriterEffect = Object.freeze({ type: "none" })
48
+
49
+ const CLEAR: TypewriterEffect = Object.freeze({ type: "clear" })
50
+
51
+ /** Mostly quick keystrokes, with an occasional hesitation. `roll` and `jitter` are uniform in [0, 1). */
52
+ export function typingStepDelayMs(roll: number, jitter: number) {
53
+ if (roll < 0.05) return 150 + jitter * 100
54
+
55
+ if (roll < 0.15) return 80 + jitter * 60
56
+
57
+ return 30 + jitter * 50
58
+ }
59
+
60
+ export function transitionTypewriter(state: TypewriterState, event: TypewriterEvent): TypewriterTransition {
61
+ if (state.status === "disposed") return { state, effect: NONE }
62
+ const timed = state.status === "typing" || state.status === "lingering"
63
+
64
+ if (event.type === "disposed") return { state: DISPOSED_TYPEWRITER_STATE, effect: timed ? CLEAR : NONE }
65
+
66
+ if (event.type === "sync") return sync(state, event)
67
+
68
+ if (event.type === "typing.step") {
69
+ if (state.status !== "typing" || event.attempt !== state.attempt) return { state, effect: NONE }
70
+
71
+ if (state.index + 1 < state.chars.length)
72
+ return {
73
+ state: { ...state, index: state.index + 1 },
74
+ effect: { type: "schedule", kind: "step", attempt: state.attempt },
75
+ }
76
+
77
+ return {
78
+ state: { status: "lingering", attempt: state.attempt, text: state.text },
79
+ effect: { type: "schedule", kind: "linger", attempt: state.attempt },
80
+ }
81
+ }
82
+
83
+ if (state.status !== "lingering" || event.attempt !== state.attempt) return { state, effect: NONE }
84
+
85
+ return { state: { status: "done", attempts: state.attempt, text: state.text }, effect: NONE }
86
+ }
87
+
88
+ function sync(
89
+ state: Exclude<TypewriterState, { status: "disposed" }>,
90
+ event: Extract<TypewriterEvent, { type: "sync" }>,
91
+ ): TypewriterTransition {
92
+ const timed = state.status === "typing" || state.status === "lingering"
93
+ const attempts = timed ? state.attempt : state.attempts
94
+ const text = event.text ?? ""
95
+
96
+ if (text === "") {
97
+ if (state.status === "empty") return { state, effect: NONE }
98
+
99
+ return {
100
+ state: attempts === 0 ? INITIAL_EMPTY_TYPEWRITER_STATE : { status: "empty", attempts },
101
+ effect: timed ? CLEAR : NONE,
102
+ }
103
+ }
104
+
105
+ // Reduced motion shows the whole text at once, without a cursor.
106
+ if (event.reducedMotion) {
107
+ if (state.status === "done" && state.text === text) return { state, effect: NONE }
108
+
109
+ return { state: { status: "done", attempts, text }, effect: timed ? CLEAR : NONE }
110
+ }
111
+
112
+ if (state.status !== "empty" && state.text === text) return { state, effect: NONE }
113
+
114
+ return {
115
+ state: { status: "typing", attempt: attempts + 1, text, chars: Array.from(text), index: 0 },
116
+ effect: { type: "schedule", kind: "initial", attempt: attempts + 1 },
117
+ }
118
+ }
119
+
120
+ export function selectTypewriterDisplayed(state: TypewriterState) {
121
+ if (state.status === "empty" || state.status === "disposed") return ""
122
+
123
+ if (state.status === "typing") return state.chars.slice(0, state.index).join("")
124
+
125
+ return state.text
126
+ }
127
+
128
+ export function selectTypewriterCursor(state: TypewriterState): TypewriterCursor {
129
+ if (state.status === "typing") return "solid"
130
+
131
+ if (state.status === "lingering") return "blinking"
132
+
133
+ return "none"
134
+ }
@@ -1,58 +1,69 @@
1
- import { createEffect, onCleanup, Show, type ValidComponent } from "solid-js"
2
- import { createStore } from "solid-js/store"
1
+ import { createEffect, createMemo, createSignal, onCleanup, Show, untrack, type ValidComponent } from "solid-js"
3
2
  import { Dynamic } from "solid-js/web"
3
+ import { observeReducedMotion, prefersReducedMotion } from "../motion/reduced"
4
+ import {
5
+ DISPOSED_TYPEWRITER_EVENT,
6
+ INITIAL_EMPTY_TYPEWRITER_STATE,
7
+ selectTypewriterCursor,
8
+ selectTypewriterDisplayed,
9
+ transitionTypewriter,
10
+ TYPEWRITER_INITIAL_DELAY_MS,
11
+ TYPEWRITER_LINGER_MS,
12
+ typingStepDelayMs,
13
+ type TypewriterEvent,
14
+ } from "./typewriter-machine"
4
15
 
5
16
  export const Typewriter = <T extends ValidComponent = "p">(props: { text?: string; class?: string; as?: T }) => {
6
- const [store, setStore] = createStore({
7
- typing: false,
8
- displayed: "",
9
- cursor: true,
10
- })
17
+ const [state, setState] = createSignal(INITIAL_EMPTY_TYPEWRITER_STATE)
18
+ let timer: ReturnType<typeof setTimeout> | undefined
11
19
 
12
- createEffect(() => {
13
- const text = props.text
20
+ const dispatch = (event: TypewriterEvent) => {
21
+ const previous = untrack(state)
22
+ const next = transitionTypewriter(previous, event)
14
23
 
15
- if (!text) return
24
+ if (next.effect.type !== "none") {
25
+ clearTimeout(timer)
26
+ timer = undefined
27
+ }
16
28
 
17
- let i = 0
18
- const timeouts: ReturnType<typeof setTimeout>[] = []
19
- setStore("typing", true)
20
- setStore("displayed", "")
21
- setStore("cursor", true)
29
+ if (next.state !== previous && next.state.status !== "disposed") setState(next.state)
22
30
 
23
- const getTypingDelay = () => {
24
- const random = Math.random()
31
+ if (next.effect.type !== "schedule") return
32
+ const effect = next.effect
25
33
 
26
- if (random < 0.05) return 150 + Math.random() * 100
34
+ const timeout: TypewriterEvent =
35
+ effect.kind === "linger"
36
+ ? { type: "linger.ended", attempt: effect.attempt }
37
+ : { type: "typing.step", attempt: effect.attempt }
27
38
 
28
- if (random < 0.15) return 80 + Math.random() * 60
39
+ timer = setTimeout(
40
+ () => {
41
+ timer = undefined
42
+ dispatch(timeout)
43
+ },
44
+ effect.kind === "initial"
45
+ ? TYPEWRITER_INITIAL_DELAY_MS
46
+ : effect.kind === "linger"
47
+ ? TYPEWRITER_LINGER_MS
48
+ : typingStepDelayMs(Math.random(), Math.random()),
49
+ )
50
+ }
29
51
 
30
- return 30 + Math.random() * 50
31
- }
52
+ const sync = () => dispatch({ type: "sync", text: props.text, reducedMotion: prefersReducedMotion() })
32
53
 
33
- const type = () => {
34
- if (i < text.length) {
35
- setStore("displayed", text.slice(0, i + 1))
36
- i++
37
- timeouts.push(setTimeout(type, getTypingDelay()))
38
- } else {
39
- setStore("typing", false)
40
- timeouts.push(setTimeout(() => setStore("cursor", false), 2000))
41
- }
42
- }
54
+ createEffect(sync)
55
+
56
+ onCleanup(observeReducedMotion(() => untrack(sync)))
43
57
 
44
- timeouts.push(setTimeout(type, 200))
58
+ onCleanup(() => dispatch(DISPOSED_TYPEWRITER_EVENT))
45
59
 
46
- onCleanup(() => {
47
- for (const timeout of timeouts) clearTimeout(timeout)
48
- })
49
- })
60
+ const cursor = createMemo(() => selectTypewriterCursor(state()))
50
61
 
51
62
  return (
52
63
  <Dynamic component={props.as || "p"} class={props.class}>
53
- {store.displayed}
54
- <Show when={store.cursor}>
55
- <span classList={{ "blinking-cursor": !store.typing }}>│</span>
64
+ {selectTypewriterDisplayed(state())}
65
+ <Show when={cursor() !== "none"}>
66
+ <span classList={{ "blinking-cursor": cursor() === "blinking" }}>│</span>
56
67
  </Show>
57
68
  </Dynamic>
58
69
  )
@@ -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
+ }