@opencode/ui 0.0.0-dev-44 → 0.0.0-dev-51

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.
@@ -11,6 +11,18 @@ export type SearchListRow = {
11
11
  onMouseDown: (event: MouseEvent) => void;
12
12
  onClick: () => void;
13
13
  };
14
+ /** Attributes for the input of a field passed as `field`: spread them onto it. */
15
+ export type SearchListField = {
16
+ role: "combobox";
17
+ "aria-expanded": boolean;
18
+ "aria-autocomplete": "list";
19
+ "aria-controls": string | undefined;
20
+ "aria-activedescendant": string | undefined;
21
+ onInput: (event: InputEvent & {
22
+ currentTarget: HTMLInputElement;
23
+ }) => void;
24
+ onKeyDown: (event: KeyboardEvent) => void;
25
+ };
14
26
  export type SearchListHandle<T> = {
15
27
  active: () => T | undefined;
16
28
  setActive: (key: string | undefined) => void;
@@ -19,13 +31,22 @@ export type SearchListHandle<T> = {
19
31
  * A search field over sectioned results. Rows that share a `section` key list together under one header, which sticks
20
32
  * while its rows scroll beneath it until the next header pushes it away. The first row is active until the user picks
21
33
  * another: ↑/↓ wrap, ⌘ or ⌥ with ↑/↓ jump between sections, Home/End go to the ends, Enter selects and Escape closes.
34
+ * A page can keep its own field through `field` and float the results beneath it with `popover`.
22
35
  */
23
36
  export declare function SearchList<T>(props: {
24
37
  query: string;
25
38
  onQuery: (query: string) => void;
26
39
  placeholder: string;
40
+ /** The results' accessible name; the placeholder by default. */
41
+ label?: string;
27
42
  /** Controls after the field, such as icon buttons. */
28
43
  trailing?: JSX.Element;
44
+ /** A search field of the page's own in place of the built-in one. */
45
+ field?: (field: SearchListField) => JSX.Element;
46
+ /** Whether the results show. Closed, only the field remains and the list leaves keys to the page. On by default. */
47
+ open?: boolean;
48
+ /** Float the results beneath the field on a raised surface, over what follows it. */
49
+ popover?: boolean;
29
50
  items: T[];
30
51
  key: (item: T) => string;
31
52
  section?: (item: T) => string;
@@ -35,6 +56,11 @@ export declare function SearchList<T>(props: {
35
56
  header?: (item: T) => JSX.Element;
36
57
  /** Shown in place of the rows when there are none. */
37
58
  empty: JSX.Element;
59
+ /** A note beneath the rows, such as a hint that more results are hidden. */
60
+ footer?: JSX.Element;
61
+ /** Where the results start scrolled each time they mount, such as a position kept from an earlier visit. */
62
+ scrollTop?: number;
63
+ onScroll?: (scrollTop: number) => void;
38
64
  onSelect: (item: T) => void;
39
65
  onActive?: (item: T | undefined) => void;
40
66
  /** Runs before the list's own keys; call `preventDefault` to claim one. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opencode/ui",
3
- "version": "0.0.0-dev-44",
3
+ "version": "0.0.0-dev-51",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -286,7 +286,7 @@
286
286
  "dependencies": {
287
287
  "@kitlangton/solid-motion": "0.2.2",
288
288
  "@kobalte/core": "0.13.13",
289
- "@opencode/util": "0.0.0-dev-44",
289
+ "@opencode/util": "0.0.0-dev-51",
290
290
  "@pierre/diffs": "1.6.1",
291
291
  "@pierre/highlights": "0.2.0",
292
292
  "@solid-primitives/event-listener": "2.4.6",
@@ -16,5 +16,9 @@
16
16
 
17
17
  [data-slot="rolling-number-glyph"][data-leaving] {
18
18
  pointer-events: none;
19
+
20
+ &::before {
21
+ content: attr(data-leaving);
22
+ }
19
23
  }
20
24
  }
@@ -46,12 +46,14 @@ function Digit(props: { digit: string; dir: number; spring: DurationOptions }) {
46
46
  let from = 0
47
47
  let to = 0
48
48
 
49
+ const progress = () => (spring ? Math.max(0, Math.min(1, spring.value)) : 1)
50
+
49
51
  const paint = () => {
50
- const p = spring ? Math.max(0, Math.min(1, spring.value)) : 1
52
+ const p = progress()
51
53
  roll(incoming, enter * (1 - p))
52
54
  roll(outgoing, from + (to - from) * p)
53
55
 
54
- if (!spring && outgoing) outgoing.textContent = ""
56
+ if (!spring && outgoing) outgoing.dataset.leaving = ""
55
57
  }
56
58
 
57
59
  const tick = (dt: number) => {
@@ -76,20 +78,30 @@ function Digit(props: { digit: string; dir: number; spring: DurationOptions }) {
76
78
  last = next
77
79
 
78
80
  if (!incoming || !outgoing || next === prev) return
79
- incoming.textContent = next
80
81
 
81
82
  if (prefersReducedMotion() || !incoming.isConnected) {
83
+ incoming.textContent = next
82
84
  spring = undefined
83
85
  paint()
84
86
 
85
87
  return
86
88
  }
87
89
 
88
- // A change mid-roll sends the digit on screen out from where it is.
89
- from = enter * (1 - (spring ? Math.max(0, Math.min(1, spring.value)) : 1))
90
+ // A change mid-roll carries on from where both digits are, so together they never show less than one digit.
91
+ // Going back to the digit still leaving brings it back from where it is, and the arriving one leaves the way it
92
+ // came. Otherwise the digit nearer the middle leaves from there, and the new one comes in as far out as the
93
+ // other was, so a digit that had barely appeared never crosses the middle.
94
+ const p = progress()
95
+ const now = { in: enter * (1 - p), out: from + (to - from) * p }
96
+ const back = !!spring && next === outgoing.dataset.leaving
97
+ const near = back || Math.abs(now.in) <= Math.abs(now.out)
98
+ const far = !spring ? 1 : Math.min(1, Math.abs(near ? now.out : now.in))
99
+
100
+ if (near) outgoing.dataset.leaving = prev
101
+ from = near ? now.in : now.out
90
102
  to = -props.dir
91
- enter = props.dir
92
- outgoing.textContent = prev
103
+ enter = back ? now.out : props.dir * far
104
+ incoming.textContent = next
93
105
  spring = Spring.fromDuration(0, props.spring).set(1)
94
106
  paint()
95
107
  stop ??= frameLoop.add(tick)
@@ -109,6 +121,7 @@ function Digit(props: { digit: string; dir: number; spring: DurationOptions }) {
109
121
  el.textContent = props.digit
110
122
  }}
111
123
  />
124
+ {/* The leaving digit is drawn from its attribute, so the number's text is only ever the value it shows. */}
112
125
  <span data-slot="rolling-number-glyph" data-leaving="" aria-hidden="true" ref={(el) => (outgoing = el)} />
113
126
  </span>
114
127
  )
@@ -1,4 +1,7 @@
1
1
  [data-component="search-list"] {
2
+ /* Sticky headers and the top fade paint this, so rows scroll under an opaque band. */
3
+ --search-list-surface: var(--v2-background-bg-base);
4
+
2
5
  display: flex;
3
6
  min-height: 0;
4
7
  flex: 1;
@@ -42,6 +45,17 @@
42
45
  }
43
46
  }
44
47
 
48
+ [data-slot="search-list-body"] {
49
+ display: flex;
50
+ min-height: 0;
51
+ flex: 1;
52
+ flex-direction: column;
53
+
54
+ &[hidden] {
55
+ display: none;
56
+ }
57
+ }
58
+
45
59
  /* Arrow keys scroll the active row clear of the sticky header. Rows fade out past the bottom edge while more follow. */
46
60
  [data-slot="search-list-scroll"] {
47
61
  min-height: 0;
@@ -69,7 +83,7 @@
69
83
  position: absolute;
70
84
  inset: var(--search-list-fade-offset, 0px) 0 auto;
71
85
  height: 24px;
72
- background: linear-gradient(var(--v2-background-bg-base), transparent);
86
+ background: linear-gradient(var(--search-list-surface), transparent);
73
87
  content: "";
74
88
  opacity: var(--search-list-fade-top, 0);
75
89
  }
@@ -79,6 +93,11 @@
79
93
  display: flex;
80
94
  flex-direction: column;
81
95
  padding: 6px 6px 8px;
96
+
97
+ /* Its padding would sit above the empty state and push it below the centre. */
98
+ &[hidden] {
99
+ display: none;
100
+ }
82
101
  }
83
102
 
84
103
  [data-slot="search-list-section"] {
@@ -104,7 +123,7 @@
104
123
  height: 28px;
105
124
  margin-inline: -6px;
106
125
  padding-inline: 18px;
107
- background: var(--v2-background-bg-base);
126
+ background: var(--search-list-surface);
108
127
  color: var(--v2-text-text-muted);
109
128
  font-size: 13px;
110
129
  font-weight: 440;
@@ -160,6 +179,40 @@
160
179
  line-height: var(--line-height-compact);
161
180
  letter-spacing: -0.04px;
162
181
  }
182
+
183
+ /* On the rows' text spine, under the bottom fade rather than inside it. */
184
+ [data-slot="search-list-footer"] {
185
+ flex-shrink: 0;
186
+ padding: 4px 18px 12px;
187
+ color: var(--v2-text-text-muted);
188
+ font-size: 13px;
189
+ font-weight: 440;
190
+ line-height: var(--line-height-compact);
191
+ letter-spacing: -0.04px;
192
+ }
193
+
194
+ &[data-popover] {
195
+ --search-list-surface: var(--v2-background-bg-layer-01);
196
+
197
+ position: relative;
198
+ flex: none;
199
+
200
+ [data-slot="search-list-body"] {
201
+ position: absolute;
202
+ inset-block-start: calc(100% + 4px);
203
+ inset-inline: 0;
204
+ z-index: 12;
205
+ max-height: min(45vh, 480px);
206
+ overflow: hidden;
207
+ border-radius: 8px;
208
+ background: var(--search-list-surface);
209
+ box-shadow: var(--v2-elevation-floating);
210
+ }
211
+
212
+ [data-slot="search-list-scroll"] {
213
+ flex: 0 1 auto;
214
+ }
215
+ }
163
216
  }
164
217
 
165
218
  [data-color-scheme="dark"] [data-component="search-list"] [data-slot="search-list-row"][data-active] {
@@ -1,4 +1,15 @@
1
- import { createContext, createEffect, createMemo, For, type JSX, on, onCleanup, Show, useContext } from "solid-js"
1
+ import {
2
+ createContext,
3
+ createEffect,
4
+ createMemo,
5
+ For,
6
+ type JSX,
7
+ on,
8
+ onCleanup,
9
+ onMount,
10
+ Show,
11
+ useContext,
12
+ } from "solid-js"
2
13
  import { createResizeObserver } from "@solid-primitives/resize-observer"
3
14
  import { Icon } from "@opencode/ui/icon"
4
15
  import { TextInput } from "../text-input/text-input"
@@ -18,6 +29,17 @@ export type SearchListRow = {
18
29
  onClick: () => void
19
30
  }
20
31
 
32
+ /** Attributes for the input of a field passed as `field`: spread them onto it. */
33
+ export type SearchListField = {
34
+ role: "combobox"
35
+ "aria-expanded": boolean
36
+ "aria-autocomplete": "list"
37
+ "aria-controls": string | undefined
38
+ "aria-activedescendant": string | undefined
39
+ onInput: (event: InputEvent & { currentTarget: HTMLInputElement }) => void
40
+ onKeyDown: (event: KeyboardEvent) => void
41
+ }
42
+
21
43
  export type SearchListHandle<T> = {
22
44
  active: () => T | undefined
23
45
  setActive: (key: string | undefined) => void
@@ -29,13 +51,22 @@ const QueryContext = createContext<() => string>(() => "")
29
51
  * A search field over sectioned results. Rows that share a `section` key list together under one header, which sticks
30
52
  * while its rows scroll beneath it until the next header pushes it away. The first row is active until the user picks
31
53
  * another: ↑/↓ wrap, ⌘ or ⌥ with ↑/↓ jump between sections, Home/End go to the ends, Enter selects and Escape closes.
54
+ * A page can keep its own field through `field` and float the results beneath it with `popover`.
32
55
  */
33
56
  export function SearchList<T>(props: {
34
57
  query: string
35
58
  onQuery: (query: string) => void
36
59
  placeholder: string
60
+ /** The results' accessible name; the placeholder by default. */
61
+ label?: string
37
62
  /** Controls after the field, such as icon buttons. */
38
63
  trailing?: JSX.Element
64
+ /** A search field of the page's own in place of the built-in one. */
65
+ field?: (field: SearchListField) => JSX.Element
66
+ /** Whether the results show. Closed, only the field remains and the list leaves keys to the page. On by default. */
67
+ open?: boolean
68
+ /** Float the results beneath the field on a raised surface, over what follows it. */
69
+ popover?: boolean
39
70
  items: T[]
40
71
  key: (item: T) => string
41
72
  section?: (item: T) => string
@@ -45,6 +76,11 @@ export function SearchList<T>(props: {
45
76
  header?: (item: T) => JSX.Element
46
77
  /** Shown in place of the rows when there are none. */
47
78
  empty: JSX.Element
79
+ /** A note beneath the rows, such as a hint that more results are hidden. */
80
+ footer?: JSX.Element
81
+ /** Where the results start scrolled each time they mount, such as a position kept from an earlier visit. */
82
+ scrollTop?: number
83
+ onScroll?: (scrollTop: number) => void
48
84
  onSelect: (item: T) => void
49
85
  onActive?: (item: T | undefined) => void
50
86
  /** Runs before the list's own keys; call `preventDefault` to claim one. */
@@ -59,6 +95,7 @@ export function SearchList<T>(props: {
59
95
  let results: HTMLDivElement | undefined
60
96
 
61
97
  const sectionOf = (item: T) => props.section?.(item) ?? ""
98
+ const open = () => props.open ?? true
62
99
 
63
100
  // Sections in the order their first row arrives; rows then list section by section, which is also the arrow order.
64
101
  const groups = createMemo(() => {
@@ -173,7 +210,7 @@ export function SearchList<T>(props: {
173
210
  if (event.isComposing) return
174
211
  props.onKeyDown?.(event, typeahead.activeItem())
175
212
 
176
- if (event.defaultPrevented) return
213
+ if (event.defaultPrevented || !open()) return
177
214
  const move = searchListMove(event)
178
215
 
179
216
  if (!move) return typeahead.onKeyDown(event)
@@ -198,6 +235,30 @@ export function SearchList<T>(props: {
198
235
  reveal()
199
236
  }
200
237
 
238
+ onMount(() => {
239
+ if (viewport && props.scrollTop) viewport.scrollTop = props.scrollTop
240
+ })
241
+
242
+ const field: SearchListField = {
243
+ role: "combobox",
244
+ get "aria-expanded"() {
245
+ return open()
246
+ },
247
+ "aria-autocomplete": "list",
248
+ get "aria-controls"() {
249
+ return open() ? typeahead.listID : undefined
250
+ },
251
+ get "aria-activedescendant"() {
252
+ return open() ? typeahead.activeID() : undefined
253
+ },
254
+ onInput: (event) => {
255
+ // A new query starts again from its first row.
256
+ typeahead.setActive(undefined)
257
+ props.onQuery(event.currentTarget.value)
258
+ },
259
+ onKeyDown,
260
+ }
261
+
201
262
  const row = (key: string, item: () => T): SearchListRow => ({
202
263
  get id() {
203
264
  return typeahead.optionID(item())
@@ -221,80 +282,91 @@ export function SearchList<T>(props: {
221
282
 
222
283
  return (
223
284
  <QueryContext.Provider value={() => props.query}>
224
- <div data-component="search-list" class={props.class}>
225
- <div data-slot="search-list-search">
226
- <TextInput
227
- value={props.query}
228
- autofocus
229
- autocomplete="off"
230
- autocorrect="off"
231
- autocapitalize="off"
232
- spellcheck={false}
233
- appearance="large"
234
- placeholder={props.placeholder}
235
- aria-label={props.placeholder}
236
- leadingIcon={<Icon name="magnifying-glass" />}
237
- trailing={props.trailing}
238
- role="combobox"
239
- aria-expanded="true"
240
- aria-autocomplete="list"
241
- aria-controls={typeahead.listID}
242
- aria-activedescendant={typeahead.activeID()}
243
- onInput={(event) => {
244
- // A new query starts again from its first row.
245
- typeahead.setActive(undefined)
246
- props.onQuery(event.currentTarget.value)
247
- }}
248
- onKeyDown={onKeyDown}
249
- />
250
- </div>
251
- <div data-slot="search-list-scroll" data-scrollable ref={(element) => (viewport = element)} onScroll={edges}>
252
- <div data-slot="search-list-fade" aria-hidden="true" />
285
+ <div data-component="search-list" data-popover={props.popover ? "" : undefined} class={props.class}>
286
+ <Show
287
+ when={props.field}
288
+ fallback={
289
+ <div data-slot="search-list-search">
290
+ <TextInput
291
+ {...field}
292
+ value={props.query}
293
+ autofocus
294
+ autocomplete="off"
295
+ autocorrect="off"
296
+ autocapitalize="off"
297
+ spellcheck={false}
298
+ appearance="large"
299
+ placeholder={props.placeholder}
300
+ aria-label={props.placeholder}
301
+ leadingIcon={<Icon name="magnifying-glass" />}
302
+ trailing={props.trailing}
303
+ />
304
+ </div>
305
+ }
306
+ >
307
+ {(custom) => custom()(field)}
308
+ </Show>
309
+ <div data-slot="search-list-body" hidden={!open()}>
253
310
  <div
254
- ref={results}
255
- id={typeahead.listID}
256
- data-slot="search-list-results"
257
- role="listbox"
258
- aria-label={props.placeholder}
311
+ data-slot="search-list-scroll"
312
+ data-scrollable
313
+ ref={(element) => (viewport = element)}
314
+ onScroll={(event) => {
315
+ edges()
316
+ props.onScroll?.(event.currentTarget.scrollTop)
317
+ }}
259
318
  >
260
- <For each={sectionKeys()}>
261
- {(section) => {
262
- const keys = createMemo<string[]>((previous) =>
263
- stable(previous, (groups().get(section) ?? []).map(props.key)),
264
- )
265
-
266
- const first = () => byKey().get(keys()[0] ?? "")
267
-
268
- const header = createMemo(() =>
269
- props.header && first() !== undefined ? props.header(first()!) : undefined,
270
- )
271
-
272
- const id = `${typeahead.listID}-section-${encodeURIComponent(section)}`
273
-
274
- return (
275
- <div data-slot="search-list-section" role="group" aria-labelledby={header() ? id : undefined}>
276
- <Show when={header()}>
277
- {(content) => (
278
- <div id={id} data-slot="search-list-header">
279
- {content()}
280
- </div>
281
- )}
282
- </Show>
283
- <For each={keys()}>
284
- {(key) => {
285
- // A row that is leaving keeps its last item until it is removed.
286
- const item = createMemo<T>((previous) => byKey().get(key) ?? previous, byKey().get(key)!)
287
-
288
- return props.children(item, row(key, item))
289
- }}
290
- </For>
291
- </div>
292
- )
293
- }}
294
- </For>
319
+ <div data-slot="search-list-fade" aria-hidden="true" />
320
+ <div
321
+ ref={results}
322
+ id={typeahead.listID}
323
+ data-slot="search-list-results"
324
+ role="listbox"
325
+ hidden={rows().length === 0}
326
+ aria-label={props.label ?? props.placeholder}
327
+ >
328
+ <For each={sectionKeys()}>
329
+ {(section) => {
330
+ const keys = createMemo<string[]>((previous) =>
331
+ stable(previous, (groups().get(section) ?? []).map(props.key)),
332
+ )
333
+
334
+ const first = () => byKey().get(keys()[0] ?? "")
335
+
336
+ const header = createMemo(() =>
337
+ props.header && first() !== undefined ? props.header(first()!) : undefined,
338
+ )
339
+
340
+ const id = `${typeahead.listID}-section-${encodeURIComponent(section)}`
341
+
342
+ return (
343
+ <div data-slot="search-list-section" role="group" aria-labelledby={header() ? id : undefined}>
344
+ <Show when={header()}>
345
+ {(content) => (
346
+ <div id={id} data-slot="search-list-header">
347
+ {content()}
348
+ </div>
349
+ )}
350
+ </Show>
351
+ <For each={keys()}>
352
+ {(key) => {
353
+ // A row that is leaving keeps its last item until it is removed.
354
+ const item = createMemo<T>((previous) => byKey().get(key) ?? previous, byKey().get(key)!)
355
+
356
+ return props.children(item, row(key, item))
357
+ }}
358
+ </For>
359
+ </div>
360
+ )
361
+ }}
362
+ </For>
363
+ </div>
364
+ <Show when={rows().length === 0}>
365
+ <div data-slot="search-list-empty">{props.empty}</div>
366
+ </Show>
295
367
  </div>
296
- <Show when={rows().length === 0}>
297
- <div data-slot="search-list-empty">{props.empty}</div>
368
+ <Show when={props.footer}>
369
+ <div data-slot="search-list-footer">{props.footer}</div>
298
370
  </Show>
299
371
  </div>
300
372
  </div>
@@ -35,7 +35,9 @@ export function createTypeahead<T>(props: {
35
35
  onKeyDown: (event: KeyboardEvent) => {
36
36
  if (event.isComposing) return
37
37
  const items = props.items()
38
- const delta = event.key === "ArrowDown" ? 1 : event.key === "ArrowUp" ? -1 : 0
38
+ // Modified arrows belong to the input: Shift selects text, Ctrl and the rest are the platform's.
39
+ const plain = !event.shiftKey && !event.ctrlKey && !event.altKey && !event.metaKey
40
+ const delta = !plain ? 0 : event.key === "ArrowDown" ? 1 : event.key === "ArrowUp" ? -1 : 0
39
41
 
40
42
  if (delta !== 0 || event.key === "Enter" || (event.key === "Escape" && props.onClose)) {
41
43
  event.preventDefault()