@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,109 @@
1
+ /**
2
+ * Every dropdown menu runs this machine, so each reachable state and event is a frozen module singleton, and an event
3
+ * that changes nothing returns the state it was given.
4
+ */
5
+ export type MenuInputGesture = "pointer" | "keyboard" | "none"
6
+
7
+ /** How the menu opened. An open without an input gesture (a shortcut handler, an async action) is `programmatic`. */
8
+ export type MenuOpenModality = "pointer" | "keyboard" | "programmatic"
9
+
10
+ export type MenuState =
11
+ | { readonly status: "closed" }
12
+ | { readonly status: "open"; readonly modality: MenuOpenModality; readonly outsideInteracted: boolean }
13
+ /**
14
+ * Kobalte reports the close first and restores focus a task later, so the modality outlives the open state until
15
+ * that focus restore has run.
16
+ */
17
+ | { readonly status: "closing"; readonly modality: MenuOpenModality; readonly outsideInteracted: boolean }
18
+ | { readonly status: "disposed" }
19
+
20
+ export type MenuEvent =
21
+ | { readonly type: "open.changed"; readonly open: boolean; readonly gesture: MenuInputGesture }
22
+ /** A press outside a non-modal menu, or a context menu anywhere, dismissed it and keeps focus where it landed. */
23
+ | { readonly type: "outside.interacted" }
24
+ | { readonly type: "close.autofocus" }
25
+ | { readonly type: "disposed" }
26
+
27
+ const MODALITIES: readonly MenuOpenModality[] = ["pointer", "keyboard", "programmatic"]
28
+
29
+ const GESTURES: readonly MenuInputGesture[] = ["pointer", "keyboard", "none"]
30
+
31
+ const CLOSED: MenuState = Object.freeze({ status: "closed" })
32
+
33
+ const DISPOSED: MenuState = Object.freeze({ status: "disposed" })
34
+
35
+ const OPEN = MODALITIES.flatMap((modality) =>
36
+ [false, true].map((outsideInteracted): MenuState => Object.freeze({ status: "open", modality, outsideInteracted })),
37
+ )
38
+
39
+ const CLOSING = MODALITIES.flatMap((modality) =>
40
+ [false, true].map(
41
+ (outsideInteracted): MenuState => Object.freeze({ status: "closing", modality, outsideInteracted }),
42
+ ),
43
+ )
44
+
45
+ const key = (modality: MenuOpenModality, outsideInteracted: boolean) =>
46
+ MODALITIES.indexOf(modality) * 2 + (outsideInteracted ? 1 : 0)
47
+
48
+ const open = (modality: MenuOpenModality, outsideInteracted: boolean) => OPEN[key(modality, outsideInteracted)]
49
+
50
+ const closing = (modality: MenuOpenModality, outsideInteracted: boolean) => CLOSING[key(modality, outsideInteracted)]
51
+
52
+ const OPEN_CHANGED = [false, true].flatMap((isOpen) =>
53
+ GESTURES.map((gesture): MenuEvent => Object.freeze({ type: "open.changed", open: isOpen, gesture })),
54
+ )
55
+
56
+ export const MENU_OUTSIDE_INTERACTED: MenuEvent = Object.freeze({ type: "outside.interacted" })
57
+
58
+ export const MENU_CLOSE_AUTOFOCUS: MenuEvent = Object.freeze({ type: "close.autofocus" })
59
+
60
+ export const MENU_DISPOSED: MenuEvent = Object.freeze({ type: "disposed" })
61
+
62
+ export function openChangedEvent(isOpen: boolean, gesture: MenuInputGesture) {
63
+ return OPEN_CHANGED[(isOpen ? GESTURES.length : 0) + GESTURES.indexOf(gesture)]
64
+ }
65
+
66
+ export function createMenuState(initialOpen: boolean) {
67
+ return initialOpen ? open("programmatic", false) : CLOSED
68
+ }
69
+
70
+ export function transitionMenu(state: MenuState, event: MenuEvent): MenuState {
71
+ if (state.status === "disposed") return state
72
+
73
+ if (event.type === "disposed") return DISPOSED
74
+
75
+ if (event.type === "open.changed") {
76
+ if (event.open && state.status !== "open") {
77
+ return open(
78
+ event.gesture === "pointer" ? "pointer" : event.gesture === "keyboard" ? "keyboard" : "programmatic",
79
+ false,
80
+ )
81
+ }
82
+
83
+ if (!event.open && state.status === "open") return closing(state.modality, state.outsideInteracted)
84
+
85
+ return state
86
+ }
87
+
88
+ if (event.type === "outside.interacted") {
89
+ if (state.status === "open" && !state.outsideInteracted) return open(state.modality, true)
90
+
91
+ if (state.status === "closing" && !state.outsideInteracted) return closing(state.modality, true)
92
+
93
+ return state
94
+ }
95
+
96
+ if (state.status === "closing") return CLOSED
97
+
98
+ return state
99
+ }
100
+
101
+ /**
102
+ * Restore focus to the trigger without a focus ring: Escape counts as keyboard use, so Chromium would ring a trigger
103
+ * the pointer opened. After an outside press, focus stays where that press put it.
104
+ */
105
+ export function selectMenuRestoreTriggerWithoutRing(state: MenuState) {
106
+ return (
107
+ (state.status === "closing" || state.status === "open") && state.modality === "pointer" && !state.outsideInteracted
108
+ )
109
+ }
@@ -1,8 +1,11 @@
1
- import { DropdownMenu } from "@kobalte/core/dropdown-menu"
1
+ import { DropdownMenu, type DropdownMenuTriggerProps } from "@kobalte/core/dropdown-menu"
2
2
  import { ContextMenu } from "@kobalte/core/context-menu"
3
- import { makeEventListener } from "@solid-primitives/event-listener"
3
+ import type { PolymorphicProps } from "@kobalte/core/polymorphic"
4
4
  import {
5
+ createComputed,
5
6
  createContext,
7
+ createSignal,
8
+ onCleanup,
6
9
  Show,
7
10
  splitProps,
8
11
  useContext,
@@ -11,7 +14,20 @@ import {
11
14
  type ComponentProps,
12
15
  type JSX,
13
16
  type ParentProps,
17
+ type ValidComponent,
14
18
  } from "solid-js"
19
+ import {
20
+ createMenuState,
21
+ MENU_CLOSE_AUTOFOCUS,
22
+ MENU_DISPOSED,
23
+ MENU_OUTSIDE_INTERACTED,
24
+ openChangedEvent,
25
+ selectMenuRestoreTriggerWithoutRing,
26
+ transitionMenu,
27
+ type MenuEvent,
28
+ type MenuInputGesture,
29
+ type MenuState,
30
+ } from "./machine"
15
31
  import "./menu.css"
16
32
 
17
33
  const ChevronRight: Component = () => (
@@ -42,13 +58,70 @@ const CheckMark: Component = () => (
42
58
 
43
59
  export type MenuAppearance = "standard" | "compact"
44
60
 
45
- const MenuAppearanceContext = createContext<{
61
+ type MenuContextValue = {
46
62
  appearance: Accessor<MenuAppearance>
47
- pointerOpened: () => boolean
48
- }>({
49
- appearance: (): MenuAppearance => "compact",
50
- pointerOpened: () => false,
51
- })
63
+ state: Accessor<MenuState>
64
+ modal: Accessor<boolean>
65
+ onOutsideInteract: () => void
66
+ onCloseAutoFocus: () => void
67
+ }
68
+
69
+ // Context menus restore focus through Kobalte alone, so they never run the dropdown machine.
70
+ const UNTRACKED_MENU: MenuContextValue = {
71
+ appearance: () => "compact",
72
+ state: () => createMenuState(false),
73
+ modal: () => true,
74
+ onOutsideInteract: () => {},
75
+ onCloseAutoFocus: () => {},
76
+ }
77
+
78
+ const MenuAppearanceContext = createContext<MenuContextValue>(UNTRACKED_MENU)
79
+
80
+ // One set of window listeners serves every mounted menu. Capture runs before the trigger's own handlers, so while an
81
+ // input event dispatches, `gesture` names the input that may open a menu. It clears once that event's task ends, so a
82
+ // later open without input counts as programmatic.
83
+ let trackers = 0
84
+
85
+ let gesture: MenuInputGesture = "none"
86
+
87
+ let gestureReset: ReturnType<typeof setTimeout> | undefined
88
+
89
+ const trackGesture = (event: Event) => {
90
+ if (!event.isTrusted) return
91
+ // Keyboard activation fires a click with no press count.
92
+ gesture =
93
+ event.type === "keydown" || (event instanceof MouseEvent && event.type === "click" && event.detail === 0)
94
+ ? "keyboard"
95
+ : "pointer"
96
+
97
+ // A microtask would run between listeners of this same dispatch, before the trigger's handler reads the gesture.
98
+ if (gestureReset !== undefined) return
99
+ gestureReset = setTimeout(() => {
100
+ gestureReset = undefined
101
+ gesture = "none"
102
+ })
103
+ }
104
+
105
+ const GESTURE_EVENTS = ["pointerdown", "click", "keydown"] as const
106
+
107
+ const GESTURE_LISTENER_OPTIONS = { capture: true, passive: true }
108
+
109
+ function retainGestureTracker() {
110
+ trackers += 1
111
+
112
+ if (trackers === 1)
113
+ GESTURE_EVENTS.forEach((type) => window.addEventListener(type, trackGesture, GESTURE_LISTENER_OPTIONS))
114
+ }
115
+
116
+ function releaseGestureTracker() {
117
+ trackers -= 1
118
+
119
+ if (trackers > 0) return
120
+ GESTURE_EVENTS.forEach((type) => window.removeEventListener(type, trackGesture, GESTURE_LISTENER_OPTIONS))
121
+ clearTimeout(gestureReset)
122
+ gestureReset = undefined
123
+ gesture = "none"
124
+ }
52
125
 
53
126
  function useMenuContext() {
54
127
  const ctx = useContext(MenuAppearanceContext)
@@ -210,6 +283,7 @@ function MenuSubContent(props: ComponentProps<typeof DropdownMenu.SubContent>) {
210
283
  return (
211
284
  <DropdownMenu.SubContent
212
285
  {...r}
286
+ data-kb-top-layer=""
213
287
  data-component="menu-v2-content"
214
288
  data-appearance={ctx.appearance()}
215
289
  classList={{ ...s.classList, [s.class ?? ""]: !!s.class }}
@@ -245,16 +319,33 @@ function MenuSeparator(props: ComponentProps<typeof DropdownMenu.Separator>) {
245
319
  )
246
320
  }
247
321
 
322
+ // Kobalte drops `data-expanded` as the menu closes but returns focus to the trigger a task later, so for a frame the
323
+ // trigger is neither expanded nor focused. `data-closing` covers that gap for UI revealed by either.
324
+ function MenuTrigger<T extends ValidComponent = "button">(props: PolymorphicProps<T, DropdownMenuTriggerProps<T>>) {
325
+ const ctx = useMenuContext()
326
+
327
+ return <DropdownMenu.Trigger {...props} data-closing={ctx.state().status === "closing" ? "" : undefined} />
328
+ }
329
+
248
330
  function MenuContent(props: ComponentProps<typeof DropdownMenu.Content>) {
249
331
  const ctx = useMenuContext()
250
- const [s, r] = splitProps(props, ["class", "classList", "onCloseAutoFocus"])
332
+ const [s, r] = splitProps(props, ["class", "classList", "onCloseAutoFocus", "onInteractOutside"])
251
333
 
252
- // A menu returns focus to its trigger when it closes. Closing with Escape counts as keyboard use, so Chromium would
253
- // ring a trigger the pointer opened; restore focus without the ring and keep it for keyboard opens.
334
+ // Kobalte's own rule for when an outside press keeps focus where it landed.
335
+ const onInteractOutside: NonNullable<typeof s.onInteractOutside> = (event) => {
336
+ s.onInteractOutside?.(event)
337
+
338
+ if (!event.defaultPrevented && (!ctx.modal() || event.detail.isContextMenu)) ctx.onOutsideInteract()
339
+ }
340
+
341
+ // A menu returns focus to its trigger when it closes. Restore it without the ring after a pointer open and keep the
342
+ // ring for keyboard and programmatic opens.
254
343
  const onCloseAutoFocus = (event: Event) => {
344
+ const restore = selectMenuRestoreTriggerWithoutRing(ctx.state())
345
+ ctx.onCloseAutoFocus()
255
346
  s.onCloseAutoFocus?.(event)
256
347
 
257
- if (event.defaultPrevented || !ctx.pointerOpened()) return
348
+ if (event.defaultPrevented || !restore) return
258
349
  event.preventDefault()
259
350
  const active = document.activeElement
260
351
 
@@ -269,6 +360,9 @@ function MenuContent(props: ComponentProps<typeof DropdownMenu.Content>) {
269
360
  <DropdownMenu.Content
270
361
  {...r}
271
362
  onCloseAutoFocus={onCloseAutoFocus}
363
+ onInteractOutside={onInteractOutside}
364
+ // A modal dialog hides what's outside it from assistive tech, and a non-modal menu is portaled outside it.
365
+ data-kb-top-layer=""
272
366
  data-component="menu-v2-content"
273
367
  data-appearance={ctx.appearance()}
274
368
  classList={{ ...s.classList, [s.class ?? ""]: !!s.class }}
@@ -283,19 +377,36 @@ export interface MenuProps extends ComponentProps<typeof DropdownMenu> {
283
377
  function MenuRoot(props: MenuProps) {
284
378
  const [local, rest] = splitProps(props, ["appearance", "children", "onOpenChange"])
285
379
  const appearance = () => local.appearance ?? "compact"
286
- let pointer = false
287
- let pointerOpened = false
288
-
289
- // Capture runs before the trigger's own handlers, so the last input is what opened the menu.
290
- makeEventListener(window, "pointerdown", () => (pointer = true), { capture: true })
291
- makeEventListener(window, "keydown", () => (pointer = false), { capture: true })
380
+ const initialOpen = !!props.open || (props.open === undefined && !!props.defaultOpen)
381
+ const [menuState, setMenuState] = createSignal(createMenuState(initialOpen))
382
+ const dispatch = (event: MenuEvent) => setMenuState((state) => transitionMenu(state, event))
383
+
384
+ retainGestureTracker()
385
+ onCleanup(() => {
386
+ releaseGestureTracker()
387
+ dispatch(MENU_DISPOSED)
388
+ })
389
+
390
+ // Kobalte does not report a controlled open it did not ask for, so a controlled menu follows `open` itself.
391
+ createComputed(() => {
392
+ if (props.open === undefined) return
393
+ dispatch(openChangedEvent(props.open, gesture))
394
+ })
395
+
396
+ const context = {
397
+ appearance,
398
+ state: menuState,
399
+ modal: () => props.modal ?? true,
400
+ onOutsideInteract: () => dispatch(MENU_OUTSIDE_INTERACTED),
401
+ onCloseAutoFocus: () => dispatch(MENU_CLOSE_AUTOFOCUS),
402
+ }
292
403
 
293
404
  return (
294
- <MenuAppearanceContext.Provider value={{ appearance, pointerOpened: () => pointerOpened }}>
405
+ <MenuAppearanceContext.Provider value={context}>
295
406
  <DropdownMenu
296
407
  {...rest}
297
408
  onOpenChange={(open) => {
298
- if (open) pointerOpened = pointer
409
+ if (props.open === undefined) dispatch(openChangedEvent(open, gesture))
299
410
  local.onOpenChange?.(open)
300
411
  }}
301
412
  >
@@ -314,7 +425,7 @@ function MenuContextRoot(props: MenuContextProps) {
314
425
  const appearance = () => local.appearance ?? "compact"
315
426
 
316
427
  return (
317
- <MenuAppearanceContext.Provider value={{ appearance, pointerOpened: () => false }}>
428
+ <MenuAppearanceContext.Provider value={{ ...UNTRACKED_MENU, appearance }}>
318
429
  <ContextMenu {...rest}>{local.children}</ContextMenu>
319
430
  </MenuAppearanceContext.Provider>
320
431
  )
@@ -341,7 +452,7 @@ const MenuContext = Object.assign(MenuContextRoot, {
341
452
  })
342
453
 
343
454
  export const Menu = Object.assign(MenuRoot, {
344
- Trigger: DropdownMenu.Trigger,
455
+ Trigger: MenuTrigger,
345
456
  Portal: DropdownMenu.Portal,
346
457
  Content: MenuContent,
347
458
  Item: MenuItem,
@@ -0,0 +1,72 @@
1
+ export type IndicatorBox = {
2
+ readonly x: number
3
+ readonly width: number
4
+ }
5
+
6
+ /**
7
+ * The pill behind the selected segment. It is `placed` only while the selected segment has a measurable box, so a
8
+ * selection that changes while the control is hidden (`display: none`) jumps to its segment once it shows instead
9
+ * of springing from the previous segment's stale position.
10
+ */
11
+ export type SegmentedIndicatorState =
12
+ | { readonly status: "hidden" }
13
+ | { readonly status: "placed"; readonly value: string; readonly box: IndicatorBox }
14
+ | { readonly status: "disposed" }
15
+
16
+ export type SegmentedIndicatorEvent =
17
+ | {
18
+ readonly type: "measured"
19
+ readonly value: string | null
20
+ readonly box: IndicatorBox | null
21
+ readonly reducedMotion: boolean
22
+ }
23
+ | { readonly type: "disposed" }
24
+
25
+ export type SegmentedIndicatorEffect =
26
+ | { readonly type: "jump"; readonly box: IndicatorBox }
27
+ | { readonly type: "animate"; readonly box: IndicatorBox }
28
+ | { readonly type: "stop" }
29
+
30
+ export type SegmentedIndicatorTransition = {
31
+ readonly state: SegmentedIndicatorState
32
+ readonly effect: SegmentedIndicatorEffect | null
33
+ }
34
+
35
+ export const HIDDEN_INDICATOR: SegmentedIndicatorState = Object.freeze({ status: "hidden" })
36
+
37
+ const DISPOSED_INDICATOR: SegmentedIndicatorState = Object.freeze({ status: "disposed" })
38
+
39
+ const STOP: SegmentedIndicatorEffect = Object.freeze({ type: "stop" })
40
+
41
+ export function transitionSegmentedIndicator(
42
+ state: SegmentedIndicatorState,
43
+ event: SegmentedIndicatorEvent,
44
+ ): SegmentedIndicatorTransition {
45
+ if (state.status === "disposed") return { state, effect: null }
46
+
47
+ if (event.type === "disposed") {
48
+ return { state: DISPOSED_INDICATOR, effect: state.status === "placed" ? STOP : null }
49
+ }
50
+
51
+ if (event.value === null || event.box === null || event.box.width <= 0) {
52
+ if (state.status === "hidden") return { state, effect: null }
53
+
54
+ return { state: HIDDEN_INDICATOR, effect: STOP }
55
+ }
56
+
57
+ const next = { status: "placed", value: event.value, box: event.box } as const
58
+
59
+ if (state.status === "hidden") return { state: next, effect: { type: "jump", box: event.box } }
60
+
61
+ if (state.value === event.value) {
62
+ if (state.box.x === event.box.x && state.box.width === event.box.width) return { state, effect: null }
63
+
64
+ return { state: next, effect: { type: "jump", box: event.box } }
65
+ }
66
+
67
+ return { state: next, effect: { type: event.reducedMotion ? "jump" : "animate", box: event.box } }
68
+ }
69
+
70
+ export function selectIndicatorReady(state: SegmentedIndicatorState) {
71
+ return state.status === "placed"
72
+ }
@@ -9,6 +9,7 @@ import {
9
9
  onMount,
10
10
  Show,
11
11
  splitProps,
12
+ untrack,
12
13
  useContext,
13
14
  type Accessor,
14
15
  type ComponentProps,
@@ -17,6 +18,12 @@ import {
17
18
  } from "solid-js"
18
19
  import "./segmented-control.css"
19
20
  import { prefersReducedMotion } from "../../motion"
21
+ import {
22
+ HIDDEN_INDICATOR,
23
+ selectIndicatorReady,
24
+ transitionSegmentedIndicator,
25
+ type SegmentedIndicatorEvent,
26
+ } from "./machine"
20
27
 
21
28
  type OnChange = (value: string | null) => void
22
29
 
@@ -104,8 +111,7 @@ export function SegmentedControl(props: SegmentedControlProps) {
104
111
  ])
105
112
 
106
113
  const [internal, setInternal] = createSignal<string | null>(local.defaultValue ?? null)
107
- const [layoutVersion, setLayoutVersion] = createSignal(0)
108
- const [ready, setReady] = createSignal(false)
114
+ const [indicator, setIndicator] = createSignal(HIDDEN_INDICATOR)
109
115
 
110
116
  const selected = createMemo(() => (isControlled() ? (local.value ?? null) : internal()))
111
117
 
@@ -115,68 +121,62 @@ export function SegmentedControl(props: SegmentedControlProps) {
115
121
  let rootEl: HTMLDivElement | undefined
116
122
  let observer: ResizeObserver | undefined
117
123
  const items = new Map<string, HTMLButtonElement>()
118
- let target = { x: 0, width: 0 }
119
- let lastSelected: string | null = null
120
124
 
121
- const syncIndicator = (shouldAnimate: boolean) => {
122
- const current = selected()
123
- const button = current !== null ? items.get(current) : undefined
125
+ const dispatch = (event: SegmentedIndicatorEvent) => {
126
+ const transition = transitionSegmentedIndicator(untrack(indicator), event)
127
+ setIndicator(transition.state)
128
+ const effect = transition.effect
124
129
 
125
- if (!rootEl || !button) {
126
- setReady(false)
130
+ if (effect?.type === "stop") {
131
+ x.stop()
132
+ width.stop()
127
133
 
128
134
  return
129
135
  }
130
136
 
131
- const next = measureOffset(rootEl, button)
132
-
133
- if (next.width === 0) return
134
-
135
- target = next
136
-
137
- if (!ready() || !shouldAnimate || prefersReducedMotion()) {
138
- x.jump(next.x)
139
- width.jump(next.width)
140
- setReady(true)
137
+ if (effect?.type === "jump") {
138
+ x.jump(effect.box.x)
139
+ width.jump(effect.box.width)
141
140
 
142
141
  return
143
142
  }
144
143
 
145
- const transition = local.transition ?? SEGMENTED_CONTROL_SPRING
146
- animate(x, next.x, transition)
147
- animate(width, next.width, transition)
144
+ if (effect?.type !== "animate") return
145
+ const spring = local.transition ?? SEGMENTED_CONTROL_SPRING
146
+ animate(x, effect.box.x, spring)
147
+ animate(width, effect.box.width, spring)
148
148
  }
149
149
 
150
- onMount(() => {
151
- if (!("ResizeObserver" in globalThis)) return
152
-
153
- observer = new ResizeObserver(() => {
154
- const current = selected()
155
- const button = current !== null ? items.get(current) : undefined
156
-
157
- if (!rootEl || !button) return
158
-
159
- const next = measureOffset(rootEl, button)
150
+ const sync = () => {
151
+ const current = untrack(selected)
152
+ const button = current !== null ? items.get(current) : undefined
160
153
 
161
- if (next.width === 0) return
154
+ dispatch({
155
+ type: "measured",
156
+ value: current,
157
+ box: rootEl && button ? measureOffset(rootEl, button) : null,
158
+ reducedMotion: prefersReducedMotion(),
159
+ })
160
+ }
162
161
 
163
- if (ready() && next.x === target.x && next.width === target.width) return
162
+ onMount(() => {
163
+ if (!("ResizeObserver" in globalThis)) return
164
164
 
165
- syncIndicator(false)
166
- })
165
+ observer = new ResizeObserver(sync)
167
166
 
168
167
  if (rootEl) observer.observe(rootEl)
169
168
 
170
169
  items.forEach((button) => observer?.observe(button))
171
- onCleanup(() => observer?.disconnect())
170
+ })
171
+
172
+ onCleanup(() => {
173
+ observer?.disconnect()
174
+ dispatch({ type: "disposed" })
172
175
  })
173
176
 
174
177
  createEffect(() => {
175
- const current = selected()
176
- layoutVersion()
177
- const shouldAnimate = ready() && lastSelected !== null && current !== null && current !== lastSelected
178
- lastSelected = current
179
- syncIndicator(shouldAnimate)
178
+ selected()
179
+ sync()
180
180
  })
181
181
 
182
182
  const setSelected = (next: string | null) => {
@@ -210,12 +210,12 @@ export function SegmentedControl(props: SegmentedControlProps) {
210
210
  const registerItem = (value: string, el: HTMLButtonElement) => {
211
211
  items.set(value, el)
212
212
  observer?.observe(el)
213
- setLayoutVersion((v) => v + 1)
213
+ sync()
214
214
 
215
215
  return () => {
216
216
  if (items.get(value) === el) items.delete(value)
217
217
  observer?.unobserve(el)
218
- setLayoutVersion((v) => v + 1)
218
+ sync()
219
219
  }
220
220
  }
221
221
 
@@ -243,13 +243,13 @@ export function SegmentedControl(props: SegmentedControlProps) {
243
243
  role="group"
244
244
  data-component="segmented-control-v2"
245
245
  data-slot="segmented-control-v2"
246
- data-ready={ready() ? "" : undefined}
246
+ data-ready={selectIndicatorReady(indicator()) ? "" : undefined}
247
247
  classList={{
248
248
  ...local.classList,
249
249
  [local.class ?? ""]: !!local.class,
250
250
  }}
251
251
  >
252
- <Show when={ready() && selected() !== null}>
252
+ <Show when={selectIndicatorReady(indicator())}>
253
253
  <motion.span data-slot="segmented-control-v2-indicator" aria-hidden="true" style={{ x, width }} />
254
254
  </Show>
255
255
  {local.children}
@@ -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
- setTimeout(() => {
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