@torpor/ui 0.0.16 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +38 -0
- package/dist/Accordion/Accordion.torp +53 -54
- package/dist/Accordion/AccordionContent.torp +5 -5
- package/dist/Accordion/AccordionHeader.torp +5 -4
- package/dist/Accordion/AccordionItem.torp +13 -8
- package/dist/Accordion/AccordionTrigger.torp +5 -5
- package/dist/Accordion/AccordionTypes.ts +1 -0
- package/dist/Dialog/Dialog.torp +156 -0
- package/dist/Dialog/DialogBody.torp +34 -0
- package/dist/Dialog/DialogButton.torp +48 -0
- package/dist/Dialog/DialogFooter.torp +13 -0
- package/dist/Dialog/DialogHeader.torp +34 -0
- package/dist/Dialog/DialogTypes.ts +16 -0
- package/dist/Dialog/index.cjs +53 -0
- package/dist/Dialog/index.cjs.map +1 -0
- package/dist/Dialog/index.js +22 -0
- package/dist/Dialog/index.js.map +1 -0
- package/dist/NavMenu/NavMenu.torp +178 -0
- package/dist/NavMenu/NavMenuButton.torp +46 -0
- package/dist/NavMenu/NavMenuElement.torp +43 -0
- package/dist/NavMenu/NavMenuGroup.torp +30 -0
- package/dist/NavMenu/NavMenuGroupHeader.torp +30 -0
- package/dist/NavMenu/NavMenuLink.torp +52 -0
- package/dist/NavMenu/NavMenuPopout.torp +69 -0
- package/dist/NavMenu/NavMenuPopoutButton.torp +120 -0
- package/dist/NavMenu/NavMenuPopoutContent.torp +79 -0
- package/dist/NavMenu/NavMenuSeparator.torp +14 -0
- package/dist/NavMenu/NavMenuTypes.ts +32 -0
- package/dist/NavMenu/index.cjs +78 -0
- package/dist/NavMenu/index.cjs.map +1 -0
- package/dist/NavMenu/index.js +42 -0
- package/dist/NavMenu/index.js.map +1 -0
- package/dist/Popover/Popover.torp +105 -0
- package/dist/Popover/PopoverContentAnchored.torp +107 -0
- package/dist/Popover/PopoverContentContextual.torp +99 -0
- package/dist/Popover/PopoverContentStatic.torp +78 -0
- package/dist/Popover/PopoverOverlay.torp +32 -0
- package/dist/Popover/PopoverTrigger.torp +29 -0
- package/dist/Popover/PopoverTriggerContextual.torp +37 -0
- package/dist/Popover/PopoverTriggerHover.torp +30 -0
- package/dist/Popover/PopoverTypes.ts +30 -0
- package/dist/Popover/index.cjs +222 -0
- package/dist/Popover/index.cjs.map +1 -0
- package/dist/Popover/index.js +183 -0
- package/dist/Popover/index.js.map +1 -0
- package/dist/Popover/mountClickPopover.ts +50 -0
- package/dist/Popover/mountContextPopover.ts +50 -0
- package/dist/Popover/mountHoverPopover.ts +57 -0
- package/dist/Popover/showPopover.ts +24 -0
- package/dist/Popover/showPopoverPrompt.ts +23 -0
- package/dist/TabGroup/TabContent.torp +36 -0
- package/dist/TabGroup/TabGroup.torp +225 -0
- package/dist/TabGroup/TabGroupTypes.ts +39 -0
- package/dist/TabGroup/TabHeader.torp +62 -0
- package/dist/TabGroup/TabList.torp +50 -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/dist/mount/disableScroll.ts +28 -0
- package/dist/mount/hoverTrigger.ts +59 -0
- package/dist/mount/trapFocus.ts +58 -0
- package/dist/utils/documentEvents.ts +17 -0
- package/dist/utils/getScrollParent.ts +22 -0
- package/dist/utils/setContextPosition.ts +31 -0
- package/dist/utils/setPopoverPosition.ts +138 -0
- package/package.json +26 -6
- package/scripts/postbuild.ts +28 -10
- package/src/Accordion/Accordion.torp +53 -54
- package/src/Accordion/AccordionContent.torp +5 -5
- package/src/Accordion/AccordionHeader.torp +5 -4
- package/src/Accordion/AccordionItem.torp +13 -8
- package/src/Accordion/AccordionTrigger.torp +5 -5
- package/src/Accordion/AccordionTypes.ts +1 -0
- package/src/Dialog/Dialog.torp +156 -0
- package/src/Dialog/DialogBody.torp +34 -0
- package/src/Dialog/DialogButton.torp +48 -0
- package/src/Dialog/DialogFooter.torp +13 -0
- package/src/Dialog/DialogHeader.torp +34 -0
- package/src/Dialog/DialogTypes.ts +16 -0
- package/src/Dialog/index.ts +7 -0
- package/src/Dialog.ts +7 -0
- package/src/NavMenu/NavMenu.torp +178 -0
- package/src/NavMenu/NavMenuButton.torp +46 -0
- package/src/NavMenu/NavMenuElement.torp +43 -0
- package/src/NavMenu/NavMenuGroup.torp +30 -0
- package/src/NavMenu/NavMenuGroupHeader.torp +30 -0
- package/src/NavMenu/NavMenuLink.torp +52 -0
- package/src/NavMenu/NavMenuPopout.torp +69 -0
- package/src/NavMenu/NavMenuPopoutButton.torp +120 -0
- package/src/NavMenu/NavMenuPopoutContent.torp +79 -0
- package/src/NavMenu/NavMenuSeparator.torp +14 -0
- package/src/NavMenu/NavMenuTypes.ts +32 -0
- package/src/NavMenu/index.ts +23 -0
- package/src/NavMenu.ts +23 -0
- package/src/Popover/Popover.torp +105 -0
- package/src/Popover/PopoverContentAnchored.torp +107 -0
- package/src/Popover/PopoverContentContextual.torp +99 -0
- package/src/Popover/PopoverContentStatic.torp +78 -0
- package/src/Popover/PopoverOverlay.torp +32 -0
- package/src/Popover/PopoverTrigger.torp +29 -0
- package/src/Popover/PopoverTriggerContextual.torp +37 -0
- package/src/Popover/PopoverTriggerHover.torp +30 -0
- package/src/Popover/PopoverTypes.ts +30 -0
- package/src/Popover/index.ts +29 -0
- package/src/Popover/mountClickPopover.ts +50 -0
- package/src/Popover/mountContextPopover.ts +50 -0
- package/src/Popover/mountHoverPopover.ts +57 -0
- package/src/Popover/showPopover.ts +24 -0
- package/src/Popover/showPopoverPrompt.ts +23 -0
- package/src/Popover.ts +29 -0
- package/src/TabGroup/TabContent.torp +36 -0
- package/src/TabGroup/TabGroup.torp +225 -0
- package/src/TabGroup/TabGroupTypes.ts +39 -0
- package/src/TabGroup/TabHeader.torp +62 -0
- package/src/TabGroup/TabList.torp +50 -0
- package/src/TabGroup/TabTrigger.torp +65 -0
- package/src/TabGroup/index.ts +7 -0
- package/src/TabGroup.ts +7 -0
- package/src/mount/disableScroll.ts +28 -0
- package/src/mount/hoverTrigger.ts +59 -0
- package/src/mount/trapFocus.ts +58 -0
- package/src/utils/documentEvents.ts +17 -0
- package/src/utils/getScrollParent.ts +22 -0
- package/src/utils/setContextPosition.ts +31 -0
- package/src/utils/setPopoverPosition.ts +138 -0
- package/test/Accordion/accessibility.test.ts +0 -3
- package/test/Accordion/for-items.test.ts +2 -4
- package/test/Accordion/keyboard.test.ts +0 -3
- package/test/Accordion/multiple-item-toggle.test.ts +3 -5
- package/test/Accordion/single-item-toggle.test.ts +3 -5
- package/test/Dialog/accessibility-alert.test.ts +40 -0
- package/test/Dialog/accessibility-modal.test.ts +52 -0
- package/test/Dialog/components/CustomDialog.torp +20 -0
- package/test/Dialog/components/DialogAccessibility.torp +31 -0
- package/test/Dialog/components/DialogFunction.torp +22 -0
- package/test/Dialog/components/DialogVariable.torp +35 -0
- package/test/Dialog/function.test.ts +26 -0
- package/test/Dialog/variable.test.ts +27 -0
- package/test/NavMenu/accessibility.test.ts +97 -0
- package/test/NavMenu/basic.test.ts +22 -0
- package/test/NavMenu/components/NavMenu.torp +28 -0
- package/test/NavMenu/components/NavMenuAccessibility.torp +35 -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 +52 -0
- package/test/TabGroup/item-trigger.test.ts +32 -0
- package/test/TabGroup/item-value.test.ts +17 -0
- package/test/TabGroup/keyboard.test.ts +72 -0
- package/tsup.config.ts +8 -1
|
@@ -0,0 +1,225 @@
|
|
|
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
|
+
let itemStates: ItemState[] = [];
|
|
34
|
+
|
|
35
|
+
// This is used to assign automatic indexes to items where needed
|
|
36
|
+
let nextHeaderIndex = 0;
|
|
37
|
+
let nextItemIndex = 0;
|
|
38
|
+
|
|
39
|
+
// Set the context to pass down to headers and content, with functions for each item to call
|
|
40
|
+
let context: TabGroupContext = {
|
|
41
|
+
registerHeader,
|
|
42
|
+
registerItem,
|
|
43
|
+
removeItem,
|
|
44
|
+
triggerItem,
|
|
45
|
+
handleHeaderKey,
|
|
46
|
+
orientation: $props.orientation,
|
|
47
|
+
activation: $props.activation,
|
|
48
|
+
};
|
|
49
|
+
$context[TabGroupContextName] = context;
|
|
50
|
+
$run(() => {
|
|
51
|
+
// HACK: This should probably be done with setters?
|
|
52
|
+
let newValue = $props.value;
|
|
53
|
+
|
|
54
|
+
// Ensure the value passed in was a string (in case e.g. the user has set values to ints)
|
|
55
|
+
newValue = newValue?.toString();
|
|
56
|
+
for (let item of itemStates) {
|
|
57
|
+
item.active = item.value === newValue;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Set the value
|
|
61
|
+
$props.value = newValue;
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
$run(() => {
|
|
65
|
+
itemStates.forEach((s) => s.parentDisabled = $props.disabled);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
// Dispatch the change event when the value changes
|
|
69
|
+
let firstChange = true;
|
|
70
|
+
$run(() => {
|
|
71
|
+
const ev = $props.onchange;
|
|
72
|
+
const value = $props.value;
|
|
73
|
+
if (firstChange) {
|
|
74
|
+
firstChange = false;
|
|
75
|
+
} else if (ev) {
|
|
76
|
+
ev(value);
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
// TODO:
|
|
81
|
+
//$mount(() => {
|
|
82
|
+
// if ($props.value === "" && itemStates.length) {
|
|
83
|
+
// $props.value = itemStates[0].value;
|
|
84
|
+
// }
|
|
85
|
+
//})
|
|
86
|
+
|
|
87
|
+
function registerHeader(index: number | undefined, value: string | undefined, setFocused: () => void): ItemState {
|
|
88
|
+
// If an index wasn't set for the header, set it automatically
|
|
89
|
+
if (!index) {
|
|
90
|
+
index = nextHeaderIndex++;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Get or add the ItemState at this index
|
|
94
|
+
const itemState = getItemState(index, value);
|
|
95
|
+
|
|
96
|
+
itemState.setFocused = setFocused;
|
|
97
|
+
|
|
98
|
+
return itemState;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function registerItem(index: number | undefined, value: string | undefined): ItemState {
|
|
102
|
+
// If an index wasn't set for the item, set it automatically
|
|
103
|
+
if (!index) {
|
|
104
|
+
index = nextItemIndex++;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Get or add the ItemState at this index
|
|
108
|
+
const itemState = getItemState(index, value);
|
|
109
|
+
|
|
110
|
+
return itemState;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function getItemState(index: number, value: string | undefined): ItemState {
|
|
114
|
+
// If we already have an item at this index, just return it
|
|
115
|
+
if (itemStates.length > index) {
|
|
116
|
+
return itemStates[index];
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// If a value wasn't set for the item, set it to the index
|
|
120
|
+
if (!value) {
|
|
121
|
+
value = index.toString();
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Add the item's state to the array
|
|
125
|
+
const newItemState: ItemState = $watch({
|
|
126
|
+
index,
|
|
127
|
+
value,
|
|
128
|
+
active: value === $props.value,
|
|
129
|
+
headerId: "",
|
|
130
|
+
contentId: "",
|
|
131
|
+
disabled: false,
|
|
132
|
+
parentDisabled: false,
|
|
133
|
+
get dataState() {
|
|
134
|
+
return this.active ? "active" : "inactive";
|
|
135
|
+
},
|
|
136
|
+
setFocused: () => {},
|
|
137
|
+
orientation: $props.orientation,
|
|
138
|
+
activation: $props.activation
|
|
139
|
+
});
|
|
140
|
+
itemStates.splice(index, 0, newItemState);
|
|
141
|
+
|
|
142
|
+
return newItemState;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// TODO: I think this should be done by value
|
|
146
|
+
function removeItem(itemIndex: number) {
|
|
147
|
+
// Remove the item and re-index
|
|
148
|
+
let index = itemStates.findIndex((item) => item.index === itemIndex);
|
|
149
|
+
if (index !== -1) {
|
|
150
|
+
itemStates.splice(itemIndex, 1);
|
|
151
|
+
}
|
|
152
|
+
itemStates.forEach((item, i) => (item.index = i));
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function triggerItem(toggleValue: string) {
|
|
156
|
+
for (let item of itemStates) {
|
|
157
|
+
// The item is active if it has the supplied value
|
|
158
|
+
if (item.value === toggleValue) {
|
|
159
|
+
$props.value = item.value;
|
|
160
|
+
break;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function handleHeaderKey(headerIndex: number, e: KeyboardEvent) {
|
|
166
|
+
switch (e.key) {
|
|
167
|
+
case "Left":
|
|
168
|
+
case "ArrowLeft": {
|
|
169
|
+
if ($props.orientation === "horizontal" && headerIndex > 0) {
|
|
170
|
+
e.preventDefault();
|
|
171
|
+
itemStates[headerIndex - 1].setFocused();
|
|
172
|
+
}
|
|
173
|
+
break;
|
|
174
|
+
}
|
|
175
|
+
case "Up":
|
|
176
|
+
case "ArrowUp": {
|
|
177
|
+
if ($props.orientation === "vertical" && headerIndex > 0) {
|
|
178
|
+
e.preventDefault();
|
|
179
|
+
itemStates[headerIndex - 1].setFocused();
|
|
180
|
+
}
|
|
181
|
+
break;
|
|
182
|
+
}
|
|
183
|
+
case "Right":
|
|
184
|
+
case "ArrowRight": {
|
|
185
|
+
if ($props.orientation === "horizontal" && headerIndex < itemStates.length - 1) {
|
|
186
|
+
e.preventDefault();
|
|
187
|
+
itemStates[headerIndex + 1].setFocused();
|
|
188
|
+
}
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
case "Down":
|
|
192
|
+
case "ArrowDown": {
|
|
193
|
+
if ($props.orientation === "vertical" && headerIndex < itemStates.length - 1) {
|
|
194
|
+
e.preventDefault();
|
|
195
|
+
itemStates[headerIndex + 1].setFocused();
|
|
196
|
+
}
|
|
197
|
+
break;
|
|
198
|
+
}
|
|
199
|
+
case "Home": {
|
|
200
|
+
if (headerIndex > 0) {
|
|
201
|
+
e.preventDefault();
|
|
202
|
+
itemStates[0].setFocused();
|
|
203
|
+
}
|
|
204
|
+
break;
|
|
205
|
+
}
|
|
206
|
+
case "End": {
|
|
207
|
+
if (headerIndex < itemStates.length - 1) {
|
|
208
|
+
e.preventDefault();
|
|
209
|
+
itemStates[itemStates.length - 1].setFocused();
|
|
210
|
+
}
|
|
211
|
+
break;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
@render {
|
|
217
|
+
<div
|
|
218
|
+
id={$props.id}
|
|
219
|
+
class={`torp-tab-group ${$props.class ?? ""}`.trim()}
|
|
220
|
+
data-orientation={$props.orientation}
|
|
221
|
+
>
|
|
222
|
+
<:slot />
|
|
223
|
+
</div>
|
|
224
|
+
}
|
|
225
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
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
|
+
dataState: "active" | "inactive";
|
|
37
|
+
/** Called in the TabGroup when a header needs to be focused */
|
|
38
|
+
setFocused: () => void;
|
|
39
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
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 a 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
|
+
id={$props.id}
|
|
41
|
+
:class={[
|
|
42
|
+
"torp-tab-header",
|
|
43
|
+
$props.class,
|
|
44
|
+
$state.orientation
|
|
45
|
+
]}
|
|
46
|
+
data-state={$state.dataState}
|
|
47
|
+
data-disabled={$state.dataDisabled}
|
|
48
|
+
>
|
|
49
|
+
<:slot />
|
|
50
|
+
</h4>
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
@style {
|
|
54
|
+
.torp-tab-header.horizontal {
|
|
55
|
+
display: inline-block;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
.torp-tab-header.vertical {
|
|
59
|
+
display: block;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
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 a 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
|
+
id={$props.id}
|
|
39
|
+
class={`torp-tab-list ${$props.class ?? ""}`.trim()}
|
|
40
|
+
role="tablist"
|
|
41
|
+
aria-label={$props.ariaLabel}
|
|
42
|
+
aria-orientation={orientation}
|
|
43
|
+
data-orientation={orientation}
|
|
44
|
+
tabindex="0"
|
|
45
|
+
onfocus={handleFocus}
|
|
46
|
+
>
|
|
47
|
+
<:slot />
|
|
48
|
+
</div>
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -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 a TabHeader");
|
|
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 ${$props.class ?? ""}`.trim()}
|
|
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
|
+
&ref={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,28 @@
|
|
|
1
|
+
export function disableScroll(): () => void {
|
|
2
|
+
// Remember body styles
|
|
3
|
+
const overflowX = document.body.style.overflowY;
|
|
4
|
+
const overflowY = document.body.style.overflowY;
|
|
5
|
+
const overscrollBehaviourX = document.body.style.overscrollBehaviorX;
|
|
6
|
+
const overscrollBehaviourY = document.body.style.overscrollBehaviorY;
|
|
7
|
+
const marginRight = document.body.style.marginRight;
|
|
8
|
+
|
|
9
|
+
// Get scrollbar width (if visible) so that we can set a margin to avoid things jumping around
|
|
10
|
+
// HACK: Does this work in all browsers/systems?
|
|
11
|
+
const scrollBarWidth = window.innerWidth - document.body.clientWidth;
|
|
12
|
+
|
|
13
|
+
// Set body styles to disallow scrolling
|
|
14
|
+
document.body.style.overflowY = "hidden";
|
|
15
|
+
document.body.style.overflowY = "hidden";
|
|
16
|
+
document.body.style.overscrollBehaviorX = "contain";
|
|
17
|
+
document.body.style.overscrollBehaviorY = "contain";
|
|
18
|
+
document.body.style.marginRight = scrollBarWidth + "px";
|
|
19
|
+
|
|
20
|
+
return () => {
|
|
21
|
+
// Reset body styles
|
|
22
|
+
document.body.style.overflowY = overflowX;
|
|
23
|
+
document.body.style.overflowY = overflowY;
|
|
24
|
+
document.body.style.overscrollBehaviorX = overscrollBehaviourX;
|
|
25
|
+
document.body.style.overscrollBehaviorY = overscrollBehaviourY;
|
|
26
|
+
document.body.style.marginRight = marginRight;
|
|
27
|
+
};
|
|
28
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
interface HoverTriggerOptions {
|
|
2
|
+
onHover: (node: HTMLElement, e: TouchEvent | MouseEvent) => void;
|
|
3
|
+
onExit?: (node: HTMLElement, e: TouchEvent | MouseEvent) => void;
|
|
4
|
+
hoverDelay?: number;
|
|
5
|
+
touchDelay?: number;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export default function hoverTrigger(node: HTMLElement, options: HoverTriggerOptions): () => void {
|
|
9
|
+
const { onHover, onExit } = options;
|
|
10
|
+
const hoverDelay = options.hoverDelay || 1000;
|
|
11
|
+
const touchDelay = options.touchDelay || 1000;
|
|
12
|
+
|
|
13
|
+
let interval: NodeJS.Timeout | undefined;
|
|
14
|
+
|
|
15
|
+
// Add click event handlers to the node
|
|
16
|
+
node.addEventListener("mouseenter", handleMouseEnter);
|
|
17
|
+
node.addEventListener("mouseleave", handleMouseLeave);
|
|
18
|
+
node.addEventListener("mousemove", handleMouseMove);
|
|
19
|
+
node.addEventListener("touchstart", handleMouseEnter);
|
|
20
|
+
node.addEventListener("touchend", handleMouseLeave);
|
|
21
|
+
|
|
22
|
+
function handleMouseEnter(e: MouseEvent | TouchEvent) {
|
|
23
|
+
// Handle the hover after a delay
|
|
24
|
+
e.preventDefault();
|
|
25
|
+
if (!interval) {
|
|
26
|
+
const delay = e.type.startsWith("mouse") ? hoverDelay : touchDelay;
|
|
27
|
+
interval = setTimeout(() => onHover(node, e), delay);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function handleMouseLeave(e: MouseEvent | TouchEvent) {
|
|
32
|
+
// Prevent short hovers or touches from firing the event
|
|
33
|
+
if (interval) {
|
|
34
|
+
clearTimeout(interval);
|
|
35
|
+
interval = undefined;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (onExit) {
|
|
39
|
+
onExit(node, e);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function handleMouseMove(e: MouseEvent) {
|
|
44
|
+
// Reset the interval on mouse move
|
|
45
|
+
if (interval) {
|
|
46
|
+
clearTimeout(interval);
|
|
47
|
+
interval = setTimeout(() => onHover(node, e), hoverDelay);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Remove the event listeners from the node on destroy
|
|
52
|
+
return () => {
|
|
53
|
+
node.removeEventListener("mouseenter", handleMouseEnter);
|
|
54
|
+
node.removeEventListener("mouseleave", handleMouseLeave);
|
|
55
|
+
node.removeEventListener("mousemove", handleMouseMove);
|
|
56
|
+
node.removeEventListener("touchstart", handleMouseEnter);
|
|
57
|
+
node.removeEventListener("touchend", handleMouseLeave);
|
|
58
|
+
};
|
|
59
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
export interface TrapFocusOptions {
|
|
2
|
+
focusableElements: HTMLElement[];
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export function trapFocus(node: HTMLElement, options?: TrapFocusOptions): () => void {
|
|
6
|
+
options = options || { focusableElements: [] };
|
|
7
|
+
|
|
8
|
+
// Selectors for all things that are focusable:
|
|
9
|
+
const focusableSelectors = [
|
|
10
|
+
"a[href]",
|
|
11
|
+
"area[href]",
|
|
12
|
+
'input:not([disabled]):not([type="hidden"]):not([aria-hidden])',
|
|
13
|
+
"select:not([disabled]):not([aria-hidden])",
|
|
14
|
+
"textarea:not([disabled]):not([aria-hidden])",
|
|
15
|
+
"button:not([disabled]):not([aria-hidden])",
|
|
16
|
+
"iframe",
|
|
17
|
+
"object",
|
|
18
|
+
"embed",
|
|
19
|
+
"[contenteditable]",
|
|
20
|
+
'[tabindex]:not([tabindex^="-"])',
|
|
21
|
+
];
|
|
22
|
+
|
|
23
|
+
// Set the focusable elements array in the options, which can be used in the caller e.g. to focus the first element
|
|
24
|
+
options.focusableElements = Array.from(node.querySelectorAll(focusableSelectors.join()));
|
|
25
|
+
|
|
26
|
+
// Store first and last focusable elements
|
|
27
|
+
const firstFocusableElement = options.focusableElements[0] as HTMLElement;
|
|
28
|
+
const lastFocusableElement = options.focusableElements[
|
|
29
|
+
options.focusableElements.length - 1
|
|
30
|
+
] as HTMLElement;
|
|
31
|
+
|
|
32
|
+
// Add the keydown event listener to the node
|
|
33
|
+
node.addEventListener("keydown", handleKey);
|
|
34
|
+
|
|
35
|
+
// On Tab or Shift+Tab, move through the focusable elements, wrapping at either end
|
|
36
|
+
function handleKey(e: KeyboardEvent) {
|
|
37
|
+
if (e.key === "Tab") {
|
|
38
|
+
if (e.shiftKey) {
|
|
39
|
+
if (document.activeElement === firstFocusableElement) {
|
|
40
|
+
// Shift + Tab on the first element, so move to the last one
|
|
41
|
+
lastFocusableElement.focus();
|
|
42
|
+
e.preventDefault();
|
|
43
|
+
}
|
|
44
|
+
} else {
|
|
45
|
+
if (document.activeElement === lastFocusableElement) {
|
|
46
|
+
// Tab on the last element, so move to the first one
|
|
47
|
+
firstFocusableElement.focus();
|
|
48
|
+
e.preventDefault();
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Remove the keydown event listener from the node on destroy
|
|
55
|
+
return () => {
|
|
56
|
+
node.removeEventListener("keydown", handleKey);
|
|
57
|
+
};
|
|
58
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export function addDocumentEvent<K extends keyof DocumentEventMap>(
|
|
2
|
+
type: K,
|
|
3
|
+
listener: (this: Document, ev: DocumentEventMap[K]) => any
|
|
4
|
+
): void {
|
|
5
|
+
if (typeof document !== 'undefined') {
|
|
6
|
+
document.addEventListener(type, listener);
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function removeDocumentEvent<K extends keyof DocumentEventMap>(
|
|
11
|
+
type: K,
|
|
12
|
+
listener: (this: Document, ev: DocumentEventMap[K]) => any
|
|
13
|
+
): void {
|
|
14
|
+
if (typeof document !== 'undefined') {
|
|
15
|
+
document.removeEventListener(type, listener);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// From https://stackoverflow.com/a/76535387
|
|
2
|
+
export default function getScrollParent(node: Element): Element {
|
|
3
|
+
let currentParent = node.parentElement;
|
|
4
|
+
while (currentParent) {
|
|
5
|
+
if (isScrollable(currentParent)) {
|
|
6
|
+
return currentParent;
|
|
7
|
+
}
|
|
8
|
+
currentParent = currentParent.parentElement;
|
|
9
|
+
}
|
|
10
|
+
return document.scrollingElement || document.documentElement;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function isScrollable(node: Element): boolean {
|
|
14
|
+
if (!(node instanceof HTMLElement || node instanceof SVGElement)) {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
const style = getComputedStyle(node);
|
|
18
|
+
return ["overflow", "overflow-x", "overflow-y"].some((propertyName) => {
|
|
19
|
+
const value = style.getPropertyValue(propertyName);
|
|
20
|
+
return value === "auto" || value === "scroll";
|
|
21
|
+
});
|
|
22
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
interface Point {
|
|
2
|
+
x: number;
|
|
3
|
+
y: number;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export default function setContextPosition(
|
|
7
|
+
anchor: HTMLElement,
|
|
8
|
+
content: HTMLElement,
|
|
9
|
+
position: Point,
|
|
10
|
+
): void {
|
|
11
|
+
var anchorRect = anchor.getBoundingClientRect();
|
|
12
|
+
var contentRect = content.getBoundingClientRect();
|
|
13
|
+
|
|
14
|
+
content.style.position = "fixed";
|
|
15
|
+
|
|
16
|
+
// Try to make sure the content is entirely on the screen horizontally
|
|
17
|
+
// Just move it left or right -- the pointer is outside the content vertically so moving it left
|
|
18
|
+
// or right won't make the content appear over the cursor
|
|
19
|
+
let left = anchorRect.left + position.x;
|
|
20
|
+
left = Math.min(left, window.innerWidth - contentRect.width);
|
|
21
|
+
left = Math.max(left, 0);
|
|
22
|
+
content.style.left = left + "px";
|
|
23
|
+
|
|
24
|
+
// Try to make sure the content is entirely on the screen vertically
|
|
25
|
+
// Show it above the position if it wouldn't fit
|
|
26
|
+
let top = anchorRect.top + position.y;
|
|
27
|
+
if (top + contentRect.height > window.innerHeight) {
|
|
28
|
+
top -= contentRect.height;
|
|
29
|
+
}
|
|
30
|
+
content.style.top = top + "px";
|
|
31
|
+
}
|