@torpor/ui 0.0.16 → 0.0.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +10 -0
- package/dist/TabGroup/TabContent.torp +36 -0
- package/dist/TabGroup/TabGroup.torp +226 -0
- package/dist/TabGroup/TabGroupTypes.ts +38 -0
- package/dist/TabGroup/TabHeader.torp +60 -0
- package/dist/TabGroup/TabList.torp +49 -0
- package/dist/TabGroup/TabTrigger.torp +65 -0
- package/dist/TabGroup/index.cjs +53 -0
- package/dist/TabGroup/index.cjs.map +1 -0
- package/dist/TabGroup/index.js +22 -0
- package/dist/TabGroup/index.js.map +1 -0
- package/dist/metafile-cjs.json +1 -1
- package/dist/metafile-esm.json +1 -1
- package/package.json +8 -3
- package/scripts/postbuild.ts +12 -6
- package/src/TabGroup/TabContent.torp +36 -0
- package/src/TabGroup/TabGroup.torp +226 -0
- package/src/TabGroup/TabGroupTypes.ts +38 -0
- package/src/TabGroup/TabHeader.torp +60 -0
- package/src/TabGroup/TabList.torp +49 -0
- package/src/TabGroup/TabTrigger.torp +65 -0
- package/src/TabGroup/index.ts +7 -0
- package/src/TabGroup.ts +7 -0
- package/test/TabGroup/accessibility.test.ts +67 -0
- package/test/TabGroup/components/TabGroupAccessibility.torp +28 -0
- package/test/TabGroup/components/TabGroupKeyboard.torp +28 -0
- package/test/TabGroup/components/TabGroupList.torp +26 -0
- package/test/TabGroup/components/TabGroupTrigger.torp +28 -0
- package/test/TabGroup/for-items.test.ts +54 -0
- package/test/TabGroup/item-trigger.test.ts +34 -0
- package/test/TabGroup/item-value.test.ts +17 -0
- package/test/TabGroup/keyboard.test.ts +72 -0
- package/tsup.config.ts +1 -1
package/scripts/postbuild.ts
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
1
|
import chalk from "chalk";
|
|
2
2
|
import { existsSync, promises as fs, lstatSync } from "node:fs";
|
|
3
|
-
import { dirname, join, resolve } from "node:path";
|
|
3
|
+
import { basename, dirname, join, resolve } from "node:path";
|
|
4
4
|
import { fileURLToPath } from "node:url";
|
|
5
5
|
|
|
6
6
|
// HACK: There is almost certainly a way to do this with standard TS/TSUP/etc
|
|
7
7
|
// but I can't figure it out
|
|
8
8
|
|
|
9
9
|
async function run() {
|
|
10
|
-
const
|
|
10
|
+
const scriptsFolder = dirname(fileURLToPath(import.meta.url));
|
|
11
|
+
const sourceFolder = resolve(scriptsFolder, "../src");
|
|
12
|
+
const distFolder = resolve(scriptsFolder, "../dist");
|
|
13
|
+
|
|
14
|
+
// Read the dist files first, not for every component
|
|
15
|
+
const distFiles = (await fs.readdir(distFolder)).filter((f) => f.endsWith(".torp"));
|
|
11
16
|
|
|
12
17
|
// Loop through component folders e.g. Accordion
|
|
13
18
|
const componentFolders = (await fs.readdir(distFolder)).filter((f) =>
|
|
@@ -27,10 +32,11 @@ async function run() {
|
|
|
27
32
|
}
|
|
28
33
|
|
|
29
34
|
// Move all *.torp files for the component into this folder
|
|
30
|
-
|
|
31
|
-
(f) => f.
|
|
32
|
-
|
|
33
|
-
|
|
35
|
+
let relatedFiles = (await fs.readdir(join(sourceFolder, component)))
|
|
36
|
+
.filter((f) => f.endsWith(".torp") && f !== component)
|
|
37
|
+
.map((f) => basename(f, ".torp"))
|
|
38
|
+
.map((f) => distFiles.find((df) => df.startsWith(`${f}-`) && df.endsWith(".torp"))!);
|
|
39
|
+
for (let file of relatedFiles) {
|
|
34
40
|
const newfile = file.substring(0, file.indexOf("-")) + ".torp";
|
|
35
41
|
await fs.rename(join(distFolder, file), join(distFolder, component, newfile));
|
|
36
42
|
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import getId from '../utils/getId';
|
|
2
|
+
import { TabGroupContextName, type TabGroupContext } from './TabGroupTypes';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Content for a TabGroup, which can be revealed or hidden by an associated TabTrigger.
|
|
6
|
+
*
|
|
7
|
+
* @prop {string} [id] -- An ID for the TabContent's div container
|
|
8
|
+
*/
|
|
9
|
+
export default function TabContent($props: any) {
|
|
10
|
+
$props.id ||= getId();
|
|
11
|
+
|
|
12
|
+
const context = $context[TabGroupContextName] as TabGroupContext;
|
|
13
|
+
if (!context) {
|
|
14
|
+
throw new Error('TabContent must be contained within a TabGroup');
|
|
15
|
+
}
|
|
16
|
+
let $state = $context[TabGroupContextName].registerItem($props.index, $props.value);
|
|
17
|
+
|
|
18
|
+
// Set the content ID so that it can be used with aria-controls in TabTrigger
|
|
19
|
+
$state.contentId = $props.id;
|
|
20
|
+
|
|
21
|
+
@render {
|
|
22
|
+
<div
|
|
23
|
+
id={$props.id}
|
|
24
|
+
class='torp-tab-content'
|
|
25
|
+
role="tabpanel"
|
|
26
|
+
aria-labelledby={$state.headerId}
|
|
27
|
+
data-orientation={$props.orientation}
|
|
28
|
+
data-state={$state.dataState}
|
|
29
|
+
data-disabled={$state.dataDisabled}
|
|
30
|
+
>
|
|
31
|
+
@if ($state.active) {
|
|
32
|
+
<:slot />
|
|
33
|
+
}
|
|
34
|
+
</div>
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import { TabGroupContextName, type TabGroupContext, type ItemState } from './TabGroupTypes';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A list of interactive headings coupled with layered sections of content that
|
|
5
|
+
* enable users to reveal a single section of content at a time.
|
|
6
|
+
*
|
|
7
|
+
* See [the WAI ARIA guidelines for Tabs](https://www.w3.org/WAI/ARIA/apg/patterns/tabs/).
|
|
8
|
+
*
|
|
9
|
+
* The structure of a TabGroup is:
|
|
10
|
+
*
|
|
11
|
+
* ```
|
|
12
|
+
* <TabGroup>
|
|
13
|
+
* <TabList>
|
|
14
|
+
* <TabHeader>
|
|
15
|
+
* <TabTrigger />
|
|
16
|
+
* </TabHeader>
|
|
17
|
+
* </TabList>
|
|
18
|
+
* <TabContent />
|
|
19
|
+
* </TabGroup>
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @prop {'horizontal' | 'vertical'} orientation -- The orientation of the tab buttons, which affects keyboard navigation
|
|
23
|
+
* @prop {'automatic' | 'manual'} activation -- How tabs are activated; if automatic they are shown on focus, if manual they must be clicked
|
|
24
|
+
* @prop {string | string[]} value -- The value of the active TabContent
|
|
25
|
+
* @prop {boolean} disabled -- If set to true, the user cannot interact with any items in the TabGroup
|
|
26
|
+
* @prop {(value: string | string[]) => void} [onchange] -- Raised when the active item has changed
|
|
27
|
+
*/
|
|
28
|
+
export default function TabGroup($props: any) {
|
|
29
|
+
$props.orientation ||= 'horizontal';
|
|
30
|
+
$props.activation ||= 'automatic';
|
|
31
|
+
$props.value ||= '';
|
|
32
|
+
|
|
33
|
+
interface State {
|
|
34
|
+
value: string;
|
|
35
|
+
itemStates: ItemState[];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
let $state: State = $watch({
|
|
39
|
+
value: $props.value,
|
|
40
|
+
itemStates: []
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
// This is used to assign automatic indexes to items where needed
|
|
44
|
+
let nextHeaderIndex = 0;
|
|
45
|
+
let nextItemIndex = 0;
|
|
46
|
+
|
|
47
|
+
// Set the context to pass down to headers and content, with functions for each item to call
|
|
48
|
+
let context: TabGroupContext = {
|
|
49
|
+
registerHeader,
|
|
50
|
+
registerItem,
|
|
51
|
+
removeItem,
|
|
52
|
+
triggerItem,
|
|
53
|
+
handleHeaderKey,
|
|
54
|
+
orientation: $props.orientation,
|
|
55
|
+
activation: $props.activation,
|
|
56
|
+
};
|
|
57
|
+
$context[TabGroupContextName] = context;
|
|
58
|
+
|
|
59
|
+
$run(() => {
|
|
60
|
+
// HACK: This should probably be done with setters?
|
|
61
|
+
let newValue = $state.value;
|
|
62
|
+
|
|
63
|
+
// Ensure the value passed in was a string (in case e.g. the user has set values to ints)
|
|
64
|
+
newValue = newValue?.toString();
|
|
65
|
+
for (let item of $state.itemStates) {
|
|
66
|
+
item.active = item.value === newValue;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Set the value
|
|
70
|
+
$state.value = newValue;
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
$run(() => {
|
|
74
|
+
$state.itemStates.forEach((s) => s.parentDisabled = $props.disabled);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
// Dispatch the change event when the value changes
|
|
78
|
+
// TODO: Should this be a setter instead?
|
|
79
|
+
$run(() => {
|
|
80
|
+
if ($props.onchange) {
|
|
81
|
+
$props.onchange($state.value);
|
|
82
|
+
}
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
// TODO:
|
|
86
|
+
//$mount(() => {
|
|
87
|
+
// if ($state.value === "" && $state.itemStates.length) {
|
|
88
|
+
// $state.value = $state.itemStates[0].value;
|
|
89
|
+
// }
|
|
90
|
+
//})
|
|
91
|
+
|
|
92
|
+
function registerHeader(index: number | undefined, value: string | undefined, setFocused: () => void): ItemState {
|
|
93
|
+
// If an index wasn't set for the header, set it automatically
|
|
94
|
+
if (!index) {
|
|
95
|
+
index = nextHeaderIndex++;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Get or add the ItemState at this index
|
|
99
|
+
const itemState = getItemState(index, value);
|
|
100
|
+
|
|
101
|
+
itemState.setFocused = setFocused;
|
|
102
|
+
|
|
103
|
+
return itemState;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function registerItem(index: number | undefined, value: string | undefined): ItemState {
|
|
107
|
+
// If an index wasn't set for the item, set it automatically
|
|
108
|
+
if (!index) {
|
|
109
|
+
index = nextItemIndex++;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// Get or add the ItemState at this index
|
|
113
|
+
const itemState = getItemState(index, value);
|
|
114
|
+
|
|
115
|
+
return itemState;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function getItemState(index: number, value: string | undefined): ItemState {
|
|
119
|
+
// If we already have an item at this index, just return it
|
|
120
|
+
if ($state.itemStates.length > index) {
|
|
121
|
+
return $state.itemStates[index];
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// If a value wasn't set for the item, set it to the index
|
|
125
|
+
if (!value) {
|
|
126
|
+
value = index.toString();
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// Add the item's state to the array
|
|
130
|
+
const newItemState: ItemState = $watch({
|
|
131
|
+
index,
|
|
132
|
+
value,
|
|
133
|
+
active: value === $state.value,
|
|
134
|
+
headerId: '',
|
|
135
|
+
contentId: '',
|
|
136
|
+
disabled: false,
|
|
137
|
+
parentDisabled: false,
|
|
138
|
+
setFocused: () => {},
|
|
139
|
+
orientation: $props.orientation,
|
|
140
|
+
activation: $props.activation
|
|
141
|
+
});
|
|
142
|
+
$state.itemStates.splice(index, 0, newItemState);
|
|
143
|
+
|
|
144
|
+
return newItemState;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// TODO: I think this should be done by value
|
|
148
|
+
function removeItem(itemIndex: number) {
|
|
149
|
+
// Remove the item and re-index
|
|
150
|
+
let index = $state.itemStates.findIndex((item) => item.index === itemIndex);
|
|
151
|
+
if (index !== -1) {
|
|
152
|
+
$state.itemStates.splice(itemIndex, 1);
|
|
153
|
+
}
|
|
154
|
+
$state.itemStates.forEach((item, i) => (item.index = i));
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function triggerItem(toggleValue: string) {
|
|
158
|
+
for (let item of $state.itemStates) {
|
|
159
|
+
// The item is active if it has the supplied value
|
|
160
|
+
if (item.value === toggleValue) {
|
|
161
|
+
$state.value = item.value;
|
|
162
|
+
break;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function handleHeaderKey(headerIndex: number, e: KeyboardEvent) {
|
|
168
|
+
switch (e.key) {
|
|
169
|
+
case 'Left':
|
|
170
|
+
case 'ArrowLeft': {
|
|
171
|
+
if ($props.orientation === 'horizontal' && headerIndex > 0) {
|
|
172
|
+
e.preventDefault();
|
|
173
|
+
$state.itemStates[headerIndex - 1].setFocused();
|
|
174
|
+
}
|
|
175
|
+
break;
|
|
176
|
+
}
|
|
177
|
+
case 'Up':
|
|
178
|
+
case 'ArrowUp': {
|
|
179
|
+
if ($props.orientation === 'vertical' && headerIndex > 0) {
|
|
180
|
+
e.preventDefault();
|
|
181
|
+
$state.itemStates[headerIndex - 1].setFocused();
|
|
182
|
+
}
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
case 'Right':
|
|
186
|
+
case 'ArrowRight': {
|
|
187
|
+
if ($props.orientation === 'horizontal' && headerIndex < $state.itemStates.length - 1) {
|
|
188
|
+
e.preventDefault();
|
|
189
|
+
$state.itemStates[headerIndex + 1].setFocused();
|
|
190
|
+
}
|
|
191
|
+
break;
|
|
192
|
+
}
|
|
193
|
+
case 'Down':
|
|
194
|
+
case 'ArrowDown': {
|
|
195
|
+
if ($props.orientation === 'vertical' && headerIndex < $state.itemStates.length - 1) {
|
|
196
|
+
e.preventDefault();
|
|
197
|
+
$state.itemStates[headerIndex + 1].setFocused();
|
|
198
|
+
}
|
|
199
|
+
break;
|
|
200
|
+
}
|
|
201
|
+
case 'Home': {
|
|
202
|
+
if (headerIndex > 0) {
|
|
203
|
+
e.preventDefault();
|
|
204
|
+
$state.itemStates[0].setFocused();
|
|
205
|
+
}
|
|
206
|
+
break;
|
|
207
|
+
}
|
|
208
|
+
case 'End': {
|
|
209
|
+
if (headerIndex < $state.itemStates.length - 1) {
|
|
210
|
+
e.preventDefault();
|
|
211
|
+
$state.itemStates[$state.itemStates.length - 1].setFocused();
|
|
212
|
+
}
|
|
213
|
+
break;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
@render {
|
|
219
|
+
<div
|
|
220
|
+
class="torp-tab-group"
|
|
221
|
+
data-orientation={$props.orientation}
|
|
222
|
+
>
|
|
223
|
+
<:slot />
|
|
224
|
+
</div>
|
|
225
|
+
}
|
|
226
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export const TabGroupContextName = "TabGroup";
|
|
2
|
+
export const TabItemContextName = "TabItem";
|
|
3
|
+
|
|
4
|
+
export interface TabGroupContext {
|
|
5
|
+
/** Called from a header when it is added */
|
|
6
|
+
registerHeader: (index: number, value: string, setFocused: () => void) => ItemState;
|
|
7
|
+
/** Called from an item when it is added */
|
|
8
|
+
registerItem: (index: number, value: string) => ItemState;
|
|
9
|
+
/** Called from an item when it is removed */
|
|
10
|
+
removeItem: (index: number) => void;
|
|
11
|
+
/** Called from an item when it is triggered */
|
|
12
|
+
triggerItem: (value: string) => void;
|
|
13
|
+
/** Called from a TabHeader when it receives a keyboard event */
|
|
14
|
+
handleHeaderKey: (index: number, e: KeyboardEvent) => void;
|
|
15
|
+
orientation: "horizontal" | "vertical";
|
|
16
|
+
activation: "automatic" | "manual";
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface TabItemContext {
|
|
20
|
+
/** Called when an item is triggered */
|
|
21
|
+
triggerItem: (value: string) => void;
|
|
22
|
+
/** Called when a TabHeader receives a keyboard event */
|
|
23
|
+
handleHeaderKey: (index: number, e: KeyboardEvent) => void;
|
|
24
|
+
/** The reactive state for an item */
|
|
25
|
+
state: ItemState;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface ItemState {
|
|
29
|
+
index: number;
|
|
30
|
+
value: string;
|
|
31
|
+
active: boolean;
|
|
32
|
+
headerId: string;
|
|
33
|
+
contentId: string;
|
|
34
|
+
disabled: boolean;
|
|
35
|
+
parentDisabled: boolean;
|
|
36
|
+
/** Called in the TabGroup when a header needs to be focused */
|
|
37
|
+
setFocused: () => void;
|
|
38
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { TabGroupContextName, TabItemContextName, type TabGroupContext, type TabItemContext } from './TabGroupTypes';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The header for a tab item, which contains the TabTrigger.
|
|
5
|
+
*
|
|
6
|
+
* The structure of an TabHeader is:
|
|
7
|
+
*
|
|
8
|
+
* ```
|
|
9
|
+
* <TabHeader>
|
|
10
|
+
* <TabTrigger />
|
|
11
|
+
* </TabHeader>
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* @prop {'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6'} as -- The type of HTML header to use
|
|
15
|
+
*/
|
|
16
|
+
export default function TabHeader($props: any) {
|
|
17
|
+
const context = $context[TabGroupContextName] as TabGroupContext;
|
|
18
|
+
if (!context) {
|
|
19
|
+
throw new Error('TabHeader must be contained within a TabGroup');
|
|
20
|
+
}
|
|
21
|
+
let $state = $context[TabGroupContextName].registerHeader($props.index, $props.value);
|
|
22
|
+
|
|
23
|
+
const itemContext: TabItemContext = {
|
|
24
|
+
triggerItem: context.triggerItem,
|
|
25
|
+
handleHeaderKey: context.handleHeaderKey,
|
|
26
|
+
state: $state,
|
|
27
|
+
};
|
|
28
|
+
$context[TabItemContextName] = itemContext;
|
|
29
|
+
|
|
30
|
+
// Remove the item from the parent TabGroup when this item is destroyed
|
|
31
|
+
$run(() => {
|
|
32
|
+
return () => {
|
|
33
|
+
context.removeItem($state.index);
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
@render {
|
|
38
|
+
@// TODO: $props.as
|
|
39
|
+
<h4
|
|
40
|
+
:class={{
|
|
41
|
+
'torp-tab-header': true,
|
|
42
|
+
horizontal: $state.orientation === "horizontal",
|
|
43
|
+
vertical: $state.orientation === "vertical" }}
|
|
44
|
+
data-state={$state.dataState}
|
|
45
|
+
data-disabled={$state.dataDisabled}
|
|
46
|
+
>
|
|
47
|
+
<:slot />
|
|
48
|
+
</h4>
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
@style {
|
|
52
|
+
.torp-tab-header.horizontal {
|
|
53
|
+
display: inline-block;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
.torp-tab-header.vertical {
|
|
57
|
+
display: block;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { TabGroupContextName, TabItemContextName, type TabGroupContext, type TabItemContext } from './TabGroupTypes';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A component that holds the TabHeaders of a TabGroup.
|
|
5
|
+
*
|
|
6
|
+
* The structure of a TabList is:
|
|
7
|
+
*
|
|
8
|
+
* ```
|
|
9
|
+
* <TabList>
|
|
10
|
+
* <TabHeader>
|
|
11
|
+
* <TabTrigger />
|
|
12
|
+
* </TabHeader>
|
|
13
|
+
* </TabList>
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* @prop {string} ariaLabel -- An ARIA label for describing the tab list to screen readers
|
|
17
|
+
*/
|
|
18
|
+
export default function TabList($props: any) {
|
|
19
|
+
// Get stuff out of the TabGroupContext
|
|
20
|
+
const context = $context[TabGroupContextName] as TabGroupContext;
|
|
21
|
+
if (!context) {
|
|
22
|
+
throw new Error('TabList must be contained within an TabGroup');
|
|
23
|
+
}
|
|
24
|
+
// TODO: const { orientation } = context;
|
|
25
|
+
const orientation = context.orientation;
|
|
26
|
+
|
|
27
|
+
function handleFocus(e: FocusEvent & { currentTarget: EventTarget & HTMLElement }) {
|
|
28
|
+
// TODO: Should this be the first button?
|
|
29
|
+
const el = e.currentTarget.childNodes[0];
|
|
30
|
+
if (el instanceof HTMLElement) {
|
|
31
|
+
el.focus();
|
|
32
|
+
e.preventDefault();
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
@render {
|
|
37
|
+
<div
|
|
38
|
+
class='torp-tab-list'
|
|
39
|
+
role="tablist"
|
|
40
|
+
aria-label={$props.ariaLabel}
|
|
41
|
+
aria-orientation={orientation}
|
|
42
|
+
data-orientation={orientation}
|
|
43
|
+
tabindex="0"
|
|
44
|
+
onfocus={handleFocus}
|
|
45
|
+
>
|
|
46
|
+
<:slot />
|
|
47
|
+
</div>
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import getId from '../utils/getId';
|
|
2
|
+
import { TabItemContextName, type TabItemContext } from './TabGroupTypes';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A header button for a tab item, which can be used to reveal an associated TabContent.
|
|
6
|
+
*/
|
|
7
|
+
export default function TabTrigger() {
|
|
8
|
+
let button: HTMLButtonElement;
|
|
9
|
+
|
|
10
|
+
$props.id ||= getId();
|
|
11
|
+
|
|
12
|
+
// Get stuff out of the TabItemContext
|
|
13
|
+
const context = $context[TabItemContextName] as TabItemContext;
|
|
14
|
+
if (!context) {
|
|
15
|
+
throw new Error('TabTrigger must be contained within an TabItem');
|
|
16
|
+
}
|
|
17
|
+
let $state = $context[TabItemContextName].state;
|
|
18
|
+
|
|
19
|
+
// Set the header ID so that it can be used with aria-labelledby in TabContent
|
|
20
|
+
$state.headerId = $props.id;
|
|
21
|
+
|
|
22
|
+
// Let the item know how to set the button's focus
|
|
23
|
+
$state.setFocused = setFocused;
|
|
24
|
+
|
|
25
|
+
// This function is called by the parent TabGroup to focus this item's button
|
|
26
|
+
function setFocused() {
|
|
27
|
+
button.focus();
|
|
28
|
+
if ($state.activation === 'automatic') {
|
|
29
|
+
context.triggerItem($state.value);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function handleClick() {
|
|
34
|
+
// Pass the value to the triggerItem method in the parent TabGroup which will handle toggling
|
|
35
|
+
// the value of active for all items (e.g. to close other items if necessary)
|
|
36
|
+
context.triggerItem($state.value);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function handleKey(e: KeyboardEvent) {
|
|
40
|
+
// Pass the key press up to the parent TabGroup which will handle setting the focused button
|
|
41
|
+
context.handleHeaderKey($state.index, e);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
@render {
|
|
45
|
+
<button
|
|
46
|
+
type="button"
|
|
47
|
+
id={$props.id}
|
|
48
|
+
class='torp-tab-trigger'
|
|
49
|
+
role="tab"
|
|
50
|
+
tabindex="-1"
|
|
51
|
+
disabled={$state.disabled || $state.parentDisabled}
|
|
52
|
+
aria-selected={$state.active.toString()}
|
|
53
|
+
aria-disabled={$state.disabled}
|
|
54
|
+
aria-controls={$state.contentId}
|
|
55
|
+
data-state={$state.dataState}
|
|
56
|
+
data-disabled={$state.disabled}
|
|
57
|
+
data-orientation={$state.orientation}
|
|
58
|
+
:self={button}
|
|
59
|
+
onclick={handleClick}
|
|
60
|
+
onkeydown={handleKey}
|
|
61
|
+
>
|
|
62
|
+
<:slot />
|
|
63
|
+
</button>
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import TabContent from "./TabContent.torp";
|
|
2
|
+
import TabGroup from "./TabGroup.torp";
|
|
3
|
+
import TabHeader from "./TabHeader.torp";
|
|
4
|
+
import TabList from "./TabList.torp";
|
|
5
|
+
import TabTrigger from "./TabTrigger.torp";
|
|
6
|
+
|
|
7
|
+
export { TabGroup, TabList, TabHeader, TabTrigger, TabContent };
|
package/src/TabGroup.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import TabContent from "./TabGroup/TabContent.torp";
|
|
2
|
+
import TabGroup from "./TabGroup/TabGroup.torp";
|
|
3
|
+
import TabHeader from "./TabGroup/TabHeader.torp";
|
|
4
|
+
import TabList from "./TabGroup/TabList.torp";
|
|
5
|
+
import TabTrigger from "./TabGroup/TabTrigger.torp";
|
|
6
|
+
|
|
7
|
+
export { TabGroup, TabList, TabHeader, TabTrigger, TabContent };
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { queryByText } from "@testing-library/dom";
|
|
2
|
+
import "@testing-library/jest-dom/vitest";
|
|
3
|
+
import { mount } from "@torpor/view";
|
|
4
|
+
import { describe, expect, it } from "vitest";
|
|
5
|
+
import TabGroupAccessibility from "./components/TabGroupAccessibility.torp";
|
|
6
|
+
|
|
7
|
+
describe("TabGroup", () => {
|
|
8
|
+
it("Accessibility", async () => {
|
|
9
|
+
const container = document.createElement("div");
|
|
10
|
+
document.body.appendChild(container);
|
|
11
|
+
mount(container, TabGroupAccessibility, { value: "0" });
|
|
12
|
+
|
|
13
|
+
// Tests from https://www.w3.org/WAI/ARIA/apg/patterns/tabs/
|
|
14
|
+
|
|
15
|
+
// The element that serves as the container for the set of tabs has role
|
|
16
|
+
// tablist
|
|
17
|
+
expect(queryByText(container, "Header 1")?.parentElement?.parentElement).toHaveAttribute(
|
|
18
|
+
"role",
|
|
19
|
+
"tablist",
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
// Each element that serves as a tab has role tab and is contained
|
|
23
|
+
// within the element with role tablist
|
|
24
|
+
expect(queryByText(container, "Header 1")).toHaveAttribute("role", "tab");
|
|
25
|
+
|
|
26
|
+
// Each element that contains the content panel for a tab has role
|
|
27
|
+
// tabpanel
|
|
28
|
+
expect(queryByText(container, "Body 1")).toHaveAttribute("role", "tabpanel");
|
|
29
|
+
|
|
30
|
+
// If the tab list has a visible label, the element with role tablist
|
|
31
|
+
// has aria-labelledby set to a value that refers to the labelling
|
|
32
|
+
// element. Otherwise, the tablist element has a label provided by
|
|
33
|
+
// aria-label
|
|
34
|
+
// NOTE: We only support aria-label and it's required
|
|
35
|
+
expect(queryByText(container, "Header 1")?.parentElement?.parentElement).toHaveAttribute(
|
|
36
|
+
"aria-label",
|
|
37
|
+
"Example tabs",
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
// Each element with role tab has the property aria-controls referring
|
|
41
|
+
// to its associated tabpanel element
|
|
42
|
+
const tabId = queryByText(container, "Header 1")?.id;
|
|
43
|
+
const contentId = queryByText(container, "Body 1")?.id;
|
|
44
|
+
expect(queryByText(container, "Header 1")).toHaveAttribute("aria-controls", contentId);
|
|
45
|
+
|
|
46
|
+
// The active tab element has the state aria-selected set to true and
|
|
47
|
+
// all other tab elements have it set to false
|
|
48
|
+
expect(queryByText(container, "Header 1")).toHaveAttribute("aria-selected", "true");
|
|
49
|
+
expect(queryByText(container, "Header 2")).toHaveAttribute("aria-selected", "false");
|
|
50
|
+
expect(queryByText(container, "Header 3")).toHaveAttribute("aria-selected", "false");
|
|
51
|
+
|
|
52
|
+
// Each element with role tabpanel has the property aria-labelledby
|
|
53
|
+
// referring to its associated tab element
|
|
54
|
+
expect(queryByText(container, "Body 1")).toHaveAttribute("aria-labelledby", tabId);
|
|
55
|
+
|
|
56
|
+
// If a tab element has a popup menu, it has the property aria-haspopup
|
|
57
|
+
// set to either menu or true
|
|
58
|
+
|
|
59
|
+
// If the tablist element is vertically oriented, it has the property
|
|
60
|
+
// aria-orientation set to vertical. The default value of
|
|
61
|
+
// aria-orientation for a tablist element is horizontal
|
|
62
|
+
expect(queryByText(container, "Header 1")?.parentElement?.parentElement).toHaveAttribute(
|
|
63
|
+
"aria-orientation",
|
|
64
|
+
"horizontal",
|
|
65
|
+
);
|
|
66
|
+
});
|
|
67
|
+
});
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import {
|
|
2
|
+
TabGroup,
|
|
3
|
+
TabList,
|
|
4
|
+
TabHeader,
|
|
5
|
+
TabTrigger,
|
|
6
|
+
TabContent,
|
|
7
|
+
} from "../../../src/TabGroup/index";
|
|
8
|
+
|
|
9
|
+
export default function TabGroupAccessibility() {
|
|
10
|
+
@render {
|
|
11
|
+
<TabGroup value={$props.value}>
|
|
12
|
+
<TabList ariaLabel="Example tabs">
|
|
13
|
+
<TabHeader as="h4">
|
|
14
|
+
<TabTrigger>Header 1</TabTrigger>
|
|
15
|
+
</TabHeader>
|
|
16
|
+
<TabHeader as="h4">
|
|
17
|
+
<TabTrigger>Header 2</TabTrigger>
|
|
18
|
+
</TabHeader>
|
|
19
|
+
<TabHeader as="h4">
|
|
20
|
+
<TabTrigger>Header 3</TabTrigger>
|
|
21
|
+
</TabHeader>
|
|
22
|
+
</TabList>
|
|
23
|
+
<TabContent>Body 1</TabContent>
|
|
24
|
+
<TabContent>Body 2</TabContent>
|
|
25
|
+
<TabContent>Body 3</TabContent>
|
|
26
|
+
</TabGroup>
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import {
|
|
2
|
+
TabGroup,
|
|
3
|
+
TabList,
|
|
4
|
+
TabHeader,
|
|
5
|
+
TabTrigger,
|
|
6
|
+
TabContent,
|
|
7
|
+
} from "../../../src/TabGroup/index";
|
|
8
|
+
|
|
9
|
+
export default function TabGroupKeyboard() {
|
|
10
|
+
@render {
|
|
11
|
+
<TabGroup value={$props.value}>
|
|
12
|
+
<TabList>
|
|
13
|
+
<TabHeader as="h4">
|
|
14
|
+
<TabTrigger>Header 1</TabTrigger>
|
|
15
|
+
</TabHeader>
|
|
16
|
+
<TabHeader as="h4">
|
|
17
|
+
<TabTrigger>Header 2</TabTrigger>
|
|
18
|
+
</TabHeader>
|
|
19
|
+
<TabHeader as="h4">
|
|
20
|
+
<TabTrigger>Header 3</TabTrigger>
|
|
21
|
+
</TabHeader>
|
|
22
|
+
</TabList>
|
|
23
|
+
<TabContent>Content 1</TabContent>
|
|
24
|
+
<TabContent>Content 2</TabContent>
|
|
25
|
+
<TabContent>Content 3</TabContent>
|
|
26
|
+
</TabGroup>
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import {
|
|
2
|
+
TabGroup,
|
|
3
|
+
TabList,
|
|
4
|
+
TabHeader,
|
|
5
|
+
TabTrigger,
|
|
6
|
+
TabContent,
|
|
7
|
+
} from "../../../src/TabGroup/index";
|
|
8
|
+
|
|
9
|
+
export default function TabGroupList() {
|
|
10
|
+
@render {
|
|
11
|
+
<TabGroup value={$props.value}>
|
|
12
|
+
<TabList>
|
|
13
|
+
@for (let item of $props.items) {
|
|
14
|
+
@key = item.id
|
|
15
|
+
<TabHeader as="h4">
|
|
16
|
+
<TabTrigger>{item.header}</TabTrigger>
|
|
17
|
+
</TabHeader>
|
|
18
|
+
}
|
|
19
|
+
</TabList>
|
|
20
|
+
@for (let item of $props.items) {
|
|
21
|
+
@key = item.id
|
|
22
|
+
<TabContent>{item.text}</TabContent>
|
|
23
|
+
}
|
|
24
|
+
</TabGroup>
|
|
25
|
+
}
|
|
26
|
+
}
|