@opencode/ui 0.0.0-dev-583 → 0.0.0-dev-627
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.
- package/dist/components/list.d.ts +0 -6
- package/dist/components/menu-scroll.d.ts +9 -3
- package/dist/hooks/list-machine.d.ts +118 -0
- package/dist/hooks/use-filtered-list.d.ts +21 -35
- package/dist/icons/catalog.d.ts +8 -0
- package/package.json +2 -2
- package/src/components/list.tsx +40 -137
- package/src/components/menu-scroll.css +19 -8
- package/src/components/menu-scroll.tsx +49 -15
- package/src/forms/search-list/search-list.css +5 -1
- package/src/forms/search-list/search-list.tsx +4 -4
- package/src/forms/select/select.css +8 -3
- package/src/forms/select/select.tsx +6 -1
- package/src/hooks/list-machine.ts +301 -0
- package/src/hooks/use-filtered-list.tsx +95 -119
- package/src/icons/catalog.ts +8 -0
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { batch, createComputed, createMemo, createReaction, createSignal, on, onCleanup, untrack } from "solid-js"
|
|
2
|
+
import { createListState, transitionList, type ListEvent, type ListGroup } from "./list-machine"
|
|
3
|
+
|
|
4
|
+
export { groupItems } from "./list-machine"
|
|
5
5
|
|
|
6
6
|
export interface FilteredListProps<T> {
|
|
7
7
|
items: T[] | ((filter: string) => T[] | Promise<T[]>)
|
|
@@ -10,134 +10,110 @@ export interface FilteredListProps<T> {
|
|
|
10
10
|
current?: T
|
|
11
11
|
groupBy?: (x: T) => string
|
|
12
12
|
sortBy?: (a: T, b: T) => number
|
|
13
|
-
sortGroupsBy?: (a:
|
|
13
|
+
sortGroupsBy?: (a: ListGroup<T>, b: ListGroup<T>) => number
|
|
14
14
|
skipFilter?: (item: T) => boolean
|
|
15
15
|
onSelect?: (value: T | undefined, index: number) => void
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
items: T[],
|
|
20
|
-
props: Pick<FilteredListProps<T>, "groupBy" | "sortBy" | "sortGroupsBy">,
|
|
21
|
-
) {
|
|
22
|
-
// A plain object keeps the group order of `Object.entries`, where numeric-looking categories sort first.
|
|
23
|
-
const byCategory: Record<string, T[]> = {}
|
|
24
|
-
|
|
25
|
-
for (const item of items) {
|
|
26
|
-
const category = props.groupBy ? props.groupBy(item) : ""
|
|
27
|
-
byCategory[category] ??= []
|
|
28
|
-
byCategory[category].push(item)
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const groups = Object.entries(byCategory).map(([category, group]) => ({
|
|
32
|
-
category,
|
|
33
|
-
items: props.sortBy ? group.sort(props.sortBy) : group,
|
|
34
|
-
}))
|
|
35
|
-
|
|
36
|
-
return props.sortGroupsBy ? groups.sort(props.sortGroupsBy) : groups
|
|
16
|
+
/** Makes the query parent-owned: the list shows this value and reports edits through `onFilter`. */
|
|
17
|
+
filter?: string
|
|
18
|
+
onFilter?: (value: string) => void
|
|
37
19
|
}
|
|
38
20
|
|
|
39
21
|
export function useFilteredList<T>(props: FilteredListProps<T>) {
|
|
40
|
-
const [
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
const [grouped, { refetch }] = createResource(
|
|
63
|
-
() => ({
|
|
64
|
-
filter: store.filter,
|
|
65
|
-
// oxlint-disable-next-line anti-slop/no-runtime-typeof -- `items` is either a list or a loader for one
|
|
66
|
-
items: typeof props.items === "function" ? props.items(store.filter) : props.items,
|
|
67
|
-
}),
|
|
68
|
-
async ({ filter, items }) => {
|
|
69
|
-
const query = filter ?? ""
|
|
70
|
-
const needle = query.toLowerCase()
|
|
71
|
-
const all = (await Promise.resolve(items)) || []
|
|
72
|
-
|
|
73
|
-
const matched = needle ? search(all, needle) : all
|
|
74
|
-
|
|
75
|
-
return groupItems(matched, props)
|
|
76
|
-
},
|
|
77
|
-
{ initialValue: empty },
|
|
78
|
-
)
|
|
79
|
-
|
|
80
|
-
const flat = createMemo(() => (grouped.latest || []).flatMap((x) => x.items))
|
|
81
|
-
|
|
82
|
-
// Lists open on their first row; `current` only marks the selected item.
|
|
83
|
-
const list = createList({
|
|
84
|
-
items: () => flat().map(props.key),
|
|
85
|
-
initialActive: flat()[0] ? props.key(flat()[0]) : "",
|
|
86
|
-
loop: true,
|
|
87
|
-
})
|
|
88
|
-
|
|
89
|
-
const reset = () => {
|
|
90
|
-
const all = flat()
|
|
91
|
-
|
|
92
|
-
if (all.length === 0) return
|
|
93
|
-
list.setActive(props.key(all[0]))
|
|
22
|
+
const [state, setState] = createSignal(createListState<T>(props.filter))
|
|
23
|
+
const lifetime = new AbortController()
|
|
24
|
+
onCleanup(() => lifetime.abort())
|
|
25
|
+
|
|
26
|
+
const dispatch = (event: ListEvent<T>, keyboard?: KeyboardEvent) => {
|
|
27
|
+
const result = transitionList(untrack(state), event, props)
|
|
28
|
+
|
|
29
|
+
if (result.handled) keyboard?.preventDefault()
|
|
30
|
+
|
|
31
|
+
// A synchronous load settles inside the batch, so readers never see its `loading` step.
|
|
32
|
+
batch(() => {
|
|
33
|
+
setState(() => result.state)
|
|
34
|
+
result.effects.forEach((effect) => {
|
|
35
|
+
if (effect.type === "load") load(effect.attempt, effect.query)
|
|
36
|
+
})
|
|
37
|
+
})
|
|
38
|
+
result.effects.forEach((effect) => {
|
|
39
|
+
if (effect.type === "notify.filter") props.onFilter?.(effect.value)
|
|
40
|
+
|
|
41
|
+
if (effect.type === "select") props.onSelect?.(effect.item, effect.index)
|
|
42
|
+
})
|
|
94
43
|
}
|
|
95
44
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
event.preventDefault()
|
|
99
|
-
const selectedIndex = flat().findIndex((x) => props.key(x) === list.active())
|
|
100
|
-
const selected = flat()[selectedIndex]
|
|
101
|
-
|
|
102
|
-
if (selected) props.onSelect?.(selected, selectedIndex)
|
|
103
|
-
} else if (event.ctrlKey && !event.metaKey && !event.altKey && !event.shiftKey) {
|
|
104
|
-
if (event.key === "n" || event.key === "p") {
|
|
105
|
-
event.preventDefault()
|
|
45
|
+
// Tracks what the last load read (`props.items`, or the signals `items(query)` reads) and reloads when it changes.
|
|
46
|
+
const track = createReaction(() => dispatch({ type: "items.reload" }))
|
|
106
47
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
48
|
+
const load = (attempt: number, query: string) =>
|
|
49
|
+
track(() => {
|
|
50
|
+
// oxlint-disable-next-line anti-slop/no-runtime-typeof -- `items` is either a list or a loader for one
|
|
51
|
+
const items = typeof props.items === "function" ? props.items(query) : props.items
|
|
52
|
+
untrack(() => settle(attempt, items))
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
const settle = (attempt: number, items: T[] | Promise<T[]>) => {
|
|
56
|
+
if (Array.isArray(items)) return dispatch({ type: "load.resolved", attempt, items })
|
|
57
|
+
|
|
58
|
+
void items.then(
|
|
59
|
+
(items) => {
|
|
60
|
+
if (!lifetime.signal.aborted) dispatch({ type: "load.resolved", attempt, items: items ?? [] })
|
|
61
|
+
},
|
|
62
|
+
() => {
|
|
63
|
+
if (!lifetime.signal.aborted) dispatch({ type: "load.failed", attempt })
|
|
64
|
+
},
|
|
65
|
+
)
|
|
119
66
|
}
|
|
120
67
|
|
|
121
|
-
|
|
122
|
-
on(
|
|
123
|
-
|
|
124
|
-
|
|
68
|
+
createComputed(
|
|
69
|
+
on(
|
|
70
|
+
() => props.filter,
|
|
71
|
+
(value) => dispatch(value === undefined ? { type: "filter.uncontrolled" } : { type: "filter.controlled", value }),
|
|
72
|
+
{ defer: true },
|
|
73
|
+
),
|
|
125
74
|
)
|
|
126
75
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
76
|
+
dispatch({ type: "items.reload" })
|
|
77
|
+
|
|
78
|
+
const groups = createMemo(() => state().groups)
|
|
79
|
+
const loading = createMemo(() => state().status === "loading")
|
|
130
80
|
|
|
131
81
|
return {
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
82
|
+
state,
|
|
83
|
+
dispatch,
|
|
84
|
+
grouped: {
|
|
85
|
+
get latest() {
|
|
86
|
+
return groups()
|
|
87
|
+
},
|
|
88
|
+
get loading() {
|
|
89
|
+
return loading()
|
|
90
|
+
},
|
|
91
|
+
get state(): "ready" | "refreshing" {
|
|
92
|
+
return loading() ? "refreshing" : "ready"
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
filter: createMemo(() => state().filter.query),
|
|
96
|
+
flat: createMemo(() => state().rows),
|
|
97
|
+
active: createMemo(() => state().highlight.activeKey),
|
|
98
|
+
reset: () => dispatch({ type: "active.reset" }),
|
|
99
|
+
refetch: () => dispatch({ type: "items.reload" }),
|
|
100
|
+
clear: () => dispatch({ type: "query.change", value: "" }),
|
|
101
|
+
onInput: (value: string) => dispatch({ type: "query.change", value }),
|
|
102
|
+
setActive: (key: string | null) => dispatch({ type: "active.set", key }),
|
|
103
|
+
onKeyDown: (event: KeyboardEvent, options?: { search?: boolean; claimed?: boolean }) =>
|
|
104
|
+
dispatch(
|
|
105
|
+
{
|
|
106
|
+
type: "key.down",
|
|
107
|
+
key: event.key,
|
|
108
|
+
ctrl: event.ctrlKey,
|
|
109
|
+
alt: event.altKey,
|
|
110
|
+
meta: event.metaKey,
|
|
111
|
+
shift: event.shiftKey,
|
|
112
|
+
composing: event.isComposing,
|
|
113
|
+
search: options?.search === true,
|
|
114
|
+
claimed: options?.claimed === true,
|
|
115
|
+
},
|
|
116
|
+
event,
|
|
117
|
+
),
|
|
142
118
|
}
|
|
143
119
|
}
|
package/src/icons/catalog.ts
CHANGED
|
@@ -2,6 +2,14 @@ import { additionalIcons, additionalIconViewBox } from "./icon/additional-icons"
|
|
|
2
2
|
|
|
3
3
|
// Consumers center the SVG viewport, so each icon must center its artwork within its viewBox.
|
|
4
4
|
const icons = {
|
|
5
|
+
"outline-list-changelog": {
|
|
6
|
+
viewBox: "0 0 16 16",
|
|
7
|
+
body: `<path d="M3 14.5V15H4V14.5H3.5H3ZM4 1.5V1H3V1.5H3.5H4ZM8.5 1.5V1H8V1.5H8.5ZM14.5 1.5H15V1H14.5V1.5ZM14.5 6.5V7H15V6.5H14.5ZM8.5 6.5H8V7H8.5V6.5ZM8.5 9.5V9H8V9.5H8.5ZM14.5 9.5H15V9H14.5V9.5ZM14.5 14.5V15H15V14.5H14.5ZM8.5 14.5H8V15H8.5V14.5ZM3.5 6H4V1.5H3.5H3V6H3.5ZM8.5 1.5V2H14.5V1.5V1H8.5V1.5ZM14.5 1.5H14V6.5H14.5H15V1.5H14.5ZM14.5 6.5V6H8.5V6.5V7H14.5V6.5ZM8.5 6.5H9V1.5H8.5H8V6.5H8.5ZM8.5 9.5V10H14.5V9.5V9H8.5V9.5ZM14.5 9.5H14V14.5H14.5H15V9.5H14.5ZM14.5 14.5V14H8.5V14.5V15H14.5V14.5ZM8.5 14.5H9V9.5H8.5H8V14.5H8.5ZM3.5 10V10.5C4.8807 10.5 6 9.38075 6 8H5.5H5C5 8.82845 4.32843 9.5 3.5 9.5V10ZM5.5 8H6C6 6.61925 4.8807 5.5 3.5 5.5V6V6.5C4.32843 6.5 5 7.17155 5 8H5.5ZM3.5 6V5.5C2.1193 5.5 1 6.61925 1 8H1.5H2C2 7.17155 2.67157 6.5 3.5 6.5V6ZM1.5 8H1C1 9.38075 2.1193 10.5 3.5 10.5V10V9.5C2.67157 9.5 2 8.82845 2 8H1.5ZM3.5 14.5H4V10H3.5H3V14.5H3.5Z" fill="currentColor"/>`,
|
|
8
|
+
},
|
|
9
|
+
"outline-pair": {
|
|
10
|
+
viewBox: "0 0 16 16",
|
|
11
|
+
body: `<path d="M4.5 10V13.5H11.5V10" stroke="currentColor"/><path d="M3.5 4.5V2.5H5.5V4.5" stroke-linecap="square" stroke="currentColor"/><path d="M10.5 4.5V2.5H12.5V4.5" stroke-linecap="square" stroke="currentColor"/><path d="M9.5 4.5H13.5V10.5H9.5V4.5Z" stroke-linecap="square" stroke="currentColor"/><path d="M2.5 4.5H6.5V10.5H2.5V4.5Z" stroke-linecap="square" stroke="currentColor"/>`,
|
|
12
|
+
},
|
|
5
13
|
lock: {
|
|
6
14
|
viewBox: "0 0 16 16",
|
|
7
15
|
body: `<rect x="3.5" y="7" width="9" height="7" rx="1" stroke="currentColor"/><path d="M5 7V5a3 3 0 0 1 6 0v2M8 10v1" stroke="currentColor" stroke-linecap="round"/>`,
|