@opencode/ui 0.0.0-dev-750 → 0.0.0-dev-784

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 (48) hide show
  1. package/dist/components/animated-number-machine.d.ts +95 -0
  2. package/dist/components/popover-machine.d.ts +96 -0
  3. package/dist/components/popover.d.ts +2 -2
  4. package/dist/components/resize-handle-machine.d.ts +71 -0
  5. package/dist/components/scroll-view-machine.d.ts +66 -0
  6. package/dist/components/scroll-view.d.ts +2 -2
  7. package/dist/components/text-field-machine.d.ts +62 -0
  8. package/dist/components/text-reveal-machine.d.ts +90 -0
  9. package/dist/data-display/line-comment/machine.d.ts +157 -0
  10. package/dist/data-display/project-avatar/animation-machine.d.ts +62 -0
  11. package/dist/data-display/project-avatar/animation.d.ts +1 -1
  12. package/dist/feedback/toast/machine.d.ts +57 -0
  13. package/dist/feedback/toast/toast.d.ts +1 -1
  14. package/dist/forms/select/machine.d.ts +45 -0
  15. package/dist/forms/typeahead/machine.d.ts +85 -0
  16. package/dist/navigation/menu/machine.d.ts +49 -0
  17. package/dist/navigation/segmented-control/machine.d.ts +46 -0
  18. package/dist/theme/context.d.ts +20 -9
  19. package/dist/theme/machine.d.ts +174 -0
  20. package/package.json +2 -2
  21. package/src/components/animated-number-machine.ts +214 -0
  22. package/src/components/animated-number.tsx +134 -83
  23. package/src/components/popover-machine.ts +198 -0
  24. package/src/components/popover.tsx +157 -89
  25. package/src/components/resize-handle-machine.ts +174 -0
  26. package/src/components/resize-handle.tsx +113 -83
  27. package/src/components/scroll-view-machine.ts +231 -0
  28. package/src/components/scroll-view.tsx +163 -116
  29. package/src/components/text-field-machine.ts +102 -0
  30. package/src/components/text-field.tsx +78 -24
  31. package/src/components/text-reveal-machine.ts +184 -0
  32. package/src/components/text-reveal.tsx +96 -93
  33. package/src/data-display/line-comment/line-comment.tsx +154 -178
  34. package/src/data-display/line-comment/machine.ts +392 -0
  35. package/src/data-display/project-avatar/animation-machine.ts +182 -0
  36. package/src/data-display/project-avatar/animation.ts +67 -38
  37. package/src/feedback/toast/machine.ts +115 -0
  38. package/src/feedback/toast/toast.tsx +55 -59
  39. package/src/forms/select/machine.ts +81 -0
  40. package/src/forms/select/select.tsx +49 -27
  41. package/src/forms/typeahead/machine.ts +253 -0
  42. package/src/forms/typeahead/typeahead.tsx +70 -62
  43. package/src/navigation/menu/machine.ts +109 -0
  44. package/src/navigation/menu/menu.tsx +118 -20
  45. package/src/navigation/segmented-control/machine.ts +72 -0
  46. package/src/navigation/segmented-control/segmented-control.tsx +46 -46
  47. package/src/theme/context.tsx +134 -240
  48. package/src/theme/machine.ts +508 -0
@@ -1,8 +1,10 @@
1
1
  import { DropdownMenu } from "@kobalte/core/dropdown-menu"
2
2
  import { ContextMenu } from "@kobalte/core/context-menu"
3
- import { makeEventListener } from "@solid-primitives/event-listener"
4
3
  import {
4
+ createComputed,
5
5
  createContext,
6
+ createSignal,
7
+ onCleanup,
6
8
  Show,
7
9
  splitProps,
8
10
  useContext,
@@ -12,6 +14,18 @@ import {
12
14
  type JSX,
13
15
  type ParentProps,
14
16
  } from "solid-js"
17
+ import {
18
+ createMenuState,
19
+ MENU_CLOSE_AUTOFOCUS,
20
+ MENU_DISPOSED,
21
+ MENU_OUTSIDE_INTERACTED,
22
+ openChangedEvent,
23
+ selectMenuRestoreTriggerWithoutRing,
24
+ transitionMenu,
25
+ type MenuEvent,
26
+ type MenuInputGesture,
27
+ type MenuState,
28
+ } from "./machine"
15
29
  import "./menu.css"
16
30
 
17
31
  const ChevronRight: Component = () => (
@@ -42,13 +56,70 @@ const CheckMark: Component = () => (
42
56
 
43
57
  export type MenuAppearance = "standard" | "compact"
44
58
 
45
- const MenuAppearanceContext = createContext<{
59
+ type MenuContextValue = {
46
60
  appearance: Accessor<MenuAppearance>
47
- pointerOpened: () => boolean
48
- }>({
49
- appearance: (): MenuAppearance => "compact",
50
- pointerOpened: () => false,
51
- })
61
+ state: Accessor<MenuState>
62
+ modal: Accessor<boolean>
63
+ onOutsideInteract: () => void
64
+ onCloseAutoFocus: () => void
65
+ }
66
+
67
+ // Context menus restore focus through Kobalte alone, so they never run the dropdown machine.
68
+ const UNTRACKED_MENU: MenuContextValue = {
69
+ appearance: () => "compact",
70
+ state: () => createMenuState(false),
71
+ modal: () => true,
72
+ onOutsideInteract: () => {},
73
+ onCloseAutoFocus: () => {},
74
+ }
75
+
76
+ const MenuAppearanceContext = createContext<MenuContextValue>(UNTRACKED_MENU)
77
+
78
+ // One set of window listeners serves every mounted menu. Capture runs before the trigger's own handlers, so while an
79
+ // input event dispatches, `gesture` names the input that may open a menu. It clears once that event's task ends, so a
80
+ // later open without input counts as programmatic.
81
+ let trackers = 0
82
+
83
+ let gesture: MenuInputGesture = "none"
84
+
85
+ let gestureReset: ReturnType<typeof setTimeout> | undefined
86
+
87
+ const trackGesture = (event: Event) => {
88
+ if (!event.isTrusted) return
89
+ // Keyboard activation fires a click with no press count.
90
+ gesture =
91
+ event.type === "keydown" || (event instanceof MouseEvent && event.type === "click" && event.detail === 0)
92
+ ? "keyboard"
93
+ : "pointer"
94
+
95
+ // A microtask would run between listeners of this same dispatch, before the trigger's handler reads the gesture.
96
+ if (gestureReset !== undefined) return
97
+ gestureReset = setTimeout(() => {
98
+ gestureReset = undefined
99
+ gesture = "none"
100
+ })
101
+ }
102
+
103
+ const GESTURE_EVENTS = ["pointerdown", "click", "keydown"] as const
104
+
105
+ const GESTURE_LISTENER_OPTIONS = { capture: true, passive: true }
106
+
107
+ function retainGestureTracker() {
108
+ trackers += 1
109
+
110
+ if (trackers === 1)
111
+ GESTURE_EVENTS.forEach((type) => window.addEventListener(type, trackGesture, GESTURE_LISTENER_OPTIONS))
112
+ }
113
+
114
+ function releaseGestureTracker() {
115
+ trackers -= 1
116
+
117
+ if (trackers > 0) return
118
+ GESTURE_EVENTS.forEach((type) => window.removeEventListener(type, trackGesture, GESTURE_LISTENER_OPTIONS))
119
+ clearTimeout(gestureReset)
120
+ gestureReset = undefined
121
+ gesture = "none"
122
+ }
52
123
 
53
124
  function useMenuContext() {
54
125
  const ctx = useContext(MenuAppearanceContext)
@@ -247,14 +318,23 @@ function MenuSeparator(props: ComponentProps<typeof DropdownMenu.Separator>) {
247
318
 
248
319
  function MenuContent(props: ComponentProps<typeof DropdownMenu.Content>) {
249
320
  const ctx = useMenuContext()
250
- const [s, r] = splitProps(props, ["class", "classList", "onCloseAutoFocus"])
321
+ const [s, r] = splitProps(props, ["class", "classList", "onCloseAutoFocus", "onInteractOutside"])
322
+
323
+ // Kobalte's own rule for when an outside press keeps focus where it landed.
324
+ const onInteractOutside: NonNullable<typeof s.onInteractOutside> = (event) => {
325
+ s.onInteractOutside?.(event)
326
+
327
+ if (!event.defaultPrevented && (!ctx.modal() || event.detail.isContextMenu)) ctx.onOutsideInteract()
328
+ }
251
329
 
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.
330
+ // A menu returns focus to its trigger when it closes. Restore it without the ring after a pointer open and keep the
331
+ // ring for keyboard and programmatic opens.
254
332
  const onCloseAutoFocus = (event: Event) => {
333
+ const restore = selectMenuRestoreTriggerWithoutRing(ctx.state())
334
+ ctx.onCloseAutoFocus()
255
335
  s.onCloseAutoFocus?.(event)
256
336
 
257
- if (event.defaultPrevented || !ctx.pointerOpened()) return
337
+ if (event.defaultPrevented || !restore) return
258
338
  event.preventDefault()
259
339
  const active = document.activeElement
260
340
 
@@ -269,6 +349,7 @@ function MenuContent(props: ComponentProps<typeof DropdownMenu.Content>) {
269
349
  <DropdownMenu.Content
270
350
  {...r}
271
351
  onCloseAutoFocus={onCloseAutoFocus}
352
+ onInteractOutside={onInteractOutside}
272
353
  data-component="menu-v2-content"
273
354
  data-appearance={ctx.appearance()}
274
355
  classList={{ ...s.classList, [s.class ?? ""]: !!s.class }}
@@ -283,19 +364,36 @@ export interface MenuProps extends ComponentProps<typeof DropdownMenu> {
283
364
  function MenuRoot(props: MenuProps) {
284
365
  const [local, rest] = splitProps(props, ["appearance", "children", "onOpenChange"])
285
366
  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 })
367
+ const initialOpen = !!props.open || (props.open === undefined && !!props.defaultOpen)
368
+ const [menuState, setMenuState] = createSignal(createMenuState(initialOpen))
369
+ const dispatch = (event: MenuEvent) => setMenuState((state) => transitionMenu(state, event))
370
+
371
+ retainGestureTracker()
372
+ onCleanup(() => {
373
+ releaseGestureTracker()
374
+ dispatch(MENU_DISPOSED)
375
+ })
376
+
377
+ // Kobalte does not report a controlled open it did not ask for, so a controlled menu follows `open` itself.
378
+ createComputed(() => {
379
+ if (props.open === undefined) return
380
+ dispatch(openChangedEvent(props.open, gesture))
381
+ })
382
+
383
+ const context = {
384
+ appearance,
385
+ state: menuState,
386
+ modal: () => props.modal ?? true,
387
+ onOutsideInteract: () => dispatch(MENU_OUTSIDE_INTERACTED),
388
+ onCloseAutoFocus: () => dispatch(MENU_CLOSE_AUTOFOCUS),
389
+ }
292
390
 
293
391
  return (
294
- <MenuAppearanceContext.Provider value={{ appearance, pointerOpened: () => pointerOpened }}>
392
+ <MenuAppearanceContext.Provider value={context}>
295
393
  <DropdownMenu
296
394
  {...rest}
297
395
  onOpenChange={(open) => {
298
- if (open) pointerOpened = pointer
396
+ if (props.open === undefined) dispatch(openChangedEvent(open, gesture))
299
397
  local.onOpenChange?.(open)
300
398
  }}
301
399
  >
@@ -314,7 +412,7 @@ function MenuContextRoot(props: MenuContextProps) {
314
412
  const appearance = () => local.appearance ?? "compact"
315
413
 
316
414
  return (
317
- <MenuAppearanceContext.Provider value={{ appearance, pointerOpened: () => false }}>
415
+ <MenuAppearanceContext.Provider value={{ ...UNTRACKED_MENU, appearance }}>
318
416
  <ContextMenu {...rest}>{local.children}</ContextMenu>
319
417
  </MenuAppearanceContext.Provider>
320
418
  )
@@ -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}