@opencode/ui 0.0.0-dev-1288 → 0.0.0-dev-1289

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.
@@ -0,0 +1,48 @@
1
+ /**
2
+ * A menu opens at most one submenu at a time. A submenu stays open while focus crosses the other items of its menu, so
3
+ * the next submenu replaces it in the same frame, and the pointer can still reach it, instead of every move closing it
4
+ * and leaving the menu without one until the next opens.
5
+ */
6
+ export type SubmenuState = {
7
+ readonly status: "closed";
8
+ } | {
9
+ readonly status: "open";
10
+ readonly sub: string;
11
+ }
12
+ /** Focus left the trigger for another item of the menu; the submenu closes unless focus returns or another opens. */
13
+ | {
14
+ readonly status: "leaving";
15
+ readonly sub: string;
16
+ } | {
17
+ readonly status: "disposed";
18
+ };
19
+ export type SubmenuEvent =
20
+ /** Kobalte asked to open or close one submenu: a hover, click or key opened it, or Escape, ArrowLeft or a pick closed it. */
21
+ {
22
+ readonly type: "requested";
23
+ readonly sub: string;
24
+ readonly open: boolean;
25
+ }
26
+ /** Focus moved to another item of the menu that holds the open submenu's trigger, or to that menu itself. */
27
+ | {
28
+ readonly type: "focus.left";
29
+ }
30
+ /** Focus came back to the open submenu's trigger, or moved into the submenu. */
31
+ | {
32
+ readonly type: "focus.returned";
33
+ }
34
+ /** The delay that the given `leaving` state started ran out. */
35
+ | {
36
+ readonly type: "leave.elapsed";
37
+ readonly state: SubmenuState;
38
+ } | {
39
+ readonly type: "disposed";
40
+ };
41
+ export declare const SUBMENU_CLOSED: SubmenuState;
42
+ export declare const SUBMENU_FOCUS_LEFT: SubmenuEvent;
43
+ export declare const SUBMENU_FOCUS_RETURNED: SubmenuEvent;
44
+ export declare const SUBMENU_DISPOSED: SubmenuEvent;
45
+ export declare function submenuRequested(sub: string, open: boolean): SubmenuEvent;
46
+ export declare function submenuLeaveElapsed(state: SubmenuState): SubmenuEvent;
47
+ export declare function transitionSubmenu(state: SubmenuState, event: SubmenuEvent): SubmenuState;
48
+ export declare function selectSubmenuOpen(state: SubmenuState, sub: string): boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opencode/ui",
3
- "version": "0.0.0-dev-1288",
3
+ "version": "0.0.0-dev-1289",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -294,7 +294,7 @@
294
294
  "generate:v2-oc2": "bun run script/build-oc2-v2-overrides.ts"
295
295
  },
296
296
  "devDependencies": {
297
- "@hona/strobe": "0.35.0",
297
+ "@hona/strobe": "0.37.0",
298
298
  "@solidjs/meta": "0.29.4",
299
299
  "@tsconfig/node22": "22.0.2",
300
300
  "@types/bun": "1.4.3",
@@ -309,7 +309,7 @@
309
309
  "dependencies": {
310
310
  "@kitlangton/solid-motion": "0.2.2",
311
311
  "@kobalte/core": "0.13.13",
312
- "@opencode/util": "0.0.0-dev-1288",
312
+ "@opencode/util": "0.0.0-dev-1289",
313
313
  "@pierre/diffs": "1.6.1",
314
314
  "@pierre/highlights": "0.2.0",
315
315
  "@solid-primitives/event-listener": "2.4.6",
@@ -160,6 +160,11 @@
160
160
  }
161
161
  }
162
162
 
163
+ /* Like a native menu, an item stays highlighted while the pointer is in the submenu it opened. */
164
+ [data-component="menu-v2-content"]:not(:has([data-highlighted])) [data-component="menu-v2-item"][data-expanded] {
165
+ background: var(--menu-v2-hover);
166
+ }
167
+
163
168
  [data-slot="menu-v2-separator"] {
164
169
  height: 1px;
165
170
  width: calc(100% + 4px);
@@ -257,6 +262,10 @@
257
262
  }
258
263
  }
259
264
 
265
+ &:not(:has([data-highlighted])) [data-component="menu-v2-item"][data-expanded] {
266
+ background: var(--surface-raised-base-hover);
267
+ }
268
+
260
269
  [data-slot="menu-v2-separator"] {
261
270
  width: calc(100% + 8px);
262
271
  height: 1px;
@@ -1,10 +1,15 @@
1
1
  import { DropdownMenu, type DropdownMenuTriggerProps } from "@kobalte/core/dropdown-menu"
2
2
  import { ContextMenu } from "@kobalte/core/context-menu"
3
3
  import type { PolymorphicProps } from "@kobalte/core/polymorphic"
4
+ import { makeEventListener } from "@solid-primitives/event-listener"
4
5
  import {
5
6
  createComputed,
6
7
  createContext,
8
+ createEffect,
9
+ createMemo,
7
10
  createSignal,
11
+ createUniqueId,
12
+ on,
8
13
  onCleanup,
9
14
  Show,
10
15
  splitProps,
@@ -28,6 +33,18 @@ import {
28
33
  type MenuInputGesture,
29
34
  type MenuState,
30
35
  } from "./machine"
36
+ import {
37
+ selectSubmenuOpen,
38
+ SUBMENU_CLOSED,
39
+ SUBMENU_DISPOSED,
40
+ SUBMENU_FOCUS_LEFT,
41
+ SUBMENU_FOCUS_RETURNED,
42
+ submenuLeaveElapsed,
43
+ submenuRequested,
44
+ transitionSubmenu,
45
+ type SubmenuEvent,
46
+ type SubmenuState,
47
+ } from "./submenu-machine"
31
48
  import "./menu.css"
32
49
 
33
50
  const ChevronRight: Component = () => (
@@ -254,17 +271,112 @@ export interface MenuSubProps extends ComponentProps<typeof DropdownMenu.Sub> {
254
271
  | "right-end"
255
272
  }
256
273
 
274
+ // Longer than Kobalte's 100ms hover delay, so a submenu the pointer rests on replaces the leaving one in the same frame.
275
+ const SUBMENU_LEAVE_DELAY = 150
276
+
277
+ type SubmenuGroup = { state: Accessor<SubmenuState>; dispatch: (event: SubmenuEvent) => void }
278
+
279
+ const SubmenuGroupContext = createContext<SubmenuGroup>()
280
+
281
+ const SubmenuTriggerContext = createContext<(element: HTMLElement) => void>()
282
+
283
+ function createSubmenuGroup(): SubmenuGroup {
284
+ const [state, setState] = createSignal(SUBMENU_CLOSED)
285
+ let leave: ReturnType<typeof setTimeout> | undefined
286
+
287
+ const dispatch = (event: SubmenuEvent) => {
288
+ const next = transitionSubmenu(state(), event)
289
+
290
+ if (next === state()) return
291
+ clearTimeout(leave)
292
+
293
+ if (next.status === "leaving") leave = setTimeout(() => dispatch(submenuLeaveElapsed(next)), SUBMENU_LEAVE_DELAY)
294
+ setState(next)
295
+ }
296
+
297
+ onCleanup(() => {
298
+ clearTimeout(leave)
299
+ dispatch(SUBMENU_DISPOSED)
300
+ })
301
+
302
+ return { state, dispatch }
303
+ }
304
+
305
+ // Each menu surface owns the submenus it lists, and mounts this with its items so every open starts with none open.
306
+ function SubmenuGroupProvider(props: ParentProps) {
307
+ const group = createSubmenuGroup()
308
+
309
+ return <SubmenuGroupContext.Provider value={group}>{props.children}</SubmenuGroupContext.Provider>
310
+ }
311
+
312
+ // The menu surface decides which of its submenus is open. A controlled submenu stays with its owner instead.
257
313
  function MenuSub(props: MenuSubProps) {
258
- return <DropdownMenu.Sub {...props} />
314
+ const [local, rest] = splitProps(props, ["open", "defaultOpen", "onOpenChange"])
315
+ const group = useContext(SubmenuGroupContext) ?? createSubmenuGroup()
316
+ const id = createUniqueId()
317
+ const open = createMemo(() => selectSubmenuOpen(group.state(), id))
318
+ let trigger: HTMLElement | undefined
319
+
320
+ if (local.defaultOpen) group.dispatch(submenuRequested(id, true))
321
+ onCleanup(() => group.dispatch(submenuRequested(id, false)))
322
+
323
+ createEffect(
324
+ on(
325
+ open,
326
+ (value) => {
327
+ if (local.open === undefined) local.onOpenChange?.(value)
328
+ },
329
+ { defer: true },
330
+ ),
331
+ )
332
+
333
+ // Kobalte closes a submenu when focus leaves it, but ignores focus inside a top layer, and every menu is one so a modal
334
+ // dialog keeps it reachable. Focus moves go to the group instead, which waits for the next submenu or the pointer.
335
+ createEffect(() => {
336
+ if (!open()) return
337
+ makeEventListener(
338
+ document,
339
+ "focusin",
340
+ (event) => {
341
+ if (!(event.target instanceof Node) || !trigger) return
342
+
343
+ const left = !trigger.contains(event.target) && !!trigger.closest('[role="menu"]')?.contains(event.target)
344
+ group.dispatch(left ? SUBMENU_FOCUS_LEFT : SUBMENU_FOCUS_RETURNED)
345
+ },
346
+ { capture: true },
347
+ )
348
+ })
349
+
350
+ return (
351
+ <SubmenuTriggerContext.Provider value={(element) => (trigger = element)}>
352
+ <DropdownMenu.Sub
353
+ {...rest}
354
+ open={local.open ?? open()}
355
+ onOpenChange={(next) => {
356
+ if (local.open !== undefined) return local.onOpenChange?.(next)
357
+ group.dispatch(submenuRequested(id, next))
358
+ }}
359
+ />
360
+ </SubmenuTriggerContext.Provider>
361
+ )
259
362
  }
260
363
 
261
364
  function MenuSubTrigger(props: ParentProps<MenuSubTriggerProps>) {
262
365
  const ctx = useMenuContext()
263
- const [s, r] = splitProps(props, ["class", "classList", "children", "shortcut", "badge"])
366
+ const registerTrigger = useContext(SubmenuTriggerContext)
367
+ const [s, r] = splitProps(props, ["class", "classList", "children", "shortcut", "badge", "ref"])
368
+
369
+ const assignRef = (element: HTMLElement) => {
370
+ registerTrigger?.(element)
371
+ const ref = s.ref
372
+
373
+ if (ref instanceof Function) ref(element)
374
+ }
264
375
 
265
376
  return (
266
377
  <DropdownMenu.SubTrigger
267
378
  {...r}
379
+ ref={assignRef}
268
380
  data-component="menu-v2-item"
269
381
  data-appearance={ctx.appearance()}
270
382
  classList={{ ...s.classList, [s.class ?? ""]: !!s.class }}
@@ -278,7 +390,7 @@ function MenuSubTrigger(props: ParentProps<MenuSubTriggerProps>) {
278
390
 
279
391
  function MenuSubContent(props: ComponentProps<typeof DropdownMenu.SubContent>) {
280
392
  const ctx = useMenuContext()
281
- const [s, r] = splitProps(props, ["class", "classList"])
393
+ const [s, r] = splitProps(props, ["class", "classList", "children"])
282
394
 
283
395
  return (
284
396
  <DropdownMenu.SubContent
@@ -287,7 +399,9 @@ function MenuSubContent(props: ComponentProps<typeof DropdownMenu.SubContent>) {
287
399
  data-component="menu-v2-content"
288
400
  data-appearance={ctx.appearance()}
289
401
  classList={{ ...s.classList, [s.class ?? ""]: !!s.class }}
290
- />
402
+ >
403
+ <SubmenuGroupProvider>{s.children}</SubmenuGroupProvider>
404
+ </DropdownMenu.SubContent>
291
405
  )
292
406
  }
293
407
 
@@ -329,7 +443,7 @@ function MenuTrigger<T extends ValidComponent = "button">(props: PolymorphicProp
329
443
 
330
444
  function MenuContent(props: ComponentProps<typeof DropdownMenu.Content>) {
331
445
  const ctx = useMenuContext()
332
- const [s, r] = splitProps(props, ["class", "classList", "onCloseAutoFocus", "onInteractOutside"])
446
+ const [s, r] = splitProps(props, ["class", "classList", "children", "onCloseAutoFocus", "onInteractOutside"])
333
447
 
334
448
  // Kobalte's own rule for when an outside press keeps focus where it landed.
335
449
  const onInteractOutside: NonNullable<typeof s.onInteractOutside> = (event) => {
@@ -366,7 +480,9 @@ function MenuContent(props: ComponentProps<typeof DropdownMenu.Content>) {
366
480
  data-component="menu-v2-content"
367
481
  data-appearance={ctx.appearance()}
368
482
  classList={{ ...s.classList, [s.class ?? ""]: !!s.class }}
369
- />
483
+ >
484
+ <SubmenuGroupProvider>{s.children}</SubmenuGroupProvider>
485
+ </DropdownMenu.Content>
370
486
  )
371
487
  }
372
488
 
@@ -433,7 +549,7 @@ function MenuContextRoot(props: MenuContextProps) {
433
549
 
434
550
  function MenuContextContent(props: ComponentProps<typeof ContextMenu.Content>) {
435
551
  const ctx = useMenuContext()
436
- const [s, r] = splitProps(props, ["class", "classList"])
552
+ const [s, r] = splitProps(props, ["class", "classList", "children"])
437
553
 
438
554
  return (
439
555
  <ContextMenu.Content
@@ -441,7 +557,9 @@ function MenuContextContent(props: ComponentProps<typeof ContextMenu.Content>) {
441
557
  data-component="menu-v2-content"
442
558
  data-appearance={ctx.appearance()}
443
559
  classList={{ ...s.classList, [s.class ?? ""]: !!s.class }}
444
- />
560
+ >
561
+ <SubmenuGroupProvider>{s.children}</SubmenuGroupProvider>
562
+ </ContextMenu.Content>
445
563
  )
446
564
  }
447
565
 
@@ -0,0 +1,67 @@
1
+ /**
2
+ * A menu opens at most one submenu at a time. A submenu stays open while focus crosses the other items of its menu, so
3
+ * the next submenu replaces it in the same frame, and the pointer can still reach it, instead of every move closing it
4
+ * and leaving the menu without one until the next opens.
5
+ */
6
+ export type SubmenuState =
7
+ | { readonly status: "closed" }
8
+ | { readonly status: "open"; readonly sub: string }
9
+ /** Focus left the trigger for another item of the menu; the submenu closes unless focus returns or another opens. */
10
+ | { readonly status: "leaving"; readonly sub: string }
11
+ | { readonly status: "disposed" }
12
+
13
+ export type SubmenuEvent =
14
+ /** Kobalte asked to open or close one submenu: a hover, click or key opened it, or Escape, ArrowLeft or a pick closed it. */
15
+ | { readonly type: "requested"; readonly sub: string; readonly open: boolean }
16
+ /** Focus moved to another item of the menu that holds the open submenu's trigger, or to that menu itself. */
17
+ | { readonly type: "focus.left" }
18
+ /** Focus came back to the open submenu's trigger, or moved into the submenu. */
19
+ | { readonly type: "focus.returned" }
20
+ /** The delay that the given `leaving` state started ran out. */
21
+ | { readonly type: "leave.elapsed"; readonly state: SubmenuState }
22
+ | { readonly type: "disposed" }
23
+
24
+ export const SUBMENU_CLOSED: SubmenuState = Object.freeze({ status: "closed" })
25
+
26
+ const DISPOSED: SubmenuState = Object.freeze({ status: "disposed" })
27
+
28
+ export const SUBMENU_FOCUS_LEFT: SubmenuEvent = Object.freeze({ type: "focus.left" })
29
+
30
+ export const SUBMENU_FOCUS_RETURNED: SubmenuEvent = Object.freeze({ type: "focus.returned" })
31
+
32
+ export const SUBMENU_DISPOSED: SubmenuEvent = Object.freeze({ type: "disposed" })
33
+
34
+ export function submenuRequested(sub: string, open: boolean): SubmenuEvent {
35
+ return Object.freeze({ type: "requested", sub, open })
36
+ }
37
+
38
+ export function submenuLeaveElapsed(state: SubmenuState): SubmenuEvent {
39
+ return Object.freeze({ type: "leave.elapsed", state })
40
+ }
41
+
42
+ export function transitionSubmenu(state: SubmenuState, event: SubmenuEvent): SubmenuState {
43
+ if (state.status === "disposed") return state
44
+
45
+ if (event.type === "disposed") return DISPOSED
46
+
47
+ if (event.type === "requested") {
48
+ if (event.open) return state.status === "open" && state.sub === event.sub ? state : open(event.sub)
49
+
50
+ return state.status !== "closed" && state.sub === event.sub ? SUBMENU_CLOSED : state
51
+ }
52
+
53
+ if (state.status === "closed") return state
54
+
55
+ // Each move away is a new state that restarts the delay, so the submenu outlives a pointer still crossing items.
56
+ if (event.type === "focus.left") return Object.freeze({ status: "leaving", sub: state.sub })
57
+
58
+ if (event.type === "focus.returned") return state.status === "leaving" ? open(state.sub) : state
59
+
60
+ return event.state === state ? SUBMENU_CLOSED : state
61
+ }
62
+
63
+ export function selectSubmenuOpen(state: SubmenuState, sub: string) {
64
+ return (state.status === "open" || state.status === "leaving") && state.sub === sub
65
+ }
66
+
67
+ const open = (sub: string): SubmenuState => Object.freeze({ status: "open", sub })