@opencode/ui 0.0.0-dev-583 → 0.0.0-dev-605

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.
@@ -1,30 +1,45 @@
1
1
  import { createResizeObserver } from "@solid-primitives/resize-observer"
2
- import { onMount, Show, splitProps, type ComponentProps } from "solid-js"
2
+ import { onCleanup, onMount, Show, splitProps, type ComponentProps, type JSX } from "solid-js"
3
3
  import { createStore } from "solid-js/store"
4
4
  import "./menu-scroll.css"
5
5
 
6
+ /**
7
+ * A scroll container for menus, popovers and dialog lists. It hides the native scrollbar and draws the shared thumb
8
+ * over the content, so rows keep equal start and end padding whether or not the list overflows. Touch devices and
9
+ * high-contrast modes keep the native scrollbar.
10
+ */
6
11
  export function MenuScroll(
7
- props: Omit<ComponentProps<"div">, "ref" | "onScroll"> & {
12
+ props: Omit<ComponentProps<"div">, "ref" | "onScroll" | "onFocusIn"> & {
8
13
  viewportClass?: string
9
14
  viewportRef?: (element: HTMLDivElement) => void
15
+ onScroll?: JSX.EventHandler<HTMLDivElement, Event>
10
16
  },
11
17
  ) {
12
- const [local, rest] = splitProps(props, ["class", "children", "viewportClass", "viewportRef"])
18
+ const [local, rest] = splitProps(props, ["class", "children", "viewportClass", "viewportRef", "onScroll"])
13
19
 
14
- const [state, setState] = createStore<{ size: number; start: number; overflow: boolean; grab?: number }>({
20
+ const [state, setState] = createStore<{
21
+ size: number
22
+ start: number
23
+ overflow: boolean
24
+ scrolling: boolean
25
+ grab?: number
26
+ }>({
15
27
  size: 0,
16
28
  start: 4,
17
29
  overflow: false,
30
+ scrolling: false,
18
31
  })
19
32
 
20
33
  let viewport!: HTMLDivElement
21
34
 
22
35
  let content!: HTMLDivElement
23
36
 
37
+ let idle: ReturnType<typeof setTimeout> | undefined
38
+
24
39
  const measure = () => {
25
40
  const track = Math.max(0, viewport.clientHeight - 8)
26
41
 
27
- const size = Math.min(track, Math.max(32, (viewport.clientHeight / Math.max(1, viewport.scrollHeight)) * track))
42
+ const size = Math.min(track, Math.max(24, (viewport.clientHeight / Math.max(1, viewport.scrollHeight)) * track))
28
43
 
29
44
  const max = viewport.scrollHeight - viewport.clientHeight
30
45
 
@@ -33,27 +48,46 @@ export function MenuScroll(
33
48
  if (max <= 0) setState("grab", undefined)
34
49
  }
35
50
 
36
- onMount(() => {
37
- local.viewportRef?.(viewport)
38
- createResizeObserver([viewport, content], measure)
39
- })
51
+ onMount(() => createResizeObserver([viewport, content], measure))
52
+
53
+ onCleanup(() => clearTimeout(idle))
40
54
 
41
55
  return (
42
- <div data-component="menu-scroll" class={local.class}>
56
+ <div
57
+ data-component="menu-scroll"
58
+ data-scrolling={state.scrolling ? "" : undefined}
59
+ data-dragging={state.grab !== undefined ? "" : undefined}
60
+ class={local.class}
61
+ >
43
62
  <div
44
- {...rest}
45
- ref={viewport}
46
63
  data-slot="menu-scroll-viewport"
64
+ {...rest}
65
+ ref={(element) => {
66
+ viewport = element
67
+ local.viewportRef?.(element)
68
+ }}
47
69
  data-scrollable
48
70
  class={local.viewportClass}
49
- onScroll={measure}
71
+ onScroll={(event) => {
72
+ measure()
73
+ // Keyboard navigation scrolls without hovering, so the thumb also shows while the list scrolls.
74
+ setState("scrolling", true)
75
+ clearTimeout(idle)
76
+ idle = setTimeout(() => setState("scrolling", false), 800)
77
+ local.onScroll?.(event)
78
+ }}
79
+ // Kobalte menus focus items without scrolling and scroll their own content, which no longer overflows here.
80
+ onFocusIn={(event) => {
81
+ if (event.target !== viewport) event.target.scrollIntoView({ block: "nearest", inline: "nearest" })
82
+ }}
50
83
  >
51
- <div ref={content}>{local.children}</div>
84
+ <div ref={content} data-slot="menu-scroll-content">
85
+ {local.children}
86
+ </div>
52
87
  </div>
53
88
  <Show when={state.overflow}>
54
89
  <div
55
90
  data-slot="menu-scroll-thumb"
56
- data-dragging={state.grab !== undefined ? "" : undefined}
57
91
  aria-hidden="true"
58
92
  style={{ height: `${state.size}px`, transform: `translateY(${state.start}px)` }}
59
93
  onPointerDown={(event) => {
@@ -56,11 +56,14 @@
56
56
  }
57
57
  }
58
58
 
59
+ [data-slot="search-list-body"] > [data-component="menu-scroll"] {
60
+ flex: 1;
61
+ }
62
+
59
63
  /* Arrow keys scroll the active row clear of the sticky header. Rows fade out past the bottom edge while more follow. */
60
64
  [data-slot="search-list-scroll"] {
61
65
  min-height: 0;
62
66
  flex: 1;
63
- overflow-y: auto;
64
67
  --search-list-edge: linear-gradient(
65
68
  to bottom,
66
69
  #000 calc(100% - 24px),
@@ -209,6 +212,7 @@
209
212
  box-shadow: var(--v2-elevation-floating);
210
213
  }
211
214
 
215
+ [data-slot="search-list-body"] > [data-component="menu-scroll"],
212
216
  [data-slot="search-list-scroll"] {
213
217
  flex: 0 1 auto;
214
218
  }
@@ -12,6 +12,7 @@ import {
12
12
  } from "solid-js"
13
13
  import { createResizeObserver } from "@solid-primitives/resize-observer"
14
14
  import { Icon } from "@opencode/ui/icon"
15
+ import { MenuScroll } from "../../components/menu-scroll"
15
16
  import { TextInput } from "../text-input/text-input"
16
17
  import { createTypeahead } from "../typeahead/controller"
17
18
  import { searchListMove, searchListTarget } from "./navigation"
@@ -307,10 +308,9 @@ export function SearchList<T>(props: {
307
308
  {(custom) => custom()(field)}
308
309
  </Show>
309
310
  <div data-slot="search-list-body" hidden={!open()}>
310
- <div
311
+ <MenuScroll
311
312
  data-slot="search-list-scroll"
312
- data-scrollable
313
- ref={(element) => (viewport = element)}
313
+ viewportRef={(element) => (viewport = element)}
314
314
  onScroll={(event) => {
315
315
  edges()
316
316
  props.onScroll?.(event.currentTarget.scrollTop)
@@ -364,7 +364,7 @@ export function SearchList<T>(props: {
364
364
  <Show when={rows().length === 0}>
365
365
  <div data-slot="search-list-empty">{props.empty}</div>
366
366
  </Show>
367
- </div>
367
+ </MenuScroll>
368
368
  <Show when={props.footer}>
369
369
  <div data-slot="search-list-footer">{props.footer}</div>
370
370
  </Show>
@@ -233,6 +233,14 @@
233
233
  padding: 0;
234
234
  }
235
235
 
236
+ [data-component="menu-v2-content"][data-slot="select-v2-content"] > [data-component="menu-scroll"] {
237
+ max-height: 12rem;
238
+
239
+ > [data-scrollable] {
240
+ overflow-x: hidden;
241
+ }
242
+ }
243
+
236
244
  /* Listbox inside menu surface */
237
245
  [data-component="menu-v2-content"][data-slot="select-v2-content"] [data-slot="select-v2-listbox"] {
238
246
  box-sizing: border-box;
@@ -244,9 +252,6 @@
244
252
  list-style: none;
245
253
  min-width: 0;
246
254
  width: 100%;
247
- max-height: 12rem;
248
- overflow-x: hidden;
249
- overflow-y: auto;
250
255
  outline: none;
251
256
  white-space: nowrap;
252
257
 
@@ -11,6 +11,7 @@ import {
11
11
  Value,
12
12
  } from "@kobalte/core/select"
13
13
  import { Show, createMemo, onCleanup, splitProps, type ComponentProps, type JSX } from "solid-js"
14
+ import { MenuScroll } from "../../components/menu-scroll"
14
15
  import "./select.css"
15
16
 
16
17
  function groupOptions<T>(options: T[], groupBy?: (x: T) => string): { category: string; options: T[] }[] {
@@ -112,6 +113,8 @@ export function Select<T>(props: SelectProps<T>) {
112
113
 
113
114
  const state: HighlightState = {}
114
115
 
116
+ let scroller: HTMLDivElement | undefined
117
+
115
118
  const stop = () => {
116
119
  state.cleanup?.()
117
120
  state.cleanup = undefined
@@ -231,7 +234,9 @@ export function Select<T>(props: SelectProps<T>) {
231
234
  </Trigger>
232
235
  <Portal>
233
236
  <Content class={local.contentClass} data-component="menu-v2-content" data-slot="select-v2-content">
234
- <Listbox data-slot="select-v2-listbox" />
237
+ <MenuScroll viewportRef={(element) => (scroller = element)}>
238
+ <Listbox data-slot="select-v2-listbox" scrollRef={() => scroller} />
239
+ </MenuScroll>
235
240
  </Content>
236
241
  </Portal>
237
242
  </Root>
@@ -0,0 +1,301 @@
1
+ import fuzzysort from "fuzzysort"
2
+
3
+ export type ListGroup<T> = { category: string; items: T[] }
4
+
5
+ export type ListConfig<T> = {
6
+ readonly key: (item: T) => string
7
+ readonly filterKeys?: string[]
8
+ readonly skipFilter?: (item: T) => boolean
9
+ readonly groupBy?: (item: T) => string
10
+ readonly sortBy?: (a: T, b: T) => number
11
+ readonly sortGroupsBy?: (a: ListGroup<T>, b: ListGroup<T>) => number
12
+ }
13
+
14
+ /** The one copy of the query. A `controlled` list shows the parent's `filter` prop and only reports edits. */
15
+ export type ListFilter = { readonly type: "uncontrolled" | "controlled"; readonly query: string }
16
+
17
+ /** `mouse` while the pointer drives the highlight, so the list does not scroll under it. */
18
+ export type ListHighlight = { readonly mode: "keyboard" | "mouse"; readonly activeKey: string | null }
19
+
20
+ type ListData<T> = {
21
+ readonly filter: ListFilter
22
+ readonly groups: ListGroup<T>[]
23
+ readonly rows: T[]
24
+ readonly keys: string[]
25
+ readonly highlight: ListHighlight
26
+ }
27
+
28
+ /** `loading` keeps the last rows on screen; `epoch` is the last attempt that settled. */
29
+ export type ListState<T> =
30
+ | (ListData<T> & { readonly status: "ready"; readonly epoch: number })
31
+ | (ListData<T> & { readonly status: "loading"; readonly attempt: number })
32
+
33
+ export type ListKeyDown = {
34
+ readonly type: "key.down"
35
+ readonly key: string
36
+ readonly ctrl: boolean
37
+ readonly alt: boolean
38
+ readonly meta: boolean
39
+ readonly shift: boolean
40
+ readonly composing: boolean
41
+ /** A search input keeps Home, End and Tab for the text field. */
42
+ readonly search: boolean
43
+ /** Escape, or a key the caller's `onKeyEvent` already handled. */
44
+ readonly claimed: boolean
45
+ }
46
+
47
+ export type ListEvent<T> =
48
+ | { readonly type: "query.change"; readonly value: string; readonly force?: boolean }
49
+ | { readonly type: "filter.controlled"; readonly value: string }
50
+ | { readonly type: "filter.uncontrolled" }
51
+ | { readonly type: "items.reload" }
52
+ | { readonly type: "load.resolved"; readonly attempt: number; readonly items: T[] }
53
+ | { readonly type: "load.failed"; readonly attempt: number }
54
+ | { readonly type: "pointer.move"; readonly key: string; readonly moved: boolean }
55
+ | { readonly type: "pointer.leave" }
56
+ | { readonly type: "active.set"; readonly key: string | null }
57
+ | { readonly type: "active.reset" }
58
+ | ListKeyDown
59
+
60
+ export type ListEffect<T> =
61
+ | { readonly type: "load"; readonly attempt: number; readonly query: string }
62
+ | { readonly type: "notify.filter"; readonly value: string }
63
+ | { readonly type: "select"; readonly item: T; readonly index: number }
64
+
65
+ export type ListTransition<T> = {
66
+ readonly state: ListState<T>
67
+ readonly effects: ListEffect<T>[]
68
+ readonly handled: boolean
69
+ }
70
+
71
+ export function createListState<T>(filter?: string): ListState<T> {
72
+ return {
73
+ status: "ready",
74
+ epoch: 0,
75
+ filter: filter === undefined ? { type: "uncontrolled", query: "" } : { type: "controlled", query: filter },
76
+ groups: [],
77
+ rows: [],
78
+ keys: [],
79
+ highlight: { mode: "keyboard", activeKey: null },
80
+ }
81
+ }
82
+
83
+ export function transitionList<T>(state: ListState<T>, event: ListEvent<T>, config: ListConfig<T>): ListTransition<T> {
84
+ if (event.type === "query.change") return queryChanged(state, event.value, event.force === true)
85
+
86
+ if (event.type === "filter.controlled") {
87
+ if (event.value !== state.filter.query) return load(state, { type: "controlled", query: event.value })
88
+
89
+ if (state.filter.type === "controlled") return unchanged(state)
90
+
91
+ return changed({ ...state, filter: { type: "controlled", query: event.value } })
92
+ }
93
+
94
+ if (event.type === "filter.uncontrolled") {
95
+ if (state.filter.type === "uncontrolled") return unchanged(state)
96
+
97
+ return changed({ ...state, filter: { type: "uncontrolled", query: state.filter.query } })
98
+ }
99
+
100
+ if (event.type === "items.reload") return load(state, state.filter)
101
+
102
+ if (event.type === "load.resolved" || event.type === "load.failed") {
103
+ if (state.status !== "loading" || event.attempt !== state.attempt) return unchanged(state)
104
+ const groups = event.type === "load.resolved" ? filterItems(event.items, state.filter.query, config) : []
105
+ const rows = groups.flatMap((group) => group.items)
106
+ const keys = rows.map(config.key)
107
+
108
+ return changed({
109
+ status: "ready",
110
+ epoch: state.attempt,
111
+ filter: state.filter,
112
+ groups,
113
+ rows,
114
+ keys,
115
+ // Each result set opens on its first row; `current` only marks the selected item.
116
+ highlight: { mode: state.highlight.mode, activeKey: keys[0] ?? null },
117
+ })
118
+ }
119
+
120
+ if (event.type === "pointer.move") {
121
+ if (!event.moved || (state.highlight.mode === "mouse" && state.highlight.activeKey === event.key))
122
+ return unchanged(state)
123
+
124
+ return changed({ ...state, highlight: { mode: "mouse", activeKey: event.key } })
125
+ }
126
+
127
+ if (event.type === "pointer.leave") {
128
+ if (state.highlight.mode === "keyboard" || state.highlight.activeKey === null) return unchanged(state)
129
+
130
+ return changed({ ...state, highlight: { mode: "mouse", activeKey: null } })
131
+ }
132
+
133
+ if (event.type === "active.set") return highlight(state, event.key || null)
134
+
135
+ if (event.type === "active.reset") return state.keys.length ? highlight(state, state.keys[0]) : unchanged(state)
136
+
137
+ return keyDown(state, event)
138
+ }
139
+
140
+ /** The highlighted row and its index among all rows. */
141
+ export function listActive<T>(state: Pick<ListState<T>, "rows" | "keys" | "highlight">) {
142
+ const index = state.highlight.activeKey === null ? -1 : state.keys.indexOf(state.highlight.activeKey)
143
+
144
+ return index < 0 ? undefined : { item: state.rows[index], index }
145
+ }
146
+
147
+ /** Where the list scrolls to keep the keyboard highlight in view; the pointer never scrolls it. */
148
+ export function listScrollTarget<T>(
149
+ state: Pick<ListState<T>, "keys" | "highlight">,
150
+ ): { type: "none" } | { type: "top" } | { type: "item"; key: string } {
151
+ const key = state.highlight.activeKey
152
+
153
+ if (state.highlight.mode === "mouse" || key === null || state.keys.length === 0) return { type: "none" }
154
+
155
+ if (key === state.keys[0]) return { type: "top" }
156
+
157
+ return { type: "item", key }
158
+ }
159
+
160
+ export function groupItems<T>(items: T[], config: Pick<ListConfig<T>, "groupBy" | "sortBy" | "sortGroupsBy">) {
161
+ // A plain object keeps the group order of `Object.entries`, where numeric-looking categories sort first.
162
+ const byCategory: Record<string, T[]> = {}
163
+
164
+ for (const item of items) {
165
+ const category = config.groupBy ? config.groupBy(item) : ""
166
+ byCategory[category] ??= []
167
+ byCategory[category].push(item)
168
+ }
169
+
170
+ const groups = Object.entries(byCategory).map(([category, group]) => ({
171
+ category,
172
+ items: config.sortBy ? group.sort(config.sortBy) : group,
173
+ }))
174
+
175
+ return config.sortGroupsBy ? groups.sort(config.sortGroupsBy) : groups
176
+ }
177
+
178
+ function queryChanged<T>(state: ListState<T>, value: string, force: boolean): ListTransition<T> {
179
+ const same = value === state.filter.query
180
+
181
+ if (same && !force) return unchanged(state)
182
+ const notify: ListEffect<T> = { type: "notify.filter", value }
183
+
184
+ // A controlled list waits for the parent to pass the new `filter` back.
185
+ if (state.filter.type === "controlled" && !same) return unchanged(state, [notify])
186
+ const next = load(state, { type: state.filter.type, query: value })
187
+
188
+ return { ...next, effects: [notify, ...next.effects] }
189
+ }
190
+
191
+ function load<T>(state: ListState<T>, filter: ListFilter): ListTransition<T> {
192
+ const attempt = (state.status === "loading" ? state.attempt : state.epoch) + 1
193
+
194
+ return changed(
195
+ {
196
+ status: "loading",
197
+ attempt,
198
+ filter,
199
+ groups: state.groups,
200
+ rows: state.rows,
201
+ keys: state.keys,
202
+ highlight: state.highlight,
203
+ },
204
+ [{ type: "load", attempt, query: filter.query }],
205
+ )
206
+ }
207
+
208
+ function highlight<T>(state: ListState<T>, activeKey: string | null): ListTransition<T> {
209
+ if (activeKey === state.highlight.activeKey) return unchanged(state)
210
+
211
+ return changed({ ...state, highlight: { mode: state.highlight.mode, activeKey } })
212
+ }
213
+
214
+ function keyDown<T>(state: ListState<T>, event: ListKeyDown): ListTransition<T> {
215
+ const next: ListState<T> =
216
+ state.highlight.mode === "keyboard" ? state : { ...state, highlight: { ...state.highlight, mode: "keyboard" } }
217
+
218
+ if (event.claimed) return changed(next)
219
+
220
+ if (event.key === "Enter" && !event.composing) {
221
+ const active = listActive(next)
222
+
223
+ return changed(next, active ? [{ type: "select", item: active.item, index: active.index }] : [], true)
224
+ }
225
+
226
+ const move = navigate(event, next.keys, next.highlight.activeKey)
227
+
228
+ if (!move) return changed(next)
229
+
230
+ if (move.key === undefined) return changed(next, [], true)
231
+
232
+ return changed({ ...next, highlight: { mode: "keyboard", activeKey: move.key } }, [], true)
233
+ }
234
+
235
+ function navigate(event: ListKeyDown, keys: string[], active: string | null): { key?: string } | undefined {
236
+ const index = active === null ? -1 : keys.indexOf(active)
237
+
238
+ if (event.ctrl && !event.meta && !event.alt && !event.shift) {
239
+ if (event.key !== "n" && event.key !== "p") return
240
+
241
+ return { key: step(keys, index, event.key === "n" ? 1 : -1) }
242
+ }
243
+
244
+ // Option+Arrow and Cmd+Arrow edit text on macOS.
245
+ if (event.alt || event.meta || keys.length === 0) return
246
+
247
+ if (event.key === "ArrowDown") return { key: step(keys, index, 1) }
248
+
249
+ if (event.key === "ArrowUp") return { key: step(keys, index, -1) }
250
+
251
+ if (event.search) return
252
+
253
+ if (event.key === "Home") return { key: keys[0] }
254
+
255
+ if (event.key === "End") return { key: keys[keys.length - 1] }
256
+
257
+ if (event.key !== "Tab" || index < 0) return
258
+ const next = index + (event.shift ? -1 : 1)
259
+
260
+ if (next < 0 || next >= keys.length) return
261
+
262
+ return { key: keys[next] }
263
+ }
264
+
265
+ function step(keys: string[], index: number, direction: 1 | -1) {
266
+ if (keys.length === 0) return
267
+
268
+ if (index < 0) return direction === 1 ? keys[0] : keys[keys.length - 1]
269
+
270
+ return keys[(index + direction + keys.length) % keys.length]
271
+ }
272
+
273
+ function filterItems<T>(items: T[], query: string, config: ListConfig<T>) {
274
+ const needle = query.toLowerCase()
275
+
276
+ return groupItems(needle ? search(items, needle, config) : items, config)
277
+ }
278
+
279
+ function search<T>(all: T[], needle: string, config: ListConfig<T>) {
280
+ const skipFilter = config.skipFilter
281
+ const filterable = skipFilter ? all.filter((item) => !skipFilter(item)) : all
282
+ const skipped = skipFilter ? all.filter(skipFilter) : []
283
+
284
+ // oxlint-disable-next-line anti-slop/no-runtime-typeof -- plain string lists have no keys to search by
285
+ const strings = !config.filterKeys && filterable.every((e) => typeof e === "string")
286
+
287
+ // SAFETY: when `strings` holds, every element is a string, so the matched targets are the items.
288
+ const filtered = strings
289
+ ? (fuzzysort.go(needle, filterable as string[]).map((x) => x.target) as T[])
290
+ : fuzzysort.go(needle, filterable, { keys: config.filterKeys! }).map((x) => x.obj)
291
+
292
+ return skipped.length ? [...filtered, ...skipped] : filtered
293
+ }
294
+
295
+ function changed<T>(state: ListState<T>, effects: ListEffect<T>[] = [], handled = false): ListTransition<T> {
296
+ return { state, effects, handled }
297
+ }
298
+
299
+ function unchanged<T>(state: ListState<T>, effects: ListEffect<T>[] = []): ListTransition<T> {
300
+ return { state, effects, handled: false }
301
+ }