@splendidlabz/svelte 0.4.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/.eslintrc.cjs +3 -0
- package/CHANGELOG.md +91 -0
- package/actions/breakpoint-observer.js +71 -0
- package/actions/index.js +6 -0
- package/actions/portal.js +20 -0
- package/actions/resize-observer.js +43 -0
- package/actions/scroll-observer.js +47 -0
- package/common.js +21 -0
- package/components/Accordion/Accordion.svelte +108 -0
- package/components/Accordion/index.js +3 -0
- package/components/Breadcrumbs/Breadcrumbs.svelte +69 -0
- package/components/Breadcrumbs/Crumb.svelte +37 -0
- package/components/Breadcrumbs/CrumbCustom.svelte +14 -0
- package/components/Breadcrumbs/index.js +5 -0
- package/components/BrowserFrame.svelte +37 -0
- package/components/BrowserWithTabs.svelte +28 -0
- package/components/CQChecker/CQChecker.astro +20 -0
- package/components/CQChecker/CQChecker.svelte +89 -0
- package/components/Callout/Callout.svelte +17 -0
- package/components/Callout/index.js +3 -0
- package/components/Codepen/Codepen.astro +29 -0
- package/components/Codepen/Codepen.svelte +24 -0
- package/components/Codepen/index.js +3 -0
- package/components/ContentList/ContentList-old.svelte +117 -0
- package/components/ContentList/ContentList.svelte +68 -0
- package/components/ContentList/index.js +3 -0
- package/components/Disclosure/Drawer/Drawer.svelte +35 -0
- package/components/Disclosure/Drawer/DrawerCloseButton.svelte +31 -0
- package/components/Disclosure/Drawer/DrawerContent-wrecked.svelte +198 -0
- package/components/Disclosure/Drawer/DrawerContent.svelte +164 -0
- package/components/Disclosure/Drawer/DrawerTrigger.svelte +32 -0
- package/components/Disclosure/Drawer/drawer.js +112 -0
- package/components/Disclosure/Drawer/index.js +6 -0
- package/components/Disclosure/Dropdown/DropdownContent.svelte +15 -0
- package/components/Disclosure/Dropdown/DropdownTrigger.svelte +14 -0
- package/components/Disclosure/Dropdown/dropdown.js +4 -0
- package/components/Disclosure/Dropdown/index.js +4 -0
- package/components/Disclosure/Modal/ModalContent.svelte +3 -0
- package/components/Disclosure/Modal/ModalTrigger.svelte +33 -0
- package/components/Disclosure/Modal/exit-intent.js +17 -0
- package/components/Disclosure/Modal/modal.js +13 -0
- package/components/Disclosure/Popover/PopoverCloseButton.svelte +29 -0
- package/components/Disclosure/Popover/PopoverContent.svelte +107 -0
- package/components/Disclosure/Popover/PopoverTrigger.svelte +33 -0
- package/components/Disclosure/Popover/floating.js +171 -0
- package/components/Disclosure/Popover/index.js +5 -0
- package/components/Disclosure/Popover/popover.js +18 -0
- package/components/Disclosure/Popover/references/FollowTheMouse.svelte +45 -0
- package/components/Disclosure/Tooltip/Tooltip.svelte +14 -0
- package/components/Disclosure/Tooltip/TooltipContent.svelte +7 -0
- package/components/Disclosure/Tooltip/TooltipTrigger.svelte +33 -0
- package/components/Disclosure/Tooltip/index.js +5 -0
- package/components/Disclosure/Tooltip/tooltip.js +22 -0
- package/components/Disclosure/disclosure-store.js +48 -0
- package/components/Disclosure/disclosure.js +439 -0
- package/components/Disclosure/index.js +3 -0
- package/components/Disclosure/state.svelte.js +138 -0
- package/components/FancyList/FancyList.svelte +49 -0
- package/components/FancyList/index.js +3 -0
- package/components/Form/Button.svelte +24 -0
- package/components/Form/Checkbox.svelte +80 -0
- package/components/Form/Checkboxes.svelte +48 -0
- package/components/Form/Combobox/AutocompleteInput.svelte +63 -0
- package/components/Form/Combobox/AutocompleteOptions.svelte +74 -0
- package/components/Form/Combobox/TimepickerInput.svelte +81 -0
- package/components/Form/Combobox/TimepickerOptions.svelte +100 -0
- package/components/Form/Combobox/autocomplete-matching.js +114 -0
- package/components/Form/Combobox/autocomplete.js +393 -0
- package/components/Form/Combobox/combobox-store.js +65 -0
- package/components/Form/Combobox/combobox.js +359 -0
- package/components/Form/Combobox/index.js +14 -0
- package/components/Form/Combobox/timepicker.js +119 -0
- package/components/Form/Form.svelte +62 -0
- package/components/Form/Hidden.svelte +16 -0
- package/components/Form/Radio.svelte +69 -0
- package/components/Form/Radios.svelte +50 -0
- package/components/Form/Select.svelte +127 -0
- package/components/Form/Switch.svelte +50 -0
- package/components/Form/TextInput.svelte +87 -0
- package/components/Form/Textarea.svelte +103 -0
- package/components/Form/check.svg +3 -0
- package/components/Form/index.js +12 -0
- package/components/Highlight/Highlight.svelte +27 -0
- package/components/Highlight/_Highlight.scss +19 -0
- package/components/Highlight/index.js +2 -0
- package/components/Image.svelte +24 -0
- package/components/InteractiveRegion/InteractiveRegion.svelte +47 -0
- package/components/InteractiveRegion/index.js +3 -0
- package/components/List/Lia.svelte +12 -0
- package/components/List/NestedListItem.svelte +50 -0
- package/components/List/index.js +3 -0
- package/components/Markdown/Markdown.svelte +35 -0
- package/components/Masonry/Masonry.svelte +79 -0
- package/components/Masonry/index.js +3 -0
- package/components/PerspectiveHover/PerspectiveHover.svelte +10 -0
- package/components/PerspectiveHover/perspective-hover.js +46 -0
- package/components/Populate/ConditionalPopulate.svelte +33 -0
- package/components/Populate/Populate.svelte +45 -0
- package/components/Populate/actions.js +29 -0
- package/components/Populate/get-value.js +17 -0
- package/components/Populate/index.js +4 -0
- package/components/Portal.svelte +27 -0
- package/components/PushToLocalStorage.svelte +30 -0
- package/components/Resizer/Resizer.svelte +32 -0
- package/components/Resizer/draggable.js +23 -0
- package/components/Resizer/index.js +3 -0
- package/components/Resizer/resizer.js +133 -0
- package/components/SVG/SVG.svelte +120 -0
- package/components/Scrambler/Scrambler.svelte +27 -0
- package/components/Scrambler/index.js +3 -0
- package/components/Spotlight/PointerPosition.js +32 -0
- package/components/Spotlight/Spotlight.svelte +9 -0
- package/components/Status.svelte +27 -0
- package/components/Switcheroo/Switcheroo.svelte +71 -0
- package/components/Switcheroo/index.js +3 -0
- package/components/Table/RowGroup.svelte +8 -0
- package/components/Table/Table.svelte +23 -0
- package/components/Table/Td.svelte +20 -0
- package/components/Table/Th.svelte +27 -0
- package/components/Table/Thead.svelte +8 -0
- package/components/Table/Tr.svelte +8 -0
- package/components/Table/index.js +17 -0
- package/components/Table/table.js +35 -0
- package/components/Tabs/Tab.svelte +12 -0
- package/components/Tabs/Tablist.svelte +62 -0
- package/components/Tabs/Tabpanel.svelte +10 -0
- package/components/Tabs/Tabpanels.svelte +12 -0
- package/components/Tabs/index.js +6 -0
- package/components/Tabs/tab-store.js +60 -0
- package/components/Tabs/tabs.js +364 -0
- package/components/Tabs-Old/Tab.svelte +9 -0
- package/components/Tabs-Old/Tablist.svelte +13 -0
- package/components/Tabs-Old/Tabpanel.svelte +10 -0
- package/components/Tabs-Old/Tabpanels.svelte +14 -0
- package/components/Tabs-Old/Tabs.svelte +83 -0
- package/components/Tabs-Old/index.js +7 -0
- package/components/Tabs-Old/tabs.js +306 -0
- package/components/Textwall/Repeat.svelte +10 -0
- package/components/Textwall/Textwall.svelte +30 -0
- package/components/Textwall/index.js +4 -0
- package/components/Typewriter/Typewriter.svelte +70 -0
- package/components/Typewriter/index.js +3 -0
- package/components/Video.svelte +46 -0
- package/components/Vimeo.svelte +18 -0
- package/components/Wrapper.svelte +22 -0
- package/components/Youtube.svelte +15 -0
- package/components/_shared/accessibility.js +9 -0
- package/components/_shared/key.js +1 -0
- package/components/_shared/transitions.js +48 -0
- package/components/_shared/use-actions.js +31 -0
- package/index.js +35 -0
- package/jsconfig.json +5 -0
- package/package.json +39 -0
|
@@ -0,0 +1,439 @@
|
|
|
1
|
+
import { delay, omitEmpty } from '@splendidlabz/utils'
|
|
2
|
+
import {
|
|
3
|
+
Focusable,
|
|
4
|
+
addListeners,
|
|
5
|
+
getChildrenElements,
|
|
6
|
+
isAncestor,
|
|
7
|
+
isTab,
|
|
8
|
+
manageFocus,
|
|
9
|
+
removeListeners,
|
|
10
|
+
trapFocus,
|
|
11
|
+
} from '@splendidlabz/utils/dom'
|
|
12
|
+
import {
|
|
13
|
+
getDisclosure as getD,
|
|
14
|
+
getDisclosures,
|
|
15
|
+
initDisclosure,
|
|
16
|
+
updateDisclosure,
|
|
17
|
+
} from './disclosure-store.js'
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @typedef {Object} DisclosureTriggerOpts
|
|
21
|
+
* @property {boolean} showOnHover - Show disclosure content on hover.
|
|
22
|
+
* @property {boolean} showOnFocus - Show disclosure content on focus.
|
|
23
|
+
* @property {number} hoverCloseDelay - Delay in milliseconds before closing disclosure content on mouse leave.
|
|
24
|
+
* @property {boolean} closeOthers - Close other disclosures when opening this disclosure.
|
|
25
|
+
*/
|
|
26
|
+
export const DEFAULT_DISCLOSURE_TRIGGER_OPTS = {
|
|
27
|
+
showOnClick: true,
|
|
28
|
+
showOnHover: false,
|
|
29
|
+
showOnFocus: false,
|
|
30
|
+
hoverCloseDelay: 120,
|
|
31
|
+
closeOthers: true,
|
|
32
|
+
// shiftTabBackToContent: true,
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export const DISCLOSURE_TRIGGER_OPT_KEYS = Object.keys(
|
|
36
|
+
DEFAULT_DISCLOSURE_TRIGGER_OPTS,
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* @typedef {Object} DisclosureContentOpts
|
|
41
|
+
* @property {boolean} closeOnOutsideClick - Close disclosure content when clicking outside of contentNode and triggerNode.
|
|
42
|
+
* @property {boolean} closeOnEscapeButton - Close disclosure content when pressing the escape key.
|
|
43
|
+
* @property {boolean} preventBodyScroll - Prevent body from scrolling when disclosure content is open.
|
|
44
|
+
* @property {boolean} trapFocus - Trap focus within disclosure content.
|
|
45
|
+
* @property {(string|HTMLElement)} focusTargetWhenOpening - Focus target when disclosure content is opened. Can be 'first', 'content', or an HTMLElement.
|
|
46
|
+
*/
|
|
47
|
+
export const DEFAULT_DISCLOSURE_CONTENT_OPTS = {
|
|
48
|
+
closeOnOutsideClick: true,
|
|
49
|
+
closeOnEscapeButton: true,
|
|
50
|
+
focusManagement: 'manage', // false, `manage`, `trap`
|
|
51
|
+
focusTargetWhenOpening: 'content', // || false, 'first', 'content'. TODO: HTMLElement
|
|
52
|
+
focusTriggerOnClose: true,
|
|
53
|
+
inertOthers: false,
|
|
54
|
+
preventBodyScroll: false,
|
|
55
|
+
useAutoWidth: false,
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export const DISCLOSURE_CONTENT_OPT_KEYS = Object.keys(
|
|
59
|
+
DEFAULT_DISCLOSURE_CONTENT_OPTS,
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
// Potentially switch to the Popover API in the future if the change warrants it.
|
|
63
|
+
/**
|
|
64
|
+
* Disclosure Trigger
|
|
65
|
+
* @prop {HTMLElement} node - The trigger element.
|
|
66
|
+
* @prop {DisclosureTriggerOpts} props - The trigger options.
|
|
67
|
+
*/
|
|
68
|
+
export function disclosureTrigger(node, props = {}) {
|
|
69
|
+
const targetId = node.getAttribute('target')
|
|
70
|
+
|
|
71
|
+
const disclosureInfo = {
|
|
72
|
+
id: targetId,
|
|
73
|
+
state: { show: false, ...omitEmpty(props.state) },
|
|
74
|
+
options: {
|
|
75
|
+
...DEFAULT_DISCLOSURE_TRIGGER_OPTS,
|
|
76
|
+
...props.options,
|
|
77
|
+
},
|
|
78
|
+
nodes: { trigger: node },
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const d = initDisclosure(disclosureInfo)
|
|
82
|
+
node.setAttribute('aria-expanded', d.state.show)
|
|
83
|
+
|
|
84
|
+
const listeners = [
|
|
85
|
+
{
|
|
86
|
+
event: 'click',
|
|
87
|
+
node,
|
|
88
|
+
handler: handleClick,
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
event: 'keydown',
|
|
92
|
+
node,
|
|
93
|
+
handler: closeOnEscapeButton,
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
event: 'keydown',
|
|
97
|
+
node,
|
|
98
|
+
handler: tabOntoContent,
|
|
99
|
+
},
|
|
100
|
+
// Testing with removing this feature
|
|
101
|
+
// {
|
|
102
|
+
// event: 'keydown',
|
|
103
|
+
// node,
|
|
104
|
+
// handler: backToDisclosureContent,
|
|
105
|
+
// },
|
|
106
|
+
{
|
|
107
|
+
event: 'pointerenter',
|
|
108
|
+
node,
|
|
109
|
+
handler: handlePointerEnter,
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
event: 'pointerleave',
|
|
113
|
+
node,
|
|
114
|
+
handler: handlePointerLeave,
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
event: 'focusin',
|
|
118
|
+
node,
|
|
119
|
+
handler: handleFocusIn,
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
event: 'focusout',
|
|
123
|
+
node,
|
|
124
|
+
handler: handleFocusOut,
|
|
125
|
+
},
|
|
126
|
+
]
|
|
127
|
+
|
|
128
|
+
function handleClick(event) {
|
|
129
|
+
if (d.options.showOnClick) {
|
|
130
|
+
if (d.state.show === false) return open(d)
|
|
131
|
+
if (d.state.show === true) return close(d)
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function closeOnEscapeButton(event) {
|
|
136
|
+
if (d.options.closeOnEscapeButton) {
|
|
137
|
+
if (event.key === 'Escape') close(d)
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function tabOntoContent(event) {
|
|
142
|
+
if (d.state.show === false) return
|
|
143
|
+
console.log('tabOntoContent')
|
|
144
|
+
if (isTab(event)) {
|
|
145
|
+
event.preventDefault()
|
|
146
|
+
d.nodes.contentFocusables.first?.focus()
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// function backToDisclosureContent(event) {
|
|
151
|
+
// if (d.options.shiftTabBackToContent === false) return
|
|
152
|
+
// if (isShiftTab(event) && d.state.show === true) {
|
|
153
|
+
// setTimeout(_ => {
|
|
154
|
+
// d.nodes.content.focus({ preventScroll: true })
|
|
155
|
+
// }, 0)
|
|
156
|
+
// }
|
|
157
|
+
// }
|
|
158
|
+
|
|
159
|
+
function handlePointerEnter(event) {
|
|
160
|
+
if (d.options.showOnHover) {
|
|
161
|
+
d.state.pointerLocation = 'trigger'
|
|
162
|
+
open(d)
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
async function handlePointerLeave(event) {
|
|
167
|
+
if (d.options.showOnHover) {
|
|
168
|
+
d.state.pointerLocation = null
|
|
169
|
+
|
|
170
|
+
await delay(d.options.hoverCloseDelay)
|
|
171
|
+
if (d.state.pointerLocation === null) {
|
|
172
|
+
d.state.isMouseOut = true
|
|
173
|
+
close(d)
|
|
174
|
+
d.state.isMouseOut = false
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function handleFocusIn(event) {
|
|
180
|
+
if (d.options.showOnFocus) open(d)
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function handleFocusOut() {
|
|
184
|
+
if (d.options.showOnFocus) close(d)
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
addListeners(listeners)
|
|
188
|
+
|
|
189
|
+
return {
|
|
190
|
+
destroy() {
|
|
191
|
+
removeListeners(listeners)
|
|
192
|
+
},
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Disclosure Content action.
|
|
198
|
+
* Disclosure content will only activate when d.state.show is updated to true
|
|
199
|
+
* @prop {HTMLElement} node - The trigger element.
|
|
200
|
+
* @prop {DisclosureContentOpts} props - The trigger options.
|
|
201
|
+
*/
|
|
202
|
+
export function disclosureContent(node, props = {}) {
|
|
203
|
+
const disclosureContentID = node.getAttribute('id')
|
|
204
|
+
const contentFocusables = Focusable(node)
|
|
205
|
+
|
|
206
|
+
const disclosureInfo = {
|
|
207
|
+
id: disclosureContentID,
|
|
208
|
+
state: omitEmpty(props.state),
|
|
209
|
+
options: {
|
|
210
|
+
...DEFAULT_DISCLOSURE_CONTENT_OPTS,
|
|
211
|
+
...omitEmpty(props.options),
|
|
212
|
+
},
|
|
213
|
+
nodes: { content: node, contentFocusables },
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
if (disclosureInfo.options.inertOthers) {
|
|
217
|
+
disclosureInfo.options.preventBodyScroll = true
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const d = initDisclosure(disclosureInfo)
|
|
221
|
+
|
|
222
|
+
// Opening actions
|
|
223
|
+
node.setAttribute('tabindex', '-1')
|
|
224
|
+
|
|
225
|
+
const { focusTargetWhenOpening, useAutoWidth } = d.options
|
|
226
|
+
|
|
227
|
+
if (d.options.inertOthers) makeOthersInert(d)
|
|
228
|
+
if (d.options.preventBodyScroll) preventBodyScroll()
|
|
229
|
+
|
|
230
|
+
if (useAutoWidth) {
|
|
231
|
+
const { width: triggerWidth } = d.nodes.trigger.getBoundingClientRect()
|
|
232
|
+
node.style.width = triggerWidth + 'px'
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// Setting focus requires timeout. Otherwise, it won't work.
|
|
236
|
+
setTimeout(_ => {
|
|
237
|
+
if (focusTargetWhenOpening) {
|
|
238
|
+
if (focusTargetWhenOpening === 'content') node.focus()
|
|
239
|
+
if (focusTargetWhenOpening === 'first') contentFocusables.first?.focus()
|
|
240
|
+
if (focusTargetWhenOpening === 'second') contentFocusables[1]?.focus()
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// Dispatch an open event
|
|
244
|
+
d.nodes.content.dispatchEvent(new CustomEvent('open'))
|
|
245
|
+
}, 0)
|
|
246
|
+
|
|
247
|
+
// Add event listeners
|
|
248
|
+
const listeners = [
|
|
249
|
+
{
|
|
250
|
+
event: 'click',
|
|
251
|
+
node: document,
|
|
252
|
+
handler: handleOutsideClick,
|
|
253
|
+
},
|
|
254
|
+
// {
|
|
255
|
+
// event: 'keydown',
|
|
256
|
+
// node: document,
|
|
257
|
+
// handler: closeAllOnEscape,
|
|
258
|
+
// },
|
|
259
|
+
{
|
|
260
|
+
event: 'keydown',
|
|
261
|
+
node,
|
|
262
|
+
handler: handleKeydown,
|
|
263
|
+
},
|
|
264
|
+
{
|
|
265
|
+
event: 'mouseenter',
|
|
266
|
+
node,
|
|
267
|
+
handler: handleMouseEnter,
|
|
268
|
+
},
|
|
269
|
+
{
|
|
270
|
+
event: 'mouseleave',
|
|
271
|
+
node,
|
|
272
|
+
handler: handleMouseLeave,
|
|
273
|
+
},
|
|
274
|
+
]
|
|
275
|
+
|
|
276
|
+
addListeners(listeners)
|
|
277
|
+
|
|
278
|
+
return {
|
|
279
|
+
destroy() {
|
|
280
|
+
removeListeners(listeners)
|
|
281
|
+
},
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// ========================
|
|
285
|
+
// Event Listeners
|
|
286
|
+
// ========================
|
|
287
|
+
// Close if click is outside of contentNode and triggerNode
|
|
288
|
+
function handleOutsideClick(event) {
|
|
289
|
+
if (d.options.closeOnOutsideClick === false) return
|
|
290
|
+
if (d.nodes.content.contains(event.target)) return
|
|
291
|
+
if (d.nodes.trigger.contains(event.target)) return
|
|
292
|
+
// Only close if state is currently set to show
|
|
293
|
+
if (d.state.show) {
|
|
294
|
+
if (d.options.closeOthers) close(d)
|
|
295
|
+
else if (!clickedOnOtherDisclosureNodes(event.target, d.id)) close(d)
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// function closeAllOnEscape(event) {
|
|
300
|
+
// if (d.options.closeOnEscapeButton && event.key === 'Escape') {
|
|
301
|
+
// const disclosures = getDisclosures()
|
|
302
|
+
// disclosures.forEach(d => close(d, { shouldFocusTrigger: true }))
|
|
303
|
+
// }
|
|
304
|
+
// }
|
|
305
|
+
|
|
306
|
+
function handleKeydown(event) {
|
|
307
|
+
const { focusManagement } = d.options
|
|
308
|
+
|
|
309
|
+
if (focusManagement === 'trap') {
|
|
310
|
+
trapFocus({ event, focusables: d.nodes.contentFocusables })
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
if (focusManagement === 'manage') {
|
|
314
|
+
manageFocus({
|
|
315
|
+
event,
|
|
316
|
+
triggerNode: d.nodes.trigger,
|
|
317
|
+
contentNode: d.nodes.content,
|
|
318
|
+
focusables: d.nodes.contentFocusables,
|
|
319
|
+
shouldFocusTriggerWhenTabbingOut: d.options.focusTriggerWhenTabbingOut,
|
|
320
|
+
onLeave(direction) {
|
|
321
|
+
if (direction === 'forward') close(d)
|
|
322
|
+
},
|
|
323
|
+
})
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
if (d.options.closeOnEscapeButton) closeOnEscape(event)
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
function closeOnEscape(event) {
|
|
330
|
+
if (event.key === 'Escape') close(d, { shouldFocusTrigger: true })
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
function handleMouseEnter() {
|
|
334
|
+
if (!d.options.showOnHover) return
|
|
335
|
+
d.state.pointerLocation = 'content'
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
function handleMouseLeave() {
|
|
339
|
+
if (!d.options.showOnHover) return
|
|
340
|
+
d.state.pointerLocation = null
|
|
341
|
+
close(d)
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
// ========================
|
|
346
|
+
// Methods
|
|
347
|
+
// ========================
|
|
348
|
+
export function open(d) {
|
|
349
|
+
if (d.state.show === true) return
|
|
350
|
+
d.state.show = true
|
|
351
|
+
const { trigger } = d.nodes
|
|
352
|
+
trigger.setAttribute('aria-expanded', d.state.show)
|
|
353
|
+
|
|
354
|
+
if (d.options.closeOthers) closeOthersExcept(d.id)
|
|
355
|
+
|
|
356
|
+
updateDisclosure(d)
|
|
357
|
+
|
|
358
|
+
// This run disclosureContent because d.state.show will be updated into stores
|
|
359
|
+
// All other opening activites are conducted within `disclosureContent` because we have to wait for content node to be ready. So @see disclosureContent for details.
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
export function close(d, { shouldFocusTrigger = false } = {}) {
|
|
363
|
+
if (d.state.show === false) return
|
|
364
|
+
d.state.show = false
|
|
365
|
+
|
|
366
|
+
const { trigger, content } = d.nodes
|
|
367
|
+
trigger.setAttribute('aria-expanded', d.state.show)
|
|
368
|
+
revertInert(d)
|
|
369
|
+
|
|
370
|
+
updateDisclosure(d)
|
|
371
|
+
|
|
372
|
+
// Dispatch a close event
|
|
373
|
+
// If check because we can close disclosure content so quickly in some implementations it hasn't been opened yet. Example: Combobox.
|
|
374
|
+
if (content) content.dispatchEvent(new CustomEvent('close'))
|
|
375
|
+
|
|
376
|
+
// If it's mouse out, we no need to change focus back to trigger. Only bring focus back to trigger when using keyboard events, and only when necessary
|
|
377
|
+
|
|
378
|
+
if (
|
|
379
|
+
!d.state.isMouseOut &&
|
|
380
|
+
d.options.focusTriggerOnClose &&
|
|
381
|
+
shouldFocusTrigger
|
|
382
|
+
) {
|
|
383
|
+
trigger.focus()
|
|
384
|
+
}
|
|
385
|
+
if (d.options.preventBodyScroll) revertPreventBodyScroll()
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
// For manual closing. Need to get disclosure before we can use `close`.
|
|
389
|
+
export function getDisclosure(id) {
|
|
390
|
+
return getD(id)
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
function closeOthersExcept(id) {
|
|
394
|
+
const disclosures = getDisclosures()
|
|
395
|
+
disclosures.forEach(d => {
|
|
396
|
+
if (d.id !== id) close(d)
|
|
397
|
+
})
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
function clickedOnOtherDisclosureNodes(target, id) {
|
|
401
|
+
const disclosures = getDisclosures()
|
|
402
|
+
return disclosures.some(d => {
|
|
403
|
+
if (d.id === id) return false
|
|
404
|
+
if (d.nodes.trigger?.contains(target)) return true
|
|
405
|
+
if (d.nodes.content?.contains(target)) return true
|
|
406
|
+
return false
|
|
407
|
+
})
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
function preventBodyScroll() {
|
|
411
|
+
const top = document.documentElement.scrollTop
|
|
412
|
+
document.body.style.top = -1 * top + 'px'
|
|
413
|
+
document.body.style.overflow = 'hidden'
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
function revertPreventBodyScroll() {
|
|
417
|
+
const top = parseFloat(document.body.style.top) * -1
|
|
418
|
+
document.body.style.top = null
|
|
419
|
+
document.body.style.overflow = null
|
|
420
|
+
document.documentElement.scrollTop = top
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
function makeOthersInert(d) {
|
|
424
|
+
const bodyChildren = getChildrenElements(document.body)
|
|
425
|
+
// Disclosures are often placed inside Portal divs — not directly on the body element — so we need to account for portals.
|
|
426
|
+
const elementsToInert = bodyChildren.filter(
|
|
427
|
+
child => !isAncestor(child, d.nodes.content),
|
|
428
|
+
)
|
|
429
|
+
elementsToInert.forEach(el => el.setAttribute('inert', ''))
|
|
430
|
+
d.state.inertElements = elementsToInert
|
|
431
|
+
// Apparently, no need to updateDisclosure before reverting Inert later.
|
|
432
|
+
// (Probably cos close() updates disclosure first).
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
function revertInert(d) {
|
|
436
|
+
d.state.inertElements?.forEach(el => {
|
|
437
|
+
if (el.removeAttribute) el.removeAttribute('inert', '')
|
|
438
|
+
})
|
|
439
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
// Temp File for reference purposes
|
|
2
|
+
// Ignore the content here.
|
|
3
|
+
|
|
4
|
+
// Store version
|
|
5
|
+
// import { get, writable } from 'svelte/store'
|
|
6
|
+
// imp ort { isEmpty } from '@splendidlabz/utils'
|
|
7
|
+
// export const disclosures = writable([])
|
|
8
|
+
// const { update } = disclosures
|
|
9
|
+
|
|
10
|
+
// export function initDisclosure(id, options = {}, initialState = {}) {
|
|
11
|
+
// let d = getDisclosure(id)
|
|
12
|
+
// if (!d) {
|
|
13
|
+
// createDisclosure(id)
|
|
14
|
+
// d = getDisclosure(id)
|
|
15
|
+
// }
|
|
16
|
+
|
|
17
|
+
// // Update comment here later
|
|
18
|
+
// // Upon getting the id, initialize the rest of the values
|
|
19
|
+
// // Need to make sure values don't get replaced?
|
|
20
|
+
// d.options = Object.assign({}, d.options, options)
|
|
21
|
+
// if (!isEmpty(initialState)) d.state = initialState
|
|
22
|
+
|
|
23
|
+
// return d
|
|
24
|
+
// }
|
|
25
|
+
|
|
26
|
+
// export function getDisclosure(id) {
|
|
27
|
+
// return get(disclosures).find(disclosure => disclosure.id === id)
|
|
28
|
+
// }
|
|
29
|
+
|
|
30
|
+
// export function createDisclosure(id) {
|
|
31
|
+
// update(disclosures => {
|
|
32
|
+
// const d = { id, options: {}, state: {}, nodes: {} }
|
|
33
|
+
// return [...disclosures, d]
|
|
34
|
+
// })
|
|
35
|
+
// }
|
|
36
|
+
|
|
37
|
+
// export function updateDisclosure(id, state) {
|
|
38
|
+
// update(disclosures => {
|
|
39
|
+
// const d = disclosures.find(d => d.id === id)
|
|
40
|
+
// d.state = state
|
|
41
|
+
// return [...disclosures, d]
|
|
42
|
+
// })
|
|
43
|
+
// }
|
|
44
|
+
|
|
45
|
+
// export function closeOthers(id) {
|
|
46
|
+
// update(disclosures => {
|
|
47
|
+
// return disclosures.map(d => {
|
|
48
|
+
// if (d.id !== id) d.state.show = false
|
|
49
|
+
// return d
|
|
50
|
+
// })
|
|
51
|
+
// })
|
|
52
|
+
// }
|
|
53
|
+
|
|
54
|
+
// export const disclosures = writable({
|
|
55
|
+
// instances: [],
|
|
56
|
+
|
|
57
|
+
// init(id, options = {}, initialState = {}) {
|
|
58
|
+
// let d = this.getInstance(id)
|
|
59
|
+
// if (!d) d = this.createInstance(id)
|
|
60
|
+
|
|
61
|
+
// // Update comment here later
|
|
62
|
+
// // Upon getting the id, initialize the rest of the values
|
|
63
|
+
// // Need to make sure values don't get replaced?
|
|
64
|
+
|
|
65
|
+
// if (!isEmpty(options)) d.options = Object.assign(d.options, options)
|
|
66
|
+
// if (!isEmpty(initialState)) d.state = initialState
|
|
67
|
+
|
|
68
|
+
// return d
|
|
69
|
+
// },
|
|
70
|
+
|
|
71
|
+
// getInstance(id) {
|
|
72
|
+
// return this.instances.find(instance => instance.id === id)
|
|
73
|
+
// },
|
|
74
|
+
|
|
75
|
+
// createInstance(id) {
|
|
76
|
+
// const d = $state({ id, options: {}, state: {}, nodes: {} })
|
|
77
|
+
|
|
78
|
+
// this.instances.push(d)
|
|
79
|
+
// return this.getInstance(id)
|
|
80
|
+
// },
|
|
81
|
+
// })
|
|
82
|
+
|
|
83
|
+
// Multiple, local scoped
|
|
84
|
+
// export const disclosures = $state({
|
|
85
|
+
// instances: [],
|
|
86
|
+
|
|
87
|
+
// init(id, options = {}, initialState = {}) {
|
|
88
|
+
// let d = this.getInstance(id)
|
|
89
|
+
// if (!d) d = this.createInstance(id)
|
|
90
|
+
|
|
91
|
+
// // Update comment here later
|
|
92
|
+
// // Upon getting the id, initialize the rest of the values
|
|
93
|
+
// // Need to make sure values don't get replaced?
|
|
94
|
+
|
|
95
|
+
// if (!isEmpty(options)) d.options = Object.assign(d.options, options)
|
|
96
|
+
// if (!isEmpty(initialState)) d.state = initialState
|
|
97
|
+
|
|
98
|
+
// return d
|
|
99
|
+
// },
|
|
100
|
+
|
|
101
|
+
// getInstance(id) {
|
|
102
|
+
// return this.instances.find(instance => instance.id === id)
|
|
103
|
+
// },
|
|
104
|
+
|
|
105
|
+
// createInstance(id) {
|
|
106
|
+
// const d = $state({ id, options: {}, state: {}, nodes: {} })
|
|
107
|
+
|
|
108
|
+
// this.instances.push(d)
|
|
109
|
+
// return this.getInstance(id)
|
|
110
|
+
// },
|
|
111
|
+
// })
|
|
112
|
+
|
|
113
|
+
// Single but global
|
|
114
|
+
// export const d = $state({
|
|
115
|
+
// show: false,
|
|
116
|
+
// })
|
|
117
|
+
|
|
118
|
+
// export function getShow() {
|
|
119
|
+
// return d.show
|
|
120
|
+
// }
|
|
121
|
+
|
|
122
|
+
// export function toggle() {
|
|
123
|
+
// d.show = !d.show
|
|
124
|
+
// }
|
|
125
|
+
|
|
126
|
+
// Single
|
|
127
|
+
// export function createDisclosure() {
|
|
128
|
+
// let show = $state(false)
|
|
129
|
+
|
|
130
|
+
// return {
|
|
131
|
+
// get show() {
|
|
132
|
+
// return show
|
|
133
|
+
// },
|
|
134
|
+
// toggle() {
|
|
135
|
+
// show = !show
|
|
136
|
+
// },
|
|
137
|
+
// }
|
|
138
|
+
// }
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
// DOESN'T WORK YET — MUST CHECK AGAIN TO BE SURE
|
|
3
|
+
@component
|
|
4
|
+
Creates list items with custom SVGs
|
|
5
|
+
|
|
6
|
+
- @prop {string} `class` - Classes to add to the component.
|
|
7
|
+
- @prop {array|object} `items` - **Required** Items in the list.
|
|
8
|
+
- `content` - Content for the list item. This will be parsed as Markdown automatically. Required
|
|
9
|
+
- `icon` - Name of the SVG to use for this specific item.
|
|
10
|
+
- `emoji` - Emoji to use for the specific item.
|
|
11
|
+
-->
|
|
12
|
+
|
|
13
|
+
<script>
|
|
14
|
+
import SVG from '../SVG/SVG.svelte'
|
|
15
|
+
import Markdown from '../Markdown/Markdown.svelte'
|
|
16
|
+
|
|
17
|
+
let klass = ''
|
|
18
|
+
export { klass as class }
|
|
19
|
+
|
|
20
|
+
export let emoji = ''
|
|
21
|
+
export let icon = ''
|
|
22
|
+
export let items = []
|
|
23
|
+
|
|
24
|
+
const sameIcon = typeof items[0] === 'string'
|
|
25
|
+
</script>
|
|
26
|
+
|
|
27
|
+
<!-- Need role because we use hide the bullets and replace them with SVGs. -->
|
|
28
|
+
<ul role="list" class={`FancyList ${klass}`} {...$$restProps}>
|
|
29
|
+
{#each items as item}
|
|
30
|
+
<li>
|
|
31
|
+
<!-- prettier-ignore -->
|
|
32
|
+
{#if sameIcon}
|
|
33
|
+
{#if emoji} <span class="emoji">{emoji}</span> {/if}
|
|
34
|
+
{#if icon} <SVG {icon} width="1em" height="1em" /> {/if}
|
|
35
|
+
<span><Markdown content={item} inline /></span>
|
|
36
|
+
{/if}
|
|
37
|
+
|
|
38
|
+
<!-- prettier-ignore -->
|
|
39
|
+
{#if !sameIcon}
|
|
40
|
+
{@const i = item.icon}
|
|
41
|
+
{@const e = item.emoji}
|
|
42
|
+
{@const content = item.content}
|
|
43
|
+
{#if e} <span class="emoji">{e}</span> {/if}
|
|
44
|
+
{#if i} <SVG icon={i} width="1em" height="1em" /> {/if}
|
|
45
|
+
<span><Markdown content={content} inline /></span>
|
|
46
|
+
{/if}
|
|
47
|
+
</li>
|
|
48
|
+
{/each}
|
|
49
|
+
</ul>
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
<script>
|
|
2
|
+
let klass = ''
|
|
3
|
+
export { klass as class }
|
|
4
|
+
export let elem = ''
|
|
5
|
+
export let type = ''
|
|
6
|
+
export let href = null
|
|
7
|
+
|
|
8
|
+
let tag = href ? 'a' : 'button'
|
|
9
|
+
</script>
|
|
10
|
+
|
|
11
|
+
<!-- Testing this version out -->
|
|
12
|
+
<svelte:element
|
|
13
|
+
this={tag}
|
|
14
|
+
bind:this={elem}
|
|
15
|
+
class="Button {klass}"
|
|
16
|
+
{type}
|
|
17
|
+
{...$$restProps}
|
|
18
|
+
>
|
|
19
|
+
<slot />
|
|
20
|
+
</svelte:element>
|
|
21
|
+
|
|
22
|
+
<!-- <button class="Button {klass}" {type} {...$$restProps} bind:this={elem}
|
|
23
|
+
><slot />
|
|
24
|
+
</button> -->
|