@undrr/undrr-mangrove 2.0.0-alpha.2 → 2.0.0-alpha.4
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/README.md +4 -4
- package/components/BookCard.js +2 -2
- package/components/Breadcrumbs.js +2 -2
- package/components/Checkbox.js +2 -2
- package/components/Chips.js +2 -2
- package/components/CtaButton.js +2 -2
- package/components/EmbedContainer.js +2 -2
- package/components/Footer.js +2 -2
- package/components/FormAction.js +2 -2
- package/components/FormErrorSummary.js +2 -2
- package/components/FormGroup.js +2 -2
- package/components/FullWidth.js +2 -2
- package/components/Gallery.js +2 -2
- package/components/Hero.js +2 -2
- package/components/HighlightBox.js +2 -2
- package/components/HorizontalBookCard.js +2 -2
- package/components/HorizontalCard.js +2 -2
- package/components/IconCard.js +2 -2
- package/components/Loader.js +2 -2
- package/components/MegaMenu.js +2 -2
- package/components/PageHeader.js +2 -2
- package/components/Pager.js +2 -2
- package/components/QuoteHighlight.js +2 -2
- package/components/Radio.js +2 -2
- package/components/ScrollContainer.js +2 -2
- package/components/Select.js +2 -2
- package/components/ShareButtons.js +2 -2
- package/components/ShowMore.js +2 -2
- package/components/StatsCard.js +2 -2
- package/components/SyndicationSearchWidget.js +2 -2
- package/components/Tab.js +2 -2
- package/components/TextCta.js +2 -2
- package/components/TextInput.js +2 -2
- package/components/Textarea.js +2 -2
- package/components/UserFeedback.js +2 -2
- package/components/VerticalCard.js +2 -2
- package/components/hydrate.js +1 -1
- package/css/style-all.css +286 -42
- package/css/style-all.css.map +1 -1
- package/css/style-delta.css +286 -42
- package/css/style-delta.css.map +1 -1
- package/css/style-gutenberg.css +254 -13
- package/css/style-gutenberg.css.map +1 -1
- package/css/style-irp.css +286 -42
- package/css/style-irp.css.map +1 -1
- package/css/style-mcr.css +286 -42
- package/css/style-mcr.css.map +1 -1
- package/css/style-preventionweb.css +286 -42
- package/css/style-preventionweb.css.map +1 -1
- package/css/style.css +285 -41
- package/css/style.css.map +1 -1
- package/error-pages/401.html +1 -1
- package/error-pages/403.html +1 -1
- package/error-pages/404.html +1 -1
- package/error-pages/429.html +1 -1
- package/error-pages/500.html +1 -1
- package/error-pages/502.html +1 -1
- package/error-pages/503.html +1 -1
- package/error-pages/504.html +1 -1
- package/error-pages/5xx.html +1 -1
- package/error-pages/challenge.html +1 -1
- package/error-pages/managed-challenge.html +1 -1
- package/js/tabs.js +577 -401
- package/package.json +10 -10
- package/scss/Atom/BaseTypography/Code/code.scss +3 -3
- package/scss/Atom/EmptyState/empty-state.scss +117 -0
- package/scss/Atom/ReachElement/Details/details.scss +1 -1
- package/scss/Atom/StatusLabel/status-label.scss +64 -0
- package/scss/Atom/Tag/tag.scss +1 -8
- package/scss/Components/Boilerplate/boilerplate.scss +1 -2
- package/scss/Components/Breadcrumbs/breadcrumbs.scss +1 -8
- package/scss/Components/Buttons/Chips/chips.scss +7 -10
- package/scss/Components/Buttons/CtaButton/cta-button.scss +19 -22
- package/scss/Components/Buttons/ShareButtons/share-buttons.scss +9 -13
- package/scss/Components/Cards/Card/card.scss +13 -13
- package/scss/Components/Cards/StatsCard/stats-card.scss +5 -14
- package/scss/Components/CookieConsentBanner/cookie-consent-banner.scss +2 -10
- package/scss/Components/Footer/footer.scss +1 -8
- package/scss/Components/Forms/_form-base.scss +49 -37
- package/scss/Components/Gallery/gallery.scss +13 -16
- package/scss/Components/Hero/hero.scss +4 -19
- package/scss/Components/MegaMenu/mega-menu.scss +337 -22
- package/scss/Components/OnThisPageNav/on-this-page-nav.scss +2 -10
- package/scss/Components/Pager/pager.scss +1 -2
- package/scss/Components/PreviewAccess/preview-access.scss +21 -29
- package/scss/Components/Snackbar/snackbar.scss +1 -1
- package/scss/Components/SyndicationSearchWidget/syndication-search-widget.scss +13 -15
- package/scss/Components/Tab/tab.scss +269 -123
- package/scss/Components/TextCta/text-cta.scss +1 -1
- package/scss/Components/UserFeedback/user-feedback.scss +2 -8
- package/scss/assets/fonts/noto-kufi-arabic/noto-kufi-arabic.scss +73 -3
- package/scss/assets/fonts/noto-sans-arabic/noto-sans-arabic.scss +79 -0
- package/scss/assets/scss/_components.scss +2 -0
- package/scss/assets/scss/_fonts.scss +158 -2
- package/scss/assets/scss/_foundational.scss +45 -30
- package/scss/assets/scss/_mixins.scss +53 -8
- package/scss/assets/scss/_theme-delta.scss +6 -63
- package/scss/assets/scss/_theme-irp.scss +6 -34
- package/scss/assets/scss/_theme-mcr.scss +7 -52
- package/scss/assets/scss/_theme-preventionweb.scss +6 -32
- package/scss/assets/scss/_tokens-data-viz.scss +300 -0
- package/scss/assets/scss/_tokens-tabs.scss +32 -0
- package/scss/assets/scss/_utility.scss +4 -6
- package/scss/assets/scss/_variables.scss +221 -300
- package/scss/assets/scss/generated/_tokens-delta.scss +248 -0
- package/scss/assets/scss/generated/_tokens-irp.scss +114 -0
- package/scss/assets/scss/generated/_tokens-mangrove.scss +607 -0
- package/scss/assets/scss/generated/_tokens-mcr.scss +135 -0
- package/scss/assets/scss/generated/_tokens-preventionweb.scss +101 -0
- package/scss/assets/fonts/dubai/dubai.scss +0 -9
package/js/tabs.js
CHANGED
|
@@ -1,395 +1,594 @@
|
|
|
1
|
-
//
|
|
1
|
+
// Shared horizontal tabs and explicit stacked disclosures.
|
|
2
|
+
const instances = new WeakMap();
|
|
3
|
+
const responsiveInstances = new WeakMap();
|
|
4
|
+
const pendingStates = new WeakMap();
|
|
5
|
+
const MOBILE_BREAKPOINT = 480;
|
|
6
|
+
const isStacked = container => container.dataset.mgJsTabsVariant === 'stacked';
|
|
2
7
|
|
|
3
|
-
// Matches $mg-breakpoint-mobile in _variables.scss
|
|
4
|
-
const BREAKPOINT_MOBILE = 480;
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Determine whether a tab container should behave as stacked (disclosure)
|
|
8
|
-
* rather than horizontal tabs.
|
|
9
|
-
* @param {Element} container - the [data-mg-js-tabs] element
|
|
10
|
-
* @returns {boolean}
|
|
11
|
-
*/
|
|
12
|
-
function isStacked(container) {
|
|
13
|
-
return (
|
|
14
|
-
container.dataset.mgJsTabsVariant === 'stacked' ||
|
|
15
|
-
window.innerWidth < BREAKPOINT_MOBILE
|
|
16
|
-
);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Set the open/close state of a stacked disclosure panel.
|
|
21
|
-
* @param {Element} trigger - the tab link acting as disclosure trigger
|
|
22
|
-
* @param {Element} panel - the section panel
|
|
23
|
-
* @param {boolean} open - true to open, false to close
|
|
24
|
-
*/
|
|
25
8
|
export function setDisclosureState(trigger, panel, open) {
|
|
26
|
-
if (open)
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
panel.setAttribute('hidden', 'until-found');
|
|
32
|
-
trigger.setAttribute('aria-expanded', 'false');
|
|
33
|
-
trigger.classList.remove('is-active', 'mg-tabs__stacked--open');
|
|
34
|
-
}
|
|
9
|
+
if (open) panel.removeAttribute('hidden');
|
|
10
|
+
else panel.setAttribute('hidden', 'until-found');
|
|
11
|
+
trigger.setAttribute('aria-expanded', String(open));
|
|
12
|
+
trigger.classList.toggle('is-active', open);
|
|
13
|
+
trigger.classList.toggle('mg-tabs__stacked--open', open);
|
|
35
14
|
}
|
|
36
15
|
|
|
37
|
-
/**
|
|
38
|
-
* Find the next visible tab index, skipping items hidden by filtering.
|
|
39
|
-
* @param {NodeList} tabs - all tab trigger elements
|
|
40
|
-
* @param {number} fromIndex - current index to start searching from
|
|
41
|
-
* @param {number} step - direction: +1 for forward, -1 for backward
|
|
42
|
-
* @returns {number} index of the next visible tab, or -1 if all hidden
|
|
43
|
-
*/
|
|
44
16
|
function findVisibleTab(tabs, fromIndex, step) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
idx = (idx + step + len) % len;
|
|
49
|
-
if (!tabs[idx].closest('.mg-tabs__item--hidden')) return idx;
|
|
17
|
+
for (let i = 0; i < tabs.length; i++) {
|
|
18
|
+
fromIndex = (fromIndex + step + tabs.length) % tabs.length;
|
|
19
|
+
if (!tabs[fromIndex].closest('.mg-tabs__item--hidden')) return fromIndex;
|
|
50
20
|
}
|
|
51
21
|
return -1;
|
|
52
22
|
}
|
|
53
23
|
|
|
54
|
-
/**
|
|
55
|
-
* Normalize text for filter matching: collapse smart quotes, dashes,
|
|
56
|
-
* and other typographic punctuation to their plain ASCII equivalents.
|
|
57
|
-
* @param {string} text
|
|
58
|
-
* @returns {string}
|
|
59
|
-
*/
|
|
60
24
|
function normalizeText(text) {
|
|
61
25
|
return text
|
|
62
|
-
.replace(/[\u2018\u2019\u201A\u2032]/g, "'")
|
|
63
|
-
.replace(/[\u201C\u201D\u201E\u2033]/g, '"')
|
|
64
|
-
.replace(/[\u2013\u2014\u2015]/g, '-')
|
|
65
|
-
.replace(/\u2026/g, '...')
|
|
66
|
-
.replace(
|
|
26
|
+
.replace(/[\u2018\u2019\u201A\u2032]/g, "'")
|
|
27
|
+
.replace(/[\u201C\u201D\u201E\u2033]/g, '"')
|
|
28
|
+
.replace(/[\u2013\u2014\u2015]/g, '-')
|
|
29
|
+
.replace(/\u2026/g, '...')
|
|
30
|
+
.replace(/\u00A0/g, ' ');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function containersIn(scope = document) {
|
|
34
|
+
if (!scope.querySelectorAll) return Array.from(scope).flatMap(containersIn);
|
|
35
|
+
return [
|
|
36
|
+
...(scope.matches?.('[data-mg-js-tabs]') ? [scope] : []),
|
|
37
|
+
...scope.querySelectorAll('[data-mg-js-tabs]'),
|
|
38
|
+
];
|
|
67
39
|
}
|
|
68
40
|
|
|
69
|
-
/**
|
|
70
|
-
* Initialize tabs on a page.
|
|
71
|
-
*
|
|
72
|
-
* @param {NodeList|HTMLElement[]|HTMLElement} [scope] - Elements to init.
|
|
73
|
-
* Accepts a NodeList, array, or a single HTMLElement.
|
|
74
|
-
* Defaults to all [data-mg-js-tabs] in the document.
|
|
75
|
-
* @param {boolean} [activateDeepLinkOnLoad] - if deep linked tabs should be activated on page load, defaults to true
|
|
76
|
-
* @example mgTabs();
|
|
77
|
-
*/
|
|
78
41
|
export function mgTabs(scope, activateDeepLinkOnLoad = true) {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
42
|
+
containersIn(scope).forEach(container => {
|
|
43
|
+
if (!scope && container.hasAttribute('data-mg-js-tabs-skip-auto-init'))
|
|
44
|
+
return;
|
|
45
|
+
const retained = pendingStates.get(container);
|
|
46
|
+
const requestedVariant = isStacked(container) ? 'stacked' : 'horizontal';
|
|
47
|
+
const initialState =
|
|
48
|
+
retained?.requestedVariant === requestedVariant ? retained : undefined;
|
|
49
|
+
pendingStates.delete(container);
|
|
50
|
+
initContainer(container, activateDeepLinkOnLoad, { initialState });
|
|
86
51
|
});
|
|
87
52
|
}
|
|
88
53
|
|
|
54
|
+
export function mgTabsRuntime(scope = document, activateDeepLinkOnLoad = true) {
|
|
55
|
+
mgTabs(scope, activateDeepLinkOnLoad);
|
|
56
|
+
}
|
|
57
|
+
|
|
89
58
|
/**
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
* @example mgTabs(document.querySelectorAll('.mg-component__container')[0]);
|
|
59
|
+
* Remove enhancement, listeners and observers so a container can be initialised again.
|
|
60
|
+
* Set preserveState for a same-layout refresh; unmounting or an explicit reset
|
|
61
|
+
* should use the default. Retained state is keyed weakly and consumed once.
|
|
94
62
|
*/
|
|
95
|
-
export function
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
// v1 compatibility
|
|
109
|
-
// If panels is empty, try finding them in data-mg-js-tabs-content
|
|
110
|
-
if (!panels.length) {
|
|
111
|
-
const tabContent = scope
|
|
112
|
-
.closest('.mg-tabs')
|
|
113
|
-
.querySelector('[data-mg-js-tabs-content]');
|
|
114
|
-
if (tabContent) {
|
|
115
|
-
panels = tabContent.querySelectorAll('[id^="mg-tabs__section"]:not(a)');
|
|
116
|
-
}
|
|
117
|
-
}
|
|
63
|
+
export function mgTabsDestroy(scope = document, preserveState = false) {
|
|
64
|
+
containersIn(scope)
|
|
65
|
+
.reverse()
|
|
66
|
+
.forEach(container => {
|
|
67
|
+
const state = preserveState ? mgTabsGetState(container) : undefined;
|
|
68
|
+
if (state)
|
|
69
|
+
pendingStates.set(container, { ...state, restoreOpenPanels: true });
|
|
70
|
+
else pendingStates.delete(container);
|
|
71
|
+
(
|
|
72
|
+
responsiveInstances.get(container) || instances.get(container)
|
|
73
|
+
)?.destroy();
|
|
74
|
+
});
|
|
75
|
+
}
|
|
118
76
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
if (tabsList.length == 0 || panels.length == 0 || tabs.length == 0) {
|
|
124
|
-
// exit: either tabs or tabbed content not found
|
|
125
|
-
return;
|
|
126
|
-
}
|
|
77
|
+
/** Capture selection and focus before replacing a responsive layout. */
|
|
78
|
+
function mgTabsGetState(container) {
|
|
79
|
+
return instances.get(container)?.getState();
|
|
80
|
+
}
|
|
127
81
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
82
|
+
function initResponsiveContainer(
|
|
83
|
+
container,
|
|
84
|
+
activateDeepLinkOnLoad,
|
|
85
|
+
initialState
|
|
86
|
+
) {
|
|
87
|
+
const root = container.closest('.mg-tabs') || container;
|
|
88
|
+
const originalVariant = container.getAttribute('data-mg-js-tabs-variant');
|
|
89
|
+
const wasHorizontal = root.classList.contains('mg-tabs--horizontal');
|
|
90
|
+
const wasStacked = root.classList.contains('mg-tabs--stacked');
|
|
91
|
+
const owned = selector =>
|
|
92
|
+
Array.from(container.querySelectorAll(selector)).filter(
|
|
93
|
+
element => element.closest('[data-mg-js-tabs]') === container
|
|
94
|
+
);
|
|
95
|
+
const tabs = owned('.mg-tabs__link:not(.mg-tabs__mobile-link)');
|
|
96
|
+
const generated = [];
|
|
97
|
+
const mobileItems = tabs
|
|
98
|
+
.map(tab => {
|
|
99
|
+
const id = tab.getAttribute('href')?.split('#')[1];
|
|
100
|
+
const panel = Array.from(root.querySelectorAll('[id]')).find(
|
|
101
|
+
element => element.id === id
|
|
102
|
+
);
|
|
103
|
+
if (!panel) return null;
|
|
104
|
+
let item = panel.previousElementSibling;
|
|
105
|
+
if (!item?.classList.contains('mg-tabs__mobile-item')) {
|
|
106
|
+
item = document.createElement('div');
|
|
107
|
+
item.className = 'mg-tabs__mobile-item';
|
|
108
|
+
const trigger = tab.cloneNode(true);
|
|
109
|
+
trigger.removeAttribute('id');
|
|
110
|
+
trigger.classList.add('mg-tabs__mobile-link');
|
|
111
|
+
item.append(trigger);
|
|
112
|
+
panel.before(item);
|
|
113
|
+
generated.push(item);
|
|
114
|
+
}
|
|
115
|
+
return item;
|
|
116
|
+
})
|
|
117
|
+
.filter(Boolean);
|
|
118
|
+
const visibility = new Map();
|
|
119
|
+
const hide = (element, hidden) => {
|
|
120
|
+
if (!element) return;
|
|
121
|
+
if (!visibility.has(element))
|
|
122
|
+
visibility.set(element, element.getAttribute('hidden'));
|
|
123
|
+
element.hidden = hidden;
|
|
124
|
+
};
|
|
125
|
+
const restoreVisibility = () => {
|
|
126
|
+
visibility.forEach((value, element) => {
|
|
127
|
+
if (value === null) element.removeAttribute('hidden');
|
|
128
|
+
else element.setAttribute('hidden', value);
|
|
129
|
+
});
|
|
130
|
+
visibility.clear();
|
|
131
|
+
};
|
|
132
|
+
let mobile;
|
|
133
|
+
const restoreVariant = () => {
|
|
134
|
+
const variantChanged =
|
|
135
|
+
container.getAttribute('data-mg-js-tabs-variant') !== originalVariant;
|
|
136
|
+
root.classList.toggle(
|
|
137
|
+
'mg-tabs--horizontal',
|
|
138
|
+
variantChanged ? !isStacked(container) : wasHorizontal
|
|
139
|
+
);
|
|
140
|
+
root.classList.toggle(
|
|
141
|
+
'mg-tabs--stacked',
|
|
142
|
+
variantChanged ? isStacked(container) : wasStacked
|
|
143
|
+
);
|
|
144
|
+
root.classList.remove('mg-tabs--responsive-stacked');
|
|
145
|
+
};
|
|
146
|
+
const update = () => {
|
|
147
|
+
const nextMobile = window.innerWidth < MOBILE_BREAKPOINT;
|
|
148
|
+
if (nextMobile === mobile) return;
|
|
149
|
+
const state = mgTabsGetState(container) || initialState;
|
|
150
|
+
initialState = undefined;
|
|
151
|
+
instances.get(container)?.destroy();
|
|
152
|
+
restoreVisibility();
|
|
153
|
+
restoreVariant();
|
|
154
|
+
mobile = nextMobile;
|
|
155
|
+
if (mobile) {
|
|
156
|
+
root.classList.remove('mg-tabs--horizontal');
|
|
157
|
+
root.classList.add('mg-tabs--stacked', 'mg-tabs--responsive-stacked');
|
|
158
|
+
}
|
|
159
|
+
tabs.forEach(tab => hide(tab.closest('.mg-tabs__item'), mobile));
|
|
160
|
+
mobileItems.forEach(item => hide(item, !mobile));
|
|
161
|
+
// Canonical React rail stays mounted. Legacy markup remains interleaved on mobile.
|
|
162
|
+
hide(owned('.mg-tabs__rail')[0], mobile);
|
|
163
|
+
initContainer(container, activateDeepLinkOnLoad, {
|
|
164
|
+
responsive: false,
|
|
165
|
+
responsiveMobile: mobile,
|
|
166
|
+
initialState: state,
|
|
167
|
+
});
|
|
168
|
+
};
|
|
169
|
+
responsiveInstances.set(container, {
|
|
170
|
+
destroy() {
|
|
171
|
+
window.removeEventListener('resize', update);
|
|
172
|
+
window.removeEventListener('orientationchange', update);
|
|
173
|
+
instances.get(container)?.destroy();
|
|
174
|
+
restoreVisibility();
|
|
175
|
+
generated.forEach(item => item.remove());
|
|
176
|
+
restoreVariant();
|
|
177
|
+
responsiveInstances.delete(container);
|
|
178
|
+
},
|
|
179
|
+
});
|
|
180
|
+
update();
|
|
181
|
+
window.addEventListener('resize', update);
|
|
182
|
+
window.addEventListener('orientationchange', update);
|
|
183
|
+
}
|
|
133
184
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
if (
|
|
185
|
+
function initContainer(container, activateDeepLinkOnLoad, options = {}) {
|
|
186
|
+
if (instances.has(container)) return;
|
|
187
|
+
if (
|
|
188
|
+
options.responsive !== false &&
|
|
189
|
+
container.hasAttribute('data-mg-js-tabs-stack-on-mobile') &&
|
|
190
|
+
!isStacked(container)
|
|
191
|
+
) {
|
|
192
|
+
if (!responsiveInstances.has(container))
|
|
193
|
+
initResponsiveContainer(
|
|
194
|
+
container,
|
|
195
|
+
activateDeepLinkOnLoad,
|
|
196
|
+
options.initialState
|
|
197
|
+
);
|
|
137
198
|
return;
|
|
138
199
|
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
200
|
+
const root = container.closest('.mg-tabs') || container;
|
|
201
|
+
const owned = selector =>
|
|
202
|
+
Array.from(
|
|
203
|
+
(options.responsiveMobile ? root : container).querySelectorAll(selector)
|
|
204
|
+
).filter(element => {
|
|
205
|
+
const owner = element.closest('[data-mg-js-tabs]');
|
|
206
|
+
return owner === container || (options.responsiveMobile && !owner);
|
|
207
|
+
});
|
|
208
|
+
const tabs = owned(
|
|
209
|
+
options.responsiveMobile
|
|
210
|
+
? '.mg-tabs__mobile-link'
|
|
211
|
+
: '.mg-tabs__link:not(.mg-tabs__mobile-link)'
|
|
212
|
+
);
|
|
213
|
+
const panels = tabs.map(tab => {
|
|
214
|
+
const id = tab.getAttribute('href')?.split('#')[1];
|
|
215
|
+
return Array.from(root.querySelectorAll('[id]')).find(element => {
|
|
216
|
+
const owner = element.closest('[data-mg-js-tabs]');
|
|
217
|
+
return element.id === id && (!owner || owner === container);
|
|
218
|
+
});
|
|
219
|
+
});
|
|
220
|
+
if (!tabs.length || panels.some(panel => !panel)) return;
|
|
221
|
+
const requestedVariant = isStacked(container) ? 'stacked' : 'horizontal';
|
|
222
|
+
const stacked = options.responsiveMobile || isStacked(container);
|
|
223
|
+
const cleanups = [];
|
|
224
|
+
const snapshots = new Map();
|
|
225
|
+
const remember = element => {
|
|
226
|
+
if (!snapshots.has(element)) {
|
|
227
|
+
const names = [
|
|
228
|
+
'role',
|
|
229
|
+
'aria-controls',
|
|
230
|
+
'aria-selected',
|
|
231
|
+
'aria-expanded',
|
|
232
|
+
'aria-labelledby',
|
|
233
|
+
'aria-label',
|
|
234
|
+
'aria-orientation',
|
|
235
|
+
'tabindex',
|
|
236
|
+
'hidden',
|
|
237
|
+
'data-tabs__item',
|
|
238
|
+
'data-mg-tabs-initialized',
|
|
239
|
+
'data-mg-tabs-overflow-start',
|
|
240
|
+
'data-mg-tabs-overflow-end',
|
|
241
|
+
];
|
|
242
|
+
if (tabs.includes(element)) names.push('id');
|
|
243
|
+
snapshots.set(
|
|
244
|
+
element,
|
|
245
|
+
names.map(name => [name, element.getAttribute(name)])
|
|
246
|
+
);
|
|
144
247
|
}
|
|
145
|
-
|
|
248
|
+
};
|
|
249
|
+
const listen = (element, event, handler, options) => {
|
|
250
|
+
element.addEventListener(event, handler, options);
|
|
251
|
+
cleanups.push(() => element.removeEventListener(event, handler, options));
|
|
252
|
+
};
|
|
253
|
+
[
|
|
254
|
+
container,
|
|
255
|
+
...tabs,
|
|
256
|
+
...panels,
|
|
257
|
+
...tabs.map(tab => tab.parentElement),
|
|
258
|
+
].forEach(remember);
|
|
259
|
+
let list = owned('.mg-tabs__list')[0] || container;
|
|
260
|
+
remember(list);
|
|
261
|
+
let rail;
|
|
262
|
+
let scroller;
|
|
263
|
+
if (!stacked) {
|
|
264
|
+
if (!root.classList.contains('mg-tabs--horizontal')) {
|
|
265
|
+
root.classList.add('mg-tabs--horizontal');
|
|
266
|
+
cleanups.push(() => root.classList.remove('mg-tabs--horizontal'));
|
|
267
|
+
}
|
|
268
|
+
rail = owned('.mg-tabs__rail')[0];
|
|
269
|
+
if (!rail) {
|
|
270
|
+
// Upgrade interleaved plain HTML without replacing triggers or panel content.
|
|
271
|
+
rail = document.createElement('div');
|
|
272
|
+
rail.className = 'mg-tabs__rail';
|
|
273
|
+
scroller = document.createElement('div');
|
|
274
|
+
scroller.className = 'mg-tabs__scroll';
|
|
275
|
+
const panelGroup = document.createElement('div');
|
|
276
|
+
panelGroup.className = 'mg-tabs__panels';
|
|
277
|
+
const listMarker = document.createComment('mg-tabs list');
|
|
278
|
+
list.before(listMarker, rail);
|
|
279
|
+
rail.append(scroller);
|
|
280
|
+
scroller.append(list);
|
|
281
|
+
rail.after(panelGroup);
|
|
282
|
+
// Determine ownership before moving anything: a shared legacy wrapper
|
|
283
|
+
// must remain shared while all of its individual panels are relocated.
|
|
284
|
+
const originals = panels.map(panel => {
|
|
285
|
+
const wrapper = panel.closest('.mg-tabs-content');
|
|
286
|
+
return wrapper &&
|
|
287
|
+
wrapper !== root &&
|
|
288
|
+
panels.filter(item => wrapper.contains(item)).length === 1
|
|
289
|
+
? wrapper
|
|
290
|
+
: panel;
|
|
291
|
+
});
|
|
292
|
+
const moved = panels.map((panel, index) => {
|
|
293
|
+
const original = originals[index];
|
|
294
|
+
const marker = document.createComment('mg-tabs panel');
|
|
295
|
+
original.before(marker);
|
|
296
|
+
const replacement = document.createElement('div');
|
|
297
|
+
replacement.className = 'mg-tabs-content';
|
|
298
|
+
replacement.setAttribute('data-mg-js-tabs-content', '');
|
|
299
|
+
panelGroup.append(replacement);
|
|
300
|
+
if (original === panel) replacement.append(panel);
|
|
301
|
+
else {
|
|
302
|
+
while (original.firstChild) replacement.append(original.firstChild);
|
|
303
|
+
original.remove();
|
|
304
|
+
}
|
|
305
|
+
return { original, marker, replacement, panel };
|
|
306
|
+
});
|
|
307
|
+
cleanups.push(() => {
|
|
308
|
+
moved.forEach(({ original, marker, replacement, panel }) => {
|
|
309
|
+
if (original === panel) marker.replaceWith(panel);
|
|
310
|
+
else {
|
|
311
|
+
while (replacement.firstChild)
|
|
312
|
+
original.append(replacement.firstChild);
|
|
313
|
+
marker.replaceWith(original);
|
|
314
|
+
}
|
|
315
|
+
});
|
|
316
|
+
listMarker.replaceWith(list);
|
|
317
|
+
rail.remove();
|
|
318
|
+
panelGroup.remove();
|
|
319
|
+
});
|
|
320
|
+
} else scroller = owned('.mg-tabs__scroll')[0];
|
|
321
|
+
remember(rail);
|
|
322
|
+
list.setAttribute('role', 'tablist');
|
|
323
|
+
list.setAttribute(
|
|
324
|
+
'aria-label',
|
|
325
|
+
container.dataset.mgJsTabsLabel ||
|
|
326
|
+
list.getAttribute('aria-label') ||
|
|
327
|
+
'Sections'
|
|
328
|
+
);
|
|
329
|
+
list.setAttribute('aria-orientation', 'horizontal');
|
|
146
330
|
}
|
|
147
331
|
|
|
148
|
-
|
|
149
|
-
const
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
332
|
+
const rtl = () => getComputedStyle(container).direction === 'rtl';
|
|
333
|
+
const updateOverflow = () => {
|
|
334
|
+
if (!scroller) return;
|
|
335
|
+
const view = scroller.getBoundingClientRect();
|
|
336
|
+
const content = list.getBoundingClientRect();
|
|
337
|
+
const overflows = scroller.scrollWidth > scroller.clientWidth + 1;
|
|
338
|
+
rail.toggleAttribute(
|
|
339
|
+
'data-mg-tabs-overflow-start',
|
|
340
|
+
overflows &&
|
|
341
|
+
(rtl() ? content.right > view.right + 1 : content.left < view.left - 1)
|
|
342
|
+
);
|
|
343
|
+
rail.toggleAttribute(
|
|
344
|
+
'data-mg-tabs-overflow-end',
|
|
345
|
+
overflows &&
|
|
346
|
+
(rtl() ? content.left < view.left - 1 : content.right > view.right + 1)
|
|
347
|
+
);
|
|
348
|
+
};
|
|
349
|
+
let activeTab;
|
|
350
|
+
let lastInteracted;
|
|
351
|
+
const reveal = (tab, animate = false) => {
|
|
352
|
+
if (!scroller || !tab) return;
|
|
353
|
+
const view = scroller.getBoundingClientRect();
|
|
354
|
+
const target = tab.getBoundingClientRect();
|
|
355
|
+
// Physical geometry also works with negative RTL scrollLeft. Native scrolling clamps at either end.
|
|
356
|
+
const listStyle = getComputedStyle(list);
|
|
357
|
+
const gutter =
|
|
358
|
+
parseFloat(
|
|
359
|
+
listStyle.paddingInlineStart ||
|
|
360
|
+
(rtl() ? listStyle.paddingRight : listStyle.paddingLeft)
|
|
361
|
+
) || 0;
|
|
362
|
+
const left =
|
|
363
|
+
target.width > view.width - gutter * 2
|
|
364
|
+
? rtl()
|
|
365
|
+
? target.right - view.right + gutter
|
|
366
|
+
: target.left - view.left - gutter
|
|
367
|
+
: (target.left + target.right - view.left - view.right) / 2;
|
|
368
|
+
const reduced = window.matchMedia?.(
|
|
369
|
+
'(prefers-reduced-motion: reduce)'
|
|
370
|
+
).matches;
|
|
371
|
+
scroller.scrollBy?.({
|
|
372
|
+
left,
|
|
373
|
+
behavior: animate && !reduced ? 'smooth' : 'instant',
|
|
374
|
+
});
|
|
375
|
+
updateOverflow();
|
|
376
|
+
};
|
|
377
|
+
const activate = (
|
|
378
|
+
tab,
|
|
379
|
+
{ focus = false, toggle = false, animate = false } = {}
|
|
380
|
+
) => {
|
|
381
|
+
const index = tabs.indexOf(tab);
|
|
382
|
+
lastInteracted = tab;
|
|
160
383
|
if (stacked) {
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
384
|
+
const open = toggle ? panels[index].hasAttribute('hidden') : true;
|
|
385
|
+
if (
|
|
386
|
+
open &&
|
|
387
|
+
!options.responsiveMobile &&
|
|
388
|
+
container.hasAttribute('data-mg-js-tabs-single-open')
|
|
389
|
+
) {
|
|
390
|
+
tabs.forEach((other, i) => setDisclosureState(other, panels[i], false));
|
|
391
|
+
}
|
|
392
|
+
setDisclosureState(tab, panels[index], open);
|
|
168
393
|
} else {
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
394
|
+
activeTab = tab;
|
|
395
|
+
tabs.forEach((other, i) => {
|
|
396
|
+
const selected = other === tab;
|
|
397
|
+
other.setAttribute('aria-selected', String(selected));
|
|
398
|
+
other.setAttribute('tabindex', selected ? '0' : '-1');
|
|
399
|
+
other.classList.toggle('is-active', selected);
|
|
400
|
+
panels[i].hidden = !selected;
|
|
401
|
+
});
|
|
402
|
+
reveal(tab, animate);
|
|
173
403
|
}
|
|
174
|
-
|
|
175
|
-
|
|
404
|
+
if (focus) tab.focus({ preventScroll: true });
|
|
405
|
+
};
|
|
406
|
+
|
|
407
|
+
tabs.forEach((tab, i) => {
|
|
408
|
+
const panel = panels[i];
|
|
409
|
+
tab.id =
|
|
410
|
+
tab.id ||
|
|
411
|
+
panel.id + (options.responsiveMobile ? '--mobile-trigger' : '--trigger');
|
|
412
|
+
tab.setAttribute('data-tabs__item', panel.id);
|
|
413
|
+
tab.setAttribute('aria-controls', panel.id);
|
|
414
|
+
tab.setAttribute('role', stacked ? 'button' : 'tab');
|
|
176
415
|
tab.removeAttribute('aria-selected');
|
|
177
|
-
tab.
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
416
|
+
if (stacked) tab.removeAttribute('tabindex');
|
|
417
|
+
else tab.parentElement.setAttribute('role', 'presentation');
|
|
418
|
+
panel.setAttribute('role', stacked ? 'region' : 'tabpanel');
|
|
419
|
+
panel.setAttribute('aria-labelledby', tab.id);
|
|
420
|
+
panel.setAttribute('tabindex', '-1');
|
|
421
|
+
listen(tab, 'click', event => {
|
|
422
|
+
event.preventDefault();
|
|
423
|
+
activate(tab, { focus: true, toggle: true, animate: true });
|
|
183
424
|
});
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
const index = Array.prototype.indexOf.call(tabs, e.currentTarget);
|
|
189
|
-
const parentContainer =
|
|
190
|
-
e.currentTarget.closest('[data-mg-js-tabs]') ||
|
|
191
|
-
e.currentTarget.closest('.mg-tabs');
|
|
192
|
-
const currentlyStacked = isStacked(parentContainer);
|
|
193
|
-
|
|
194
|
-
// Stacked: Space/Enter to toggle, Up/Down to navigate, Home/End for first/last
|
|
195
|
-
// Horizontal: Left/Right to navigate tabs, Down to focus panel
|
|
196
|
-
if (currentlyStacked && (e.key === ' ' || e.key === 'Enter')) {
|
|
197
|
-
e.preventDefault();
|
|
198
|
-
mgTabsSwitch(e.currentTarget, panels);
|
|
425
|
+
listen(tab, 'keydown', event => {
|
|
426
|
+
if (event.key === ' ' || event.key === 'Enter') {
|
|
427
|
+
event.preventDefault();
|
|
428
|
+
activate(tab, { focus: true, toggle: true, animate: true });
|
|
199
429
|
return;
|
|
200
430
|
}
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
if (e.key === prevKey) {
|
|
207
|
-
dir = findVisibleTab(tabs, index, -1);
|
|
208
|
-
} else if (e.key === nextKey) {
|
|
209
|
-
dir = findVisibleTab(tabs, index, 1);
|
|
210
|
-
} else if (!currentlyStacked && e.key === 'ArrowDown') {
|
|
211
|
-
dir = 'down';
|
|
212
|
-
} else if (e.key === 'Home') {
|
|
213
|
-
dir = findVisibleTab(tabs, -1, 1);
|
|
214
|
-
} else if (e.key === 'End') {
|
|
215
|
-
dir = findVisibleTab(tabs, tabs.length, -1);
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
if (dir !== null && dir !== -1) {
|
|
219
|
-
e.preventDefault();
|
|
220
|
-
if (dir === 'down') {
|
|
221
|
-
panels[i].focus({ preventScroll: true });
|
|
222
|
-
} else if (tabs[dir]) {
|
|
223
|
-
if (currentlyStacked) {
|
|
224
|
-
// In stacked mode, just move focus without switching
|
|
225
|
-
tabs[dir].focus({ preventScroll: true });
|
|
226
|
-
} else {
|
|
227
|
-
mgTabsSwitch(tabs[dir], panels);
|
|
228
|
-
}
|
|
229
|
-
}
|
|
431
|
+
if (!stacked && event.key === 'ArrowDown') {
|
|
432
|
+
event.preventDefault();
|
|
433
|
+
panel.focus({ preventScroll: true });
|
|
434
|
+
return;
|
|
230
435
|
}
|
|
436
|
+
const prevKey = stacked ? 'ArrowUp' : rtl() ? 'ArrowRight' : 'ArrowLeft';
|
|
437
|
+
const nextKey = stacked
|
|
438
|
+
? 'ArrowDown'
|
|
439
|
+
: rtl()
|
|
440
|
+
? 'ArrowLeft'
|
|
441
|
+
: 'ArrowRight';
|
|
442
|
+
let next = -1;
|
|
443
|
+
if (event.key === prevKey) next = findVisibleTab(tabs, i, -1);
|
|
444
|
+
else if (event.key === nextKey) next = findVisibleTab(tabs, i, 1);
|
|
445
|
+
else if (event.key === 'Home') next = findVisibleTab(tabs, -1, 1);
|
|
446
|
+
else if (event.key === 'End')
|
|
447
|
+
next = findVisibleTab(tabs, tabs.length, -1);
|
|
448
|
+
if (next < 0) return;
|
|
449
|
+
event.preventDefault();
|
|
450
|
+
if (stacked) tabs[next].focus({ preventScroll: true });
|
|
451
|
+
else activate(tabs[next], { focus: true, animate: true });
|
|
231
452
|
});
|
|
453
|
+
if (stacked) listen(panel, 'beforematch', () => activate(tab));
|
|
232
454
|
});
|
|
233
455
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
if (stacked) {
|
|
242
|
-
// Disclosure pattern: panels are regions labelled by their trigger
|
|
243
|
-
panel.setAttribute('role', 'region');
|
|
244
|
-
panel.setAttribute('aria-labelledby', labelId);
|
|
245
|
-
panel.setAttribute('hidden', 'until-found');
|
|
246
|
-
} else {
|
|
247
|
-
// Horizontal tabs: standard tabpanel
|
|
248
|
-
panel.setAttribute('role', 'tabpanel');
|
|
249
|
-
panel.setAttribute('aria-labelledby', labelId);
|
|
250
|
-
panel.hidden = true;
|
|
456
|
+
const activateHash = () => {
|
|
457
|
+
let hash = window.location.hash.slice(1);
|
|
458
|
+
try {
|
|
459
|
+
hash = decodeURIComponent(hash);
|
|
460
|
+
} catch {
|
|
461
|
+
/* Keep malformed fragments literal. */
|
|
251
462
|
}
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
// Initially activate the first tab
|
|
269
|
-
let firstTab = tabsListset.querySelectorAll('.mg-tabs__link')[0];
|
|
270
|
-
firstTab.removeAttribute('tabindex');
|
|
271
|
-
firstTab.setAttribute('aria-selected', 'true');
|
|
272
|
-
firstTab.classList.add('is-active');
|
|
273
|
-
|
|
274
|
-
// Initially reveal the first tab panel
|
|
275
|
-
const panelsList = tabsListset.querySelectorAll(
|
|
276
|
-
'[data-mg-js-tabs-content]'
|
|
277
|
-
);
|
|
278
|
-
Array.prototype.forEach.call(panelsList, panel => {
|
|
279
|
-
let firstPanel = tabsListset.querySelectorAll('.mg-tabs__section')[0];
|
|
280
|
-
firstPanel.hidden = false;
|
|
281
|
-
});
|
|
282
|
-
}
|
|
283
|
-
});
|
|
284
|
-
|
|
285
|
-
// Activate any deep links to a specific tab
|
|
286
|
-
if (activateDeepLinkOnLoad) {
|
|
287
|
-
mgTabsDeepLinkOnLoad(tabs, panels);
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
// Initialize filter if the container has the filterable attribute
|
|
291
|
-
if (stacked && container.dataset.mgJsTabsFilterable != null) {
|
|
292
|
-
mgTabsInitFilter(container, tabs, panels);
|
|
463
|
+
const index = panels.findIndex(panel => panel.id === hash);
|
|
464
|
+
if (index < 0) return false;
|
|
465
|
+
activate(tabs[index]);
|
|
466
|
+
return true;
|
|
467
|
+
};
|
|
468
|
+
if (stacked) mgTabsApplyStackedDefaults(container, tabs, panels);
|
|
469
|
+
else
|
|
470
|
+
activate(
|
|
471
|
+
tabs.find(tab => tab.dataset.mgJsTabsDefault === 'true') || tabs[0]
|
|
472
|
+
);
|
|
473
|
+
if (options.responsiveMobile && !options.initialState) {
|
|
474
|
+
tabs.forEach((tab, index) => setDisclosureState(tab, panels[index], false));
|
|
475
|
+
activate(
|
|
476
|
+
tabs.find(tab => tab.dataset.mgJsTabsDefault === 'true') || tabs[0]
|
|
477
|
+
);
|
|
293
478
|
}
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
const
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
479
|
+
if (activateDeepLinkOnLoad) activateHash();
|
|
480
|
+
if (options.initialState) {
|
|
481
|
+
const { panelId, focus } = options.initialState;
|
|
482
|
+
const index = panels.findIndex(panel => panel.id === panelId);
|
|
483
|
+
if (index >= 0) {
|
|
484
|
+
if (stacked)
|
|
485
|
+
tabs.forEach((tab, i) => setDisclosureState(tab, panels[i], false));
|
|
486
|
+
activate(tabs[index]);
|
|
487
|
+
if (focus) {
|
|
488
|
+
let target = focus.trigger ? tabs[index] : panels[index];
|
|
489
|
+
if (!focus.trigger)
|
|
490
|
+
focus.path.forEach(childIndex => {
|
|
491
|
+
target = target?.children[childIndex];
|
|
492
|
+
});
|
|
493
|
+
target?.focus({ preventScroll: true });
|
|
308
494
|
}
|
|
309
|
-
});
|
|
310
|
-
|
|
311
|
-
if (targetPanelFound) {
|
|
312
|
-
mgTabsDeepLinkOnLoad(tabs, panels);
|
|
313
495
|
}
|
|
314
|
-
});
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
// The tab switching function
|
|
318
|
-
const mgTabsSwitch = (newTab, panels) => {
|
|
319
|
-
// get the parent ul of the clicked tab
|
|
320
|
-
let parentTabContainer =
|
|
321
|
-
newTab.closest('[data-mg-js-tabs]') || newTab.closest('.mg-tabs'); // compatibility with v1 tabs
|
|
322
|
-
const stacked = isStacked(parentTabContainer);
|
|
323
|
-
const targetPanelId = newTab.getAttribute('data-tabs__item');
|
|
324
|
-
let oldTab = parentTabContainer.querySelector('[aria-selected]');
|
|
325
|
-
|
|
326
|
-
// if stacked, toggle the clicked panel independently
|
|
327
|
-
if (stacked) {
|
|
328
|
-
const isSingleOpen = parentTabContainer.dataset.mgJsTabsSingleOpen != null;
|
|
329
|
-
|
|
330
|
-
for (let item = 0; item < panels.length; item++) {
|
|
331
|
-
const panel = panels[item];
|
|
332
|
-
if (panel.id === targetPanelId) {
|
|
333
|
-
const wasHidden = panel.hidden || panel.getAttribute('hidden') === 'until-found';
|
|
334
|
-
|
|
335
|
-
// In single-open mode, close all other panels before opening
|
|
336
|
-
if (isSingleOpen && wasHidden) {
|
|
337
|
-
for (let j = 0; j < panels.length; j++) {
|
|
338
|
-
const otherPanel = panels[j];
|
|
339
|
-
if (otherPanel !== panel && !otherPanel.hidden && otherPanel.getAttribute('hidden') !== 'until-found') {
|
|
340
|
-
const otherTrigger = parentTabContainer.querySelector(
|
|
341
|
-
`[data-tabs__item="${otherPanel.id}"]`
|
|
342
|
-
);
|
|
343
|
-
if (otherTrigger) {
|
|
344
|
-
setDisclosureState(otherTrigger, otherPanel, false);
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
setDisclosureState(newTab, panel, wasHidden);
|
|
351
|
-
break;
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
// In stacked/disclosure mode, we don't deselect other tabs or use aria-selected
|
|
355
|
-
newTab.focus({ preventScroll: true });
|
|
356
|
-
return;
|
|
357
496
|
}
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
for (let item = 0; item < panels.length; item++) {
|
|
368
|
-
const panel = panels[item];
|
|
369
|
-
if (panel.id === oldPanelId) {
|
|
370
|
-
panel.hidden = true;
|
|
371
|
-
break;
|
|
372
|
-
}
|
|
373
|
-
}
|
|
497
|
+
if (
|
|
498
|
+
stacked &&
|
|
499
|
+
options.initialState?.stacked &&
|
|
500
|
+
options.initialState.restoreOpenPanels
|
|
501
|
+
) {
|
|
502
|
+
const openIds = new Set(options.initialState.openPanelIds);
|
|
503
|
+
tabs.forEach((tab, index) =>
|
|
504
|
+
setDisclosureState(tab, panels[index], openIds.has(panels[index].id))
|
|
505
|
+
);
|
|
374
506
|
}
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
507
|
+
listen(window, 'hashchange', activateHash);
|
|
508
|
+
if (
|
|
509
|
+
stacked &&
|
|
510
|
+
!options.responsiveMobile &&
|
|
511
|
+
container.hasAttribute('data-mg-js-tabs-filterable')
|
|
512
|
+
)
|
|
513
|
+
cleanups.push(
|
|
514
|
+
mgTabsInitFilter(container, tabs, panels, options.initialState?.filter)
|
|
515
|
+
);
|
|
516
|
+
let alive = true;
|
|
517
|
+
if (scroller) {
|
|
518
|
+
listen(scroller, 'scroll', updateOverflow, { passive: true });
|
|
519
|
+
const refresh = () => {
|
|
520
|
+
if (alive) reveal(activeTab);
|
|
521
|
+
};
|
|
522
|
+
listen(window, 'resize', refresh);
|
|
523
|
+
listen(window, 'orientationchange', refresh);
|
|
524
|
+
if (typeof ResizeObserver !== 'undefined') {
|
|
525
|
+
const observer = new ResizeObserver(refresh);
|
|
526
|
+
observer.observe(scroller);
|
|
527
|
+
observer.observe(list);
|
|
528
|
+
cleanups.push(() => observer.disconnect());
|
|
529
|
+
}
|
|
530
|
+
if (document.fonts) {
|
|
531
|
+
document.fonts.ready.then(refresh);
|
|
532
|
+
listen(document.fonts, 'loadingdone', refresh);
|
|
389
533
|
}
|
|
390
534
|
}
|
|
391
|
-
|
|
392
|
-
|
|
535
|
+
container.setAttribute('data-mg-tabs-initialized', 'true');
|
|
536
|
+
instances.set(container, {
|
|
537
|
+
getState() {
|
|
538
|
+
const focused = document.activeElement;
|
|
539
|
+
const triggerIndex = tabs.indexOf(focused);
|
|
540
|
+
const panelIndex = panels.findIndex(panel => panel.contains(focused));
|
|
541
|
+
const focusedIndex = panelIndex >= 0 ? panelIndex : triggerIndex;
|
|
542
|
+
const selected =
|
|
543
|
+
lastInteracted ||
|
|
544
|
+
activeTab ||
|
|
545
|
+
tabs.find(tab => tab.getAttribute('aria-expanded') === 'true') ||
|
|
546
|
+
tabs[0];
|
|
547
|
+
const index = focusedIndex >= 0 ? focusedIndex : tabs.indexOf(selected);
|
|
548
|
+
let focus;
|
|
549
|
+
if (focusedIndex >= 0) {
|
|
550
|
+
const path = [];
|
|
551
|
+
if (panelIndex >= 0) {
|
|
552
|
+
let child = focused;
|
|
553
|
+
while (child !== panels[panelIndex]) {
|
|
554
|
+
path.unshift(
|
|
555
|
+
Array.from(child.parentElement.children).indexOf(child)
|
|
556
|
+
);
|
|
557
|
+
child = child.parentElement;
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
focus = { trigger: triggerIndex >= 0, path };
|
|
561
|
+
}
|
|
562
|
+
const filterInput = container.querySelector(
|
|
563
|
+
':scope > .mg-tabs__filter > .mg-tabs__filter-input'
|
|
564
|
+
);
|
|
565
|
+
return {
|
|
566
|
+
filter: filterInput
|
|
567
|
+
? { query: filterInput.value, focused: filterInput === focused }
|
|
568
|
+
: undefined,
|
|
569
|
+
panelId: panels[index].id,
|
|
570
|
+
requestedVariant,
|
|
571
|
+
focus,
|
|
572
|
+
stacked,
|
|
573
|
+
openPanelIds: panels
|
|
574
|
+
.filter(panel => !panel.hasAttribute('hidden'))
|
|
575
|
+
.map(panel => panel.id),
|
|
576
|
+
};
|
|
577
|
+
},
|
|
578
|
+
destroy() {
|
|
579
|
+
alive = false;
|
|
580
|
+
cleanups.reverse().forEach(cleanup => cleanup());
|
|
581
|
+
snapshots.forEach((attributes, element) => {
|
|
582
|
+
attributes.forEach(([name, value]) => {
|
|
583
|
+
if (value === null) element.removeAttribute(name);
|
|
584
|
+
else element.setAttribute(name, value);
|
|
585
|
+
});
|
|
586
|
+
element.classList.remove('is-active', 'mg-tabs__stacked--open');
|
|
587
|
+
});
|
|
588
|
+
instances.delete(container);
|
|
589
|
+
},
|
|
590
|
+
});
|
|
591
|
+
}
|
|
393
592
|
/**
|
|
394
593
|
* Apply default open/close state for stacked tabs.
|
|
395
594
|
*
|
|
@@ -426,7 +625,7 @@ export function mgTabsApplyStackedDefaults(container, tabs, panels) {
|
|
|
426
625
|
shouldOpen = false;
|
|
427
626
|
} else {
|
|
428
627
|
// No container default — preserve existing behavior: open first tab
|
|
429
|
-
shouldOpen =
|
|
628
|
+
shouldOpen = i === 0;
|
|
430
629
|
}
|
|
431
630
|
|
|
432
631
|
setDisclosureState(tab, matchingPanel, shouldOpen);
|
|
@@ -443,8 +642,9 @@ export function mgTabsApplyStackedDefaults(container, tabs, panels) {
|
|
|
443
642
|
* @param {NodeList} tabs - the trigger links
|
|
444
643
|
* @param {NodeList} panels - the section panels
|
|
445
644
|
*/
|
|
446
|
-
function mgTabsInitFilter(container, tabs, panels) {
|
|
447
|
-
const placeholder =
|
|
645
|
+
function mgTabsInitFilter(container, tabs, panels, initialFilter) {
|
|
646
|
+
const placeholder =
|
|
647
|
+
container.dataset.mgJsTabsFilterPlaceholder || 'Filter sections\u2026';
|
|
448
648
|
const ariaLabel = placeholder.replace(/\u2026$/, '').trim();
|
|
449
649
|
const totalCount = tabs.length;
|
|
450
650
|
|
|
@@ -458,7 +658,8 @@ function mgTabsInitFilter(container, tabs, panels) {
|
|
|
458
658
|
input.placeholder = placeholder;
|
|
459
659
|
input.setAttribute('aria-label', ariaLabel);
|
|
460
660
|
|
|
461
|
-
const hintId =
|
|
661
|
+
const hintId =
|
|
662
|
+
'mg-tabs-filter-hint-' + Math.random().toString(36).slice(2, 8);
|
|
462
663
|
input.setAttribute('aria-describedby', hintId);
|
|
463
664
|
|
|
464
665
|
const hint = document.createElement('span');
|
|
@@ -499,7 +700,7 @@ function mgTabsInitFilter(container, tabs, panels) {
|
|
|
499
700
|
if (!query) {
|
|
500
701
|
if (!hasFiltered) return;
|
|
501
702
|
// Restore: show all items, reset to default state
|
|
502
|
-
const items =
|
|
703
|
+
const items = tabs.map(tab => tab.closest('.mg-tabs__item'));
|
|
503
704
|
items.forEach(item => {
|
|
504
705
|
item.classList.remove('mg-tabs__item--hidden');
|
|
505
706
|
const contentLi = item.nextElementSibling;
|
|
@@ -514,7 +715,7 @@ function mgTabsInitFilter(container, tabs, panels) {
|
|
|
514
715
|
}
|
|
515
716
|
|
|
516
717
|
hasFiltered = true;
|
|
517
|
-
const items =
|
|
718
|
+
const items = tabs.map(tab => tab.closest('.mg-tabs__item'));
|
|
518
719
|
const words = normalizeText(query).split(/\s+/).filter(Boolean);
|
|
519
720
|
if (words.length === 0) return;
|
|
520
721
|
let matches = 0;
|
|
@@ -556,65 +757,40 @@ function mgTabsInitFilter(container, tabs, panels) {
|
|
|
556
757
|
}
|
|
557
758
|
}
|
|
558
759
|
|
|
559
|
-
|
|
760
|
+
const onInput = () => {
|
|
560
761
|
clearTimeout(debounceTimer);
|
|
561
762
|
debounceTimer = setTimeout(applyFilter, 150);
|
|
562
|
-
}
|
|
763
|
+
};
|
|
563
764
|
|
|
564
765
|
// Also handle the native search clear button (fires 'search' event in some browsers)
|
|
565
|
-
|
|
766
|
+
const onSearch = () => {
|
|
566
767
|
clearTimeout(debounceTimer);
|
|
567
768
|
applyFilter();
|
|
568
|
-
}
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
// 1. See if there is a `#mg-tabs__section--88888`
|
|
576
|
-
var hash = window.location.hash.substring(1); //Puts hash in variable, and removes the # character
|
|
577
|
-
|
|
578
|
-
// 2. loop through all tabs, if a match then activate
|
|
579
|
-
Array.prototype.forEach.call(tabs, tab => {
|
|
580
|
-
let tabId = tab.getAttribute('data-tabs__item');
|
|
581
|
-
if (tabId == hash) {
|
|
582
|
-
document.getElementById(hash)?.scrollIntoView({ behavior: 'smooth' });
|
|
583
|
-
mgTabsSwitch(tab, panels);
|
|
584
|
-
mgTabAnchorFound = true;
|
|
585
|
-
return true;
|
|
586
|
-
}
|
|
587
|
-
});
|
|
588
|
-
}
|
|
589
|
-
|
|
590
|
-
if (!mgTabAnchorFound) {
|
|
591
|
-
// Determine the container to check for stacked mode
|
|
592
|
-
const container = tabs.length > 0
|
|
593
|
-
? (tabs[0].closest('[data-mg-js-tabs]') || tabs[0].closest('.mg-tabs'))
|
|
594
|
-
: null;
|
|
595
|
-
|
|
596
|
-
if (container && isStacked(container)) {
|
|
597
|
-
mgTabsApplyStackedDefaults(container, tabs, panels);
|
|
598
|
-
} else {
|
|
599
|
-
// Horizontal tabs: find default or activate first
|
|
600
|
-
let defaultTabFound = false;
|
|
601
|
-
Array.from(tabs).forEach(tab => {
|
|
602
|
-
if (tab.getAttribute('data-mg-js-tabs-default') === 'true') {
|
|
603
|
-
mgTabsSwitch(tab, panels);
|
|
604
|
-
defaultTabFound = true;
|
|
605
|
-
}
|
|
606
|
-
});
|
|
607
|
-
|
|
608
|
-
if (!defaultTabFound && tabs.length > 0) {
|
|
609
|
-
mgTabsSwitch(tabs[0], panels);
|
|
610
|
-
}
|
|
611
|
-
}
|
|
769
|
+
};
|
|
770
|
+
input.addEventListener('input', onInput);
|
|
771
|
+
input.addEventListener('search', onSearch);
|
|
772
|
+
if (initialFilter) {
|
|
773
|
+
input.value = initialFilter.query;
|
|
774
|
+
if (input.value) applyFilter();
|
|
775
|
+
if (initialFilter.focused) input.focus({ preventScroll: true });
|
|
612
776
|
}
|
|
777
|
+
return () => {
|
|
778
|
+
clearTimeout(debounceTimer);
|
|
779
|
+
input.removeEventListener('input', onInput);
|
|
780
|
+
input.removeEventListener('search', onSearch);
|
|
781
|
+
tabs.forEach(tab => {
|
|
782
|
+
const item = tab.closest('.mg-tabs__item');
|
|
783
|
+
item.classList.remove('mg-tabs__item--hidden');
|
|
784
|
+
item.nextElementSibling?.classList.remove('mg-tabs-content--hidden');
|
|
785
|
+
});
|
|
786
|
+
filterWrapper.remove();
|
|
787
|
+
noResults.remove();
|
|
788
|
+
status.remove();
|
|
789
|
+
};
|
|
613
790
|
}
|
|
614
791
|
|
|
615
|
-
// Auto-wrap so the browser Event object is not passed as scope
|
|
616
792
|
if (document.readyState === 'loading') {
|
|
617
|
-
document.addEventListener('DOMContentLoaded', () => mgTabs(),
|
|
793
|
+
document.addEventListener('DOMContentLoaded', () => mgTabs(), { once: true });
|
|
618
794
|
} else {
|
|
619
795
|
mgTabs();
|
|
620
796
|
}
|