@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,364 @@
|
|
|
1
|
+
import LayoutScripts from '@splendidlabz/layouts/scripts/all'
|
|
2
|
+
import { omitEmpty } from '@splendidlabz/utils'
|
|
3
|
+
import {
|
|
4
|
+
Focusable,
|
|
5
|
+
addListeners,
|
|
6
|
+
dispatchEvent,
|
|
7
|
+
getSelfIndex,
|
|
8
|
+
isArrowKey,
|
|
9
|
+
removeListeners,
|
|
10
|
+
} from '@splendidlabz/utils/dom'
|
|
11
|
+
import { initTab, updateTab } from './tab-store'
|
|
12
|
+
|
|
13
|
+
export const DEFAULT_TABLIST_OPTIONS = {
|
|
14
|
+
state: {
|
|
15
|
+
placement: 'top', // top, left, right
|
|
16
|
+
},
|
|
17
|
+
options: {
|
|
18
|
+
useHash: false,
|
|
19
|
+
scrollIntoView: false,
|
|
20
|
+
scrollIntoViewOnLoad: true,
|
|
21
|
+
scrollBehaviour: 'smooth', // smooth, auto
|
|
22
|
+
loop: false,
|
|
23
|
+
autoplay: false,
|
|
24
|
+
autoplayInterval: 3000,
|
|
25
|
+
runLayoutScripts: false,
|
|
26
|
+
},
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export const TABLIST_OPTION_KEYS = Object.keys({
|
|
30
|
+
...DEFAULT_TABLIST_OPTIONS.state,
|
|
31
|
+
...DEFAULT_TABLIST_OPTIONS.options,
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
export function tablist(node, props) {
|
|
35
|
+
const targetId = node.getAttribute('target')
|
|
36
|
+
|
|
37
|
+
// The filter allows us to nest tabs in tabs
|
|
38
|
+
const tablistItems = Array.from(node.querySelectorAll('[role="tab"]')).filter(
|
|
39
|
+
tab => tab.closest('.Tablist') === node,
|
|
40
|
+
)
|
|
41
|
+
let activeTabIndex
|
|
42
|
+
activeTabIndex = tablistItems.findIndex(tab =>
|
|
43
|
+
tab.hasAttribute('aria-selected'),
|
|
44
|
+
)
|
|
45
|
+
if (activeTabIndex === -1) activeTabIndex = 0
|
|
46
|
+
|
|
47
|
+
const tabInfo = {
|
|
48
|
+
id: targetId,
|
|
49
|
+
state: {
|
|
50
|
+
...DEFAULT_TABLIST_OPTIONS.state,
|
|
51
|
+
placement: props.state.placement,
|
|
52
|
+
activeTabIndex,
|
|
53
|
+
length: tablistItems.length,
|
|
54
|
+
},
|
|
55
|
+
nodes: { tablist: node, tablistItems },
|
|
56
|
+
options: {
|
|
57
|
+
...DEFAULT_TABLIST_OPTIONS.options,
|
|
58
|
+
...omitEmpty(props.options),
|
|
59
|
+
},
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const Tab = initTab(tabInfo)
|
|
63
|
+
|
|
64
|
+
setInitialState(Tab)
|
|
65
|
+
|
|
66
|
+
// Add listeners
|
|
67
|
+
const listeners = [
|
|
68
|
+
{
|
|
69
|
+
event: 'click',
|
|
70
|
+
node,
|
|
71
|
+
handler: handleClick,
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
event: 'keydown',
|
|
75
|
+
node,
|
|
76
|
+
handler: handleKeydown,
|
|
77
|
+
},
|
|
78
|
+
]
|
|
79
|
+
|
|
80
|
+
addListeners(listeners)
|
|
81
|
+
|
|
82
|
+
const { autoplay } = Tab.options
|
|
83
|
+
if (autoplay) setAutoplayInterval(Tab)
|
|
84
|
+
|
|
85
|
+
return {
|
|
86
|
+
destroy() {
|
|
87
|
+
removeListeners(listeners)
|
|
88
|
+
},
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function handleClick(event) {
|
|
92
|
+
const tab = event.target.closest('[role="tab"]')
|
|
93
|
+
if (!tab) return
|
|
94
|
+
const { tablistItems } = Tab.nodes
|
|
95
|
+
|
|
96
|
+
const index = tablistItems.findIndex(t => t === tab)
|
|
97
|
+
selectTab(Tab, index)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function handleKeydown(event) {
|
|
101
|
+
const { placement } = Tab.state
|
|
102
|
+
const { key } = event
|
|
103
|
+
if (isArrowKey(key)) {
|
|
104
|
+
event.preventDefault() // prevents scrolling
|
|
105
|
+
|
|
106
|
+
if (placement === 'top') {
|
|
107
|
+
if (key === 'ArrowLeft') selectPrevTab(Tab)
|
|
108
|
+
if (key === 'ArrowRight') selectNextTab(Tab)
|
|
109
|
+
if (key === 'ArrowDown') focusOnTabpanel(Tab)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (placement === 'left') {
|
|
113
|
+
if (key === 'ArrowUp') selectPrevTab(Tab)
|
|
114
|
+
if (key === 'ArrowDown') selectNextTab(Tab)
|
|
115
|
+
if (key === 'ArrowRight') focusOnTabpanel(Tab)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (placement === 'right') {
|
|
119
|
+
if (key === 'ArrowUp') selectPrevTab(Tab)
|
|
120
|
+
if (key === 'ArrowDown') selectNextTab(Tab)
|
|
121
|
+
if (key === 'ArrowLeft') focusOnTabpanel(Tab)
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function tabpanels(node, props) {
|
|
128
|
+
const tabpanelsId = node.id
|
|
129
|
+
|
|
130
|
+
// The filter allows us to nest tabs in tabs
|
|
131
|
+
const tabpanelItems = Array.from(
|
|
132
|
+
node.querySelectorAll('[role="tabpanel"]'),
|
|
133
|
+
).filter(tab => tab.closest('.Tabpanels') === node)
|
|
134
|
+
|
|
135
|
+
let activeTabIndex
|
|
136
|
+
activeTabIndex = tabpanelItems.findIndex(
|
|
137
|
+
tabpanel => !tabpanel.hasAttribute('hidden'),
|
|
138
|
+
)
|
|
139
|
+
if (activeTabIndex === -1) activeTabIndex = 0
|
|
140
|
+
|
|
141
|
+
// Initialize the Tab
|
|
142
|
+
const Tab = initTab({
|
|
143
|
+
id: tabpanelsId,
|
|
144
|
+
state: { activeTabIndex, length: tabpanelItems.length },
|
|
145
|
+
nodes: { tabpanels: node, tabpanelItems },
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
setInitialState(Tab)
|
|
149
|
+
|
|
150
|
+
// Add listeners
|
|
151
|
+
const listeners = [
|
|
152
|
+
{
|
|
153
|
+
event: 'keydown',
|
|
154
|
+
node,
|
|
155
|
+
handler: handleKeydown,
|
|
156
|
+
},
|
|
157
|
+
]
|
|
158
|
+
|
|
159
|
+
addListeners(listeners)
|
|
160
|
+
|
|
161
|
+
return {
|
|
162
|
+
destroy() {
|
|
163
|
+
removeListeners(listeners)
|
|
164
|
+
},
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function handleKeydown(event) {
|
|
168
|
+
if (!event.target.matches('.Tabpanel')) return
|
|
169
|
+
|
|
170
|
+
if (event.target.closest('.Tabpanels') !== node) return
|
|
171
|
+
|
|
172
|
+
const { placement } = Tab.state
|
|
173
|
+
const { key } = event
|
|
174
|
+
|
|
175
|
+
if (isArrowKey(key)) {
|
|
176
|
+
event.preventDefault() // prevents scrolling
|
|
177
|
+
event.stopPropagation() // Prevents nested tabpanel from focusing on external tabpanel
|
|
178
|
+
|
|
179
|
+
if (placement === 'top' && key === 'ArrowUp') focusOnTab(Tab)
|
|
180
|
+
if (placement === 'left' && key === 'ArrowLeft') focusOnTab(Tab)
|
|
181
|
+
if (placement === 'right' && key === 'ArrowRight') focusOnTab(Tab)
|
|
182
|
+
|
|
183
|
+
// The following would be to focus on the first content item.
|
|
184
|
+
// Not necessary, but would be nice and seems like a good idea.
|
|
185
|
+
if (key === 'ArrowDown') focusOnTabpanelContent(event.target)
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function setInitialState(Tab) {
|
|
191
|
+
// Wait until component is fully initialized before proceeding.
|
|
192
|
+
const { tablist, tabpanels } = Tab.nodes
|
|
193
|
+
if (!(tablist && tabpanels)) return
|
|
194
|
+
|
|
195
|
+
const { hash } = window.location
|
|
196
|
+
|
|
197
|
+
// Standard initialization
|
|
198
|
+
if (!hash) {
|
|
199
|
+
selectTab(Tab, Tab.state.activeTabIndex, { shouldFocusOnTab: false })
|
|
200
|
+
return
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// Hash-based initiation
|
|
204
|
+
// Activates tab and tabpanel if hashed ID is within the tabpanel.
|
|
205
|
+
const element = document.querySelector(hash)
|
|
206
|
+
const inTabpanel = element?.closest('.Tabpanels') === tabpanels
|
|
207
|
+
if (!inTabpanel) return
|
|
208
|
+
|
|
209
|
+
const closestTabpanel = element.closest('.Tabpanel')
|
|
210
|
+
const index = getSelfIndex(closestTabpanel)
|
|
211
|
+
selectTab(Tab, index, { shouldUpdateHash: false, shouldScroll: false })
|
|
212
|
+
|
|
213
|
+
// Scroll to content
|
|
214
|
+
if (element !== closestTabpanel)
|
|
215
|
+
element.scrollIntoView({ behavior: Tab.options.scrollBehaviour })
|
|
216
|
+
else {
|
|
217
|
+
// Scrolls to Tablist
|
|
218
|
+
if (Tab.options.scrollIntoViewOnLoad) scrollIntoView(Tab)
|
|
219
|
+
else scrollIntoView(Tab, 'instant')
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
export function selectTab(
|
|
224
|
+
Tab,
|
|
225
|
+
index,
|
|
226
|
+
{
|
|
227
|
+
shouldFocusOnTab = true,
|
|
228
|
+
shouldUpdateHash = true,
|
|
229
|
+
shouldScroll = true,
|
|
230
|
+
} = {},
|
|
231
|
+
) {
|
|
232
|
+
Tab.state.activeTabIndex = index
|
|
233
|
+
updateTab(Tab)
|
|
234
|
+
|
|
235
|
+
setActiveTab(Tab)
|
|
236
|
+
setActivePanel(Tab)
|
|
237
|
+
if (shouldFocusOnTab) focusOnTab(Tab)
|
|
238
|
+
|
|
239
|
+
const activeTab = Tab.nodes.tablistItems[index]
|
|
240
|
+
const activePanel = Tab.nodes.tabpanelItems[index]
|
|
241
|
+
|
|
242
|
+
dispatchEvent(Tab.nodes.tablist, 'tab-change', {
|
|
243
|
+
Tab,
|
|
244
|
+
activeTab,
|
|
245
|
+
activePanel,
|
|
246
|
+
})
|
|
247
|
+
|
|
248
|
+
// TODO: Does this break if we don't include splendidLabz/layout in the build?
|
|
249
|
+
// If it does, we need to import layout scripts with await import.
|
|
250
|
+
// Then we'll have to turn `selectTab` into an async function and manage the rest accordingly
|
|
251
|
+
if (Tab.options.runLayoutScripts) {
|
|
252
|
+
setTimeout(_ => LayoutScripts(activePanel), 0)
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
const { useHash, autoplay } = Tab.options
|
|
256
|
+
|
|
257
|
+
// Update the URL hash url if link to tab is active
|
|
258
|
+
if (useHash && shouldUpdateHash) {
|
|
259
|
+
const activeTabpanelId = Tab.nodes.tabpanelItems[index].id
|
|
260
|
+
history.pushState(null, null, `#${activeTabpanelId}`)
|
|
261
|
+
if (shouldScroll && Tab.options.scrollIntoView) scrollIntoView(Tab)
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// Reset progression interval ID
|
|
265
|
+
if (autoplay) {
|
|
266
|
+
clearInterval(Tab.state.autoplayID)
|
|
267
|
+
setAutoplayInterval(Tab)
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function scrollIntoView(Tab, behaviour) {
|
|
272
|
+
const { scrollBehaviour } = Tab.options
|
|
273
|
+
Tab.nodes.tablist.scrollIntoView({
|
|
274
|
+
behavior: behaviour || scrollBehaviour,
|
|
275
|
+
})
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
export function selectNextTab(Tab) {
|
|
279
|
+
const { activeTabIndex, length } = Tab.state
|
|
280
|
+
const { loop } = Tab.options
|
|
281
|
+
|
|
282
|
+
let nextIndex = activeTabIndex + 1
|
|
283
|
+
if (nextIndex >= length) {
|
|
284
|
+
if (loop) nextIndex = 0
|
|
285
|
+
else return
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
selectTab(Tab, nextIndex)
|
|
289
|
+
}
|
|
290
|
+
export function selectPrevTab(Tab) {
|
|
291
|
+
const { activeTabIndex, length } = Tab.state
|
|
292
|
+
const { loop } = Tab.options
|
|
293
|
+
|
|
294
|
+
let prevIndex = activeTabIndex - 1
|
|
295
|
+
if (prevIndex < 0) {
|
|
296
|
+
if (loop) prevIndex = length - 1
|
|
297
|
+
else return
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
selectTab(Tab, prevIndex)
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
export function focusOnTab(Tab) {
|
|
304
|
+
const { activeTabIndex } = Tab.state
|
|
305
|
+
const { tablistItems } = Tab.nodes
|
|
306
|
+
tablistItems[activeTabIndex].focus()
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
export function focusOnTabpanel(Tab) {
|
|
310
|
+
const { activeTabIndex } = Tab.state
|
|
311
|
+
const { tabpanelItems } = Tab.nodes
|
|
312
|
+
tabpanelItems[activeTabIndex].focus()
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
export function focusOnTabpanelContent(Tabpanel) {
|
|
316
|
+
const focusables = Focusable(Tabpanel)
|
|
317
|
+
if (focusables.length) focusables.first.focus()
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
function setActiveTab(Tab) {
|
|
321
|
+
const { activeTabIndex } = Tab.state
|
|
322
|
+
const { tablistItems } = Tab.nodes
|
|
323
|
+
|
|
324
|
+
tablistItems.forEach((tab, index) => {
|
|
325
|
+
const isActiveTab = index === activeTabIndex
|
|
326
|
+
if (isActiveTab) {
|
|
327
|
+
tab.setAttribute('aria-selected', 'true')
|
|
328
|
+
tab.setAttribute('tabindex', '0')
|
|
329
|
+
} else {
|
|
330
|
+
tab.removeAttribute('aria-selected')
|
|
331
|
+
tab.setAttribute('tabindex', '-1')
|
|
332
|
+
}
|
|
333
|
+
})
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
function setActivePanel(Tab) {
|
|
337
|
+
const { activeTabIndex } = Tab.state
|
|
338
|
+
const { tabpanelItems } = Tab.nodes
|
|
339
|
+
|
|
340
|
+
tabpanelItems.forEach((panel, index) => {
|
|
341
|
+
const isActiveTab = index === activeTabIndex
|
|
342
|
+
if (isActiveTab) {
|
|
343
|
+
panel.removeAttribute('hidden')
|
|
344
|
+
panel.setAttribute('tabindex', '0')
|
|
345
|
+
} else {
|
|
346
|
+
panel.setAttribute('hidden', 'true')
|
|
347
|
+
panel.removeAttribute('tabindex')
|
|
348
|
+
}
|
|
349
|
+
})
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
export function updatePlacement(Tab, placement) {
|
|
353
|
+
Tab.state.placement = placement
|
|
354
|
+
updateTab(Tab)
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
function setAutoplayInterval(Tab) {
|
|
358
|
+
const { autoplayInterval } = Tab.options
|
|
359
|
+
const intervalID = setInterval(() => {
|
|
360
|
+
selectNextTab(Tab)
|
|
361
|
+
}, autoplayInterval)
|
|
362
|
+
Tab.state.autoplayID = intervalID
|
|
363
|
+
updateTab(Tab)
|
|
364
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
<script>
|
|
2
|
+
import { getContext } from 'svelte'
|
|
3
|
+
|
|
4
|
+
export let klass = ''
|
|
5
|
+
export { klass as class }
|
|
6
|
+
|
|
7
|
+
const context = getContext('Tabs')
|
|
8
|
+
const { tablist } = context
|
|
9
|
+
</script>
|
|
10
|
+
|
|
11
|
+
<div class="Tablist {klass}" role="tablist" use:tablist>
|
|
12
|
+
<slot />
|
|
13
|
+
</div>
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
<script>
|
|
2
|
+
import { key } from '../_shared/key'
|
|
3
|
+
import { getContext } from 'svelte'
|
|
4
|
+
|
|
5
|
+
export let klass = ''
|
|
6
|
+
export { klass as class }
|
|
7
|
+
|
|
8
|
+
const context = getContext('Tabs')
|
|
9
|
+
const { tabpanels } = context
|
|
10
|
+
</script>
|
|
11
|
+
|
|
12
|
+
<div class="Tabpanels {klass}" use:tabpanels>
|
|
13
|
+
<slot />
|
|
14
|
+
</div>
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
<script>
|
|
2
|
+
import { setContext } from 'svelte'
|
|
3
|
+
|
|
4
|
+
import { resizeObserver } from '../../actions'
|
|
5
|
+
import createTabs from './tabs'
|
|
6
|
+
|
|
7
|
+
let klass = ''
|
|
8
|
+
export { klass as class }
|
|
9
|
+
|
|
10
|
+
export let activeTab = 0
|
|
11
|
+
export let orientation = 'horizontal'
|
|
12
|
+
export let placement = 'left'
|
|
13
|
+
export let scrollBehaviour = 'auto'
|
|
14
|
+
export let mode = 'tab'
|
|
15
|
+
export let breakpoints = null
|
|
16
|
+
|
|
17
|
+
let _breakpoints = null
|
|
18
|
+
let _shouldObserve = false
|
|
19
|
+
|
|
20
|
+
if (breakpoints) {
|
|
21
|
+
_shouldObserve = true
|
|
22
|
+
_breakpoints = Object.entries(breakpoints).map(([key, value]) => {
|
|
23
|
+
return { breakpoint: parseFloat(key), value }
|
|
24
|
+
})
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const { tabs, tablist, tabpanels, updateTabStructure } = createTabs({
|
|
28
|
+
orientation,
|
|
29
|
+
activeTab,
|
|
30
|
+
placement,
|
|
31
|
+
scrollBehaviour,
|
|
32
|
+
breakpoints,
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
setContext('Tabs', {
|
|
36
|
+
tablist,
|
|
37
|
+
tabpanels,
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
// Orientation change
|
|
41
|
+
function observe(event) {
|
|
42
|
+
const { entry } = event.detail
|
|
43
|
+
const { width } = entry.contentRect
|
|
44
|
+
if (!_shouldObserve) return
|
|
45
|
+
|
|
46
|
+
for (let i = _breakpoints.length - 1; i >= 0; i--) {
|
|
47
|
+
const { breakpoint, value } = _breakpoints[i]
|
|
48
|
+
if (width >= breakpoint) {
|
|
49
|
+
updateTabs(value)
|
|
50
|
+
break
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function updateTabs(config) {
|
|
56
|
+
// Default values
|
|
57
|
+
config = {
|
|
58
|
+
mode: 'tab',
|
|
59
|
+
placement: 'left',
|
|
60
|
+
orientation: 'horizontal',
|
|
61
|
+
...config,
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
mode = config.mode
|
|
65
|
+
placement = config.placement
|
|
66
|
+
orientation = config.orientation
|
|
67
|
+
updateTabStructure(config)
|
|
68
|
+
}
|
|
69
|
+
</script>
|
|
70
|
+
|
|
71
|
+
<div
|
|
72
|
+
class="Tabs {klass}"
|
|
73
|
+
{orientation}
|
|
74
|
+
{placement}
|
|
75
|
+
{mode}
|
|
76
|
+
{...$$restProps}
|
|
77
|
+
use:tabs
|
|
78
|
+
on:change
|
|
79
|
+
use:resizeObserver={{ shouldObserve: _shouldObserve }}
|
|
80
|
+
on:resize={observe}
|
|
81
|
+
>
|
|
82
|
+
<slot />
|
|
83
|
+
</div>
|