@y14e/tabs 1.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +111 -0
- package/dist/index.cjs +573 -0
- package/dist/index.d.cts +43 -0
- package/dist/index.d.ts +43 -0
- package/dist/index.js +571 -0
- package/package.json +52 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Yusuke Kamiyamane
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# Tabs
|
|
2
|
+
|
|
3
|
+
WAI-ARIA compliant [tabs](https://www.w3.org/WAI/ARIA/apg/patterns/tabs/) pattern implementation in TypeScript.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm i @y14e/tabs
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
// npm
|
|
13
|
+
import Tabs from '@y14e/tabs';
|
|
14
|
+
|
|
15
|
+
// CDNs
|
|
16
|
+
import Tabs from 'https://esm.sh/@y14e/tabs'
|
|
17
|
+
// or
|
|
18
|
+
import Tabs from 'https://cdn.jsdelivr.net/npm/@y14e/tabs/+esm';
|
|
19
|
+
// or
|
|
20
|
+
import Tabs from 'https://unpkg.com/@y14e/tabs/dist/index.js';
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
new Tabs(root, options);
|
|
27
|
+
// => Tabs
|
|
28
|
+
//
|
|
29
|
+
// root: HTMLElement
|
|
30
|
+
// options (optional): TabsOptions
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## 🪄 Options
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
interface TabsOptions {
|
|
37
|
+
animation?: {
|
|
38
|
+
content?: {
|
|
39
|
+
crossFade?: boolean; // default: true
|
|
40
|
+
duration?: number; // ms (default: 300)
|
|
41
|
+
easing?: string; // <easing-function> (default: 'ease')
|
|
42
|
+
fade?: boolean; // default: false
|
|
43
|
+
};
|
|
44
|
+
indicator?: {
|
|
45
|
+
duration?: number; // ms (default: 300)
|
|
46
|
+
easing?: string; // <easing-function> (default: 'ease')
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
avoidDuplicates?: boolean; // default: false
|
|
50
|
+
manual?: boolean; // default: false
|
|
51
|
+
selector?: {
|
|
52
|
+
content?: string; // default: '[role="tablist"] + *'
|
|
53
|
+
indicator?: string; // default: '[data-tabs-indicator]'
|
|
54
|
+
list?: string; // default: '[role="tablist"]'
|
|
55
|
+
panel?: string; // default: '[role="tabpanel"]'
|
|
56
|
+
tab?: string; // default: '[role="tab"]'
|
|
57
|
+
};
|
|
58
|
+
vertical?: boolean; // default: false
|
|
59
|
+
}
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### `avoidDuplicates`
|
|
63
|
+
|
|
64
|
+
If `true`, only the first tab list remains interactive; subsequent duplicates are excluded from focus and navigation.
|
|
65
|
+
|
|
66
|
+
### ⚙️ Customize defaults
|
|
67
|
+
|
|
68
|
+
Override the global default settings applied to all accordion instances.
|
|
69
|
+
|
|
70
|
+
```ts
|
|
71
|
+
import Tabs from '@y14e/tabs';
|
|
72
|
+
|
|
73
|
+
Tabs.defaults = {
|
|
74
|
+
animation: {
|
|
75
|
+
content: {
|
|
76
|
+
crossFade: false,
|
|
77
|
+
duration: 1000,
|
|
78
|
+
fade: true,
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
manual: true,
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
new Tabs(root);
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## 📦 APIs
|
|
88
|
+
|
|
89
|
+
### `activate`
|
|
90
|
+
|
|
91
|
+
```ts
|
|
92
|
+
tabs.activate(tab);
|
|
93
|
+
// => void
|
|
94
|
+
//
|
|
95
|
+
// tab: HTMLElement
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### `destroy`
|
|
99
|
+
|
|
100
|
+
Destroys the instance and cleans up all event listeners.
|
|
101
|
+
|
|
102
|
+
```ts
|
|
103
|
+
tabs.destroy(force);
|
|
104
|
+
// => Promise<void>
|
|
105
|
+
//
|
|
106
|
+
// force (optional): If true, skips waiting for animations to finish.
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## Demo
|
|
110
|
+
|
|
111
|
+
https://y14e.github.io/tabs/
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,573 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/index.ts
|
|
4
|
+
var Tabs = class _Tabs {
|
|
5
|
+
static defaults = {};
|
|
6
|
+
#rootElement;
|
|
7
|
+
#defaults = {
|
|
8
|
+
animation: {
|
|
9
|
+
content: {
|
|
10
|
+
crossFade: true,
|
|
11
|
+
duration: 300,
|
|
12
|
+
easing: "ease",
|
|
13
|
+
fade: false
|
|
14
|
+
},
|
|
15
|
+
indicator: {
|
|
16
|
+
duration: 300,
|
|
17
|
+
easing: "ease"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
avoidDuplicates: false,
|
|
21
|
+
manual: false,
|
|
22
|
+
selector: {
|
|
23
|
+
content: '[role="tablist"] + *',
|
|
24
|
+
indicator: "[data-tabs-indicator]",
|
|
25
|
+
list: '[role="tablist"]',
|
|
26
|
+
panel: '[role="tabpanel"]',
|
|
27
|
+
tab: '[role="tab"]'
|
|
28
|
+
},
|
|
29
|
+
vertical: false
|
|
30
|
+
};
|
|
31
|
+
#settings;
|
|
32
|
+
#listElements;
|
|
33
|
+
#tabElements;
|
|
34
|
+
#indicatorElements;
|
|
35
|
+
#contentElement;
|
|
36
|
+
#panelElements;
|
|
37
|
+
#bindings = /* @__PURE__ */ new WeakMap();
|
|
38
|
+
#eventController = null;
|
|
39
|
+
#animationController = null;
|
|
40
|
+
#animation = null;
|
|
41
|
+
#indicators = [];
|
|
42
|
+
#isDestroyed = false;
|
|
43
|
+
constructor(root, options = {}) {
|
|
44
|
+
if (!(root instanceof HTMLElement)) {
|
|
45
|
+
throw new TypeError("Invalid root element");
|
|
46
|
+
}
|
|
47
|
+
if (root.hasAttribute("data-tabs-initialized")) {
|
|
48
|
+
console.warn("Already initialized");
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
this.#rootElement = root;
|
|
52
|
+
this.#defaults = this.#mergeOptions(this.#defaults, _Tabs.defaults);
|
|
53
|
+
this.#settings = this.#mergeOptions(this.#defaults, options);
|
|
54
|
+
if (matchMedia("(prefers-reduced-motion: reduce)").matches) {
|
|
55
|
+
Object.assign(this.#settings.animation, {
|
|
56
|
+
content: { duration: 0 },
|
|
57
|
+
indicator: { duration: 0 }
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
const NOT_NESTED = `:not(:scope ${this.#settings.selector.panel} *)`;
|
|
61
|
+
this.#listElements = [
|
|
62
|
+
...this.#rootElement.querySelectorAll(
|
|
63
|
+
`${this.#settings.selector.list}${NOT_NESTED}`
|
|
64
|
+
)
|
|
65
|
+
];
|
|
66
|
+
if (!this.#listElements.length) {
|
|
67
|
+
console.warn("Missing list elements");
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
this.#tabElements = [
|
|
71
|
+
...this.#rootElement.querySelectorAll(
|
|
72
|
+
`${this.#settings.selector.tab}${NOT_NESTED}`
|
|
73
|
+
)
|
|
74
|
+
];
|
|
75
|
+
if (!this.#tabElements.length) {
|
|
76
|
+
console.warn("Missing tab elements");
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
this.#indicatorElements = [
|
|
80
|
+
...this.#rootElement.querySelectorAll(
|
|
81
|
+
`${this.#settings.selector.indicator}${NOT_NESTED}`
|
|
82
|
+
)
|
|
83
|
+
];
|
|
84
|
+
this.#contentElement = this.#rootElement.querySelector(
|
|
85
|
+
this.#settings.selector.content
|
|
86
|
+
);
|
|
87
|
+
if (!this.#contentElement) {
|
|
88
|
+
console.warn("Missing content element");
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
this.#panelElements = [
|
|
92
|
+
...this.#rootElement.querySelectorAll(
|
|
93
|
+
`${this.#settings.selector.panel}${NOT_NESTED}`
|
|
94
|
+
)
|
|
95
|
+
];
|
|
96
|
+
const length = this.#panelElements.length;
|
|
97
|
+
if (!length) {
|
|
98
|
+
console.warn("Missing panel elements");
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
const tabs = [];
|
|
102
|
+
this.#tabElements.forEach((tab, i) => {
|
|
103
|
+
const index = i % length;
|
|
104
|
+
const tabsByIndex = tabs[index] ?? [];
|
|
105
|
+
tabsByIndex.push(tab);
|
|
106
|
+
tabs[index] = tabsByIndex;
|
|
107
|
+
const panel = this.#panelElements[index];
|
|
108
|
+
if (!panel) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
const binding = createBinding(tabsByIndex, panel);
|
|
112
|
+
this.#bindings.set(tab, binding);
|
|
113
|
+
if (i < length) {
|
|
114
|
+
this.#bindings.set(panel, binding);
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
this.#initialize();
|
|
118
|
+
}
|
|
119
|
+
async activate(tab, isMatch = false) {
|
|
120
|
+
if (this.#isDestroyed) {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
if (!(tab instanceof HTMLElement) || !this.#bindings.has(tab)) {
|
|
124
|
+
console.warn("Invalid tab element");
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
if (tab.ariaSelected === "true") {
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
this.#tabElements.forEach((t) => {
|
|
131
|
+
const isSelected = this.#bindings.get(t)?.tabs.some((tt) => tt === tab);
|
|
132
|
+
t.setAttribute("aria-selected", String(isSelected));
|
|
133
|
+
t.setAttribute(
|
|
134
|
+
"tabindex",
|
|
135
|
+
isSelected && !this.#isAvoidedTab(t) ? "0" : "-1"
|
|
136
|
+
);
|
|
137
|
+
});
|
|
138
|
+
if (!this.#contentElement) {
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
const size = this.#contentElement.offsetHeight;
|
|
142
|
+
this.#rootElement.setAttribute("data-tabs-animating", "");
|
|
143
|
+
const { style } = this.#contentElement;
|
|
144
|
+
style.setProperty("overflow", "clip");
|
|
145
|
+
style.setProperty("position", "relative");
|
|
146
|
+
const { fade, crossFade } = this.#settings.animation.content;
|
|
147
|
+
const panel = this.#bindings.get(tab)?.panel;
|
|
148
|
+
if (!panel) {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
this.#panelElements.forEach((p) => {
|
|
152
|
+
const { style: style2 } = p;
|
|
153
|
+
if (fade || crossFade) {
|
|
154
|
+
style2.setProperty("content-visibility", "visible");
|
|
155
|
+
style2.setProperty("display", "block");
|
|
156
|
+
style2.setProperty("opacity", p.hidden ? "0" : "1");
|
|
157
|
+
}
|
|
158
|
+
style2.setProperty("inline-size", "100%");
|
|
159
|
+
style2.setProperty("position", "absolute");
|
|
160
|
+
if (p === panel && !hasFocusable(p)) {
|
|
161
|
+
p.setAttribute("tabindex", "0");
|
|
162
|
+
} else {
|
|
163
|
+
p.removeAttribute("tabindex");
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
this.#panelElements.forEach((p, i) => {
|
|
167
|
+
if (p === panel) {
|
|
168
|
+
p.removeAttribute("hidden");
|
|
169
|
+
} else {
|
|
170
|
+
const tab2 = this.#tabElements[i];
|
|
171
|
+
if (!tab2) {
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
p.setAttribute("hidden", isFocusable(tab2) ? "until-found" : "");
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
this.#animation?.cancel();
|
|
178
|
+
const { duration, easing } = this.#settings.animation.content;
|
|
179
|
+
this.#animation = this.#contentElement.animate(
|
|
180
|
+
{
|
|
181
|
+
blockSize: [
|
|
182
|
+
`${size}px`,
|
|
183
|
+
getComputedStyle(panel).getPropertyValue("block-size")
|
|
184
|
+
]
|
|
185
|
+
},
|
|
186
|
+
{
|
|
187
|
+
duration: isMatch ? 0 : duration,
|
|
188
|
+
easing
|
|
189
|
+
}
|
|
190
|
+
);
|
|
191
|
+
const cleanup = () => {
|
|
192
|
+
this.#animation = null;
|
|
193
|
+
};
|
|
194
|
+
this.#animationController = new AbortController();
|
|
195
|
+
const { signal } = this.#animationController;
|
|
196
|
+
this.#animation.addEventListener("cancel", cleanup, {
|
|
197
|
+
once: true,
|
|
198
|
+
signal
|
|
199
|
+
});
|
|
200
|
+
this.#animation.addEventListener(
|
|
201
|
+
"finish",
|
|
202
|
+
() => {
|
|
203
|
+
this.#onAnimationFinish();
|
|
204
|
+
cleanup();
|
|
205
|
+
},
|
|
206
|
+
{
|
|
207
|
+
once: true,
|
|
208
|
+
signal
|
|
209
|
+
}
|
|
210
|
+
);
|
|
211
|
+
this.#panelElements.forEach((p) => {
|
|
212
|
+
const binding = this.#bindings.get(p);
|
|
213
|
+
if (!binding) {
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
const opacity = getComputedStyle(p).getPropertyValue("opacity");
|
|
217
|
+
binding.animation?.cancel();
|
|
218
|
+
const isSelected = p === panel;
|
|
219
|
+
const animation = p.animate(
|
|
220
|
+
{
|
|
221
|
+
opacity: fade ? isSelected ? [opacity, opacity, "1"] : [opacity, "0", "0"] : isSelected ? [opacity, "1"] : [opacity, "0"]
|
|
222
|
+
},
|
|
223
|
+
{
|
|
224
|
+
duration: isMatch || !(fade || crossFade) ? 0 : this.#settings.animation.content.duration,
|
|
225
|
+
easing: "ease"
|
|
226
|
+
}
|
|
227
|
+
);
|
|
228
|
+
binding.animation = animation;
|
|
229
|
+
const cleanup2 = () => {
|
|
230
|
+
if (binding.animation === animation) {
|
|
231
|
+
binding.animation = null;
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
this.#animationController = new AbortController();
|
|
235
|
+
const { signal: signal2 } = this.#animationController;
|
|
236
|
+
animation.addEventListener("cancel", cleanup2, { once: true, signal: signal2 });
|
|
237
|
+
animation.addEventListener("finish", cleanup2, { once: true, signal: signal2 });
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
async destroy(force = false) {
|
|
241
|
+
if (this.#isDestroyed) {
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
this.#isDestroyed = true;
|
|
245
|
+
this.#eventController?.abort();
|
|
246
|
+
this.#eventController = null;
|
|
247
|
+
this.#indicators.forEach((indicator) => {
|
|
248
|
+
indicator.destroy(force);
|
|
249
|
+
});
|
|
250
|
+
this.#indicators.length = 0;
|
|
251
|
+
if (this.#animation) {
|
|
252
|
+
if (!force) {
|
|
253
|
+
try {
|
|
254
|
+
await this.#animation.finished;
|
|
255
|
+
} catch {
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
this.#animation.cancel();
|
|
259
|
+
}
|
|
260
|
+
if (!force) {
|
|
261
|
+
await Promise.all(
|
|
262
|
+
this.#panelElements.map(
|
|
263
|
+
(panel) => this.#bindings.get(panel)?.animation?.finished.catch(() => {
|
|
264
|
+
})
|
|
265
|
+
)
|
|
266
|
+
);
|
|
267
|
+
}
|
|
268
|
+
this.#panelElements.forEach((panel) => {
|
|
269
|
+
const animation = this.#bindings.get(panel)?.animation;
|
|
270
|
+
if (animation) {
|
|
271
|
+
animation.cancel();
|
|
272
|
+
}
|
|
273
|
+
});
|
|
274
|
+
this.#onAnimationFinish();
|
|
275
|
+
this.#animationController?.abort();
|
|
276
|
+
this.#animationController = null;
|
|
277
|
+
this.#listElements.length = 0;
|
|
278
|
+
this.#tabElements.length = 0;
|
|
279
|
+
this.#contentElement = null;
|
|
280
|
+
this.#panelElements.length = 0;
|
|
281
|
+
this.#rootElement.removeAttribute("data-tabs-initialized");
|
|
282
|
+
}
|
|
283
|
+
#initialize() {
|
|
284
|
+
const { signal } = this.#eventController ?? new AbortController();
|
|
285
|
+
this.#listElements.forEach((list, i) => {
|
|
286
|
+
if (this.#settings.avoidDuplicates && i) {
|
|
287
|
+
list.setAttribute("aria-hidden", "true");
|
|
288
|
+
}
|
|
289
|
+
if (this.#settings.vertical) {
|
|
290
|
+
list.setAttribute("aria-orientation", "vertical");
|
|
291
|
+
}
|
|
292
|
+
list.setAttribute("role", "tablist");
|
|
293
|
+
});
|
|
294
|
+
this.#tabElements.forEach((tab, i) => {
|
|
295
|
+
const id = Math.random().toString(36).slice(-8);
|
|
296
|
+
const panel = this.#panelElements[i % this.#panelElements.length];
|
|
297
|
+
if (!panel) {
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
panel.id ||= `tabs-panel-${id}`;
|
|
301
|
+
addTokenToAttribute(tab, "aria-controls", panel.id);
|
|
302
|
+
if (!tab.hasAttribute("aria-selected")) {
|
|
303
|
+
tab.setAttribute("aria-selected", "false");
|
|
304
|
+
}
|
|
305
|
+
const isAvoided = this.#isAvoidedTab(tab);
|
|
306
|
+
if (!isAvoided) {
|
|
307
|
+
tab.id ||= `tabs-tab-${id}`;
|
|
308
|
+
}
|
|
309
|
+
tab.setAttribute("role", "tab");
|
|
310
|
+
tab.setAttribute(
|
|
311
|
+
"tabindex",
|
|
312
|
+
tab.ariaSelected === "true" && !isAvoided ? "0" : "-1"
|
|
313
|
+
);
|
|
314
|
+
if (!isFocusable(tab)) {
|
|
315
|
+
tab.style.setProperty("pointer-events", "none");
|
|
316
|
+
}
|
|
317
|
+
addTokenToAttribute(panel, "aria-labelledby", tab.id);
|
|
318
|
+
tab.addEventListener("click", this.#onTabClick, { signal });
|
|
319
|
+
tab.addEventListener("keydown", this.#onTabKeyDown, { signal });
|
|
320
|
+
});
|
|
321
|
+
this.#indicatorElements.forEach((indicator) => {
|
|
322
|
+
indicator.closest(this.#settings.selector.list)?.style.setProperty("position", "relative");
|
|
323
|
+
const { style } = indicator;
|
|
324
|
+
style.setProperty("display", "block");
|
|
325
|
+
style.setProperty("position", "absolute");
|
|
326
|
+
this.#indicators.push(new TabsIndicator(indicator, this.#settings));
|
|
327
|
+
});
|
|
328
|
+
this.#panelElements.forEach((panel) => {
|
|
329
|
+
panel.setAttribute("role", "tabpanel");
|
|
330
|
+
if (!panel.hasAttribute("hidden") && !hasFocusable(panel)) {
|
|
331
|
+
panel.setAttribute("tabindex", "0");
|
|
332
|
+
}
|
|
333
|
+
panel.addEventListener("beforematch", this.#onPanelBeforeMatch, {
|
|
334
|
+
signal
|
|
335
|
+
});
|
|
336
|
+
});
|
|
337
|
+
this.#rootElement.setAttribute("data-tabs-initialized", "");
|
|
338
|
+
}
|
|
339
|
+
#onTabClick = (event) => {
|
|
340
|
+
event.preventDefault();
|
|
341
|
+
event.stopPropagation();
|
|
342
|
+
const tab = event.currentTarget;
|
|
343
|
+
if (!(tab instanceof HTMLElement)) {
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
this.activate(tab);
|
|
347
|
+
};
|
|
348
|
+
#onTabKeyDown = (event) => {
|
|
349
|
+
const currentTab = event.currentTarget;
|
|
350
|
+
if (!(currentTab instanceof HTMLElement)) {
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
const list = currentTab.closest(this.#settings.selector.list);
|
|
354
|
+
if (!list) {
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
const isBoth = list.ariaOrientation === "undefined";
|
|
358
|
+
const isHorizontal = list.ariaOrientation !== "vertical";
|
|
359
|
+
const { key } = event;
|
|
360
|
+
if (![
|
|
361
|
+
"Enter",
|
|
362
|
+
" ",
|
|
363
|
+
"End",
|
|
364
|
+
"Home",
|
|
365
|
+
...isBoth ? ["ArrowLeft", "ArrowUp"] : [`Arrow${isHorizontal ? "Left" : "Up"}`],
|
|
366
|
+
...isBoth ? ["ArrowRight", "ArrowDown"] : [`Arrow${isHorizontal ? "Right" : "Down"}`]
|
|
367
|
+
].includes(key)) {
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
event.preventDefault();
|
|
371
|
+
event.stopPropagation();
|
|
372
|
+
const focusables = [
|
|
373
|
+
...list.querySelectorAll(this.#settings.selector.tab)
|
|
374
|
+
].filter(isFocusable);
|
|
375
|
+
const active = getActiveElement();
|
|
376
|
+
if (!(active instanceof HTMLElement)) {
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
const currentIndex = focusables.indexOf(active);
|
|
380
|
+
let newIndex = currentIndex;
|
|
381
|
+
switch (key) {
|
|
382
|
+
case "Enter":
|
|
383
|
+
case " ":
|
|
384
|
+
active.click();
|
|
385
|
+
return;
|
|
386
|
+
case "End":
|
|
387
|
+
newIndex = -1;
|
|
388
|
+
break;
|
|
389
|
+
case "Home":
|
|
390
|
+
newIndex = 0;
|
|
391
|
+
break;
|
|
392
|
+
case "ArrowLeft":
|
|
393
|
+
case "ArrowUp":
|
|
394
|
+
newIndex = currentIndex - 1;
|
|
395
|
+
break;
|
|
396
|
+
case "ArrowRight":
|
|
397
|
+
case "ArrowDown":
|
|
398
|
+
newIndex = (currentIndex + 1) % focusables.length;
|
|
399
|
+
break;
|
|
400
|
+
}
|
|
401
|
+
const newTab = focusables.at(newIndex);
|
|
402
|
+
if (!newTab) {
|
|
403
|
+
return;
|
|
404
|
+
}
|
|
405
|
+
newTab.focus();
|
|
406
|
+
if (!this.#settings.manual) {
|
|
407
|
+
newTab.click();
|
|
408
|
+
}
|
|
409
|
+
};
|
|
410
|
+
#onPanelBeforeMatch = (event) => {
|
|
411
|
+
const panel = event.currentTarget;
|
|
412
|
+
if (!(panel instanceof HTMLElement)) {
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
const tab = this.#bindings.get(panel)?.tabs[0];
|
|
416
|
+
if (!tab) {
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
this.activate(tab, true);
|
|
420
|
+
};
|
|
421
|
+
#isAvoidedTab(tab) {
|
|
422
|
+
const binding = this.#bindings.get(tab);
|
|
423
|
+
if (!binding) {
|
|
424
|
+
return false;
|
|
425
|
+
}
|
|
426
|
+
return this.#settings.avoidDuplicates && binding.tabs.indexOf(tab) > 0;
|
|
427
|
+
}
|
|
428
|
+
#mergeOptions(target, source) {
|
|
429
|
+
return {
|
|
430
|
+
...target,
|
|
431
|
+
...source,
|
|
432
|
+
animation: {
|
|
433
|
+
content: {
|
|
434
|
+
...target.animation.content,
|
|
435
|
+
...source.animation?.content ?? {}
|
|
436
|
+
},
|
|
437
|
+
indicator: {
|
|
438
|
+
...target.animation.indicator,
|
|
439
|
+
...source.animation?.indicator ?? {}
|
|
440
|
+
}
|
|
441
|
+
},
|
|
442
|
+
selector: {
|
|
443
|
+
...target.selector,
|
|
444
|
+
...source.selector ?? {}
|
|
445
|
+
}
|
|
446
|
+
};
|
|
447
|
+
}
|
|
448
|
+
#onAnimationFinish() {
|
|
449
|
+
if (!this.#contentElement) {
|
|
450
|
+
return;
|
|
451
|
+
}
|
|
452
|
+
const { style } = this.#contentElement;
|
|
453
|
+
style.removeProperty("block-size");
|
|
454
|
+
style.removeProperty("overflow");
|
|
455
|
+
style.removeProperty("position");
|
|
456
|
+
this.#panelElements.forEach((panel) => {
|
|
457
|
+
const { style: style2 } = panel;
|
|
458
|
+
style2.removeProperty("content-visibility");
|
|
459
|
+
style2.removeProperty("display");
|
|
460
|
+
style2.removeProperty("inline-size");
|
|
461
|
+
style2.removeProperty("opacity");
|
|
462
|
+
style2.removeProperty("position");
|
|
463
|
+
});
|
|
464
|
+
this.#rootElement.removeAttribute("data-tabs-animating");
|
|
465
|
+
}
|
|
466
|
+
};
|
|
467
|
+
var TabsIndicator = class {
|
|
468
|
+
#rootElement;
|
|
469
|
+
#settings;
|
|
470
|
+
#listElement = null;
|
|
471
|
+
#animation = null;
|
|
472
|
+
#resizeObserver = null;
|
|
473
|
+
#mutationObserver = null;
|
|
474
|
+
constructor(root, settings) {
|
|
475
|
+
this.#rootElement = root;
|
|
476
|
+
this.#settings = settings;
|
|
477
|
+
this.#listElement = root.closest(settings.selector.list);
|
|
478
|
+
if (!this.#listElement) {
|
|
479
|
+
return;
|
|
480
|
+
}
|
|
481
|
+
this.#resizeObserver = new ResizeObserver(this.#update);
|
|
482
|
+
this.#resizeObserver.observe(this.#listElement);
|
|
483
|
+
this.#mutationObserver = new MutationObserver(this.#update);
|
|
484
|
+
this.#mutationObserver.observe(this.#listElement, {
|
|
485
|
+
attributeFilter: ["aria-selected"],
|
|
486
|
+
subtree: true
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
#update = () => {
|
|
490
|
+
if (!this.#rootElement.checkVisibility()) {
|
|
491
|
+
return;
|
|
492
|
+
}
|
|
493
|
+
if (!this.#listElement) {
|
|
494
|
+
return;
|
|
495
|
+
}
|
|
496
|
+
const isHorizontal = this.#listElement.ariaOrientation !== "vertical";
|
|
497
|
+
const position = `inset${isHorizontal ? "Inline" : "Block"}Start`;
|
|
498
|
+
const size = `${isHorizontal ? "inline" : "block"}Size`;
|
|
499
|
+
const tab = this.#listElement.querySelector(
|
|
500
|
+
'[aria-selected="true"]'
|
|
501
|
+
);
|
|
502
|
+
if (!tab) {
|
|
503
|
+
return;
|
|
504
|
+
}
|
|
505
|
+
const { x: tabX, y: tabY, width, height } = tab.getBoundingClientRect();
|
|
506
|
+
const { x: listX, y: listY } = this.#listElement.getBoundingClientRect();
|
|
507
|
+
const { duration, easing } = this.#settings.animation.indicator;
|
|
508
|
+
this.#animation = this.#rootElement.animate(
|
|
509
|
+
{
|
|
510
|
+
[position]: `${isHorizontal ? tabX - listX : tabY - listY}px`,
|
|
511
|
+
[size]: `${isHorizontal ? width : height}px`
|
|
512
|
+
},
|
|
513
|
+
{ duration, easing, fill: "forwards" }
|
|
514
|
+
);
|
|
515
|
+
};
|
|
516
|
+
async destroy(force = false) {
|
|
517
|
+
this.#resizeObserver?.disconnect();
|
|
518
|
+
this.#resizeObserver = null;
|
|
519
|
+
this.#mutationObserver?.disconnect();
|
|
520
|
+
this.#mutationObserver = null;
|
|
521
|
+
if (!this.#animation) {
|
|
522
|
+
return;
|
|
523
|
+
}
|
|
524
|
+
if (!force) {
|
|
525
|
+
try {
|
|
526
|
+
await this.#animation.finished;
|
|
527
|
+
} catch {
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
this.#animation.cancel();
|
|
531
|
+
this.#animation = null;
|
|
532
|
+
this.#listElement = null;
|
|
533
|
+
}
|
|
534
|
+
};
|
|
535
|
+
function addTokenToAttribute(element, attribute, token) {
|
|
536
|
+
const tokens = new Set(
|
|
537
|
+
element.getAttribute(attribute)?.trim().split(/\s+/) ?? []
|
|
538
|
+
);
|
|
539
|
+
tokens.add(token);
|
|
540
|
+
element.setAttribute(attribute, [...tokens].join(" "));
|
|
541
|
+
}
|
|
542
|
+
function createBinding(tabs, panel) {
|
|
543
|
+
return { tabs, panel, animation: null };
|
|
544
|
+
}
|
|
545
|
+
function getActiveElement() {
|
|
546
|
+
let current = document.activeElement;
|
|
547
|
+
while (current?.shadowRoot?.activeElement) {
|
|
548
|
+
current = current.shadowRoot.activeElement;
|
|
549
|
+
}
|
|
550
|
+
return current;
|
|
551
|
+
}
|
|
552
|
+
function hasFocusable(container) {
|
|
553
|
+
return !![
|
|
554
|
+
...container.querySelectorAll(
|
|
555
|
+
`:is(a[href], area[href], button, embed, iframe, input:not([type="hidden" i]), object, select, details > summary:first-of-type, textarea, [contenteditable]:not([contenteditable="false" i]), [controls], [tabindex]):not(:disabled, [hidden], [inert], [tabindex="-1"])`
|
|
556
|
+
)
|
|
557
|
+
].filter((element) => element.checkVisibility()).length;
|
|
558
|
+
}
|
|
559
|
+
function isFocusable(element) {
|
|
560
|
+
return !element.hasAttribute("disabled");
|
|
561
|
+
}
|
|
562
|
+
/**
|
|
563
|
+
* Tabs
|
|
564
|
+
* WAI-ARIA compliant tabs pattern implementation in TypeScript.
|
|
565
|
+
*
|
|
566
|
+
* @version 1.3.3
|
|
567
|
+
* @author Yusuke Kamiyamane
|
|
568
|
+
* @license MIT
|
|
569
|
+
* @copyright Copyright (c) Yusuke Kamiyamane
|
|
570
|
+
* @see {@link https://github.com/y14e/tabs}
|
|
571
|
+
*/
|
|
572
|
+
|
|
573
|
+
module.exports = Tabs;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tabs
|
|
3
|
+
* WAI-ARIA compliant tabs pattern implementation in TypeScript.
|
|
4
|
+
*
|
|
5
|
+
* @version 1.3.3
|
|
6
|
+
* @author Yusuke Kamiyamane
|
|
7
|
+
* @license MIT
|
|
8
|
+
* @copyright Copyright (c) Yusuke Kamiyamane
|
|
9
|
+
* @see {@link https://github.com/y14e/tabs}
|
|
10
|
+
*/
|
|
11
|
+
interface TabsOptions {
|
|
12
|
+
readonly animation?: {
|
|
13
|
+
readonly content?: {
|
|
14
|
+
readonly crossFade?: boolean;
|
|
15
|
+
readonly duration?: number;
|
|
16
|
+
readonly easing?: string;
|
|
17
|
+
readonly fade?: boolean;
|
|
18
|
+
};
|
|
19
|
+
readonly indicator?: {
|
|
20
|
+
readonly duration?: number;
|
|
21
|
+
readonly easing?: string;
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
readonly avoidDuplicates?: boolean;
|
|
25
|
+
readonly manual?: boolean;
|
|
26
|
+
readonly selector?: {
|
|
27
|
+
readonly content?: string;
|
|
28
|
+
readonly indicator?: string;
|
|
29
|
+
readonly list?: string;
|
|
30
|
+
readonly panel?: string;
|
|
31
|
+
readonly tab?: string;
|
|
32
|
+
};
|
|
33
|
+
readonly vertical?: boolean;
|
|
34
|
+
}
|
|
35
|
+
declare class Tabs {
|
|
36
|
+
#private;
|
|
37
|
+
static defaults: TabsOptions;
|
|
38
|
+
constructor(root: HTMLElement, options?: TabsOptions);
|
|
39
|
+
activate(tab: HTMLElement, isMatch?: boolean): Promise<void>;
|
|
40
|
+
destroy(force?: boolean): Promise<void>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export { type TabsOptions, Tabs as default };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tabs
|
|
3
|
+
* WAI-ARIA compliant tabs pattern implementation in TypeScript.
|
|
4
|
+
*
|
|
5
|
+
* @version 1.3.3
|
|
6
|
+
* @author Yusuke Kamiyamane
|
|
7
|
+
* @license MIT
|
|
8
|
+
* @copyright Copyright (c) Yusuke Kamiyamane
|
|
9
|
+
* @see {@link https://github.com/y14e/tabs}
|
|
10
|
+
*/
|
|
11
|
+
interface TabsOptions {
|
|
12
|
+
readonly animation?: {
|
|
13
|
+
readonly content?: {
|
|
14
|
+
readonly crossFade?: boolean;
|
|
15
|
+
readonly duration?: number;
|
|
16
|
+
readonly easing?: string;
|
|
17
|
+
readonly fade?: boolean;
|
|
18
|
+
};
|
|
19
|
+
readonly indicator?: {
|
|
20
|
+
readonly duration?: number;
|
|
21
|
+
readonly easing?: string;
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
readonly avoidDuplicates?: boolean;
|
|
25
|
+
readonly manual?: boolean;
|
|
26
|
+
readonly selector?: {
|
|
27
|
+
readonly content?: string;
|
|
28
|
+
readonly indicator?: string;
|
|
29
|
+
readonly list?: string;
|
|
30
|
+
readonly panel?: string;
|
|
31
|
+
readonly tab?: string;
|
|
32
|
+
};
|
|
33
|
+
readonly vertical?: boolean;
|
|
34
|
+
}
|
|
35
|
+
declare class Tabs {
|
|
36
|
+
#private;
|
|
37
|
+
static defaults: TabsOptions;
|
|
38
|
+
constructor(root: HTMLElement, options?: TabsOptions);
|
|
39
|
+
activate(tab: HTMLElement, isMatch?: boolean): Promise<void>;
|
|
40
|
+
destroy(force?: boolean): Promise<void>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export { type TabsOptions, Tabs as default };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,571 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
var Tabs = class _Tabs {
|
|
3
|
+
static defaults = {};
|
|
4
|
+
#rootElement;
|
|
5
|
+
#defaults = {
|
|
6
|
+
animation: {
|
|
7
|
+
content: {
|
|
8
|
+
crossFade: true,
|
|
9
|
+
duration: 300,
|
|
10
|
+
easing: "ease",
|
|
11
|
+
fade: false
|
|
12
|
+
},
|
|
13
|
+
indicator: {
|
|
14
|
+
duration: 300,
|
|
15
|
+
easing: "ease"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
avoidDuplicates: false,
|
|
19
|
+
manual: false,
|
|
20
|
+
selector: {
|
|
21
|
+
content: '[role="tablist"] + *',
|
|
22
|
+
indicator: "[data-tabs-indicator]",
|
|
23
|
+
list: '[role="tablist"]',
|
|
24
|
+
panel: '[role="tabpanel"]',
|
|
25
|
+
tab: '[role="tab"]'
|
|
26
|
+
},
|
|
27
|
+
vertical: false
|
|
28
|
+
};
|
|
29
|
+
#settings;
|
|
30
|
+
#listElements;
|
|
31
|
+
#tabElements;
|
|
32
|
+
#indicatorElements;
|
|
33
|
+
#contentElement;
|
|
34
|
+
#panelElements;
|
|
35
|
+
#bindings = /* @__PURE__ */ new WeakMap();
|
|
36
|
+
#eventController = null;
|
|
37
|
+
#animationController = null;
|
|
38
|
+
#animation = null;
|
|
39
|
+
#indicators = [];
|
|
40
|
+
#isDestroyed = false;
|
|
41
|
+
constructor(root, options = {}) {
|
|
42
|
+
if (!(root instanceof HTMLElement)) {
|
|
43
|
+
throw new TypeError("Invalid root element");
|
|
44
|
+
}
|
|
45
|
+
if (root.hasAttribute("data-tabs-initialized")) {
|
|
46
|
+
console.warn("Already initialized");
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
this.#rootElement = root;
|
|
50
|
+
this.#defaults = this.#mergeOptions(this.#defaults, _Tabs.defaults);
|
|
51
|
+
this.#settings = this.#mergeOptions(this.#defaults, options);
|
|
52
|
+
if (matchMedia("(prefers-reduced-motion: reduce)").matches) {
|
|
53
|
+
Object.assign(this.#settings.animation, {
|
|
54
|
+
content: { duration: 0 },
|
|
55
|
+
indicator: { duration: 0 }
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
const NOT_NESTED = `:not(:scope ${this.#settings.selector.panel} *)`;
|
|
59
|
+
this.#listElements = [
|
|
60
|
+
...this.#rootElement.querySelectorAll(
|
|
61
|
+
`${this.#settings.selector.list}${NOT_NESTED}`
|
|
62
|
+
)
|
|
63
|
+
];
|
|
64
|
+
if (!this.#listElements.length) {
|
|
65
|
+
console.warn("Missing list elements");
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
this.#tabElements = [
|
|
69
|
+
...this.#rootElement.querySelectorAll(
|
|
70
|
+
`${this.#settings.selector.tab}${NOT_NESTED}`
|
|
71
|
+
)
|
|
72
|
+
];
|
|
73
|
+
if (!this.#tabElements.length) {
|
|
74
|
+
console.warn("Missing tab elements");
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
this.#indicatorElements = [
|
|
78
|
+
...this.#rootElement.querySelectorAll(
|
|
79
|
+
`${this.#settings.selector.indicator}${NOT_NESTED}`
|
|
80
|
+
)
|
|
81
|
+
];
|
|
82
|
+
this.#contentElement = this.#rootElement.querySelector(
|
|
83
|
+
this.#settings.selector.content
|
|
84
|
+
);
|
|
85
|
+
if (!this.#contentElement) {
|
|
86
|
+
console.warn("Missing content element");
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
this.#panelElements = [
|
|
90
|
+
...this.#rootElement.querySelectorAll(
|
|
91
|
+
`${this.#settings.selector.panel}${NOT_NESTED}`
|
|
92
|
+
)
|
|
93
|
+
];
|
|
94
|
+
const length = this.#panelElements.length;
|
|
95
|
+
if (!length) {
|
|
96
|
+
console.warn("Missing panel elements");
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
const tabs = [];
|
|
100
|
+
this.#tabElements.forEach((tab, i) => {
|
|
101
|
+
const index = i % length;
|
|
102
|
+
const tabsByIndex = tabs[index] ?? [];
|
|
103
|
+
tabsByIndex.push(tab);
|
|
104
|
+
tabs[index] = tabsByIndex;
|
|
105
|
+
const panel = this.#panelElements[index];
|
|
106
|
+
if (!panel) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
const binding = createBinding(tabsByIndex, panel);
|
|
110
|
+
this.#bindings.set(tab, binding);
|
|
111
|
+
if (i < length) {
|
|
112
|
+
this.#bindings.set(panel, binding);
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
this.#initialize();
|
|
116
|
+
}
|
|
117
|
+
async activate(tab, isMatch = false) {
|
|
118
|
+
if (this.#isDestroyed) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
if (!(tab instanceof HTMLElement) || !this.#bindings.has(tab)) {
|
|
122
|
+
console.warn("Invalid tab element");
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
if (tab.ariaSelected === "true") {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
this.#tabElements.forEach((t) => {
|
|
129
|
+
const isSelected = this.#bindings.get(t)?.tabs.some((tt) => tt === tab);
|
|
130
|
+
t.setAttribute("aria-selected", String(isSelected));
|
|
131
|
+
t.setAttribute(
|
|
132
|
+
"tabindex",
|
|
133
|
+
isSelected && !this.#isAvoidedTab(t) ? "0" : "-1"
|
|
134
|
+
);
|
|
135
|
+
});
|
|
136
|
+
if (!this.#contentElement) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
const size = this.#contentElement.offsetHeight;
|
|
140
|
+
this.#rootElement.setAttribute("data-tabs-animating", "");
|
|
141
|
+
const { style } = this.#contentElement;
|
|
142
|
+
style.setProperty("overflow", "clip");
|
|
143
|
+
style.setProperty("position", "relative");
|
|
144
|
+
const { fade, crossFade } = this.#settings.animation.content;
|
|
145
|
+
const panel = this.#bindings.get(tab)?.panel;
|
|
146
|
+
if (!panel) {
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
this.#panelElements.forEach((p) => {
|
|
150
|
+
const { style: style2 } = p;
|
|
151
|
+
if (fade || crossFade) {
|
|
152
|
+
style2.setProperty("content-visibility", "visible");
|
|
153
|
+
style2.setProperty("display", "block");
|
|
154
|
+
style2.setProperty("opacity", p.hidden ? "0" : "1");
|
|
155
|
+
}
|
|
156
|
+
style2.setProperty("inline-size", "100%");
|
|
157
|
+
style2.setProperty("position", "absolute");
|
|
158
|
+
if (p === panel && !hasFocusable(p)) {
|
|
159
|
+
p.setAttribute("tabindex", "0");
|
|
160
|
+
} else {
|
|
161
|
+
p.removeAttribute("tabindex");
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
this.#panelElements.forEach((p, i) => {
|
|
165
|
+
if (p === panel) {
|
|
166
|
+
p.removeAttribute("hidden");
|
|
167
|
+
} else {
|
|
168
|
+
const tab2 = this.#tabElements[i];
|
|
169
|
+
if (!tab2) {
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
p.setAttribute("hidden", isFocusable(tab2) ? "until-found" : "");
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
this.#animation?.cancel();
|
|
176
|
+
const { duration, easing } = this.#settings.animation.content;
|
|
177
|
+
this.#animation = this.#contentElement.animate(
|
|
178
|
+
{
|
|
179
|
+
blockSize: [
|
|
180
|
+
`${size}px`,
|
|
181
|
+
getComputedStyle(panel).getPropertyValue("block-size")
|
|
182
|
+
]
|
|
183
|
+
},
|
|
184
|
+
{
|
|
185
|
+
duration: isMatch ? 0 : duration,
|
|
186
|
+
easing
|
|
187
|
+
}
|
|
188
|
+
);
|
|
189
|
+
const cleanup = () => {
|
|
190
|
+
this.#animation = null;
|
|
191
|
+
};
|
|
192
|
+
this.#animationController = new AbortController();
|
|
193
|
+
const { signal } = this.#animationController;
|
|
194
|
+
this.#animation.addEventListener("cancel", cleanup, {
|
|
195
|
+
once: true,
|
|
196
|
+
signal
|
|
197
|
+
});
|
|
198
|
+
this.#animation.addEventListener(
|
|
199
|
+
"finish",
|
|
200
|
+
() => {
|
|
201
|
+
this.#onAnimationFinish();
|
|
202
|
+
cleanup();
|
|
203
|
+
},
|
|
204
|
+
{
|
|
205
|
+
once: true,
|
|
206
|
+
signal
|
|
207
|
+
}
|
|
208
|
+
);
|
|
209
|
+
this.#panelElements.forEach((p) => {
|
|
210
|
+
const binding = this.#bindings.get(p);
|
|
211
|
+
if (!binding) {
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
const opacity = getComputedStyle(p).getPropertyValue("opacity");
|
|
215
|
+
binding.animation?.cancel();
|
|
216
|
+
const isSelected = p === panel;
|
|
217
|
+
const animation = p.animate(
|
|
218
|
+
{
|
|
219
|
+
opacity: fade ? isSelected ? [opacity, opacity, "1"] : [opacity, "0", "0"] : isSelected ? [opacity, "1"] : [opacity, "0"]
|
|
220
|
+
},
|
|
221
|
+
{
|
|
222
|
+
duration: isMatch || !(fade || crossFade) ? 0 : this.#settings.animation.content.duration,
|
|
223
|
+
easing: "ease"
|
|
224
|
+
}
|
|
225
|
+
);
|
|
226
|
+
binding.animation = animation;
|
|
227
|
+
const cleanup2 = () => {
|
|
228
|
+
if (binding.animation === animation) {
|
|
229
|
+
binding.animation = null;
|
|
230
|
+
}
|
|
231
|
+
};
|
|
232
|
+
this.#animationController = new AbortController();
|
|
233
|
+
const { signal: signal2 } = this.#animationController;
|
|
234
|
+
animation.addEventListener("cancel", cleanup2, { once: true, signal: signal2 });
|
|
235
|
+
animation.addEventListener("finish", cleanup2, { once: true, signal: signal2 });
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
async destroy(force = false) {
|
|
239
|
+
if (this.#isDestroyed) {
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
this.#isDestroyed = true;
|
|
243
|
+
this.#eventController?.abort();
|
|
244
|
+
this.#eventController = null;
|
|
245
|
+
this.#indicators.forEach((indicator) => {
|
|
246
|
+
indicator.destroy(force);
|
|
247
|
+
});
|
|
248
|
+
this.#indicators.length = 0;
|
|
249
|
+
if (this.#animation) {
|
|
250
|
+
if (!force) {
|
|
251
|
+
try {
|
|
252
|
+
await this.#animation.finished;
|
|
253
|
+
} catch {
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
this.#animation.cancel();
|
|
257
|
+
}
|
|
258
|
+
if (!force) {
|
|
259
|
+
await Promise.all(
|
|
260
|
+
this.#panelElements.map(
|
|
261
|
+
(panel) => this.#bindings.get(panel)?.animation?.finished.catch(() => {
|
|
262
|
+
})
|
|
263
|
+
)
|
|
264
|
+
);
|
|
265
|
+
}
|
|
266
|
+
this.#panelElements.forEach((panel) => {
|
|
267
|
+
const animation = this.#bindings.get(panel)?.animation;
|
|
268
|
+
if (animation) {
|
|
269
|
+
animation.cancel();
|
|
270
|
+
}
|
|
271
|
+
});
|
|
272
|
+
this.#onAnimationFinish();
|
|
273
|
+
this.#animationController?.abort();
|
|
274
|
+
this.#animationController = null;
|
|
275
|
+
this.#listElements.length = 0;
|
|
276
|
+
this.#tabElements.length = 0;
|
|
277
|
+
this.#contentElement = null;
|
|
278
|
+
this.#panelElements.length = 0;
|
|
279
|
+
this.#rootElement.removeAttribute("data-tabs-initialized");
|
|
280
|
+
}
|
|
281
|
+
#initialize() {
|
|
282
|
+
const { signal } = this.#eventController ?? new AbortController();
|
|
283
|
+
this.#listElements.forEach((list, i) => {
|
|
284
|
+
if (this.#settings.avoidDuplicates && i) {
|
|
285
|
+
list.setAttribute("aria-hidden", "true");
|
|
286
|
+
}
|
|
287
|
+
if (this.#settings.vertical) {
|
|
288
|
+
list.setAttribute("aria-orientation", "vertical");
|
|
289
|
+
}
|
|
290
|
+
list.setAttribute("role", "tablist");
|
|
291
|
+
});
|
|
292
|
+
this.#tabElements.forEach((tab, i) => {
|
|
293
|
+
const id = Math.random().toString(36).slice(-8);
|
|
294
|
+
const panel = this.#panelElements[i % this.#panelElements.length];
|
|
295
|
+
if (!panel) {
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
panel.id ||= `tabs-panel-${id}`;
|
|
299
|
+
addTokenToAttribute(tab, "aria-controls", panel.id);
|
|
300
|
+
if (!tab.hasAttribute("aria-selected")) {
|
|
301
|
+
tab.setAttribute("aria-selected", "false");
|
|
302
|
+
}
|
|
303
|
+
const isAvoided = this.#isAvoidedTab(tab);
|
|
304
|
+
if (!isAvoided) {
|
|
305
|
+
tab.id ||= `tabs-tab-${id}`;
|
|
306
|
+
}
|
|
307
|
+
tab.setAttribute("role", "tab");
|
|
308
|
+
tab.setAttribute(
|
|
309
|
+
"tabindex",
|
|
310
|
+
tab.ariaSelected === "true" && !isAvoided ? "0" : "-1"
|
|
311
|
+
);
|
|
312
|
+
if (!isFocusable(tab)) {
|
|
313
|
+
tab.style.setProperty("pointer-events", "none");
|
|
314
|
+
}
|
|
315
|
+
addTokenToAttribute(panel, "aria-labelledby", tab.id);
|
|
316
|
+
tab.addEventListener("click", this.#onTabClick, { signal });
|
|
317
|
+
tab.addEventListener("keydown", this.#onTabKeyDown, { signal });
|
|
318
|
+
});
|
|
319
|
+
this.#indicatorElements.forEach((indicator) => {
|
|
320
|
+
indicator.closest(this.#settings.selector.list)?.style.setProperty("position", "relative");
|
|
321
|
+
const { style } = indicator;
|
|
322
|
+
style.setProperty("display", "block");
|
|
323
|
+
style.setProperty("position", "absolute");
|
|
324
|
+
this.#indicators.push(new TabsIndicator(indicator, this.#settings));
|
|
325
|
+
});
|
|
326
|
+
this.#panelElements.forEach((panel) => {
|
|
327
|
+
panel.setAttribute("role", "tabpanel");
|
|
328
|
+
if (!panel.hasAttribute("hidden") && !hasFocusable(panel)) {
|
|
329
|
+
panel.setAttribute("tabindex", "0");
|
|
330
|
+
}
|
|
331
|
+
panel.addEventListener("beforematch", this.#onPanelBeforeMatch, {
|
|
332
|
+
signal
|
|
333
|
+
});
|
|
334
|
+
});
|
|
335
|
+
this.#rootElement.setAttribute("data-tabs-initialized", "");
|
|
336
|
+
}
|
|
337
|
+
#onTabClick = (event) => {
|
|
338
|
+
event.preventDefault();
|
|
339
|
+
event.stopPropagation();
|
|
340
|
+
const tab = event.currentTarget;
|
|
341
|
+
if (!(tab instanceof HTMLElement)) {
|
|
342
|
+
return;
|
|
343
|
+
}
|
|
344
|
+
this.activate(tab);
|
|
345
|
+
};
|
|
346
|
+
#onTabKeyDown = (event) => {
|
|
347
|
+
const currentTab = event.currentTarget;
|
|
348
|
+
if (!(currentTab instanceof HTMLElement)) {
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
const list = currentTab.closest(this.#settings.selector.list);
|
|
352
|
+
if (!list) {
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
const isBoth = list.ariaOrientation === "undefined";
|
|
356
|
+
const isHorizontal = list.ariaOrientation !== "vertical";
|
|
357
|
+
const { key } = event;
|
|
358
|
+
if (![
|
|
359
|
+
"Enter",
|
|
360
|
+
" ",
|
|
361
|
+
"End",
|
|
362
|
+
"Home",
|
|
363
|
+
...isBoth ? ["ArrowLeft", "ArrowUp"] : [`Arrow${isHorizontal ? "Left" : "Up"}`],
|
|
364
|
+
...isBoth ? ["ArrowRight", "ArrowDown"] : [`Arrow${isHorizontal ? "Right" : "Down"}`]
|
|
365
|
+
].includes(key)) {
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
event.preventDefault();
|
|
369
|
+
event.stopPropagation();
|
|
370
|
+
const focusables = [
|
|
371
|
+
...list.querySelectorAll(this.#settings.selector.tab)
|
|
372
|
+
].filter(isFocusable);
|
|
373
|
+
const active = getActiveElement();
|
|
374
|
+
if (!(active instanceof HTMLElement)) {
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
const currentIndex = focusables.indexOf(active);
|
|
378
|
+
let newIndex = currentIndex;
|
|
379
|
+
switch (key) {
|
|
380
|
+
case "Enter":
|
|
381
|
+
case " ":
|
|
382
|
+
active.click();
|
|
383
|
+
return;
|
|
384
|
+
case "End":
|
|
385
|
+
newIndex = -1;
|
|
386
|
+
break;
|
|
387
|
+
case "Home":
|
|
388
|
+
newIndex = 0;
|
|
389
|
+
break;
|
|
390
|
+
case "ArrowLeft":
|
|
391
|
+
case "ArrowUp":
|
|
392
|
+
newIndex = currentIndex - 1;
|
|
393
|
+
break;
|
|
394
|
+
case "ArrowRight":
|
|
395
|
+
case "ArrowDown":
|
|
396
|
+
newIndex = (currentIndex + 1) % focusables.length;
|
|
397
|
+
break;
|
|
398
|
+
}
|
|
399
|
+
const newTab = focusables.at(newIndex);
|
|
400
|
+
if (!newTab) {
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
newTab.focus();
|
|
404
|
+
if (!this.#settings.manual) {
|
|
405
|
+
newTab.click();
|
|
406
|
+
}
|
|
407
|
+
};
|
|
408
|
+
#onPanelBeforeMatch = (event) => {
|
|
409
|
+
const panel = event.currentTarget;
|
|
410
|
+
if (!(panel instanceof HTMLElement)) {
|
|
411
|
+
return;
|
|
412
|
+
}
|
|
413
|
+
const tab = this.#bindings.get(panel)?.tabs[0];
|
|
414
|
+
if (!tab) {
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
this.activate(tab, true);
|
|
418
|
+
};
|
|
419
|
+
#isAvoidedTab(tab) {
|
|
420
|
+
const binding = this.#bindings.get(tab);
|
|
421
|
+
if (!binding) {
|
|
422
|
+
return false;
|
|
423
|
+
}
|
|
424
|
+
return this.#settings.avoidDuplicates && binding.tabs.indexOf(tab) > 0;
|
|
425
|
+
}
|
|
426
|
+
#mergeOptions(target, source) {
|
|
427
|
+
return {
|
|
428
|
+
...target,
|
|
429
|
+
...source,
|
|
430
|
+
animation: {
|
|
431
|
+
content: {
|
|
432
|
+
...target.animation.content,
|
|
433
|
+
...source.animation?.content ?? {}
|
|
434
|
+
},
|
|
435
|
+
indicator: {
|
|
436
|
+
...target.animation.indicator,
|
|
437
|
+
...source.animation?.indicator ?? {}
|
|
438
|
+
}
|
|
439
|
+
},
|
|
440
|
+
selector: {
|
|
441
|
+
...target.selector,
|
|
442
|
+
...source.selector ?? {}
|
|
443
|
+
}
|
|
444
|
+
};
|
|
445
|
+
}
|
|
446
|
+
#onAnimationFinish() {
|
|
447
|
+
if (!this.#contentElement) {
|
|
448
|
+
return;
|
|
449
|
+
}
|
|
450
|
+
const { style } = this.#contentElement;
|
|
451
|
+
style.removeProperty("block-size");
|
|
452
|
+
style.removeProperty("overflow");
|
|
453
|
+
style.removeProperty("position");
|
|
454
|
+
this.#panelElements.forEach((panel) => {
|
|
455
|
+
const { style: style2 } = panel;
|
|
456
|
+
style2.removeProperty("content-visibility");
|
|
457
|
+
style2.removeProperty("display");
|
|
458
|
+
style2.removeProperty("inline-size");
|
|
459
|
+
style2.removeProperty("opacity");
|
|
460
|
+
style2.removeProperty("position");
|
|
461
|
+
});
|
|
462
|
+
this.#rootElement.removeAttribute("data-tabs-animating");
|
|
463
|
+
}
|
|
464
|
+
};
|
|
465
|
+
var TabsIndicator = class {
|
|
466
|
+
#rootElement;
|
|
467
|
+
#settings;
|
|
468
|
+
#listElement = null;
|
|
469
|
+
#animation = null;
|
|
470
|
+
#resizeObserver = null;
|
|
471
|
+
#mutationObserver = null;
|
|
472
|
+
constructor(root, settings) {
|
|
473
|
+
this.#rootElement = root;
|
|
474
|
+
this.#settings = settings;
|
|
475
|
+
this.#listElement = root.closest(settings.selector.list);
|
|
476
|
+
if (!this.#listElement) {
|
|
477
|
+
return;
|
|
478
|
+
}
|
|
479
|
+
this.#resizeObserver = new ResizeObserver(this.#update);
|
|
480
|
+
this.#resizeObserver.observe(this.#listElement);
|
|
481
|
+
this.#mutationObserver = new MutationObserver(this.#update);
|
|
482
|
+
this.#mutationObserver.observe(this.#listElement, {
|
|
483
|
+
attributeFilter: ["aria-selected"],
|
|
484
|
+
subtree: true
|
|
485
|
+
});
|
|
486
|
+
}
|
|
487
|
+
#update = () => {
|
|
488
|
+
if (!this.#rootElement.checkVisibility()) {
|
|
489
|
+
return;
|
|
490
|
+
}
|
|
491
|
+
if (!this.#listElement) {
|
|
492
|
+
return;
|
|
493
|
+
}
|
|
494
|
+
const isHorizontal = this.#listElement.ariaOrientation !== "vertical";
|
|
495
|
+
const position = `inset${isHorizontal ? "Inline" : "Block"}Start`;
|
|
496
|
+
const size = `${isHorizontal ? "inline" : "block"}Size`;
|
|
497
|
+
const tab = this.#listElement.querySelector(
|
|
498
|
+
'[aria-selected="true"]'
|
|
499
|
+
);
|
|
500
|
+
if (!tab) {
|
|
501
|
+
return;
|
|
502
|
+
}
|
|
503
|
+
const { x: tabX, y: tabY, width, height } = tab.getBoundingClientRect();
|
|
504
|
+
const { x: listX, y: listY } = this.#listElement.getBoundingClientRect();
|
|
505
|
+
const { duration, easing } = this.#settings.animation.indicator;
|
|
506
|
+
this.#animation = this.#rootElement.animate(
|
|
507
|
+
{
|
|
508
|
+
[position]: `${isHorizontal ? tabX - listX : tabY - listY}px`,
|
|
509
|
+
[size]: `${isHorizontal ? width : height}px`
|
|
510
|
+
},
|
|
511
|
+
{ duration, easing, fill: "forwards" }
|
|
512
|
+
);
|
|
513
|
+
};
|
|
514
|
+
async destroy(force = false) {
|
|
515
|
+
this.#resizeObserver?.disconnect();
|
|
516
|
+
this.#resizeObserver = null;
|
|
517
|
+
this.#mutationObserver?.disconnect();
|
|
518
|
+
this.#mutationObserver = null;
|
|
519
|
+
if (!this.#animation) {
|
|
520
|
+
return;
|
|
521
|
+
}
|
|
522
|
+
if (!force) {
|
|
523
|
+
try {
|
|
524
|
+
await this.#animation.finished;
|
|
525
|
+
} catch {
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
this.#animation.cancel();
|
|
529
|
+
this.#animation = null;
|
|
530
|
+
this.#listElement = null;
|
|
531
|
+
}
|
|
532
|
+
};
|
|
533
|
+
function addTokenToAttribute(element, attribute, token) {
|
|
534
|
+
const tokens = new Set(
|
|
535
|
+
element.getAttribute(attribute)?.trim().split(/\s+/) ?? []
|
|
536
|
+
);
|
|
537
|
+
tokens.add(token);
|
|
538
|
+
element.setAttribute(attribute, [...tokens].join(" "));
|
|
539
|
+
}
|
|
540
|
+
function createBinding(tabs, panel) {
|
|
541
|
+
return { tabs, panel, animation: null };
|
|
542
|
+
}
|
|
543
|
+
function getActiveElement() {
|
|
544
|
+
let current = document.activeElement;
|
|
545
|
+
while (current?.shadowRoot?.activeElement) {
|
|
546
|
+
current = current.shadowRoot.activeElement;
|
|
547
|
+
}
|
|
548
|
+
return current;
|
|
549
|
+
}
|
|
550
|
+
function hasFocusable(container) {
|
|
551
|
+
return !![
|
|
552
|
+
...container.querySelectorAll(
|
|
553
|
+
`:is(a[href], area[href], button, embed, iframe, input:not([type="hidden" i]), object, select, details > summary:first-of-type, textarea, [contenteditable]:not([contenteditable="false" i]), [controls], [tabindex]):not(:disabled, [hidden], [inert], [tabindex="-1"])`
|
|
554
|
+
)
|
|
555
|
+
].filter((element) => element.checkVisibility()).length;
|
|
556
|
+
}
|
|
557
|
+
function isFocusable(element) {
|
|
558
|
+
return !element.hasAttribute("disabled");
|
|
559
|
+
}
|
|
560
|
+
/**
|
|
561
|
+
* Tabs
|
|
562
|
+
* WAI-ARIA compliant tabs pattern implementation in TypeScript.
|
|
563
|
+
*
|
|
564
|
+
* @version 1.3.3
|
|
565
|
+
* @author Yusuke Kamiyamane
|
|
566
|
+
* @license MIT
|
|
567
|
+
* @copyright Copyright (c) Yusuke Kamiyamane
|
|
568
|
+
* @see {@link https://github.com/y14e/tabs}
|
|
569
|
+
*/
|
|
570
|
+
|
|
571
|
+
export { Tabs as default };
|
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@y14e/tabs",
|
|
3
|
+
"version": "1.3.3",
|
|
4
|
+
"description": "WAI-ARIA compliant tabs pattern implementation in TypeScript",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"LICENSE",
|
|
19
|
+
"README.md"
|
|
20
|
+
],
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "tsup",
|
|
23
|
+
"prepublishOnly": "npm run build",
|
|
24
|
+
"lint": "tsc --noEmit"
|
|
25
|
+
},
|
|
26
|
+
"keywords": [
|
|
27
|
+
"a11y",
|
|
28
|
+
"accessibility",
|
|
29
|
+
"tabs",
|
|
30
|
+
"component",
|
|
31
|
+
"typescript",
|
|
32
|
+
"utility"
|
|
33
|
+
],
|
|
34
|
+
"author": "Yusuke Kamiyamane",
|
|
35
|
+
"license": "MIT",
|
|
36
|
+
"repository": {
|
|
37
|
+
"type": "git",
|
|
38
|
+
"url": "git+https://github.com/y14e/tabs.git"
|
|
39
|
+
},
|
|
40
|
+
"bugs": {
|
|
41
|
+
"url": "https://github.com/y14e/tabs/issues"
|
|
42
|
+
},
|
|
43
|
+
"homepage": "https://github.com/y14e/tabs#readme",
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"bun-types": "latest",
|
|
46
|
+
"tsup": "^8.0.0",
|
|
47
|
+
"typescript": "^5.6.0"
|
|
48
|
+
},
|
|
49
|
+
"engines": {
|
|
50
|
+
"node": ">=18"
|
|
51
|
+
}
|
|
52
|
+
}
|