@svgrid/grid 2.2.0 → 2.2.2

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 (114) hide show
  1. package/dist/FlexRender.svelte +96 -96
  2. package/dist/GridFooter.svelte +178 -178
  3. package/dist/SvCarousel.svelte +141 -141
  4. package/dist/SvColorInput.svelte +187 -187
  5. package/dist/SvContextMenu.svelte +116 -116
  6. package/dist/SvDrawer.svelte +250 -250
  7. package/dist/SvGrid.svelte +3131 -3131
  8. package/dist/SvGridBoard.svelte +2339 -2339
  9. package/dist/SvGridChart.svelte +1724 -1724
  10. package/dist/SvGridChartPanel.svelte +485 -485
  11. package/dist/SvGridDropdown.svelte +689 -689
  12. package/dist/SvGroupCell.svelte +109 -109
  13. package/dist/SvMenu.svelte +124 -124
  14. package/dist/SvMenuList.svelte +117 -117
  15. package/dist/SvNumberInput.svelte +131 -131
  16. package/dist/SvOtpInput.svelte +158 -158
  17. package/dist/SvPopover.svelte +139 -139
  18. package/dist/SvRowGroupPanel.svelte +170 -170
  19. package/dist/SvScrollArea.svelte +61 -61
  20. package/dist/SvTooltip.svelte +127 -127
  21. package/dist/SvTour.svelte +204 -204
  22. package/dist/SvTree.svelte +377 -377
  23. package/dist/SvTreeSelect.svelte +230 -230
  24. package/dist/cdn/svgrid.js +8 -8
  25. package/dist/cdn/svgrid.svelte-external.js +8 -8
  26. package/dist/chart-export.js +8 -8
  27. package/package.json +10 -10
  28. package/src/FlexRender.svelte +96 -96
  29. package/src/GridFooter.svelte +178 -178
  30. package/src/SvCarousel.svelte +141 -141
  31. package/src/SvColorInput.svelte +187 -187
  32. package/src/SvContextMenu.svelte +116 -116
  33. package/src/SvDrawer.svelte +250 -250
  34. package/src/SvGrid.controller.svelte.ts +3158 -3158
  35. package/src/SvGrid.svelte +3131 -3131
  36. package/src/SvGrid.types.ts +1489 -1489
  37. package/src/SvGridBoard.svelte +2339 -2339
  38. package/src/SvGridChart.svelte +1724 -1724
  39. package/src/SvGridChartPanel.svelte +485 -485
  40. package/src/SvGridDropdown.svelte +689 -689
  41. package/src/SvGroupCell.svelte +109 -109
  42. package/src/SvMenu.svelte +124 -124
  43. package/src/SvMenu.test.ts +97 -97
  44. package/src/SvMenuList.svelte +117 -117
  45. package/src/SvNumberInput.svelte +131 -131
  46. package/src/SvOtpInput.svelte +158 -158
  47. package/src/SvPopover.svelte +139 -139
  48. package/src/SvRowGroupPanel.svelte +170 -170
  49. package/src/SvScrollArea.svelte +61 -61
  50. package/src/SvTooltip.svelte +127 -127
  51. package/src/SvTour.svelte +204 -204
  52. package/src/SvTree.svelte +377 -377
  53. package/src/SvTreeSelect.svelte +230 -230
  54. package/src/a11y.contract.test.ts +49 -49
  55. package/src/a11y.test.ts +59 -59
  56. package/src/a11y.ts +61 -61
  57. package/src/build-api.ts +870 -870
  58. package/src/cell-formatting.ts +169 -169
  59. package/src/cell-render.ts +469 -469
  60. package/src/chart-export.ts +201 -201
  61. package/src/chart.ts +2296 -2296
  62. package/src/collaboration.test.ts +104 -104
  63. package/src/collaboration.ts +167 -167
  64. package/src/core.performance.test.ts +30 -30
  65. package/src/core.ts +1111 -1111
  66. package/src/createAutocomplete.svelte.ts +132 -132
  67. package/src/createCombobox.svelte.ts +179 -179
  68. package/src/createCountryInput.svelte.ts +157 -157
  69. package/src/createDropdownList.svelte.ts +162 -162
  70. package/src/createGrid.svelte.ts +42 -42
  71. package/src/createGrid.test.ts +10 -10
  72. package/src/createGridState.svelte.ts +17 -17
  73. package/src/createMenu.svelte.ts +202 -202
  74. package/src/createPopoverSelect.svelte.ts +206 -206
  75. package/src/createTree.svelte.ts +319 -319
  76. package/src/editing.test.ts +859 -859
  77. package/src/editing.ts +675 -675
  78. package/src/export-data-api.test.ts +126 -126
  79. package/src/export-format.test.ts +107 -107
  80. package/src/export-format.ts +598 -598
  81. package/src/flex-render.ts +3 -3
  82. package/src/index.ts +623 -623
  83. package/src/keyboard.test.ts +59 -59
  84. package/src/keyboard.ts +97 -97
  85. package/src/list-nav.test.ts +49 -49
  86. package/src/list-nav.ts +29 -29
  87. package/src/menus.ts +639 -639
  88. package/src/merge-objects.ts +48 -48
  89. package/src/overlays.test.ts +90 -90
  90. package/src/render-component.ts +28 -28
  91. package/src/selection.test.ts +754 -754
  92. package/src/selection.ts +600 -600
  93. package/src/server-data-source.test.ts +289 -289
  94. package/src/server-data-source.ts +413 -413
  95. package/src/sparkline.test.ts +68 -68
  96. package/src/sparkline.ts +169 -169
  97. package/src/spreadsheet.test.ts +489 -489
  98. package/src/spreadsheet.ts +312 -312
  99. package/src/static-functions.ts +11 -11
  100. package/src/subscribe.ts +38 -38
  101. package/src/svgrid-wrapper.types.ts +501 -501
  102. package/src/svgrid.behavior.test.ts +706 -706
  103. package/src/svgrid.charting.test.ts +527 -527
  104. package/src/svgrid.features.test.ts +157 -157
  105. package/src/svgrid.new-features.wrapper.test.ts +251 -251
  106. package/src/svgrid.wrapper.test.ts +40 -40
  107. package/src/test-setup.ts +62 -62
  108. package/src/themes/index.ts +159 -159
  109. package/src/virtualization/column-virtualizer.test.ts +27 -27
  110. package/src/virtualization/column-virtualizer.ts +30 -30
  111. package/src/virtualization/svelte-virtualizer.svelte.ts +26 -26
  112. package/src/virtualization/types.ts +30 -30
  113. package/src/virtualization/virtualizer.test.ts +47 -47
  114. package/src/virtualization/virtualizer.ts +296 -296
@@ -1,689 +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
- 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>
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>