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