@splendidlabz/svelte 3.0.0-alpha.2 → 3.0.0-alpha.5
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/CHANGELOG.md +29 -0
- package/actions/index.js +2 -0
- package/actions/sync.svelte.js +21 -0
- package/actions/use-actions.js +27 -0
- package/{components/_shared/use-actions.js → actions/use-transition.js} +1 -0
- package/components/Accordion/Accordion.svelte +3 -3
- package/components/Disclosure/Disclosure/DisclosureCloseButton.svelte +37 -0
- package/components/Disclosure/Disclosure/DisclosureContent.svelte +97 -0
- package/components/Disclosure/Disclosure/DisclosureTrigger.svelte +51 -0
- package/components/Disclosure/Disclosure/disclosure-popover.svelte.js +7 -0
- package/components/Disclosure/Disclosure/disclosure.svelte.js +461 -0
- package/components/Disclosure/Disclosure/index.js +4 -0
- package/components/Disclosure/DisclosureNew/DisclosureCloseButton.svelte +38 -0
- package/components/Disclosure/DisclosureNew/DisclosureContent.svelte +53 -0
- package/components/Disclosure/DisclosureNew/DisclosureTrigger.svelte +79 -0
- package/components/Disclosure/DisclosureNew/disclosures.svelte.js +55 -0
- package/components/Disclosure/DisclosureNew/index.js +5 -0
- package/components/Disclosure/Drawer/Drawer.svelte +18 -22
- package/components/Disclosure/Drawer/DrawerCloseButton.svelte +2 -2
- package/components/Disclosure/Drawer/DrawerContent.svelte +41 -77
- package/components/Disclosure/Drawer/DrawerNew.svelte +0 -0
- package/components/Disclosure/Drawer/DrawerTrigger.svelte +7 -46
- package/components/Disclosure/Drawer/drawer.js +2 -70
- package/components/Disclosure/DrawerNew/Drawer.svelte +39 -0
- package/components/Disclosure/DrawerNew/DrawerClose.svelte +0 -0
- package/components/Disclosure/DrawerNew/DrawerTrigger.svelte +16 -0
- package/components/Disclosure/DrawerNew/drawer.js +44 -0
- package/components/Disclosure/DrawerNew/index.js +5 -0
- package/components/Disclosure/Modal/Modal.svelte +30 -0
- package/components/Disclosure/Modal/ModalCloseButton.svelte +7 -0
- package/components/Disclosure/Modal/ModalContent.svelte +40 -1
- package/components/Disclosure/Modal/ModalTrigger.svelte +7 -29
- package/components/Disclosure/Modal/index.js +5 -0
- package/components/Disclosure/Modal/modal.js +3 -12
- package/components/Disclosure/ModalNew/Modal.svelte +110 -0
- package/components/Disclosure/ModalNew/ModalClose.svelte +42 -0
- package/components/Disclosure/ModalNew/ModalTrigger.svelte +68 -0
- package/components/Disclosure/ModalNew/index.js +5 -0
- package/components/Disclosure/Popover/PopoverContent.svelte +2 -2
- package/components/Disclosure/Popover/PopoverTrigger.svelte +1 -1
- package/components/Disclosure/PopoverNew/Popover.svelte +105 -0
- package/components/Disclosure/PopoverNew/PopoverClose.svelte +38 -0
- package/components/Disclosure/PopoverNew/PopoverTrigger.svelte +67 -0
- package/components/Disclosure/PopoverNew/floating.js +329 -0
- package/components/Disclosure/PopoverNew/index.js +5 -0
- package/components/Disclosure/disclosure.js +1 -1
- package/components/Disclosure/disclosure.svelte.js +3 -3
- package/components/Disclosure/index.js +7 -3
- package/components/Disclosure/shared.js +9 -0
- package/components/Form/Checkbox.svelte +72 -57
- package/components/Form/Checkboxes.svelte +42 -24
- package/components/Form/HiddenInput.svelte +1 -1
- package/components/Form/Radio.svelte +62 -45
- package/components/Form/Radios.svelte +42 -24
- package/components/Form/Select.svelte +125 -77
- package/components/Form/SelectOld2.svelte +127 -0
- package/components/Form/TextInput.svelte +1 -1
- package/components/Form/Textarea.svelte +1 -1
- package/components/Icon/IconSnippet.svelte +7 -0
- package/components/Markdown/Markdown.svelte +7 -21
- package/components/Portal.svelte +8 -5
- package/eslint.config.js +2 -0
- package/package.json +7 -11
- package/svelte.config.js +5 -0
- package/utils/merge-opts.js +9 -1
- package/utils/transitions.js +75 -4
- package/.eslintrc.cjs +0 -3
- package/components/_shared/accessibility.js +0 -10
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
import {
|
|
2
|
+
arrow as arrowFn,
|
|
3
|
+
autoUpdate,
|
|
4
|
+
computePosition,
|
|
5
|
+
flip,
|
|
6
|
+
offset as offsetFn,
|
|
7
|
+
shift,
|
|
8
|
+
} from '@floating-ui/dom'
|
|
9
|
+
import { getCSSVar, setCSSVar, toPx } from '@splendidlabz/utils/dom'
|
|
10
|
+
import { mix, splitUnit, toRadians } from '@splendidlabz/utils'
|
|
11
|
+
|
|
12
|
+
// TODO: Add inline for tooltips
|
|
13
|
+
export const FLOATING_OPTS = {
|
|
14
|
+
placement: 'top',
|
|
15
|
+
strategy: 'absolute',
|
|
16
|
+
offset: '0',
|
|
17
|
+
arrow: false,
|
|
18
|
+
arrowHeight: 0,
|
|
19
|
+
arrowRadius: '5px',
|
|
20
|
+
boxStyle: 'standard', // standard, fancy
|
|
21
|
+
useReferenceBoundaries: false,
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export const FLOATING_OPTS_KEYS = Object.keys(FLOATING_OPTS)
|
|
25
|
+
|
|
26
|
+
export function initFloating(options) {
|
|
27
|
+
options = mix(FLOATING_OPTS, options)
|
|
28
|
+
const {
|
|
29
|
+
referenceNode,
|
|
30
|
+
floatingNode,
|
|
31
|
+
boxStyle,
|
|
32
|
+
arrow,
|
|
33
|
+
strategy,
|
|
34
|
+
placement,
|
|
35
|
+
arrowRadius,
|
|
36
|
+
offset: offsetValue,
|
|
37
|
+
} = options
|
|
38
|
+
let _cleanup = null
|
|
39
|
+
|
|
40
|
+
// Determine box style and apply styles as necessary.
|
|
41
|
+
applyBoxStyles(options)
|
|
42
|
+
|
|
43
|
+
// Get necessary values
|
|
44
|
+
const borderWidth = toPx(
|
|
45
|
+
getCSSVar(floatingNode, 'border-width'),
|
|
46
|
+
floatingNode,
|
|
47
|
+
)
|
|
48
|
+
const borderRadius = toPx(
|
|
49
|
+
getCSSVar(floatingNode, 'border-radius'),
|
|
50
|
+
floatingNode,
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
// Create arrow node — used for both styles
|
|
54
|
+
const arrowNode = document.createElement('div')
|
|
55
|
+
const arrowHeight = resolveArrowHeight(options)
|
|
56
|
+
const arrowWidth = getArrowWidth({ ...options, arrowHeight })
|
|
57
|
+
|
|
58
|
+
const arrowProps = {
|
|
59
|
+
arrow,
|
|
60
|
+
arrowNode,
|
|
61
|
+
arrowWidth,
|
|
62
|
+
arrowHeight,
|
|
63
|
+
arrowRadius,
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
applyArrowStyles({ boxStyle, ...arrowProps })
|
|
67
|
+
if (arrow) floatingNode.appendChild(arrowNode)
|
|
68
|
+
|
|
69
|
+
// Calculate offset
|
|
70
|
+
const offset = calculateOffset({
|
|
71
|
+
floatingNode,
|
|
72
|
+
boxStyle,
|
|
73
|
+
arrow,
|
|
74
|
+
borderWidth,
|
|
75
|
+
offsetValue,
|
|
76
|
+
arrowHeight,
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
// Create floating options
|
|
80
|
+
const floatingOpts = {
|
|
81
|
+
strategy,
|
|
82
|
+
placement,
|
|
83
|
+
middleware: [
|
|
84
|
+
offsetFn(offset),
|
|
85
|
+
flip({ fallbackAxisSideDirection: 'start' }),
|
|
86
|
+
shift(),
|
|
87
|
+
arrow && arrowFn({ element: arrowNode }),
|
|
88
|
+
],
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return {
|
|
92
|
+
// Update floating position.
|
|
93
|
+
// Run when mounting.
|
|
94
|
+
update() {
|
|
95
|
+
_cleanup = autoUpdate(referenceNode, floatingNode, _ => {
|
|
96
|
+
computePosition(referenceNode, floatingNode, floatingOpts).then(
|
|
97
|
+
middlewareState => {
|
|
98
|
+
const { x, y } = middlewareState
|
|
99
|
+
|
|
100
|
+
// Position Popover
|
|
101
|
+
Object.assign(floatingNode.style, {
|
|
102
|
+
left: `${x}px`,
|
|
103
|
+
top: `${y}px`,
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
if (!arrow) return
|
|
107
|
+
|
|
108
|
+
// Position arrow
|
|
109
|
+
positionArrow({
|
|
110
|
+
...options,
|
|
111
|
+
...arrowProps,
|
|
112
|
+
borderWidth,
|
|
113
|
+
borderRadius,
|
|
114
|
+
middlewareState,
|
|
115
|
+
})
|
|
116
|
+
},
|
|
117
|
+
)
|
|
118
|
+
})
|
|
119
|
+
},
|
|
120
|
+
|
|
121
|
+
// Cleanup autoupdate.
|
|
122
|
+
// Run when dismounting.
|
|
123
|
+
cleanup() {
|
|
124
|
+
if (_cleanup) _cleanup()
|
|
125
|
+
},
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function applyBoxStyles({
|
|
130
|
+
referenceNode,
|
|
131
|
+
floatingNode,
|
|
132
|
+
strategy,
|
|
133
|
+
boxStyle,
|
|
134
|
+
placement,
|
|
135
|
+
arrow,
|
|
136
|
+
useReferenceBoundaries,
|
|
137
|
+
}) {
|
|
138
|
+
floatingNode.style.position = strategy
|
|
139
|
+
floatingNode.style.overflow = 'visible' // Users can specify max-height on the popover content to enable scrolling.
|
|
140
|
+
|
|
141
|
+
if (useReferenceBoundaries) {
|
|
142
|
+
const { width, height } = referenceNode.getBoundingClientRect()
|
|
143
|
+
|
|
144
|
+
if (['top', 'bottom'].includes(placement)) {
|
|
145
|
+
floatingNode.style.width = `${width}px`
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (['left', 'right'].includes(placement)) {
|
|
149
|
+
floatingNode.style.height = `${height}px`
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (boxStyle === 'fancy') {
|
|
154
|
+
if (!arrow) floatingNode.classList.add('fancy-box')
|
|
155
|
+
else {
|
|
156
|
+
const side = getArrowSide(placement)
|
|
157
|
+
floatingNode.classList.add('fancy-box-arrow', `arrow-${side}`)
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function resolveArrowHeight({ floatingNode, arrow, arrowHeight, boxStyle }) {
|
|
163
|
+
if (!arrow) return 0
|
|
164
|
+
if (boxStyle === 'standard') return toPx(arrowHeight, floatingNode)
|
|
165
|
+
|
|
166
|
+
// Fancy box style
|
|
167
|
+
if (arrowHeight) {
|
|
168
|
+
const h = toPx(arrowHeight, floatingNode)
|
|
169
|
+
setCSSVar(floatingNode, '--h', `${h}px`)
|
|
170
|
+
return h
|
|
171
|
+
} else {
|
|
172
|
+
const h = getCSSVar(floatingNode, '--h')
|
|
173
|
+
return toPx(h, floatingNode)
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function getArrowWidth({ boxStyle, floatingNode, arrowHeight }) {
|
|
178
|
+
// Standard box is a square, so it must use the same value for both width and height
|
|
179
|
+
if (boxStyle === 'standard') return arrowHeight
|
|
180
|
+
|
|
181
|
+
// Fancy box can have different width and height values depending on the angle used. Downside is the arrow will never be rounded.
|
|
182
|
+
|
|
183
|
+
let [angle, unit] = splitUnit(getCSSVar(floatingNode, '--a'))
|
|
184
|
+
if (unit === 'deg') angle = toRadians(angle)
|
|
185
|
+
return Math.tan(angle / 2) * arrowHeight * 2
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function getArrowSide(placement) {
|
|
189
|
+
const side = {
|
|
190
|
+
top: 'bottom',
|
|
191
|
+
right: 'left',
|
|
192
|
+
bottom: 'top',
|
|
193
|
+
left: 'right',
|
|
194
|
+
}[placement.split('-')[0]]
|
|
195
|
+
return side
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function applyArrowStyles({
|
|
199
|
+
arrowNode,
|
|
200
|
+
arrowWidth,
|
|
201
|
+
arrowHeight,
|
|
202
|
+
arrowRadius,
|
|
203
|
+
boxStyle,
|
|
204
|
+
}) {
|
|
205
|
+
// Need to check which borders and radius to remove
|
|
206
|
+
const styles = {
|
|
207
|
+
width: `${arrowWidth}px`,
|
|
208
|
+
height: `${arrowHeight}px`,
|
|
209
|
+
position: 'absolute',
|
|
210
|
+
borderRadius: '0',
|
|
211
|
+
borderTopRightRadius: arrowRadius,
|
|
212
|
+
borderWidth: 'inherit',
|
|
213
|
+
borderLeftWidth: '0',
|
|
214
|
+
borderBottomWidth: '0',
|
|
215
|
+
background: 'inherit',
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
if (boxStyle === 'fancy') {
|
|
219
|
+
Object.assign(styles, { opacity: '0' })
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
Object.assign(arrowNode.style, styles)
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function calculateOffset({
|
|
226
|
+
floatingNode,
|
|
227
|
+
boxStyle,
|
|
228
|
+
offsetValue,
|
|
229
|
+
borderWidth,
|
|
230
|
+
arrow,
|
|
231
|
+
arrowHeight,
|
|
232
|
+
}) {
|
|
233
|
+
let offset = toPx(offsetValue, floatingNode)
|
|
234
|
+
if (!arrow) return offset
|
|
235
|
+
|
|
236
|
+
if (boxStyle === 'standard') {
|
|
237
|
+
const arrowHypotenuse = Math.sqrt(arrowHeight ** 2 + borderWidth ** 2)
|
|
238
|
+
offset += Math.floor(arrowHypotenuse / 2)
|
|
239
|
+
}
|
|
240
|
+
if (boxStyle === 'fancy') offset += arrowHeight
|
|
241
|
+
return offset
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function positionArrow({
|
|
245
|
+
floatingNode,
|
|
246
|
+
middlewareState,
|
|
247
|
+
arrowNode,
|
|
248
|
+
arrowWidth,
|
|
249
|
+
arrowHeight,
|
|
250
|
+
borderWidth,
|
|
251
|
+
borderRadius,
|
|
252
|
+
boxStyle,
|
|
253
|
+
}) {
|
|
254
|
+
const { placement } = middlewareState
|
|
255
|
+
const { x, y } = middlewareState.middlewareData.arrow
|
|
256
|
+
const { width, height } = floatingNode.getBoundingClientRect()
|
|
257
|
+
|
|
258
|
+
// Do nothing if popover is not opened yet
|
|
259
|
+
if (!(width || height)) return
|
|
260
|
+
// Do nothing if no arrow data
|
|
261
|
+
if (!(x || y)) return
|
|
262
|
+
|
|
263
|
+
const side = getArrowSide(placement)
|
|
264
|
+
|
|
265
|
+
if (boxStyle === 'standard') {
|
|
266
|
+
// Rotate arrow box
|
|
267
|
+
let rotate = 45
|
|
268
|
+
// prettier-ignore
|
|
269
|
+
switch (side) {
|
|
270
|
+
case 'top': rotate = -45; break;
|
|
271
|
+
case 'right': rotate = 45; break;
|
|
272
|
+
case 'bottom': rotate = 135; break;
|
|
273
|
+
case 'left': rotate = 225; break;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
const adjusted = { x, y }
|
|
277
|
+
const safetySpace = 3
|
|
278
|
+
|
|
279
|
+
if (x || x === 0) {
|
|
280
|
+
const startEdge = borderRadius + safetySpace
|
|
281
|
+
const endEdge = width - borderRadius - arrowWidth - safetySpace
|
|
282
|
+
|
|
283
|
+
if (x <= startEdge) adjusted.x = startEdge
|
|
284
|
+
if (x >= endEdge) adjusted.x = endEdge
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
if (y || y === 0) {
|
|
288
|
+
const startEdge = borderRadius + safetySpace
|
|
289
|
+
const endEdge = height - borderRadius - arrowWidth - safetySpace
|
|
290
|
+
|
|
291
|
+
if (y <= startEdge) adjusted.y = startEdge
|
|
292
|
+
if (y >= endEdge) adjusted.y = endEdge
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
const arrowCrossOffset = Math.ceil((arrowHeight + borderWidth) / 2)
|
|
296
|
+
|
|
297
|
+
// prettier-ignore
|
|
298
|
+
Object.assign(arrowNode.style, {
|
|
299
|
+
left: x ? `${adjusted.x}px` : '',
|
|
300
|
+
top: y ? `${adjusted.y}px` : '',
|
|
301
|
+
[side]: `-${arrowCrossOffset}px`,
|
|
302
|
+
transform: `rotate(${rotate}deg)`,
|
|
303
|
+
})
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
if (boxStyle === 'fancy') {
|
|
307
|
+
if (x) arrowNode.style.left = `${x}px`
|
|
308
|
+
if (y) arrowNode.style.top = `${y}px`
|
|
309
|
+
|
|
310
|
+
let arrowCenter, arrowPosition
|
|
311
|
+
if (x) {
|
|
312
|
+
arrowCenter = x + arrowWidth / 2
|
|
313
|
+
arrowPosition = ((arrowCenter + borderWidth) / width) * 100
|
|
314
|
+
}
|
|
315
|
+
if (y) {
|
|
316
|
+
arrowCenter = y + arrowWidth / 2
|
|
317
|
+
arrowPosition = ((arrowCenter + borderWidth) / height) * 100
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
floatingNode.classList.remove(
|
|
321
|
+
'arrow-top',
|
|
322
|
+
'arrow-right',
|
|
323
|
+
'arrow-bottom',
|
|
324
|
+
'arrow-left',
|
|
325
|
+
)
|
|
326
|
+
floatingNode.classList.add(`arrow-${side}`)
|
|
327
|
+
setCSSVar(floatingNode, '--arrow-position', `${arrowPosition}%`)
|
|
328
|
+
}
|
|
329
|
+
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { delay, omitEmpty } from '@splendidlabz/utils'
|
|
2
1
|
import {
|
|
3
2
|
Focusable,
|
|
4
3
|
addListeners,
|
|
@@ -9,6 +8,7 @@ import {
|
|
|
9
8
|
removeListeners,
|
|
10
9
|
trapFocus,
|
|
11
10
|
} from '@splendidlabz/utils/dom'
|
|
11
|
+
import { delay, omitEmpty } from '@splendidlabz/utils'
|
|
12
12
|
import {
|
|
13
13
|
getDisclosure as getD,
|
|
14
14
|
getDisclosures,
|
|
@@ -43,12 +43,12 @@ export const DEFAULT_DISCLOSURE_CONTENT_OPTS = {
|
|
|
43
43
|
closeOnOutsideClick: true,
|
|
44
44
|
closeOnEscapeButton: true,
|
|
45
45
|
focusManagement: 'manage', // false, `manage`, `trap`
|
|
46
|
-
focusWhenOpen: 'content', // || false, 'first', 'content'. TODO: HTMLElement
|
|
46
|
+
focusWhenOpen: 'content', // || false, 'first', 'content'. TODO: HTMLElement. (No need, HTMLElement. Just use autofocus and set focusWhenOpen to false)
|
|
47
47
|
focusWhenClose: 'trigger', // false
|
|
48
48
|
usePortal: true,
|
|
49
49
|
useModal: false,
|
|
50
50
|
useTriggerWidth: false,
|
|
51
|
-
|
|
51
|
+
inertOthers: false,
|
|
52
52
|
// preventBodyScroll: false,
|
|
53
53
|
}
|
|
54
54
|
|
|
@@ -110,6 +110,7 @@ export function disclosureTrigger(node, props = {}) {
|
|
|
110
110
|
|
|
111
111
|
let d = initDisclosure('trigger', disclosureInfo)
|
|
112
112
|
node.setAttribute('aria-expanded', d.state.show)
|
|
113
|
+
node.setAttribute('aria-haspopup', d.options['aria-haspopup'])
|
|
113
114
|
|
|
114
115
|
const listeners = [
|
|
115
116
|
{
|
|
@@ -164,7 +165,6 @@ export function disclosureTrigger(node, props = {}) {
|
|
|
164
165
|
|
|
165
166
|
function tabOntoContent(event) {
|
|
166
167
|
if (d.state.show === false) return
|
|
167
|
-
console.log('tabOntoContent')
|
|
168
168
|
if (isTab(event)) {
|
|
169
169
|
event.preventDefault()
|
|
170
170
|
d.nodes.contentFocusables.first?.focus()
|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
-
export * from './
|
|
2
|
-
export * from './
|
|
3
|
-
export * from './
|
|
1
|
+
export * from './DrawerNew'
|
|
2
|
+
export * from './ModalNew'
|
|
3
|
+
export * from './PopoverNew'
|
|
4
|
+
// export * from './Drawer'
|
|
5
|
+
// export * from './Dropdown'
|
|
6
|
+
// export * from './Popover'
|
|
7
|
+
// export * from './DisclosureNew'
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { dispatchEvent } from '@splendidlabz/utils/dom'
|
|
2
|
+
export function getTrigger(selector) {
|
|
3
|
+
return document.querySelector(selector)
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export function passEventToTrigger(event, trigger) {
|
|
7
|
+
const { newState, oldState } = event
|
|
8
|
+
dispatchEvent(trigger, event.type, { newState, oldState })
|
|
9
|
+
}
|
|
@@ -1,79 +1,94 @@
|
|
|
1
1
|
<script>
|
|
2
|
-
import {
|
|
2
|
+
import { toSentence } from '@splendidlabz/utils'
|
|
3
3
|
import { randomString } from '@splendidlabz/utils/dom'
|
|
4
4
|
import SVG from '../Icon/Icon.svelte'
|
|
5
5
|
import TextInput from './TextInput.svelte'
|
|
6
|
-
let klass = ''
|
|
7
|
-
export { klass as class }
|
|
8
6
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
7
|
+
let {
|
|
8
|
+
name = '',
|
|
9
|
+
label = null,
|
|
10
|
+
value = 'on',
|
|
11
|
+
checked = $bindable(false),
|
|
12
|
+
ref = $bindable(null),
|
|
13
|
+
...p
|
|
14
|
+
} = $props()
|
|
13
15
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
export let readonly = null
|
|
21
|
-
export let ref = null
|
|
22
|
-
export let alwaysSend = false
|
|
23
|
-
export let hasInput = false
|
|
16
|
+
const {
|
|
17
|
+
class: klass = '',
|
|
18
|
+
icon = null,
|
|
19
|
+
iconClass = null,
|
|
20
|
+
fieldClass = '',
|
|
21
|
+
labelClass = '',
|
|
24
22
|
|
|
25
|
-
|
|
26
|
-
|
|
23
|
+
id = randomString(8),
|
|
24
|
+
required = null,
|
|
25
|
+
readonly = null,
|
|
26
|
+
disabled = null,
|
|
27
|
+
alwaysSend = false,
|
|
28
|
+
hasInput = false,
|
|
29
|
+
...rest
|
|
30
|
+
} = p
|
|
27
31
|
|
|
32
|
+
// Original value
|
|
33
|
+
const ov = value
|
|
34
|
+
|
|
35
|
+
// Name and value are often confused for checkboxes. Value should be on, but user mistake value as name. If so, we swap it for them
|
|
36
|
+
if (value !== 'on' && !name) name = value
|
|
37
|
+
|
|
38
|
+
// Safety check
|
|
28
39
|
if (!name) throw new Error('Checkbox requires a name')
|
|
29
40
|
|
|
30
|
-
// Create label if not provided
|
|
31
|
-
if (name && !label && label !== '') label =
|
|
41
|
+
// Create label from name if not provided
|
|
42
|
+
if (name && !label && label !== '') label = toSentence(name)
|
|
32
43
|
|
|
33
|
-
|
|
34
|
-
|
|
44
|
+
$effect(() => {
|
|
45
|
+
// Sends the checked value upwards
|
|
46
|
+
if (checked) value = ov
|
|
47
|
+
|
|
48
|
+
// Allows sendAlways to work (I think)
|
|
35
49
|
if (!checked) value = 'off'
|
|
36
|
-
}
|
|
50
|
+
})
|
|
37
51
|
</script>
|
|
38
52
|
|
|
39
|
-
<div class="
|
|
40
|
-
<div class="
|
|
41
|
-
<
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
+
<div class="checkbox {klass}" class:checked class:disabled class:readonly>
|
|
54
|
+
<div class="group">
|
|
55
|
+
<div class="checkmark-container">
|
|
56
|
+
<input
|
|
57
|
+
{...rest}
|
|
58
|
+
class="input {fieldClass}"
|
|
59
|
+
type="checkbox"
|
|
60
|
+
{id}
|
|
61
|
+
{name}
|
|
62
|
+
{value}
|
|
63
|
+
bind:checked
|
|
64
|
+
bind:this={ref}
|
|
65
|
+
disabled={disabled || readonly}
|
|
66
|
+
/>
|
|
53
67
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
68
|
+
{#if readonly || alwaysSend}
|
|
69
|
+
<input type="hidden" {name} {checked} {value} />
|
|
70
|
+
{/if}
|
|
57
71
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
72
|
+
{#if icon}
|
|
73
|
+
<SVG class="checkmark {iconClass}" {icon} />
|
|
74
|
+
{:else}
|
|
75
|
+
<svg class="checkmark" viewBox="0 0 24 24" fill="none">
|
|
76
|
+
<path
|
|
77
|
+
d="M5 12L10 17L20 7"
|
|
78
|
+
stroke="currentcolor"
|
|
79
|
+
stroke-width="3"
|
|
80
|
+
stroke-linecap="round"
|
|
81
|
+
stroke-linejoin="round"
|
|
82
|
+
/>
|
|
83
|
+
</svg>
|
|
84
|
+
{/if}
|
|
85
|
+
</div>
|
|
86
|
+
|
|
87
|
+
{#if label}
|
|
88
|
+
<label class="label {labelClass}" for={id}>{label}</label>
|
|
70
89
|
{/if}
|
|
71
90
|
</div>
|
|
72
91
|
|
|
73
|
-
{#if label}
|
|
74
|
-
<label class="label {labelClass}" for={id}>{label}</label>
|
|
75
|
-
{/if}
|
|
76
|
-
|
|
77
92
|
{#if hasInput}
|
|
78
93
|
<TextInput {name} />
|
|
79
94
|
{/if}
|
|
@@ -1,44 +1,62 @@
|
|
|
1
1
|
<script>
|
|
2
2
|
import Checkbox from './Checkbox.svelte'
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
3
|
+
|
|
4
|
+
let {
|
|
5
|
+
legend = '',
|
|
6
|
+
legendClass = '',
|
|
7
|
+
label = '',
|
|
8
|
+
labelClass = '',
|
|
9
|
+
items = $bindable([]),
|
|
10
|
+
checkboxes = $bindable([]),
|
|
11
|
+
ref = $bindable(null),
|
|
12
|
+
checked = false,
|
|
13
|
+
...p
|
|
14
|
+
} = $props()
|
|
15
|
+
|
|
16
|
+
// Let users use either items or checkboxes. Does this work?
|
|
17
|
+
if (checkboxes.length) items = checkboxes
|
|
18
|
+
if (!items.length && !checkboxes.length) {
|
|
19
|
+
console.warn('No checkboxes or items provided to <Checkboxes />')
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Let users use either label or legend
|
|
23
|
+
if (label) legend = label
|
|
24
|
+
if (labelClass) legendClass = labelClass
|
|
25
|
+
|
|
26
|
+
const {
|
|
27
|
+
class: klass = '',
|
|
28
|
+
checkboxesClass = '',
|
|
29
|
+
disabled = null,
|
|
30
|
+
readonly = null,
|
|
31
|
+
alwaysSend = null,
|
|
32
|
+
...rest
|
|
33
|
+
} = p
|
|
34
|
+
|
|
35
|
+
const staticCheckboxProps = {
|
|
18
36
|
disabled,
|
|
19
37
|
readonly,
|
|
20
38
|
alwaysSend,
|
|
21
39
|
}
|
|
22
40
|
|
|
23
|
-
|
|
41
|
+
items = items.map(checkbox => {
|
|
24
42
|
if (typeof checkbox === 'string') checkbox = { name: checkbox }
|
|
25
|
-
return { checked, ...checkbox }
|
|
43
|
+
return { checked, ref: null, ...checkbox }
|
|
26
44
|
})
|
|
27
45
|
|
|
28
|
-
//
|
|
29
|
-
// Useful for groups of checkboxes.
|
|
46
|
+
// TODO Later: Indeterminate checkboxes
|
|
47
|
+
// Useful for groups of checkboxes.
|
|
30
48
|
// inputInstance.indeterminate = true
|
|
31
49
|
</script>
|
|
32
50
|
|
|
33
|
-
<fieldset class="fieldset {klass}"
|
|
51
|
+
<fieldset {...rest} class="fieldset {klass}" bind:this={ref}>
|
|
34
52
|
{#if legend}
|
|
35
|
-
<legend class="
|
|
53
|
+
<legend class="legend {legendClass}">{legend}</legend>
|
|
36
54
|
{/if}
|
|
37
55
|
|
|
38
|
-
<div class="
|
|
39
|
-
{#each
|
|
56
|
+
<div class="checkboxes {checkboxesClass}">
|
|
57
|
+
{#each items as checkbox}
|
|
40
58
|
<Checkbox
|
|
41
|
-
{...
|
|
59
|
+
{...staticCheckboxProps}
|
|
42
60
|
{...checkbox}
|
|
43
61
|
bind:ref={checkbox.ref}
|
|
44
62
|
bind:checked={checkbox.checked}
|