@opencode/ui 2.2.5 → 2.2.6

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 (117) hide show
  1. package/dist/actions/button/button.d.ts +2 -2
  2. package/dist/actions/icon-button/icon-button.d.ts +1 -1
  3. package/dist/components/animated-number-machine.d.ts +95 -0
  4. package/dist/components/file-icon-machine.d.ts +40 -0
  5. package/dist/components/file-icon.d.ts +0 -2
  6. package/dist/components/file-icons/choose-icon-name.d.ts +2 -0
  7. package/dist/components/image-flight-machine.d.ts +135 -0
  8. package/dist/components/image-zoom-machine.d.ts +95 -0
  9. package/dist/components/image-zoom.d.ts +6 -3
  10. package/dist/components/list.d.ts +2 -6
  11. package/dist/components/menu-scroll.d.ts +9 -3
  12. package/dist/components/popover-machine.d.ts +96 -0
  13. package/dist/components/popover.d.ts +2 -2
  14. package/dist/components/resize-handle-machine.d.ts +71 -0
  15. package/dist/components/resize-handle.d.ts +2 -0
  16. package/dist/components/scroll-view-machine.d.ts +68 -0
  17. package/dist/components/scroll-view.d.ts +2 -2
  18. package/dist/components/spinner.d.ts +11 -3
  19. package/dist/components/text-field-machine.d.ts +62 -0
  20. package/dist/components/text-reveal-machine.d.ts +90 -0
  21. package/dist/components/typewriter-machine.d.ts +62 -0
  22. package/dist/context/dialog-machine.d.ts +125 -0
  23. package/dist/context/dialog.d.ts +17 -16
  24. package/dist/data-display/line-comment/machine.d.ts +157 -0
  25. package/dist/data-display/project-avatar/animation-machine.d.ts +62 -0
  26. package/dist/data-display/project-avatar/animation.d.ts +1 -1
  27. package/dist/feedback/activity-ring/activity-ring.d.ts +2 -1
  28. package/dist/feedback/toast/machine.d.ts +57 -0
  29. package/dist/feedback/toast/toast.d.ts +1 -1
  30. package/dist/forms/checkbox/checkbox.d.ts +6 -1
  31. package/dist/forms/select/machine.d.ts +45 -0
  32. package/dist/forms/select/select.d.ts +4 -0
  33. package/dist/forms/typeahead/machine.d.ts +85 -0
  34. package/dist/hooks/auto-scroll-machine.d.ts +89 -0
  35. package/dist/hooks/create-auto-scroll.d.ts +1 -1
  36. package/dist/hooks/list-machine.d.ts +150 -0
  37. package/dist/hooks/use-filtered-list.d.ts +21 -35
  38. package/dist/i18n/en.d.ts +5 -0
  39. package/dist/icons/catalog.d.ts +8 -0
  40. package/dist/navigation/menu/machine.d.ts +49 -0
  41. package/dist/navigation/menu/menu.d.ts +5 -3
  42. package/dist/navigation/segmented-control/machine.d.ts +46 -0
  43. package/dist/overlays/mcp-sign-in/machine.d.ts +184 -0
  44. package/dist/overlays/mcp-sign-in/mcp-sign-in.d.ts +40 -0
  45. package/dist/overlays/tooltip/machine.d.ts +146 -0
  46. package/dist/theme/context.d.ts +38 -14
  47. package/dist/theme/machine.d.ts +174 -0
  48. package/dist/typography/text-shimmer/machine.d.ts +48 -0
  49. package/package.json +19 -3
  50. package/src/actions/button/button.tsx +11 -6
  51. package/src/actions/icon-button/icon-button.tsx +17 -4
  52. package/src/components/animated-number-machine.ts +214 -0
  53. package/src/components/animated-number.tsx +134 -83
  54. package/src/components/file-icon-machine.ts +78 -0
  55. package/src/components/file-icon.tsx +96 -579
  56. package/src/components/file-icons/choose-icon-name.ts +574 -0
  57. package/src/components/image-flight-machine.ts +247 -0
  58. package/src/components/image-preview.css +5 -2
  59. package/src/components/image-preview.tsx +390 -252
  60. package/src/components/image-zoom-machine.ts +191 -0
  61. package/src/components/image-zoom.ts +22 -14
  62. package/src/components/list.tsx +46 -137
  63. package/src/components/menu-scroll.css +19 -8
  64. package/src/components/menu-scroll.tsx +111 -34
  65. package/src/components/popover-machine.ts +198 -0
  66. package/src/components/popover.tsx +157 -89
  67. package/src/components/resize-handle-machine.ts +174 -0
  68. package/src/components/resize-handle.tsx +116 -67
  69. package/src/components/scroll-view-machine.ts +244 -0
  70. package/src/components/scroll-view.tsx +163 -116
  71. package/src/components/spinner.css +18 -5
  72. package/src/components/spinner.tsx +28 -14
  73. package/src/components/text-field-machine.ts +102 -0
  74. package/src/components/text-field.tsx +78 -24
  75. package/src/components/text-reveal-machine.ts +184 -0
  76. package/src/components/text-reveal.tsx +96 -93
  77. package/src/components/typewriter-machine.ts +134 -0
  78. package/src/components/typewriter.tsx +50 -39
  79. package/src/context/dialog-machine.ts +280 -0
  80. package/src/context/dialog.tsx +168 -177
  81. package/src/data-display/line-comment/line-comment.tsx +157 -178
  82. package/src/data-display/line-comment/machine.ts +391 -0
  83. package/src/data-display/project-avatar/animation-machine.ts +182 -0
  84. package/src/data-display/project-avatar/animation.ts +67 -38
  85. package/src/feedback/activity-ring/activity-ring.tsx +1 -1
  86. package/src/feedback/toast/machine.ts +115 -0
  87. package/src/feedback/toast/toast.tsx +55 -59
  88. package/src/forms/checkbox/checkbox-compact.css +169 -0
  89. package/src/forms/checkbox/checkbox.tsx +93 -31
  90. package/src/forms/search-list/search-list.css +5 -1
  91. package/src/forms/search-list/search-list.tsx +4 -4
  92. package/src/forms/select/machine.ts +81 -0
  93. package/src/forms/select/select.css +59 -17
  94. package/src/forms/select/select.tsx +67 -31
  95. package/src/forms/typeahead/machine.ts +253 -0
  96. package/src/forms/typeahead/typeahead.tsx +70 -62
  97. package/src/hooks/auto-scroll-machine.ts +261 -0
  98. package/src/hooks/create-auto-scroll.tsx +110 -205
  99. package/src/hooks/list-machine.ts +359 -0
  100. package/src/hooks/use-filtered-list.tsx +101 -112
  101. package/src/i18n/en.ts +6 -1
  102. package/src/icons/catalog.ts +8 -0
  103. package/src/navigation/menu/machine.ts +109 -0
  104. package/src/navigation/menu/menu.tsx +130 -22
  105. package/src/navigation/segmented-control/machine.ts +72 -0
  106. package/src/navigation/segmented-control/segmented-control.tsx +46 -46
  107. package/src/overlays/dialog/dialog.tsx +7 -1
  108. package/src/overlays/mcp-sign-in/machine.ts +343 -0
  109. package/src/overlays/mcp-sign-in/mcp-sign-in.ts +159 -0
  110. package/src/overlays/tooltip/machine.ts +303 -0
  111. package/src/overlays/tooltip/tooltip.tsx +157 -92
  112. package/src/styles/theme.css +10 -2
  113. package/src/theme/context.tsx +192 -269
  114. package/src/theme/machine.ts +508 -0
  115. package/src/typography/text-shimmer/machine.ts +90 -0
  116. package/src/typography/text-shimmer/text-shimmer.css +71 -39
  117. package/src/typography/text-shimmer/text-shimmer.tsx +41 -24
@@ -1,44 +1,106 @@
1
1
  import { Control, Description, ErrorMessage, Indicator, Input, Label, Root } from "@kobalte/core/checkbox"
2
- import { Show, splitProps } from "solid-js"
2
+ import { Match, Show, Switch, splitProps } from "solid-js"
3
3
  import type { ComponentProps, JSX, ParentProps } from "solid-js"
4
+ import "./checkbox-compact.css"
4
5
 
5
6
  export interface CheckboxProps extends ParentProps<ComponentProps<typeof Root>> {
7
+ /** `compact` is the design system checkbox; `standard` keeps the legacy look. */
8
+ appearance?: "standard" | "compact"
9
+ /** Background of the compact control. */
10
+ surface?: "base" | "layer-1"
6
11
  hideLabel?: boolean
7
- description?: string
12
+ description?: JSX.Element
8
13
  icon?: JSX.Element
9
14
  }
10
15
 
11
16
  export function Checkbox(props: CheckboxProps) {
12
- const [local, others] = splitProps(props, ["children", "class", "label", "hideLabel", "description", "icon"])
17
+ const [local, others] = splitProps(props, [
18
+ "children",
19
+ "class",
20
+ "label",
21
+ "appearance",
22
+ "surface",
23
+ "hideLabel",
24
+ "description",
25
+ "icon",
26
+ ])
13
27
 
14
28
  return (
15
- <Root {...others} data-component="checkbox">
16
- <Input data-slot="checkbox-checkbox-input" />
17
- <Control data-slot="checkbox-checkbox-control">
18
- <Indicator data-slot="checkbox-checkbox-indicator">
19
- {local.icon || (
20
- <svg viewBox="0 0 12 12" fill="none" width="10" height="10" xmlns="http://www.w3.org/2000/svg">
21
- <path
22
- d="M3 7.17905L5.02703 8.85135L9 3.5"
23
- stroke="currentColor"
24
- stroke-width="1.5"
25
- stroke-linecap="square"
26
- />
27
- </svg>
28
- )}
29
- </Indicator>
30
- </Control>
31
- <div data-slot="checkbox-checkbox-content">
32
- <Show when={props.children}>
33
- <Label data-slot="checkbox-checkbox-label" classList={{ "sr-only": local.hideLabel }}>
34
- {props.children}
35
- </Label>
36
- </Show>
37
- <Show when={local.description}>
38
- <Description data-slot="checkbox-checkbox-description">{local.description}</Description>
39
- </Show>
40
- <ErrorMessage data-slot="checkbox-checkbox-error" />
41
- </div>
42
- </Root>
29
+ <Switch>
30
+ <Match when={local.appearance === "compact"}>
31
+ <Root {...others} class={local.class} data-slot="checkbox-v2" data-surface={local.surface ?? "base"}>
32
+ <div data-slot="checkbox-v2-row">
33
+ <Input data-slot="checkbox-v2-input" />
34
+ <div data-slot="checkbox-v2-control-stack">
35
+ <Control data-slot="checkbox-v2-control">
36
+ <Indicator data-slot="checkbox-v2-indicator">
37
+ <svg
38
+ class="checkbox-v2-icon checkbox-v2-icon--check"
39
+ width="16"
40
+ height="16"
41
+ viewBox="0 0 16 16"
42
+ fill="none"
43
+ aria-hidden="true"
44
+ >
45
+ <path d="M3.53564 8.17857L6.39279 11.75L12.4642 4.25" stroke="currentColor" stroke-width="1" />
46
+ </svg>
47
+ <svg
48
+ class="checkbox-v2-icon checkbox-v2-icon--minus"
49
+ width="16"
50
+ height="16"
51
+ viewBox="0 0 16 16"
52
+ fill="none"
53
+ aria-hidden="true"
54
+ >
55
+ <path d="M12.75 8H3.25" stroke="currentColor" stroke-linejoin="round" stroke-width="1" />
56
+ </svg>
57
+ </Indicator>
58
+ </Control>
59
+ </div>
60
+ <div data-slot="checkbox-v2-text">
61
+ <Show when={local.children}>
62
+ <Label data-slot="checkbox-v2-label" classList={{ "sr-only": local.hideLabel }}>
63
+ {local.children}
64
+ </Label>
65
+ </Show>
66
+ <Show when={local.description}>
67
+ <Description data-slot="checkbox-v2-description">{local.description}</Description>
68
+ </Show>
69
+ </div>
70
+ </div>
71
+ <ErrorMessage data-slot="checkbox-v2-error" />
72
+ </Root>
73
+ </Match>
74
+ <Match when={true}>
75
+ <Root {...others} class={local.class} data-component="checkbox">
76
+ <Input data-slot="checkbox-checkbox-input" />
77
+ <Control data-slot="checkbox-checkbox-control">
78
+ <Indicator data-slot="checkbox-checkbox-indicator">
79
+ {local.icon || (
80
+ <svg viewBox="0 0 12 12" fill="none" width="10" height="10" xmlns="http://www.w3.org/2000/svg">
81
+ <path
82
+ d="M3 7.17905L5.02703 8.85135L9 3.5"
83
+ stroke="currentColor"
84
+ stroke-width="1.5"
85
+ stroke-linecap="square"
86
+ />
87
+ </svg>
88
+ )}
89
+ </Indicator>
90
+ </Control>
91
+ <div data-slot="checkbox-checkbox-content">
92
+ <Show when={props.children}>
93
+ <Label data-slot="checkbox-checkbox-label" classList={{ "sr-only": local.hideLabel }}>
94
+ {props.children}
95
+ </Label>
96
+ </Show>
97
+ <Show when={local.description}>
98
+ <Description data-slot="checkbox-checkbox-description">{local.description}</Description>
99
+ </Show>
100
+ <ErrorMessage data-slot="checkbox-checkbox-error" />
101
+ </div>
102
+ </Root>
103
+ </Match>
104
+ </Switch>
43
105
  )
44
106
  }
@@ -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>
@@ -0,0 +1,81 @@
1
+ /**
2
+ * The preview a `<Select>` shows for its highlighted option (`onHighlight`, which may return an undo). Unlike a one-shot
3
+ * palette, a select opens and closes repeatedly: choosing the previewed option keeps its preview, choosing another
4
+ * reverts it first, and closing without a choice reverts it. Nothing previews while the menu is closed or closing.
5
+ */
6
+ export type SelectHighlightState =
7
+ | { readonly status: "closed" }
8
+ | { readonly status: "idle" }
9
+ | { readonly status: "previewing"; readonly key: string }
10
+ | { readonly status: "committed" }
11
+ | { readonly status: "disposed" }
12
+
13
+ export type SelectHighlightEvent =
14
+ | { readonly type: "open.changed"; readonly open: boolean }
15
+ | { readonly type: "highlight.changed"; readonly key: string; readonly previewable: boolean }
16
+ /** `closing` is whether the menu closes on this choice (`closeOnSelection`). */
17
+ | { readonly type: "option.selected"; readonly key: string | undefined; readonly closing: boolean }
18
+ | { readonly type: "disposed" }
19
+
20
+ export type SelectHighlightEffect =
21
+ | { readonly type: "preview.apply"; readonly key: string }
22
+ | { readonly type: "preview.revert" }
23
+
24
+ export type SelectHighlightTransition = {
25
+ readonly state: SelectHighlightState
26
+ readonly effects: readonly SelectHighlightEffect[]
27
+ }
28
+
29
+ const CLOSED: SelectHighlightState = Object.freeze({ status: "closed" })
30
+
31
+ const IDLE: SelectHighlightState = Object.freeze({ status: "idle" })
32
+
33
+ const COMMITTED: SelectHighlightState = Object.freeze({ status: "committed" })
34
+
35
+ const DISPOSED: SelectHighlightState = Object.freeze({ status: "disposed" })
36
+
37
+ const NONE: readonly SelectHighlightEffect[] = Object.freeze([])
38
+
39
+ const REVERT: readonly SelectHighlightEffect[] = Object.freeze([{ type: "preview.revert" }])
40
+
41
+ export function createSelectHighlightState(open: boolean): SelectHighlightState {
42
+ return open ? IDLE : CLOSED
43
+ }
44
+
45
+ export function transitionSelectHighlight(
46
+ state: SelectHighlightState,
47
+ event: SelectHighlightEvent,
48
+ ): SelectHighlightTransition {
49
+ if (state.status === "disposed") return { state, effects: NONE }
50
+ const previewing = state.status === "previewing"
51
+
52
+ if (event.type === "disposed") return { state: DISPOSED, effects: previewing ? REVERT : NONE }
53
+
54
+ if (event.type === "open.changed") {
55
+ if (event.open)
56
+ return { state: state.status === "closed" || state.status === "committed" ? IDLE : state, effects: NONE }
57
+
58
+ return { state: CLOSED, effects: previewing ? REVERT : NONE }
59
+ }
60
+
61
+ if (event.type === "option.selected") {
62
+ // The chosen option's preview is the committed value; any other preview reverts before the caller commits.
63
+ const effects = previewing && (event.key === undefined || state.key !== event.key) ? REVERT : NONE
64
+
65
+ if (!event.closing) return { state: IDLE, effects }
66
+
67
+ return { state: state.status === "closed" ? CLOSED : COMMITTED, effects }
68
+ }
69
+
70
+ // A closed or closing menu still delivers focus and pointer events to its items (forceMount, exit animation).
71
+ if (state.status === "closed" || state.status === "committed") return { state, effects: NONE }
72
+
73
+ if (!event.previewable) return previewing ? { state: IDLE, effects: REVERT } : { state, effects: NONE }
74
+
75
+ if (previewing && state.key === event.key) return { state, effects: NONE }
76
+
77
+ return {
78
+ state: { status: "previewing", key: event.key },
79
+ effects: [...(previewing ? REVERT : NONE), { type: "preview.apply", key: event.key }],
80
+ }
81
+ }
@@ -32,10 +32,9 @@
32
32
  border: 0;
33
33
  border-radius: 6px;
34
34
  outline: 1px solid transparent;
35
- outline-offset: 0;
36
- background:
37
- linear-gradient(180deg, var(--v2-alpha-light-2) 0%, var(--v2-alpha-light-0) 100%), var(--v2-background-bg-base);
38
- box-shadow: var(--v2-elevation-button-neutral);
35
+ outline-offset: -1px;
36
+ background: var(--v2-background-bg-base);
37
+ box-shadow: var(--v2-elevation-input);
39
38
  flex: none;
40
39
  align-self: stretch;
41
40
  user-select: none;
@@ -45,21 +44,59 @@
45
44
  box-shadow 85ms ease-out;
46
45
  }
47
46
 
48
- [data-component="select-v2"]:where(:hover):not([data-disabled], [data-invalid]):not(:focus-within):not(
49
- [data-expanded]
50
- ) {
51
- background:
52
- linear-gradient(0deg, var(--v2-overlay-simple-overlay-hover), var(--v2-overlay-simple-overlay-hover)),
53
- linear-gradient(180deg, var(--v2-alpha-light-2) 0%, var(--v2-alpha-light-0) 100%), var(--v2-background-bg-base);
47
+ [data-component="select-v2"][data-appearance="small"] {
48
+ height: 24px;
49
+ }
50
+
51
+ [data-component="select-v2"][data-appearance="large"] {
52
+ height: 32px;
53
+ padding-inline: 0 8px;
54
+ }
55
+
56
+ [data-component="select-v2"][data-surface="layer-1"] {
57
+ background: var(--v2-background-bg-layer-01);
54
58
  }
55
59
 
56
- [data-component="select-v2"]:where([data-expanded]):not([data-disabled], [data-invalid]) {
57
- background:
58
- linear-gradient(0deg, var(--v2-overlay-simple-overlay-hover), var(--v2-overlay-simple-overlay-hover)),
59
- linear-gradient(180deg, var(--v2-alpha-light-2) 0%, var(--v2-alpha-light-0) 100%), var(--v2-background-bg-base);
60
+ [data-component="select-v2"][data-surface="invisible"] {
61
+ background: transparent;
62
+ box-shadow: none;
63
+ }
64
+
65
+ /* An inset overlay keeps the surface colour underneath, the same hover the text input uses. */
66
+ @media (hover: hover) {
67
+ [data-component="select-v2"]:where(:hover):not(
68
+ [data-disabled],
69
+ [data-invalid],
70
+ [data-expanded],
71
+ [data-appearance="inline"],
72
+ [data-surface="invisible"]
73
+ ):not(:focus-within) {
74
+ box-shadow:
75
+ inset 0 0 0 100vmax var(--v2-overlay-simple-overlay-hover),
76
+ var(--v2-elevation-input);
77
+ }
78
+
79
+ [data-component="select-v2"][data-surface="invisible"]:where(:hover):not(
80
+ [data-disabled],
81
+ [data-invalid],
82
+ [data-expanded]
83
+ ):not(:focus-within) {
84
+ background: var(--v2-overlay-simple-overlay-hover);
85
+ }
86
+ }
87
+
88
+ [data-component="select-v2"]:where([data-expanded]):not([data-disabled], [data-invalid], [data-appearance="inline"]) {
89
+ box-shadow:
90
+ inset 0 0 0 100vmax var(--v2-overlay-simple-overlay-hover),
91
+ var(--v2-elevation-input);
60
92
  outline-color: transparent;
61
93
  }
62
94
 
95
+ [data-component="select-v2"][data-surface="invisible"]:where([data-expanded]):not([data-disabled], [data-invalid]) {
96
+ background: var(--v2-overlay-simple-overlay-hover);
97
+ box-shadow: none;
98
+ }
99
+
63
100
  [data-component="select-v2"]:where(:focus-within):not([data-disabled], [data-invalid]):not([data-expanded]) {
64
101
  outline-color: var(--v2-border-border-focus);
65
102
  box-shadow: none;
@@ -196,6 +233,14 @@
196
233
  padding: 0;
197
234
  }
198
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
+
199
244
  /* Listbox inside menu surface */
200
245
  [data-component="menu-v2-content"][data-slot="select-v2-content"] [data-slot="select-v2-listbox"] {
201
246
  box-sizing: border-box;
@@ -207,9 +252,6 @@
207
252
  list-style: none;
208
253
  min-width: 0;
209
254
  width: 100%;
210
- max-height: 12rem;
211
- overflow-x: hidden;
212
- overflow-y: auto;
213
255
  outline: none;
214
256
  white-space: nowrap;
215
257
 
@@ -10,7 +10,19 @@ import {
10
10
  Trigger,
11
11
  Value,
12
12
  } from "@kobalte/core/select"
13
- import { Show, createMemo, onCleanup, splitProps, type ComponentProps, type JSX } from "solid-js"
13
+ import {
14
+ Show,
15
+ createComputed,
16
+ createMemo,
17
+ on,
18
+ onCleanup,
19
+ splitProps,
20
+ untrack,
21
+ type ComponentProps,
22
+ type JSX,
23
+ } from "solid-js"
24
+ import { MenuScroll } from "../../components/menu-scroll"
25
+ import { createSelectHighlightState, transitionSelectHighlight, type SelectHighlightEvent } from "./machine"
14
26
  import "./select.css"
15
27
 
16
28
  function groupOptions<T>(options: T[], groupBy?: (x: T) => string): { category: string; options: T[] }[] {
@@ -70,6 +82,10 @@ export type SelectProps<T> = Omit<
70
82
  onHighlight?: (value: T | undefined) => void | (() => void)
71
83
  invalid?: boolean
72
84
  numeric?: boolean
85
+ /** `inline` is the borderless settings trigger; `small`, `base` and `large` are 24px, 28px and 32px input fields. */
86
+ appearance?: "inline" | "small" | "base" | "large"
87
+ /** Background surface of an input field; invisible gains a fill on hover. */
88
+ surface?: "base" | "layer-1" | "invisible"
73
89
  children?: (item: T) => JSX.Element
74
90
  valueClass?: string
75
91
  contentClass?: string
@@ -91,6 +107,8 @@ export function Select<T>(props: SelectProps<T>) {
91
107
  "children",
92
108
  "invalid",
93
109
  "numeric",
110
+ "appearance",
111
+ "surface",
94
112
  "disabled",
95
113
  "valueClass",
96
114
  "contentClass",
@@ -102,34 +120,46 @@ export function Select<T>(props: SelectProps<T>) {
102
120
  "fitViewport",
103
121
  ])
104
122
 
105
- const state: { key?: string; cleanup?: void | (() => void) } = {}
106
-
107
- const stop = () => {
108
- state.cleanup?.()
109
- state.cleanup = undefined
110
- state.key = undefined
111
- }
123
+ let highlight = createSelectHighlightState(untrack(() => others.open ?? others.defaultOpen ?? false))
124
+ // The highlighted item and the undo its preview returned; the machine decides when each runs.
125
+ let highlighted: T | undefined
126
+ let revert: void | (() => void)
112
127
 
113
- const keyFor = (item: T) => (local.value ? local.value(item) : String(item as string))
128
+ let scroller: HTMLDivElement | undefined
114
129
 
115
- const move = (item: T | undefined) => {
116
- if (!local.onHighlight) return
130
+ const dispatch = (event: SelectHighlightEvent) => {
131
+ const next = transitionSelectHighlight(highlight, event)
132
+ highlight = next.state
133
+ next.effects.forEach((effect) => {
134
+ if (effect.type === "preview.revert") {
135
+ const undo = revert
136
+ revert = undefined
117
137
 
118
- if (!item) {
119
- stop()
138
+ return undo?.()
139
+ }
120
140
 
121
- return
122
- }
141
+ revert = highlighted === undefined ? undefined : local.onHighlight?.(highlighted)
142
+ })
143
+ }
123
144
 
124
- const key = keyFor(item)
145
+ const keyFor = (item: T) => (local.value ? local.value(item) : String(item))
125
146
 
126
- if (state.key === key) return
127
- state.cleanup?.()
128
- state.cleanup = local.onHighlight(item)
129
- state.key = key
147
+ const move = (item: T) => {
148
+ highlighted = item
149
+ dispatch({ type: "highlight.changed", key: keyFor(item), previewable: !!local.onHighlight })
130
150
  }
131
151
 
132
- onCleanup(stop)
152
+ // A controlled menu closed by its owner does not call onOpenChange.
153
+ createComputed(
154
+ on(
155
+ () => others.open,
156
+ (open) => {
157
+ if (open !== undefined) dispatch({ type: "open.changed", open })
158
+ },
159
+ ),
160
+ )
161
+
162
+ onCleanup(() => dispatch({ type: "disposed" }))
133
163
 
134
164
  const grouped = createMemo(() => groupOptions(local.options, local.groupBy))
135
165
 
@@ -148,8 +178,8 @@ export function Select<T>(props: SelectProps<T>) {
148
178
  fitViewport={local.fitViewport ?? false}
149
179
  value={local.current}
150
180
  options={grouped()}
151
- optionValue={(x) => (local.value ? local.value(x) : String(x as string))}
152
- optionTextValue={(x) => (local.label ? local.label(x) : String(x as string))}
181
+ optionValue={(x) => (local.value ? local.value(x) : String(x))}
182
+ optionTextValue={(x) => (local.label ? local.label(x) : String(x))}
153
183
  optionGroupChildren="options"
154
184
  placeholder={local.placeholder}
155
185
  sectionComponent={(sectionProps) => (
@@ -172,7 +202,7 @@ export function Select<T>(props: SelectProps<T>) {
172
202
  ? local.children(itemProps.item.rawValue)
173
203
  : local.label
174
204
  ? local.label(itemProps.item.rawValue)
175
- : String(itemProps.item.rawValue as string)}
205
+ : String(itemProps.item.rawValue)}
176
206
  </ItemLabel>
177
207
  <ItemIndicator data-slot="menu-v2-item-indicator" forceMount>
178
208
  <CheckSmall />
@@ -180,14 +210,17 @@ export function Select<T>(props: SelectProps<T>) {
180
210
  </Item>
181
211
  )}
182
212
  onChange={(next) => {
183
- const v = next == null ? null : Array.isArray(next) ? ((next[0] as T) ?? null) : (next as T)
213
+ const v = next == null ? null : Array.isArray(next) ? (next[0] ?? null) : next
214
+ dispatch({
215
+ type: "option.selected",
216
+ key: v === null ? undefined : keyFor(v),
217
+ closing: others.closeOnSelection ?? true,
218
+ })
184
219
  local.onSelect?.(v)
185
- stop()
186
220
  }}
187
221
  onOpenChange={(open) => {
222
+ if (others.open === undefined) dispatch({ type: "open.changed", open })
188
223
  local.onOpenChange?.(open)
189
-
190
- if (!open) stop()
191
224
  }}
192
225
  >
193
226
  <Trigger
@@ -195,7 +228,8 @@ export function Select<T>(props: SelectProps<T>) {
195
228
  aria-label={props["aria-label"]}
196
229
  aria-labelledby={props["aria-labelledby"]}
197
230
  data-component="select-v2"
198
- data-appearance="inline"
231
+ data-appearance={local.appearance ?? "inline"}
232
+ data-surface={local.appearance && local.appearance !== "inline" ? (local.surface ?? "base") : undefined}
199
233
  data-invalid={local.invalid ? "" : undefined}
200
234
  data-numeric={local.numeric ? "" : undefined}
201
235
  disabled={local.disabled}
@@ -212,7 +246,7 @@ export function Select<T>(props: SelectProps<T>) {
212
246
 
213
247
  if (local.label && selected != null) return local.label(selected)
214
248
 
215
- return selected != null ? (selected as string) : ""
249
+ return selected != null ? String(selected) : ""
216
250
  }}
217
251
  </Value>
218
252
  </div>
@@ -222,7 +256,9 @@ export function Select<T>(props: SelectProps<T>) {
222
256
  </Trigger>
223
257
  <Portal>
224
258
  <Content class={local.contentClass} data-component="menu-v2-content" data-slot="select-v2-content">
225
- <Listbox data-slot="select-v2-listbox" />
259
+ <MenuScroll viewportRef={(element) => (scroller = element)}>
260
+ <Listbox data-slot="select-v2-listbox" scrollRef={() => scroller} />
261
+ </MenuScroll>
226
262
  </Content>
227
263
  </Portal>
228
264
  </Root>