@svgrid/grid 2.1.22 → 2.2.0

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 (295) hide show
  1. package/README.md +9 -2
  2. package/dist/DockNodeView.svelte +306 -0
  3. package/dist/DockNodeView.svelte.d.ts +7 -0
  4. package/dist/DockPopoutHost.svelte +17 -0
  5. package/dist/DockPopoutHost.svelte.d.ts +14 -0
  6. package/dist/FlexRender.svelte +96 -96
  7. package/dist/GridFooter.svelte +178 -178
  8. package/dist/GridMenus.svelte +83 -3
  9. package/dist/SvAutoComplete.svelte +5 -3
  10. package/dist/SvCarousel.svelte +141 -163
  11. package/dist/SvColorInput.svelte +187 -186
  12. package/dist/SvColorInput.svelte.d.ts +1 -1
  13. package/dist/SvComboBox.svelte +7 -3
  14. package/dist/SvCommand.svelte +28 -85
  15. package/dist/SvContextMenu.svelte +116 -116
  16. package/dist/SvCountryInput.svelte +1 -1
  17. package/dist/SvDateRangeInput.svelte +1 -1
  18. package/dist/SvDateTimePicker.svelte +9 -3
  19. package/dist/SvDateTimePicker.svelte.d.ts +3 -0
  20. package/dist/SvDockLayout.svelte +244 -0
  21. package/dist/SvDockLayout.svelte.d.ts +77 -0
  22. package/dist/SvDockManager.svelte +922 -0
  23. package/dist/SvDockManager.svelte.d.ts +106 -0
  24. package/dist/SvDrawer.svelte +250 -228
  25. package/dist/SvDrawer.svelte.d.ts +4 -0
  26. package/dist/SvDropDownList.svelte +85 -13
  27. package/dist/SvDropDownList.svelte.d.ts +3 -0
  28. package/dist/SvField.svelte +21 -4
  29. package/dist/SvField.svelte.d.ts +1 -0
  30. package/dist/SvForm.svelte +201 -91
  31. package/dist/SvForm.svelte.d.ts +15 -22
  32. package/dist/SvGrid.controller.svelte.d.ts +32 -1
  33. package/dist/SvGrid.controller.svelte.js +156 -27
  34. package/dist/SvGrid.css +193 -5
  35. package/dist/SvGrid.svelte +3131 -2786
  36. package/dist/SvGrid.types.d.ts +452 -1
  37. package/dist/SvGridBoard.svelte +2339 -2339
  38. package/dist/SvGridChart.svelte +1724 -1724
  39. package/dist/SvGridChartPanel.svelte +485 -485
  40. package/dist/SvGridDropdown.svelte +689 -682
  41. package/dist/SvGridSelect.svelte +1 -0
  42. package/dist/SvGroupCell.svelte +109 -109
  43. package/dist/SvListBox.svelte +36 -14
  44. package/dist/SvMaskedInput.svelte +4 -3
  45. package/dist/SvMaskedInput.svelte.d.ts +1 -1
  46. package/dist/SvMenu.svelte +124 -124
  47. package/dist/SvMenuList.svelte +117 -165
  48. package/dist/SvMenuList.svelte.d.ts +2 -13
  49. package/dist/SvModal.svelte +12 -29
  50. package/dist/SvMultiSelect.svelte +1 -0
  51. package/dist/SvNumberInput.svelte +131 -130
  52. package/dist/SvNumberInput.svelte.d.ts +1 -1
  53. package/dist/SvOtpInput.svelte +158 -158
  54. package/dist/SvPagination.svelte +18 -18
  55. package/dist/SvPasswordInput.svelte +3 -2
  56. package/dist/SvPasswordInput.svelte.d.ts +1 -1
  57. package/dist/SvPhoneInput.svelte +3 -2
  58. package/dist/SvPhoneInput.svelte.d.ts +1 -1
  59. package/dist/SvPopover.svelte +139 -139
  60. package/dist/SvRowGroupPanel.svelte +170 -170
  61. package/dist/SvScrollArea.svelte +61 -61
  62. package/dist/SvStepper.svelte +6 -9
  63. package/dist/SvTagsInput.svelte +3 -2
  64. package/dist/SvTagsInput.svelte.d.ts +1 -1
  65. package/dist/SvTooltip.svelte +127 -138
  66. package/dist/SvTour.svelte +204 -176
  67. package/dist/SvTree.svelte +377 -377
  68. package/dist/SvTreeSelect.svelte +230 -229
  69. package/dist/build-api.js +76 -54
  70. package/dist/cdn/svgrid.js +10913 -6450
  71. package/dist/cdn/svgrid.svelte-external.js +9628 -5481
  72. package/dist/chart-export.js +8 -8
  73. package/dist/columns.js +6 -0
  74. package/dist/conditional-formatting.d.ts +67 -3
  75. package/dist/conditional-formatting.js +124 -17
  76. package/dist/createCarousel.svelte.d.ts +72 -0
  77. package/dist/createCarousel.svelte.js +80 -0
  78. package/dist/createCommand.svelte.d.ts +62 -0
  79. package/dist/createCommand.svelte.js +109 -0
  80. package/dist/createForm.svelte.d.ts +62 -0
  81. package/dist/createForm.svelte.js +374 -0
  82. package/dist/createMenu.svelte.d.ts +88 -0
  83. package/dist/createMenu.svelte.js +153 -0
  84. package/dist/createOverlay.svelte.d.ts +38 -0
  85. package/dist/createOverlay.svelte.js +68 -0
  86. package/dist/createPagination.d.ts +74 -0
  87. package/dist/createPagination.js +71 -0
  88. package/dist/createPopoverSelect.svelte.js +6 -12
  89. package/dist/createStepper.d.ts +44 -0
  90. package/dist/createStepper.js +26 -0
  91. package/dist/createTooltip.svelte.d.ts +32 -0
  92. package/dist/createTooltip.svelte.js +61 -0
  93. package/dist/datetime/timezone.d.ts +36 -0
  94. package/dist/datetime/timezone.js +110 -0
  95. package/dist/dock-context.d.ts +52 -0
  96. package/dist/dock-context.js +1 -0
  97. package/dist/dock-manager-model.d.ts +136 -0
  98. package/dist/dock-manager-model.js +514 -0
  99. package/dist/dock-model.d.ts +105 -0
  100. package/dist/dock-model.js +288 -0
  101. package/dist/dock-popout.d.ts +30 -0
  102. package/dist/dock-popout.js +81 -0
  103. package/dist/editor-contract.d.ts +2 -0
  104. package/dist/filter-operators.d.ts +6 -0
  105. package/dist/filter-operators.js +35 -2
  106. package/dist/filtering/excel-filters.d.ts +18 -1
  107. package/dist/filtering/excel-filters.js +56 -0
  108. package/dist/form-field.d.ts +73 -0
  109. package/dist/form-field.js +15 -0
  110. package/dist/index.d.ts +25 -5
  111. package/dist/index.js +40 -4
  112. package/dist/list-nav.d.ts +18 -0
  113. package/dist/list-nav.js +30 -0
  114. package/dist/list-option.d.ts +2 -0
  115. package/dist/menu-item.d.ts +20 -0
  116. package/dist/menu-item.js +1 -0
  117. package/dist/menus.d.ts +5 -0
  118. package/dist/menus.js +68 -16
  119. package/dist/panel-resize.d.ts +25 -0
  120. package/dist/panel-resize.js +40 -0
  121. package/dist/popover.d.ts +29 -3
  122. package/dist/popover.js +22 -6
  123. package/dist/recurrence.d.ts +33 -7
  124. package/dist/recurrence.js +151 -13
  125. package/dist/row-drag.d.ts +8 -0
  126. package/dist/row-drag.js +11 -0
  127. package/dist/scheduler-ical.d.ts +30 -0
  128. package/dist/scheduler-ical.js +180 -0
  129. package/dist/scheduler-model.d.ts +269 -0
  130. package/dist/scheduler-model.js +602 -0
  131. package/dist/scheduler-view.svelte.d.ts +22 -0
  132. package/dist/scheduler-view.svelte.js +13 -0
  133. package/dist/spreadsheet.js +20 -12
  134. package/dist/ui-app.types.d.ts +3 -0
  135. package/package.json +14 -10
  136. package/src/DockNodeView.svelte +306 -0
  137. package/src/DockPopoutHost.svelte +17 -0
  138. package/src/FlexRender.svelte +96 -96
  139. package/src/GridFooter.svelte +178 -178
  140. package/src/GridMenus.svelte +83 -3
  141. package/src/SvAutoComplete.svelte +5 -3
  142. package/src/SvCarousel.svelte +141 -163
  143. package/src/SvCarousel.test.ts +68 -0
  144. package/src/SvColorInput.svelte +187 -186
  145. package/src/SvComboBox.svelte +7 -3
  146. package/src/SvCommand.svelte +28 -85
  147. package/src/SvCommand.test.ts +78 -0
  148. package/src/SvContextMenu.svelte +116 -116
  149. package/src/SvCountryInput.svelte +1 -1
  150. package/src/SvDateRangeInput.svelte +1 -1
  151. package/src/SvDateTimePicker.svelte +9 -3
  152. package/src/SvDockLayout.svelte +244 -0
  153. package/src/SvDockManager.svelte +922 -0
  154. package/src/SvDrawer.svelte +250 -228
  155. package/src/SvDropDownList.svelte +85 -13
  156. package/src/SvField.svelte +21 -4
  157. package/src/SvForm.svelte +201 -91
  158. package/src/SvForm.test.ts +299 -0
  159. package/src/SvGrid.controller.svelte.ts +3158 -3034
  160. package/src/SvGrid.css +193 -5
  161. package/src/SvGrid.svelte +3131 -2786
  162. package/src/SvGrid.types.ts +1489 -1027
  163. package/src/SvGridBoard.svelte +2339 -2339
  164. package/src/SvGridChart.svelte +1724 -1724
  165. package/src/SvGridChartPanel.svelte +485 -485
  166. package/src/SvGridDropdown.svelte +689 -682
  167. package/src/SvGridSelect.svelte +1 -0
  168. package/src/SvGroupCell.svelte +109 -109
  169. package/src/SvListBox.svelte +36 -14
  170. package/src/SvMaskedInput.svelte +4 -3
  171. package/src/SvMenu.svelte +124 -124
  172. package/src/SvMenu.test.ts +97 -73
  173. package/src/SvMenuList.svelte +117 -165
  174. package/src/SvModal.svelte +12 -29
  175. package/src/SvModal.test.ts +82 -0
  176. package/src/SvMultiSelect.svelte +1 -0
  177. package/src/SvNumberInput.svelte +131 -130
  178. package/src/SvOtpInput.svelte +158 -158
  179. package/src/SvPagination.svelte +18 -18
  180. package/src/SvPasswordInput.svelte +3 -2
  181. package/src/SvPhoneInput.svelte +3 -2
  182. package/src/SvPopover.svelte +139 -139
  183. package/src/SvRowGroupPanel.svelte +170 -170
  184. package/src/SvScrollArea.svelte +61 -61
  185. package/src/SvStepper.svelte +6 -9
  186. package/src/SvTagsInput.svelte +3 -2
  187. package/src/SvTooltip.svelte +127 -138
  188. package/src/SvTooltip.test.ts +73 -0
  189. package/src/SvTour.svelte +204 -176
  190. package/src/SvTree.svelte +377 -377
  191. package/src/SvTreeSelect.svelte +230 -229
  192. package/src/a11y.contract.test.ts +49 -49
  193. package/src/a11y.test.ts +59 -59
  194. package/src/a11y.ts +61 -61
  195. package/src/build-api.ts +870 -847
  196. package/src/cell-formatting.ts +169 -169
  197. package/src/cell-render.ts +469 -469
  198. package/src/chart-export.ts +201 -201
  199. package/src/chart.ts +2296 -2296
  200. package/src/collaboration.test.ts +104 -104
  201. package/src/collaboration.ts +167 -167
  202. package/src/columns.ts +5 -0
  203. package/src/conditional-formatting.test.ts +104 -0
  204. package/src/conditional-formatting.ts +176 -19
  205. package/src/core.performance.test.ts +30 -30
  206. package/src/core.ts +1111 -1111
  207. package/src/createAutocomplete.svelte.ts +132 -132
  208. package/src/createCarousel.svelte.ts +104 -0
  209. package/src/createCombobox.svelte.ts +179 -179
  210. package/src/createCommand.svelte.ts +125 -0
  211. package/src/createCountryInput.svelte.ts +157 -157
  212. package/src/createDropdownList.svelte.ts +162 -162
  213. package/src/createForm.svelte.ts +333 -0
  214. package/src/createGrid.svelte.ts +42 -42
  215. package/src/createGrid.test.ts +10 -10
  216. package/src/createGridState.svelte.ts +17 -17
  217. package/src/createMenu.svelte.ts +202 -0
  218. package/src/createOverlay.svelte.ts +101 -0
  219. package/src/createPagination.test.ts +65 -0
  220. package/src/createPagination.ts +84 -0
  221. package/src/createPopoverSelect.svelte.ts +206 -210
  222. package/src/createStepper.test.ts +62 -0
  223. package/src/createStepper.ts +61 -0
  224. package/src/createTooltip.svelte.ts +86 -0
  225. package/src/createTree.svelte.ts +319 -319
  226. package/src/datetime/timezone.test.ts +69 -0
  227. package/src/datetime/timezone.ts +134 -0
  228. package/src/dock-context.ts +47 -0
  229. package/src/dock-manager-model.test.ts +275 -0
  230. package/src/dock-manager-model.ts +593 -0
  231. package/src/dock-model.test.ts +163 -0
  232. package/src/dock-model.ts +368 -0
  233. package/src/dock-popout.ts +94 -0
  234. package/src/editing.test.ts +859 -859
  235. package/src/editing.ts +675 -675
  236. package/src/editor-contract.ts +2 -0
  237. package/src/export-data-api.test.ts +126 -126
  238. package/src/export-format.test.ts +107 -107
  239. package/src/export-format.ts +598 -598
  240. package/src/filter-operators.test.ts +59 -0
  241. package/src/filter-operators.ts +35 -2
  242. package/src/filtering/excel-filters.test.ts +86 -1
  243. package/src/filtering/excel-filters.ts +65 -0
  244. package/src/flex-render.ts +3 -3
  245. package/src/form-field.ts +87 -0
  246. package/src/index.ts +623 -469
  247. package/src/keyboard.test.ts +59 -59
  248. package/src/keyboard.ts +97 -97
  249. package/src/list-nav.test.ts +49 -0
  250. package/src/list-nav.ts +29 -0
  251. package/src/list-option.ts +2 -0
  252. package/src/menu-item.ts +21 -0
  253. package/src/menus.test.ts +48 -0
  254. package/src/menus.ts +639 -582
  255. package/src/merge-objects.ts +48 -48
  256. package/src/overlays.test.ts +90 -90
  257. package/src/panel-resize.ts +52 -0
  258. package/src/popover.bounds.test.ts +93 -0
  259. package/src/popover.ts +51 -7
  260. package/src/recurrence.test.ts +88 -1
  261. package/src/recurrence.ts +166 -15
  262. package/src/render-component.ts +28 -28
  263. package/src/row-drag.ts +10 -0
  264. package/src/scheduler-ical.test.ts +68 -0
  265. package/src/scheduler-ical.ts +180 -0
  266. package/src/scheduler-model.test.ts +546 -0
  267. package/src/scheduler-model.ts +853 -0
  268. package/src/scheduler-view.svelte.ts +33 -0
  269. package/src/selection.test.ts +754 -754
  270. package/src/selection.ts +600 -600
  271. package/src/server-data-source.test.ts +289 -289
  272. package/src/server-data-source.ts +413 -413
  273. package/src/sparkline.test.ts +68 -68
  274. package/src/sparkline.ts +169 -169
  275. package/src/spreadsheet.test.ts +489 -489
  276. package/src/spreadsheet.ts +312 -304
  277. package/src/static-functions.ts +11 -11
  278. package/src/subscribe.ts +38 -38
  279. package/src/svgrid-wrapper.types.ts +501 -501
  280. package/src/svgrid.behavior.test.ts +706 -706
  281. package/src/svgrid.charting.test.ts +527 -527
  282. package/src/svgrid.effect-driven-filter.svelte.test.ts +116 -0
  283. package/src/svgrid.features.test.ts +157 -157
  284. package/src/svgrid.new-features.wrapper.test.ts +251 -251
  285. package/src/svgrid.wrapper.test.ts +40 -40
  286. package/src/test-setup.ts +62 -62
  287. package/src/themes/index.ts +159 -159
  288. package/src/ui-app.types.ts +3 -0
  289. package/src/virtual.test.ts +6 -4
  290. package/src/virtualization/column-virtualizer.test.ts +27 -27
  291. package/src/virtualization/column-virtualizer.ts +30 -30
  292. package/src/virtualization/svelte-virtualizer.svelte.ts +26 -26
  293. package/src/virtualization/types.ts +30 -30
  294. package/src/virtualization/virtualizer.test.ts +47 -47
  295. package/src/virtualization/virtualizer.ts +296 -296
@@ -1,682 +1,689 @@
1
- <script lang="ts">
2
- /**
3
- * Internal dropdown / listbox control used by the `list` and `chips` cell
4
- * editors. We deliberately don't reuse the browser-native `<select>` -
5
- * the native dropdown can't be styled to match the grid theme, picks up
6
- * the OS color scheme, and on Windows draws the option list with an
7
- * opaque white system surface that fights the dark theme.
8
- *
9
- * Modes:
10
- * single - picking an option fires `onChange(value)` and `onCommit()`.
11
- * multi - toggling fires `onChange(values: array)`; the user
12
- * presses Enter / clicks "Done" to fire `onCommit()`.
13
- *
14
- * Keyboard model:
15
- * ArrowDown / ArrowUp - move highlight (opens the panel if closed).
16
- * Enter - single: toggle highlighted + commit.
17
- * multi: toggle highlighted (stay open).
18
- * Escape - close + onCancel.
19
- * Tab - commit and let focus escape.
20
- */
21
- import type { CellEditorOption } from './editors/cell-editors'
22
- import { anchoredRect, portalToBody, popIn, type AnchoredRect } from './popover'
23
-
24
- type Props = {
25
- options: ReadonlyArray<CellEditorOption>
26
- value: unknown
27
- multiple?: boolean
28
- placeholder?: string
29
- /** When provided, the trigger renders as removable chips of the current value(s). */
30
- renderChipsInTrigger?: boolean
31
- /** Add a typeahead input at the top of the popover that filters the
32
- * option list as the user types. Used by the rich-select editor. */
33
- searchable?: boolean
34
- /** Focus the trigger and open the panel on mount. True for in-cell editing
35
- * (you clicked into the cell to edit); set false for form fields, which
36
- * should stay closed and unfocused until the user acts. Default true. */
37
- autoOpen?: boolean
38
- /** Called with the new full value (scalar for single, array for multi). */
39
- onChange?: (next: unknown) => void
40
- /** Called when the user finalizes the selection (single pick, Enter, blur with selection). */
41
- onCommit?: () => void
42
- /** Called when the user dismisses (Escape, blur with no change). */
43
- onCancel?: () => void
44
- }
45
-
46
- let {
47
- options,
48
- value,
49
- multiple = false,
50
- placeholder = 'Select…',
51
- renderChipsInTrigger = false,
52
- searchable = false,
53
- autoOpen = true,
54
- onChange,
55
- onCommit,
56
- onCancel,
57
- }: Props = $props()
58
-
59
- /** Stable per-instance id used to build option ids for
60
- * `aria-activedescendant` - screen readers need a real id to announce
61
- * the highlighted option while arrow-keying through the list. */
62
- const instanceId = $props.id()
63
- function optionId(index: number): string | undefined {
64
- return index >= 0 && index < visibleOptions.length ? `${instanceId}-opt-${index}` : undefined
65
- }
66
-
67
- let searchQuery = $state('')
68
- const visibleOptions = $derived(
69
- !searchable || !searchQuery.trim()
70
- ? options
71
- : (() => {
72
- const q = searchQuery.trim().toLowerCase()
73
- return options.filter((o) => o.label.toLowerCase().includes(q))
74
- })(),
75
- )
76
-
77
- let open = $state(false)
78
- let highlighted = $state(-1)
79
- let rootEl: HTMLDivElement | null = $state(null)
80
- let triggerEl: HTMLButtonElement | null = $state(null)
81
- let panelEl: HTMLDivElement | null = $state(null)
82
-
83
- /** Viewport-anchored panel position. We `position: fixed` the panel so it
84
- * escapes the grid's overflow:hidden scroll container - otherwise the
85
- * bottom of the list gets clipped by the pager or the grid's footer. */
86
- let panelRect = $state<AnchoredRect>({
87
- top: 0,
88
- left: 0,
89
- width: 0,
90
- openUpward: false,
91
- })
92
-
93
- const selectedArr = $derived(
94
- Array.isArray(value)
95
- ? (value as Array<string | number>)
96
- : value == null || value === ''
97
- ? []
98
- : [value as string | number],
99
- )
100
-
101
- const triggerSummary = $derived.by(() => {
102
- if (selectedArr.length === 0) return placeholder
103
- if (!multiple) {
104
- const v = selectedArr[0]!
105
- const match = options.find((o) => String(o.value) === String(v))
106
- return match ? match.label : String(v)
107
- }
108
- return `${selectedArr.length} selected`
109
- })
110
-
111
- /** Color of the single selected option, if any (for colored trigger). */
112
- const selectedColor = $derived.by(() => {
113
- if (multiple || selectedArr.length !== 1) return undefined
114
- const v = selectedArr[0]!
115
- return options.find((o) => String(o.value) === String(v))?.color
116
- })
117
-
118
- function isSelected(opt: CellEditorOption): boolean {
119
- return selectedArr.some((v) => String(v) === String(opt.value))
120
- }
121
-
122
- function toggleOption(opt: CellEditorOption) {
123
- if (multiple) {
124
- const exists = isSelected(opt)
125
- const next = exists
126
- ? selectedArr.filter((v) => String(v) !== String(opt.value))
127
- : [...selectedArr, opt.value]
128
- onChange?.(next)
129
- } else {
130
- onChange?.(opt.value)
131
- open = false
132
- onCommit?.()
133
- }
134
- }
135
-
136
- function openPanel() {
137
- if (open) return
138
- open = true
139
- // Seed the highlight to the first selected option, or top of the list.
140
- if (highlighted < 0) {
141
- const idx = options.findIndex((o) => isSelected(o))
142
- highlighted = idx >= 0 ? idx : 0
143
- }
144
- updatePanelPosition()
145
- }
146
-
147
- /** Recompute the panel's fixed-position rect from the trigger. Auto-
148
- * flips upward when there isn't enough room below the trigger. */
149
- function updatePanelPosition() {
150
- if (!triggerEl) return
151
- // Use the smaller of (option count, 10) for the upward-flip math so
152
- // a 3-option list doesn't think it needs 320px of headroom.
153
- const visibleCount = Math.min(options.length, 10)
154
- const estimatedHeight = visibleCount * 32 + (multiple ? 40 : 0) + 8
155
- panelRect = anchoredRect(triggerEl.getBoundingClientRect(), {
156
- estimatedHeight,
157
- // Preserve the original behavior: anchor to the trigger, no clamping.
158
- clampHorizontal: false,
159
- })
160
- }
161
-
162
- // Keep the panel anchored as the page scrolls or resizes - without this,
163
- // scrolling the underlying grid would leave the panel hanging in space.
164
- $effect(() => {
165
- if (!open) return
166
- const reposition = () => updatePanelPosition()
167
- window.addEventListener('scroll', reposition, true)
168
- window.addEventListener('resize', reposition)
169
- return () => {
170
- window.removeEventListener('scroll', reposition, true)
171
- window.removeEventListener('resize', reposition)
172
- }
173
- })
174
-
175
- function closePanel() {
176
- open = false
177
- highlighted = -1
178
- }
179
-
180
- function onKeyDown(event: KeyboardEvent) {
181
- event.stopPropagation()
182
- if (event.key === 'Escape') {
183
- event.preventDefault()
184
- closePanel()
185
- onCancel?.()
186
- return
187
- }
188
- if (event.key === 'ArrowDown') {
189
- event.preventDefault()
190
- if (!open) {
191
- openPanel()
192
- return
193
- }
194
- highlighted = Math.min(highlighted + 1, options.length - 1)
195
- scrollHighlightedIntoView()
196
- } else if (event.key === 'ArrowUp') {
197
- event.preventDefault()
198
- if (!open) {
199
- openPanel()
200
- return
201
- }
202
- highlighted = Math.max(highlighted - 1, 0)
203
- scrollHighlightedIntoView()
204
- } else if (event.key === 'Home') {
205
- if (!open) return
206
- event.preventDefault()
207
- highlighted = 0
208
- scrollHighlightedIntoView()
209
- } else if (event.key === 'End') {
210
- if (!open) return
211
- event.preventDefault()
212
- highlighted = options.length - 1
213
- scrollHighlightedIntoView()
214
- } else if (event.key === 'Enter') {
215
- event.preventDefault()
216
- if (!open) {
217
- openPanel()
218
- return
219
- }
220
- const opt = options[highlighted]
221
- if (opt) toggleOption(opt)
222
- else if (multiple) {
223
- closePanel()
224
- onCommit?.()
225
- }
226
- } else if (event.key === ' ' && open) {
227
- event.preventDefault()
228
- const opt = options[highlighted]
229
- if (opt) toggleOption(opt)
230
- } else if (event.key === 'Tab') {
231
- if (open) closePanel()
232
- onCommit?.()
233
- }
234
- }
235
-
236
- function scrollHighlightedIntoView() {
237
- queueMicrotask(() => {
238
- if (!panelEl) return
239
- const el = panelEl.querySelector<HTMLElement>(`[data-opt-idx="${highlighted}"]`)
240
- el?.scrollIntoView({ block: 'nearest' })
241
- })
242
- }
243
-
244
- function onRootBlur(event: FocusEvent) {
245
- // Defer to the next tick so a click on an option (mousedown → option
246
- // commits, then blur) doesn't race-cancel the selection.
247
- const next = event.relatedTarget as Node | null
248
- if (next && rootEl?.contains(next)) return
249
- if (open) closePanel()
250
- onCommit?.()
251
- }
252
-
253
- function removeChip(v: string | number, event: MouseEvent) {
254
- event.preventDefault()
255
- event.stopPropagation()
256
- const next = selectedArr.filter((x) => String(x) !== String(v))
257
- onChange?.(multiple ? next : next[0] ?? null)
258
- }
259
-
260
- function focusTrigger(node: HTMLButtonElement) {
261
- // Form fields (autoOpen=false) stay closed + unfocused until the user acts;
262
- // in-cell editing auto-focuses + opens to match clicking into a cell.
263
- if (!autoOpen) return
264
- node.focus()
265
- openPanel()
266
- }
267
-
268
- /** Close the panel when the user clicks outside both trigger AND
269
- * panel - `onfocusout` on the dropdown root no longer catches this
270
- * because the portal'd panel is a sibling of body, not a descendant
271
- * of rootEl. Wired up only while the panel is open. */
272
- $effect(() => {
273
- if (!open) return
274
- const onDocPointerDown = (event: PointerEvent) => {
275
- const target = event.target as Node | null
276
- if (!target) return
277
- if (rootEl?.contains(target)) return
278
- if (panelEl?.contains(target)) return
279
- closePanel()
280
- onCommit?.()
281
- }
282
- document.addEventListener('pointerdown', onDocPointerDown, true)
283
- return () => document.removeEventListener('pointerdown', onDocPointerDown, true)
284
- })
285
-
286
- /** Inline style for a chip with a configured `color`. Mirrors the
287
- * helper in SvGrid.svelte so colorful chips look identical in the
288
- * trigger and the in-cell readonly render. */
289
- function colorfulChipStyleInline(color: string): string {
290
- return (
291
- `background: color-mix(in srgb, ${color} 22%, transparent);` +
292
- `border-color: color-mix(in srgb, ${color} 45%, transparent);` +
293
- `color: color-mix(in srgb, ${color} 80%, var(--sg-fg, #0f172a));`
294
- )
295
- }
296
- </script>
297
-
298
- <!-- All pointer/click events are swallowed at the root so they never reach
299
- the surrounding cell's selection / pointerdown handlers - picking a
300
- dropdown option must not also toggle cell selection. -->
301
- <div
302
- class="sv-grid-dropdown"
303
- class:sv-grid-dropdown-open={open}
304
- bind:this={rootEl}
305
- onfocusout={onRootBlur}
306
- onclick={(event) => event.stopPropagation()}
307
- ondblclick={(event) => event.stopPropagation()}
308
- onpointerdown={(event) => event.stopPropagation()}
309
- onmousedown={(event) => event.stopPropagation()}
310
- >
311
- <!-- The implicit "button" role isn't in ARIA's supported-roles list for
312
- aria-activedescendant, but focus deliberately stays on the trigger
313
- (not the portal'd listbox) so arrow-key highlighting has to be
314
- announced from here - without it, screen-reader users get no
315
- feedback about which option is highlighted while navigating. -->
316
- <!-- svelte-ignore a11y_role_supports_aria_props_implicit -->
317
- <button
318
- type="button"
319
- class="sv-grid-dropdown-trigger"
320
- class:sv-grid-dropdown-trigger-chips={renderChipsInTrigger && selectedArr.length > 0}
321
- aria-haspopup="listbox"
322
- aria-expanded={open}
323
- aria-activedescendant={open ? optionId(highlighted) : undefined}
324
- bind:this={triggerEl}
325
- use:focusTrigger
326
- onclick={() => (open ? closePanel() : openPanel())}
327
- onkeydown={onKeyDown}
328
- >
329
- {#if renderChipsInTrigger && selectedArr.length > 0}
330
- <span class="sv-grid-dropdown-chips">
331
- {#each selectedArr as v (String(v))}
332
- {@const opt = options.find((o) => String(o.value) === String(v))}
333
- <span
334
- class="sv-grid-chip sv-grid-chip-removable"
335
- style={opt?.color ? colorfulChipStyleInline(opt.color) : ''}
336
- >
337
- {opt ? opt.label : String(v)}
338
- <!-- A <button> can't be nested inside the trigger <button>; this is a
339
- pointer-only affordance (tabindex -1), so a role="button" span is
340
- valid HTML and preserves identical behaviour. -->
341
- <!-- svelte-ignore a11y_no_static_element_interactions a11y_click_events_have_key_events -->
342
- <span
343
- class="sv-grid-chip-remove"
344
- role="button"
345
- aria-label="Remove {opt ? opt.label : String(v)}"
346
- tabindex={-1}
347
- onmousedown={(event) => event.preventDefault()}
348
- onclick={(event) => removeChip(v, event)}
349
- >×</span>
350
- </span>
351
- {/each}
352
- </span>
353
- {:else if !multiple && selectedArr.length === 1 && selectedColor}
354
- <!-- Single-select list whose chosen option carries a color → show it
355
- as a colored chip inside the trigger, matching how the cell
356
- renders when not in edit mode. -->
357
- <span class="sv-grid-chip" style={colorfulChipStyleInline(selectedColor)}>
358
- {triggerSummary}
359
- </span>
360
- {:else}
361
- <span class="sv-grid-dropdown-label">{triggerSummary}</span>
362
- {/if}
363
- <span class="sv-grid-dropdown-caret" aria-hidden="true">▾</span>
364
- </button>
365
-
366
- {#if open}
367
- <!-- Sizing rule:
368
- - ≤ 10 options → no max-height. The panel grows to its content,
369
- overflow:auto stays inert, and no scrollbar is ever drawn.
370
- This matters because measuring "10 × 32px" against the real
371
- rendered option height (which depends on font + line-height)
372
- rounds inconsistently across themes and used to produce a
373
- 1-pixel scrollbar on lists as short as 3 items.
374
- - > 10 options cap at 10 items + Done-footer chrome so the
375
- panel never overflows the viewport, and the scrollbar appears.
376
- Panel is `position: fixed` + portal'd to <body> so it escapes
377
- the grid's overflow:hidden scroll container. -->
378
- {@const needsScroll = options.length > 10}
379
- {@const panelMax = needsScroll ? 10 * 32 + (multiple ? 40 : 0) + 8 : null}
380
- <div
381
- class="sv-grid-dropdown-panel"
382
- class:sv-grid-dropdown-panel-fits={!needsScroll}
383
- role="listbox"
384
- aria-multiselectable={multiple}
385
- bind:this={panelEl}
386
- use:portalToBody
387
- use:popIn={{ up: panelRect.openUpward }}
388
- style:position="fixed"
389
- style:top={`${panelRect.top}px`}
390
- style:left={`${panelRect.left}px`}
391
- style:width={`${panelRect.width}px`}
392
- style:max-height={panelMax !== null ? `${panelMax}px` : null}
393
- style:z-index="2147483647"
394
- onpointerdown={(event) => event.stopPropagation()}
395
- onmousedown={(event) => event.stopPropagation()}
396
- onclick={(event) => event.stopPropagation()}
397
- >
398
- {#if searchable}
399
- <input
400
- type="search"
401
- class="sv-grid-dropdown-search"
402
- placeholder="Search…"
403
- bind:value={searchQuery}
404
- onmousedown={(event) => event.stopPropagation()}
405
- onclick={(event) => event.stopPropagation()}
406
- onkeydown={(event) => {
407
- if (event.key === 'Escape') { event.preventDefault(); open = false; onCancel?.() }
408
- }}
409
- />
410
- {/if}
411
- {#if visibleOptions.length === 0}
412
- <div class="sv-grid-dropdown-empty">{searchable && searchQuery ? `No matches for "${searchQuery}"` : 'No options'}</div>
413
- {:else}
414
- {#each visibleOptions as opt, i (String(opt.value))}
415
- {@const selected = isSelected(opt)}
416
- <div
417
- class="sv-grid-dropdown-option"
418
- class:sv-grid-dropdown-option-selected={selected}
419
- class:sv-grid-dropdown-option-highlighted={i === highlighted}
420
- role="option"
421
- id={optionId(i)}
422
- aria-selected={selected}
423
- data-opt-idx={i}
424
- onmousedown={(event) => {
425
- event.preventDefault()
426
- toggleOption(opt)
427
- }}
428
- onmouseenter={() => (highlighted = i)}
429
- >
430
- {#if multiple}
431
- <span class="sv-grid-dropdown-check" aria-hidden="true">
432
- {selected ? '✓' : ''}
433
- </span>
434
- {/if}
435
- {#if opt.color}
436
- <!-- Colored options render as a full chip (pill) so the
437
- dropdown choices match how the value looks in the cell. -->
438
- <span class="sv-grid-dropdown-option-chip" style={colorfulChipStyleInline(opt.color)}>{opt.label}</span>
439
- {:else}
440
- <span class="sv-grid-dropdown-option-label">{opt.label}</span>
441
- {/if}
442
- </div>
443
- {/each}
444
- {/if}
445
-
446
- {#if multiple}
447
- <div class="sv-grid-dropdown-footer">
448
- <button
449
- type="button"
450
- class="sv-grid-dropdown-done"
451
- onmousedown={(event) => event.preventDefault()}
452
- onclick={() => {
453
- closePanel()
454
- onCommit?.()
455
- }}
456
- >Done</button>
457
- </div>
458
- {/if}
459
- </div>
460
- {/if}
461
- </div>
462
-
463
- <style>
464
- /* All colors derive from the grid's host CSS vars so light/dark themes
465
- "just work". Hard fallbacks are kept so the component is still
466
- usable in an unthemed page. */
467
- .sv-grid-dropdown {
468
- position: absolute;
469
- top: 0;
470
- left: 0;
471
- right: 0;
472
- height: 100%;
473
- background: var(--sg-bg, #ffffff);
474
- color: var(--sg-fg, #0f172a);
475
- }
476
-
477
- .sv-grid-dropdown-trigger {
478
- display: flex;
479
- align-items: center;
480
- gap: 6px;
481
- width: 100%;
482
- height: 100%;
483
- min-height: 28px;
484
- padding: 0 8px;
485
- background: var(--sg-bg, #ffffff);
486
- color: var(--sg-fg, #0f172a);
487
- border: 0;
488
- outline: none;
489
- font: inherit;
490
- text-align: start;
491
- cursor: pointer;
492
- box-sizing: border-box;
493
- }
494
- .sv-grid-dropdown-trigger:focus {
495
- outline: 2px solid var(--sg-accent, #2563eb);
496
- outline-offset: -2px;
497
- }
498
-
499
- .sv-grid-dropdown-trigger-chips {
500
- align-items: center;
501
- padding: 4px 8px;
502
- flex-wrap: wrap;
503
- }
504
- .sv-grid-dropdown-chips {
505
- display: inline-flex;
506
- flex-wrap: wrap;
507
- gap: 4px;
508
- flex: 1 1 auto;
509
- min-width: 0;
510
- align-items: center;
511
- }
512
-
513
- .sv-grid-dropdown-label {
514
- flex: 1 1 auto;
515
- min-width: 0;
516
- overflow: hidden;
517
- text-overflow: ellipsis;
518
- white-space: nowrap;
519
- }
520
- .sv-grid-dropdown-caret {
521
- flex: 0 0 auto;
522
- font-size: 10px;
523
- opacity: 0.7;
524
- line-height: 1;
525
- }
526
-
527
- /* Popover sits absolutely below the trigger. The parent
528
- `.sv-grid-cell-editing` has `overflow: visible`, so this can hang
529
- past the row. Pulls its surface color from the grid's header bg
530
- so it reads as a slightly elevated layer on either theme. */
531
- /* Panel is `position: fixed` AND portal'd into document.body via
532
- `use:portalToBody`, so it escapes every ancestor overflow / clip /
533
- transform / stacking context. Coords come from updatePanelPosition(). */
534
- :global(.sv-grid-dropdown-panel) {
535
- overflow-y: auto;
536
- background: var(--sg-header-bg, var(--sg-bg, #ffffff));
537
- color: var(--sg-fg, #0f172a);
538
- border: 1px solid var(--sg-accent, #2563eb);
539
- border-radius: 6px;
540
- box-shadow: 0 12px 28px rgba(0, 0, 0, 0.45);
541
- padding: 4px 0;
542
- font-family: inherit;
543
- }
544
- /* When the option list fits in one panel we want NO scrollbar at all -
545
- * not even a sub-pixel ghost track. `overflow: visible` rules out the
546
- * scrollbar gutter that some browsers still reserve under
547
- * `overflow: auto` when content equals max-height. */
548
- :global(.sv-grid-dropdown-panel.sv-grid-dropdown-panel-fits) {
549
- overflow: visible;
550
- }
551
- :global(.sv-grid-dropdown-option) {
552
- display: flex;
553
- align-items: center;
554
- gap: 8px;
555
- padding: 6px 10px;
556
- cursor: pointer;
557
- user-select: none;
558
- color: var(--sg-fg, #0f172a);
559
- }
560
- :global(.sv-grid-dropdown-option-highlighted) {
561
- background: var(--sg-row-hover-bg, rgba(37, 99, 235, 0.1));
562
- }
563
- :global(.sv-grid-dropdown-option-selected) {
564
- font-weight: 600;
565
- background: var(--sg-selection-bg, rgba(37, 99, 235, 0.15));
566
- }
567
- :global(.sv-grid-dropdown-option-selected.sv-grid-dropdown-option-highlighted) {
568
- background: var(--sg-row-hover-bg, rgba(37, 99, 235, 0.18));
569
- }
570
- :global(.sv-grid-dropdown-option-label) {
571
- flex: 1 1 auto;
572
- min-width: 0;
573
- overflow: hidden;
574
- text-overflow: ellipsis;
575
- white-space: nowrap;
576
- }
577
- :global(.sv-grid-dropdown-check) {
578
- flex: 0 0 16px;
579
- display: inline-flex;
580
- align-items: center;
581
- justify-content: center;
582
- color: var(--sg-accent, #2563eb);
583
- font-weight: 700;
584
- }
585
- :global(.sv-grid-dropdown-swatch) {
586
- flex: 0 0 12px;
587
- width: 12px;
588
- height: 12px;
589
- border-radius: 999px;
590
- border: 1px solid color-mix(in srgb, var(--sg-fg, #0f172a) 25%, transparent);
591
- }
592
- /* Colored option chip. Declared `:global` because the option panel is
593
- portal'd to <body>, where Svelte's scoped classes don't reach. The
594
- per-chip background / border / text color come from an inline style
595
- (see colorfulChipStyleInline) so each option matches its cell chip. */
596
- :global(.sv-grid-dropdown-option-chip) {
597
- display: inline-flex;
598
- align-items: center;
599
- max-width: 100%;
600
- padding: 2px 11px;
601
- border: 1px solid transparent;
602
- border-radius: 999px;
603
- font-size: 0.9em;
604
- font-weight: 600;
605
- line-height: 1.5;
606
- overflow: hidden;
607
- text-overflow: ellipsis;
608
- white-space: nowrap;
609
- }
610
- :global(.sv-grid-dropdown-empty) {
611
- padding: 8px 10px;
612
- color: var(--sg-muted, rgba(15, 23, 42, 0.55));
613
- font-style: italic;
614
- font-size: 0.9em;
615
- }
616
- :global(.sv-grid-dropdown-footer) {
617
- display: flex;
618
- justify-content: flex-end;
619
- padding: 4px 6px;
620
- border-top: 1px solid var(--sg-border, rgba(15, 23, 42, 0.08));
621
- margin-top: 2px;
622
- }
623
- :global(.sv-grid-dropdown-done) {
624
- font-size: 11px;
625
- text-transform: uppercase;
626
- letter-spacing: 0.04em;
627
- color: #fff;
628
- background: var(--sg-accent, #2563eb);
629
- border: 0;
630
- border-radius: 4px;
631
- padding: 4px 10px;
632
- cursor: pointer;
633
- }
634
- :global(.sv-grid-dropdown-done:hover) {
635
- filter: brightness(1.12);
636
- }
637
-
638
- /* Note: option / footer / swatch styles are declared `:global(...)`
639
- above because the panel is portal'd to <body> and Svelte's scoped
640
- class won't survive on children of an element living outside the
641
- component subtree. */
642
-
643
- /* Theme-aware chip badges. Inside the trigger they sit on the cell
644
- background, so we need a tone that contrasts on BOTH themes - a
645
- translucent accent works well in dark mode and reads as a soft
646
- blue pill in light mode. */
647
- .sv-grid-chip {
648
- display: inline-flex;
649
- align-items: center;
650
- gap: 4px;
651
- padding: 2px 8px;
652
- font-size: 0.85em;
653
- line-height: 1.4;
654
- background: color-mix(in srgb, var(--sg-accent, #2563eb) 18%, transparent);
655
- color: var(--sg-fg, #0f172a);
656
- border-radius: 999px;
657
- border: 1px solid color-mix(in srgb, var(--sg-accent, #2563eb) 35%, transparent);
658
- white-space: nowrap;
659
- }
660
- .sv-grid-chip-removable {
661
- padding-inline-end: 2px;
662
- }
663
- .sv-grid-chip-remove {
664
- display: inline-flex;
665
- align-items: center;
666
- justify-content: center;
667
- width: 16px;
668
- height: 16px;
669
- border: 0;
670
- border-radius: 999px;
671
- background: color-mix(in srgb, var(--sg-fg, #0f172a) 18%, transparent);
672
- color: inherit;
673
- font-size: 12px;
674
- line-height: 1;
675
- cursor: pointer;
676
- padding: 0;
677
- }
678
- .sv-grid-chip-remove:hover {
679
- background: rgba(220, 38, 38, 0.7);
680
- color: #fff;
681
- }
682
- </style>
1
+ <script lang="ts">
2
+ /**
3
+ * Internal dropdown / listbox control used by the `list` and `chips` cell
4
+ * editors. We deliberately don't reuse the browser-native `<select>` -
5
+ * the native dropdown can't be styled to match the grid theme, picks up
6
+ * the OS color scheme, and on Windows draws the option list with an
7
+ * opaque white system surface that fights the dark theme.
8
+ *
9
+ * Modes:
10
+ * single - picking an option fires `onChange(value)` and `onCommit()`.
11
+ * multi - toggling fires `onChange(values: array)`; the user
12
+ * presses Enter / clicks "Done" to fire `onCommit()`.
13
+ *
14
+ * Keyboard model:
15
+ * ArrowDown / ArrowUp - move highlight (opens the panel if closed).
16
+ * Enter - single: toggle highlighted + commit.
17
+ * multi: toggle highlighted (stay open).
18
+ * Escape - close + onCancel.
19
+ * Tab - commit and let focus escape.
20
+ */
21
+ import type { CellEditorOption } from './editors/cell-editors'
22
+ import { anchoredRect, portalToBody, popIn, type AnchoredRect } from './popover'
23
+
24
+ type Props = {
25
+ options: ReadonlyArray<CellEditorOption>
26
+ value: unknown
27
+ multiple?: boolean
28
+ placeholder?: string
29
+ /** When provided, the trigger renders as removable chips of the current value(s). */
30
+ renderChipsInTrigger?: boolean
31
+ /** Add a typeahead input at the top of the popover that filters the
32
+ * option list as the user types. Used by the rich-select editor. */
33
+ searchable?: boolean
34
+ /** Focus the trigger and open the panel on mount. True for in-cell editing
35
+ * (you clicked into the cell to edit); set false for form fields, which
36
+ * should stay closed and unfocused until the user acts. Default true. */
37
+ autoOpen?: boolean
38
+ /** Called with the new full value (scalar for single, array for multi). */
39
+ onChange?: (next: unknown) => void
40
+ /** Called when the user finalizes the selection (single pick, Enter, blur with selection). */
41
+ onCommit?: () => void
42
+ /** Called when the user dismisses (Escape, blur with no change). */
43
+ onCancel?: () => void
44
+ }
45
+
46
+ let {
47
+ options,
48
+ value,
49
+ multiple = false,
50
+ placeholder = 'Select…',
51
+ renderChipsInTrigger = false,
52
+ searchable = false,
53
+ autoOpen = true,
54
+ onChange,
55
+ onCommit,
56
+ onCancel,
57
+ }: Props = $props()
58
+
59
+ /** Stable per-instance id used to build option ids for
60
+ * `aria-activedescendant` - screen readers need a real id to announce
61
+ * the highlighted option while arrow-keying through the list. */
62
+ const instanceId = $props.id()
63
+ function optionId(index: number): string | undefined {
64
+ return index >= 0 && index < visibleOptions.length ? `${instanceId}-opt-${index}` : undefined
65
+ }
66
+
67
+ let searchQuery = $state('')
68
+ const visibleOptions = $derived(
69
+ !searchable || !searchQuery.trim()
70
+ ? options
71
+ : (() => {
72
+ const q = searchQuery.trim().toLowerCase()
73
+ return options.filter((o) => o.label.toLowerCase().includes(q))
74
+ })(),
75
+ )
76
+
77
+ let open = $state(false)
78
+ let highlighted = $state(-1)
79
+ let rootEl: HTMLDivElement | null = $state(null)
80
+ let triggerEl: HTMLButtonElement | null = $state(null)
81
+ let panelEl: HTMLDivElement | null = $state(null)
82
+
83
+ /** Viewport-anchored panel position. We `position: fixed` the panel so it
84
+ * escapes the grid's overflow:hidden scroll container - otherwise the
85
+ * bottom of the list gets clipped by the pager or the grid's footer. */
86
+ let panelRect = $state<AnchoredRect>({
87
+ top: 0,
88
+ left: 0,
89
+ width: 0,
90
+ openUpward: false,
91
+ maxHeight: 0,
92
+ availHeight: 0,
93
+ })
94
+
95
+ const selectedArr = $derived(
96
+ Array.isArray(value)
97
+ ? (value as Array<string | number>)
98
+ : value == null || value === ''
99
+ ? []
100
+ : [value as string | number],
101
+ )
102
+
103
+ const triggerSummary = $derived.by(() => {
104
+ if (selectedArr.length === 0) return placeholder
105
+ if (!multiple) {
106
+ const v = selectedArr[0]!
107
+ const match = options.find((o) => String(o.value) === String(v))
108
+ return match ? match.label : String(v)
109
+ }
110
+ return `${selectedArr.length} selected`
111
+ })
112
+
113
+ /** Color of the single selected option, if any (for colored trigger). */
114
+ const selectedColor = $derived.by(() => {
115
+ if (multiple || selectedArr.length !== 1) return undefined
116
+ const v = selectedArr[0]!
117
+ return options.find((o) => String(o.value) === String(v))?.color
118
+ })
119
+
120
+ function isSelected(opt: CellEditorOption): boolean {
121
+ return selectedArr.some((v) => String(v) === String(opt.value))
122
+ }
123
+
124
+ function toggleOption(opt: CellEditorOption) {
125
+ if (multiple) {
126
+ const exists = isSelected(opt)
127
+ const next = exists
128
+ ? selectedArr.filter((v) => String(v) !== String(opt.value))
129
+ : [...selectedArr, opt.value]
130
+ onChange?.(next)
131
+ } else {
132
+ onChange?.(opt.value)
133
+ open = false
134
+ onCommit?.()
135
+ }
136
+ }
137
+
138
+ function openPanel() {
139
+ if (open) return
140
+ open = true
141
+ // Seed the highlight to the first selected option, or top of the list.
142
+ if (highlighted < 0) {
143
+ const idx = options.findIndex((o) => isSelected(o))
144
+ highlighted = idx >= 0 ? idx : 0
145
+ }
146
+ updatePanelPosition()
147
+ }
148
+
149
+ /** Recompute the panel's fixed-position rect from the trigger. Auto-
150
+ * flips upward when there isn't enough room below the trigger. */
151
+ function updatePanelPosition() {
152
+ if (!triggerEl) return
153
+ // Use the smaller of (option count, 10) for the upward-flip math so
154
+ // a 3-option list doesn't think it needs 320px of headroom.
155
+ const visibleCount = Math.min(options.length, 10)
156
+ const estimatedHeight = visibleCount * 32 + (multiple ? 40 : 0) + 8
157
+ panelRect = anchoredRect(triggerEl.getBoundingClientRect(), {
158
+ estimatedHeight,
159
+ // Preserve the original behavior: anchor to the trigger, no clamping.
160
+ clampHorizontal: false,
161
+ })
162
+ }
163
+
164
+ // Keep the panel anchored as the page scrolls or resizes - without this,
165
+ // scrolling the underlying grid would leave the panel hanging in space.
166
+ $effect(() => {
167
+ if (!open) return
168
+ const reposition = () => updatePanelPosition()
169
+ window.addEventListener('scroll', reposition, true)
170
+ window.addEventListener('resize', reposition)
171
+ return () => {
172
+ window.removeEventListener('scroll', reposition, true)
173
+ window.removeEventListener('resize', reposition)
174
+ }
175
+ })
176
+
177
+ function closePanel() {
178
+ open = false
179
+ highlighted = -1
180
+ }
181
+
182
+ function onKeyDown(event: KeyboardEvent) {
183
+ event.stopPropagation()
184
+ if (event.key === 'Escape') {
185
+ event.preventDefault()
186
+ closePanel()
187
+ onCancel?.()
188
+ return
189
+ }
190
+ if (event.key === 'ArrowDown') {
191
+ event.preventDefault()
192
+ if (!open) {
193
+ openPanel()
194
+ return
195
+ }
196
+ highlighted = Math.min(highlighted + 1, options.length - 1)
197
+ scrollHighlightedIntoView()
198
+ } else if (event.key === 'ArrowUp') {
199
+ event.preventDefault()
200
+ if (!open) {
201
+ openPanel()
202
+ return
203
+ }
204
+ highlighted = Math.max(highlighted - 1, 0)
205
+ scrollHighlightedIntoView()
206
+ } else if (event.key === 'Home') {
207
+ if (!open) return
208
+ event.preventDefault()
209
+ highlighted = 0
210
+ scrollHighlightedIntoView()
211
+ } else if (event.key === 'End') {
212
+ if (!open) return
213
+ event.preventDefault()
214
+ highlighted = options.length - 1
215
+ scrollHighlightedIntoView()
216
+ } else if (event.key === 'Enter') {
217
+ event.preventDefault()
218
+ if (!open) {
219
+ openPanel()
220
+ return
221
+ }
222
+ const opt = options[highlighted]
223
+ if (opt) toggleOption(opt)
224
+ else if (multiple) {
225
+ closePanel()
226
+ onCommit?.()
227
+ }
228
+ } else if (event.key === ' ' && open) {
229
+ event.preventDefault()
230
+ const opt = options[highlighted]
231
+ if (opt) toggleOption(opt)
232
+ } else if (event.key === 'Tab') {
233
+ if (open) closePanel()
234
+ onCommit?.()
235
+ }
236
+ }
237
+
238
+ function scrollHighlightedIntoView() {
239
+ queueMicrotask(() => {
240
+ if (!panelEl) return
241
+ const el = panelEl.querySelector<HTMLElement>(`[data-opt-idx="${highlighted}"]`)
242
+ el?.scrollIntoView({ block: 'nearest' })
243
+ })
244
+ }
245
+
246
+ function onRootBlur(event: FocusEvent) {
247
+ // Defer to the next tick so a click on an option (mousedown → option
248
+ // commits, then blur) doesn't race-cancel the selection.
249
+ const next = event.relatedTarget as Node | null
250
+ if (next && rootEl?.contains(next)) return
251
+ if (open) closePanel()
252
+ onCommit?.()
253
+ }
254
+
255
+ function removeChip(v: string | number, event: MouseEvent) {
256
+ event.preventDefault()
257
+ event.stopPropagation()
258
+ const next = selectedArr.filter((x) => String(x) !== String(v))
259
+ onChange?.(multiple ? next : next[0] ?? null)
260
+ }
261
+
262
+ function focusTrigger(node: HTMLButtonElement) {
263
+ // Form fields (autoOpen=false) stay closed + unfocused until the user acts;
264
+ // in-cell editing auto-focuses + opens to match clicking into a cell.
265
+ if (!autoOpen) return
266
+ node.focus()
267
+ openPanel()
268
+ }
269
+
270
+ /** Close the panel when the user clicks outside both trigger AND
271
+ * panel - `onfocusout` on the dropdown root no longer catches this
272
+ * because the portal'd panel is a sibling of body, not a descendant
273
+ * of rootEl. Wired up only while the panel is open. */
274
+ $effect(() => {
275
+ if (!open) return
276
+ const onDocPointerDown = (event: PointerEvent) => {
277
+ const target = event.target as Node | null
278
+ if (!target) return
279
+ if (rootEl?.contains(target)) return
280
+ if (panelEl?.contains(target)) return
281
+ closePanel()
282
+ onCommit?.()
283
+ }
284
+ document.addEventListener('pointerdown', onDocPointerDown, true)
285
+ return () => document.removeEventListener('pointerdown', onDocPointerDown, true)
286
+ })
287
+
288
+ /** Inline style for a chip with a configured `color`. Mirrors the
289
+ * helper in SvGrid.svelte so colorful chips look identical in the
290
+ * trigger and the in-cell readonly render. */
291
+ function colorfulChipStyleInline(color: string): string {
292
+ return (
293
+ `background: color-mix(in srgb, ${color} 22%, transparent);` +
294
+ `border-color: color-mix(in srgb, ${color} 45%, transparent);` +
295
+ `color: color-mix(in srgb, ${color} 80%, var(--sg-fg, #0f172a));`
296
+ )
297
+ }
298
+ </script>
299
+
300
+ <!-- All pointer/click events are swallowed at the root so they never reach
301
+ the surrounding cell's selection / pointerdown handlers - picking a
302
+ dropdown option must not also toggle cell selection. -->
303
+ <div
304
+ class="sv-grid-dropdown"
305
+ class:sv-grid-dropdown-open={open}
306
+ bind:this={rootEl}
307
+ onfocusout={onRootBlur}
308
+ onclick={(event) => event.stopPropagation()}
309
+ ondblclick={(event) => event.stopPropagation()}
310
+ onpointerdown={(event) => event.stopPropagation()}
311
+ onmousedown={(event) => event.stopPropagation()}
312
+ >
313
+ <!-- The implicit "button" role isn't in ARIA's supported-roles list for
314
+ aria-activedescendant, but focus deliberately stays on the trigger
315
+ (not the portal'd listbox) so arrow-key highlighting has to be
316
+ announced from here - without it, screen-reader users get no
317
+ feedback about which option is highlighted while navigating. -->
318
+ <!-- svelte-ignore a11y_role_supports_aria_props_implicit -->
319
+ <button
320
+ type="button"
321
+ class="sv-grid-dropdown-trigger"
322
+ class:sv-grid-dropdown-trigger-chips={renderChipsInTrigger && selectedArr.length > 0}
323
+ aria-haspopup="listbox"
324
+ aria-expanded={open}
325
+ aria-activedescendant={open ? optionId(highlighted) : undefined}
326
+ bind:this={triggerEl}
327
+ use:focusTrigger
328
+ onclick={() => (open ? closePanel() : openPanel())}
329
+ onkeydown={onKeyDown}
330
+ >
331
+ {#if renderChipsInTrigger && selectedArr.length > 0}
332
+ <span class="sv-grid-dropdown-chips">
333
+ {#each selectedArr as v (String(v))}
334
+ {@const opt = options.find((o) => String(o.value) === String(v))}
335
+ <span
336
+ class="sv-grid-chip sv-grid-chip-removable"
337
+ style={opt?.color ? colorfulChipStyleInline(opt.color) : ''}
338
+ >
339
+ {opt ? opt.label : String(v)}
340
+ <!-- A <button> can't be nested inside the trigger <button>; this is a
341
+ pointer-only affordance (tabindex -1), so a role="button" span is
342
+ valid HTML and preserves identical behaviour. -->
343
+ <!-- svelte-ignore a11y_no_static_element_interactions a11y_click_events_have_key_events -->
344
+ <span
345
+ class="sv-grid-chip-remove"
346
+ role="button"
347
+ aria-label="Remove {opt ? opt.label : String(v)}"
348
+ tabindex={-1}
349
+ onmousedown={(event) => event.preventDefault()}
350
+ onclick={(event) => removeChip(v, event)}
351
+ >×</span>
352
+ </span>
353
+ {/each}
354
+ </span>
355
+ {:else if !multiple && selectedArr.length === 1 && selectedColor}
356
+ <!-- Single-select list whose chosen option carries a color → show it
357
+ as a colored chip inside the trigger, matching how the cell
358
+ renders when not in edit mode. -->
359
+ <span class="sv-grid-chip" style={colorfulChipStyleInline(selectedColor)}>
360
+ {triggerSummary}
361
+ </span>
362
+ {:else}
363
+ <span class="sv-grid-dropdown-label">{triggerSummary}</span>
364
+ {/if}
365
+ <span class="sv-grid-dropdown-caret" aria-hidden="true">▾</span>
366
+ </button>
367
+
368
+ {#if open}
369
+ <!-- Sizing rule:
370
+ - 10 options no max-height. The panel grows to its content,
371
+ overflow:auto stays inert, and no scrollbar is ever drawn.
372
+ This matters because measuring "10 × 32px" against the real
373
+ rendered option height (which depends on font + line-height)
374
+ rounds inconsistently across themes and used to produce a
375
+ 1-pixel scrollbar on lists as short as 3 items.
376
+ - > 10 options → cap at 10 items + Done-footer chrome so the
377
+ panel never overflows the viewport, and the scrollbar appears.
378
+ Panel is `position: fixed` + portal'd to <body> so it escapes
379
+ the grid's overflow:hidden scroll container. -->
380
+ {@const needsScroll = options.length > 10}
381
+ <!-- Cap to the comfortable 10-row height AND the room the viewport allows,
382
+ so a panel near the bottom edge scrolls instead of overflowing. -->
383
+ {@const panelMax = Math.min(
384
+ needsScroll ? 10 * 32 + (multiple ? 40 : 0) + 8 : Number.POSITIVE_INFINITY,
385
+ panelRect.maxHeight,
386
+ )}
387
+ <div
388
+ class="sv-grid-dropdown-panel"
389
+ class:sv-grid-dropdown-panel-fits={!needsScroll}
390
+ role="listbox"
391
+ aria-multiselectable={multiple}
392
+ bind:this={panelEl}
393
+ use:portalToBody
394
+ use:popIn={{ up: panelRect.openUpward }}
395
+ style:position="fixed"
396
+ style:top={`${panelRect.top}px`}
397
+ style:left={`${panelRect.left}px`}
398
+ style:width={`${panelRect.width}px`}
399
+ style:max-height={panelMax !== null ? `${panelMax}px` : null}
400
+ style:z-index="2147483647"
401
+ onpointerdown={(event) => event.stopPropagation()}
402
+ onmousedown={(event) => event.stopPropagation()}
403
+ onclick={(event) => event.stopPropagation()}
404
+ >
405
+ {#if searchable}
406
+ <input
407
+ type="search"
408
+ class="sv-grid-dropdown-search"
409
+ placeholder="Search…"
410
+ bind:value={searchQuery}
411
+ onmousedown={(event) => event.stopPropagation()}
412
+ onclick={(event) => event.stopPropagation()}
413
+ onkeydown={(event) => {
414
+ if (event.key === 'Escape') { event.preventDefault(); open = false; onCancel?.() }
415
+ }}
416
+ />
417
+ {/if}
418
+ {#if visibleOptions.length === 0}
419
+ <div class="sv-grid-dropdown-empty">{searchable && searchQuery ? `No matches for "${searchQuery}"` : 'No options'}</div>
420
+ {:else}
421
+ {#each visibleOptions as opt, i (String(opt.value))}
422
+ {@const selected = isSelected(opt)}
423
+ <div
424
+ class="sv-grid-dropdown-option"
425
+ class:sv-grid-dropdown-option-selected={selected}
426
+ class:sv-grid-dropdown-option-highlighted={i === highlighted}
427
+ role="option"
428
+ id={optionId(i)}
429
+ aria-selected={selected}
430
+ data-opt-idx={i}
431
+ onmousedown={(event) => {
432
+ event.preventDefault()
433
+ toggleOption(opt)
434
+ }}
435
+ onmouseenter={() => (highlighted = i)}
436
+ >
437
+ {#if multiple}
438
+ <span class="sv-grid-dropdown-check" aria-hidden="true">
439
+ {selected ? '✓' : ''}
440
+ </span>
441
+ {/if}
442
+ {#if opt.color}
443
+ <!-- Colored options render as a full chip (pill) so the
444
+ dropdown choices match how the value looks in the cell. -->
445
+ <span class="sv-grid-dropdown-option-chip" style={colorfulChipStyleInline(opt.color)}>{opt.label}</span>
446
+ {:else}
447
+ <span class="sv-grid-dropdown-option-label">{opt.label}</span>
448
+ {/if}
449
+ </div>
450
+ {/each}
451
+ {/if}
452
+
453
+ {#if multiple}
454
+ <div class="sv-grid-dropdown-footer">
455
+ <button
456
+ type="button"
457
+ class="sv-grid-dropdown-done"
458
+ onmousedown={(event) => event.preventDefault()}
459
+ onclick={() => {
460
+ closePanel()
461
+ onCommit?.()
462
+ }}
463
+ >Done</button>
464
+ </div>
465
+ {/if}
466
+ </div>
467
+ {/if}
468
+ </div>
469
+
470
+ <style>
471
+ /* All colors derive from the grid's host CSS vars so light/dark themes
472
+ "just work". Hard fallbacks are kept so the component is still
473
+ usable in an unthemed page. */
474
+ .sv-grid-dropdown {
475
+ position: absolute;
476
+ top: 0;
477
+ left: 0;
478
+ right: 0;
479
+ height: 100%;
480
+ background: var(--sg-bg, #ffffff);
481
+ color: var(--sg-fg, #0f172a);
482
+ }
483
+
484
+ .sv-grid-dropdown-trigger {
485
+ display: flex;
486
+ align-items: center;
487
+ gap: 6px;
488
+ width: 100%;
489
+ height: 100%;
490
+ min-height: 28px;
491
+ padding: 0 8px;
492
+ background: var(--sg-bg, #ffffff);
493
+ color: var(--sg-fg, #0f172a);
494
+ border: 0;
495
+ outline: none;
496
+ font: inherit;
497
+ text-align: start;
498
+ cursor: pointer;
499
+ box-sizing: border-box;
500
+ }
501
+ .sv-grid-dropdown-trigger:focus {
502
+ outline: 2px solid var(--sg-accent, #2563eb);
503
+ outline-offset: -2px;
504
+ }
505
+
506
+ .sv-grid-dropdown-trigger-chips {
507
+ align-items: center;
508
+ padding: 4px 8px;
509
+ flex-wrap: wrap;
510
+ }
511
+ .sv-grid-dropdown-chips {
512
+ display: inline-flex;
513
+ flex-wrap: wrap;
514
+ gap: 4px;
515
+ flex: 1 1 auto;
516
+ min-width: 0;
517
+ align-items: center;
518
+ }
519
+
520
+ .sv-grid-dropdown-label {
521
+ flex: 1 1 auto;
522
+ min-width: 0;
523
+ overflow: hidden;
524
+ text-overflow: ellipsis;
525
+ white-space: nowrap;
526
+ }
527
+ .sv-grid-dropdown-caret {
528
+ flex: 0 0 auto;
529
+ font-size: 10px;
530
+ opacity: 0.7;
531
+ line-height: 1;
532
+ }
533
+
534
+ /* Popover sits absolutely below the trigger. The parent
535
+ `.sv-grid-cell-editing` has `overflow: visible`, so this can hang
536
+ past the row. Pulls its surface color from the grid's header bg
537
+ so it reads as a slightly elevated layer on either theme. */
538
+ /* Panel is `position: fixed` AND portal'd into document.body via
539
+ `use:portalToBody`, so it escapes every ancestor overflow / clip /
540
+ transform / stacking context. Coords come from updatePanelPosition(). */
541
+ :global(.sv-grid-dropdown-panel) {
542
+ overflow-y: auto;
543
+ background: var(--sg-header-bg, var(--sg-bg, #ffffff));
544
+ color: var(--sg-fg, #0f172a);
545
+ border: 1px solid var(--sg-accent, #2563eb);
546
+ border-radius: 6px;
547
+ box-shadow: 0 12px 28px rgba(0, 0, 0, 0.45);
548
+ padding: 4px 0;
549
+ font-family: inherit;
550
+ }
551
+ /* When the option list fits in one panel we want NO scrollbar at all -
552
+ * not even a sub-pixel ghost track. `overflow: visible` rules out the
553
+ * scrollbar gutter that some browsers still reserve under
554
+ * `overflow: auto` when content equals max-height. */
555
+ :global(.sv-grid-dropdown-panel.sv-grid-dropdown-panel-fits) {
556
+ overflow: visible;
557
+ }
558
+ :global(.sv-grid-dropdown-option) {
559
+ display: flex;
560
+ align-items: center;
561
+ gap: 8px;
562
+ padding: 6px 10px;
563
+ cursor: pointer;
564
+ user-select: none;
565
+ color: var(--sg-fg, #0f172a);
566
+ }
567
+ :global(.sv-grid-dropdown-option-highlighted) {
568
+ background: var(--sg-row-hover-bg, rgba(37, 99, 235, 0.1));
569
+ }
570
+ :global(.sv-grid-dropdown-option-selected) {
571
+ font-weight: 600;
572
+ background: var(--sg-selection-bg, rgba(37, 99, 235, 0.15));
573
+ }
574
+ :global(.sv-grid-dropdown-option-selected.sv-grid-dropdown-option-highlighted) {
575
+ background: var(--sg-row-hover-bg, rgba(37, 99, 235, 0.18));
576
+ }
577
+ :global(.sv-grid-dropdown-option-label) {
578
+ flex: 1 1 auto;
579
+ min-width: 0;
580
+ overflow: hidden;
581
+ text-overflow: ellipsis;
582
+ white-space: nowrap;
583
+ }
584
+ :global(.sv-grid-dropdown-check) {
585
+ flex: 0 0 16px;
586
+ display: inline-flex;
587
+ align-items: center;
588
+ justify-content: center;
589
+ color: var(--sg-accent, #2563eb);
590
+ font-weight: 700;
591
+ }
592
+ :global(.sv-grid-dropdown-swatch) {
593
+ flex: 0 0 12px;
594
+ width: 12px;
595
+ height: 12px;
596
+ border-radius: 999px;
597
+ border: 1px solid color-mix(in srgb, var(--sg-fg, #0f172a) 25%, transparent);
598
+ }
599
+ /* Colored option chip. Declared `:global` because the option panel is
600
+ portal'd to <body>, where Svelte's scoped classes don't reach. The
601
+ per-chip background / border / text color come from an inline style
602
+ (see colorfulChipStyleInline) so each option matches its cell chip. */
603
+ :global(.sv-grid-dropdown-option-chip) {
604
+ display: inline-flex;
605
+ align-items: center;
606
+ max-width: 100%;
607
+ padding: 2px 11px;
608
+ border: 1px solid transparent;
609
+ border-radius: 999px;
610
+ font-size: 0.9em;
611
+ font-weight: 600;
612
+ line-height: 1.5;
613
+ overflow: hidden;
614
+ text-overflow: ellipsis;
615
+ white-space: nowrap;
616
+ }
617
+ :global(.sv-grid-dropdown-empty) {
618
+ padding: 8px 10px;
619
+ color: var(--sg-muted, rgba(15, 23, 42, 0.55));
620
+ font-style: italic;
621
+ font-size: 0.9em;
622
+ }
623
+ :global(.sv-grid-dropdown-footer) {
624
+ display: flex;
625
+ justify-content: flex-end;
626
+ padding: 4px 6px;
627
+ border-top: 1px solid var(--sg-border, rgba(15, 23, 42, 0.08));
628
+ margin-top: 2px;
629
+ }
630
+ :global(.sv-grid-dropdown-done) {
631
+ font-size: 11px;
632
+ text-transform: uppercase;
633
+ letter-spacing: 0.04em;
634
+ color: #fff;
635
+ background: var(--sg-accent, #2563eb);
636
+ border: 0;
637
+ border-radius: 4px;
638
+ padding: 4px 10px;
639
+ cursor: pointer;
640
+ }
641
+ :global(.sv-grid-dropdown-done:hover) {
642
+ filter: brightness(1.12);
643
+ }
644
+
645
+ /* Note: option / footer / swatch styles are declared `:global(...)`
646
+ above because the panel is portal'd to <body> and Svelte's scoped
647
+ class won't survive on children of an element living outside the
648
+ component subtree. */
649
+
650
+ /* Theme-aware chip badges. Inside the trigger they sit on the cell
651
+ background, so we need a tone that contrasts on BOTH themes - a
652
+ translucent accent works well in dark mode and reads as a soft
653
+ blue pill in light mode. */
654
+ .sv-grid-chip {
655
+ display: inline-flex;
656
+ align-items: center;
657
+ gap: 4px;
658
+ padding: 2px 8px;
659
+ font-size: 0.85em;
660
+ line-height: 1.4;
661
+ background: color-mix(in srgb, var(--sg-accent, #2563eb) 18%, transparent);
662
+ color: var(--sg-fg, #0f172a);
663
+ border-radius: 999px;
664
+ border: 1px solid color-mix(in srgb, var(--sg-accent, #2563eb) 35%, transparent);
665
+ white-space: nowrap;
666
+ }
667
+ .sv-grid-chip-removable {
668
+ padding-inline-end: 2px;
669
+ }
670
+ .sv-grid-chip-remove {
671
+ display: inline-flex;
672
+ align-items: center;
673
+ justify-content: center;
674
+ width: 16px;
675
+ height: 16px;
676
+ border: 0;
677
+ border-radius: 999px;
678
+ background: color-mix(in srgb, var(--sg-fg, #0f172a) 18%, transparent);
679
+ color: inherit;
680
+ font-size: 12px;
681
+ line-height: 1;
682
+ cursor: pointer;
683
+ padding: 0;
684
+ }
685
+ .sv-grid-chip-remove:hover {
686
+ background: rgba(220, 38, 38, 0.7);
687
+ color: #fff;
688
+ }
689
+ </style>