@torpor/ui 0.0.17 → 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 +28 -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 +4 -4
- package/dist/TabGroup/TabGroup.torp +58 -59
- package/dist/TabGroup/TabGroupTypes.ts +1 -0
- package/dist/TabGroup/TabHeader.torp +10 -8
- package/dist/TabGroup/TabList.torp +4 -3
- package/dist/TabGroup/TabTrigger.torp +6 -6
- 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 +21 -6
- package/scripts/postbuild.ts +17 -5
- 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 +4 -4
- package/src/TabGroup/TabGroup.torp +58 -59
- package/src/TabGroup/TabGroupTypes.ts +1 -0
- package/src/TabGroup/TabHeader.torp +10 -8
- package/src/TabGroup/TabList.torp +4 -3
- package/src/TabGroup/TabTrigger.torp +6 -6
- 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/for-items.test.ts +2 -4
- package/test/TabGroup/item-trigger.test.ts +3 -5
- package/tsup.config.ts +8 -1
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { Component } from "@torpor/view";
|
|
2
|
+
import showPopover from "./showPopover";
|
|
3
|
+
|
|
4
|
+
interface ClickPopoverOptions {
|
|
5
|
+
popover: Component;
|
|
6
|
+
props?: Record<PropertyKey, any>;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* A mount directive for showing a popover when an element is clicked.
|
|
11
|
+
* @param node The element to show the popover for
|
|
12
|
+
* @param options Options for showing the popover, consisting of the popover component and any props
|
|
13
|
+
*/
|
|
14
|
+
export default function clickPopover(node: HTMLElement, options: ClickPopoverOptions) {
|
|
15
|
+
const { popover, props } = options;
|
|
16
|
+
|
|
17
|
+
// Add a click event handler to the node
|
|
18
|
+
node.addEventListener("click", handleClick);
|
|
19
|
+
|
|
20
|
+
function handleClick(e: MouseEvent) {
|
|
21
|
+
e.preventDefault();
|
|
22
|
+
show();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
let div: HTMLDivElement;
|
|
26
|
+
|
|
27
|
+
function show() {
|
|
28
|
+
// Show the popover in a div
|
|
29
|
+
if (div) {
|
|
30
|
+
document.body.removeChild(div);
|
|
31
|
+
}
|
|
32
|
+
div = document.createElement("div");
|
|
33
|
+
document.body.appendChild(div);
|
|
34
|
+
|
|
35
|
+
showPopover(popover, node, props, div);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return {
|
|
39
|
+
destroy(): void {
|
|
40
|
+
// Remove the event listener from the node on destroy
|
|
41
|
+
node.removeEventListener("click", handleClick);
|
|
42
|
+
|
|
43
|
+
// Remove the div we added
|
|
44
|
+
// TODO: Should we do this in more places?
|
|
45
|
+
if (div) {
|
|
46
|
+
document.body.removeChild(div);
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { Component } from "@torpor/view";
|
|
2
|
+
import showPopover from "./showPopover";
|
|
3
|
+
|
|
4
|
+
interface ContextPopoverOptions {
|
|
5
|
+
popover: Component;
|
|
6
|
+
props?: Record<PropertyKey, any>;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* A mount directive for showing a popover when the user opens an element's context menu.
|
|
11
|
+
* @param node The element to show the popover for
|
|
12
|
+
* @param options Options for showing the popover, consisting of the popover component and any props
|
|
13
|
+
*/
|
|
14
|
+
export default function contextPopover(node: HTMLElement, options: ContextPopoverOptions) {
|
|
15
|
+
const { popover, props } = options;
|
|
16
|
+
|
|
17
|
+
// Add a contextmenu event handler to the node
|
|
18
|
+
node.addEventListener("contextmenu", handleClick);
|
|
19
|
+
|
|
20
|
+
function handleClick(e: MouseEvent) {
|
|
21
|
+
e.preventDefault();
|
|
22
|
+
show();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
let div: HTMLDivElement;
|
|
26
|
+
|
|
27
|
+
function show() {
|
|
28
|
+
// Show the popover in a div
|
|
29
|
+
if (div) {
|
|
30
|
+
document.body.removeChild(div);
|
|
31
|
+
}
|
|
32
|
+
div = document.createElement("div");
|
|
33
|
+
document.body.appendChild(div);
|
|
34
|
+
|
|
35
|
+
showPopover(popover, node, props, div);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return {
|
|
39
|
+
destroy(): void {
|
|
40
|
+
// Remove the event listener from the node on destroy
|
|
41
|
+
node.removeEventListener("contextmenu", handleClick);
|
|
42
|
+
|
|
43
|
+
// Remove the div we added
|
|
44
|
+
// TODO: Should we do this in more places?
|
|
45
|
+
if (div) {
|
|
46
|
+
document.body.removeChild(div);
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { Component } from "@torpor/view";
|
|
2
|
+
import hoverTrigger from "../mount/hoverTrigger";
|
|
3
|
+
import showPopover from "./showPopover";
|
|
4
|
+
|
|
5
|
+
interface HoverPopoverOptions {
|
|
6
|
+
popover: Component;
|
|
7
|
+
props?: Record<PropertyKey, any>;
|
|
8
|
+
hoverDelay?: number;
|
|
9
|
+
touchDelay?: number;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* A mount directive for showing a popover when an element is hovered.
|
|
14
|
+
* @param node The element to show the popover for
|
|
15
|
+
* @param options Options for showing the popover, consisting of the popover component, any props and delays for hover and touch
|
|
16
|
+
*/
|
|
17
|
+
export default function hoverPopover(node: HTMLElement, options: HoverPopoverOptions): () => void {
|
|
18
|
+
const { popover, props, hoverDelay, touchDelay } = options;
|
|
19
|
+
|
|
20
|
+
let div: HTMLDivElement | undefined;
|
|
21
|
+
|
|
22
|
+
function onHover(node: HTMLElement, e: MouseEvent | TouchEvent) {
|
|
23
|
+
// Show the popover in a div
|
|
24
|
+
if (div) {
|
|
25
|
+
document.body.removeChild(div);
|
|
26
|
+
}
|
|
27
|
+
div = document.createElement("div");
|
|
28
|
+
document.body.appendChild(div);
|
|
29
|
+
|
|
30
|
+
// Get the position relative to the element, so we can reposition on scroll
|
|
31
|
+
const rect = node.getBoundingClientRect();
|
|
32
|
+
let position: { x: number; y: number };
|
|
33
|
+
if (e.type.startsWith("mouse")) {
|
|
34
|
+
const me = e as MouseEvent;
|
|
35
|
+
position = { x: me.clientX - rect.left, y: me.clientY - rect.top };
|
|
36
|
+
} else {
|
|
37
|
+
const te = e as TouchEvent;
|
|
38
|
+
position = {
|
|
39
|
+
x: te.touches[0].clientX - rect.left,
|
|
40
|
+
y: te.touches[0].clientY - rect.top,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
const allProps = Object.assign({}, props, { anchor: node, position });
|
|
44
|
+
|
|
45
|
+
showPopover(popover, node, allProps, div);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function onExit() {
|
|
49
|
+
// Remove the div
|
|
50
|
+
if (div) {
|
|
51
|
+
document.body.removeChild(div);
|
|
52
|
+
div = undefined;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return hoverTrigger(node, { onHover, onExit, hoverDelay, touchDelay });
|
|
57
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// TODO: Allow passing in a variable -- so that you can show an inline popover
|
|
2
|
+
import type { Component } from "@torpor/view";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Shows a popover component in code.
|
|
6
|
+
* @param popover The popover component
|
|
7
|
+
* @param anchor The element to use as an anchor for the popover component
|
|
8
|
+
* @param props The properties for the popover component
|
|
9
|
+
* @param target The target element to show the popover in
|
|
10
|
+
* @returns The value selected by the user
|
|
11
|
+
*/
|
|
12
|
+
export default function showPopover(
|
|
13
|
+
popover: Component,
|
|
14
|
+
anchor: HTMLElement,
|
|
15
|
+
props?: Record<PropertyKey, any>,
|
|
16
|
+
target?: Element,
|
|
17
|
+
): void {
|
|
18
|
+
// Add the anchor element to the properties
|
|
19
|
+
const allProps = Object.assign({}, props, { anchor });
|
|
20
|
+
|
|
21
|
+
// Create the popover
|
|
22
|
+
// TODO: This needs to clean up after itself, like the mount methods do
|
|
23
|
+
popover(target || anchor.parentElement || document.body, null, allProps);
|
|
24
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { Component } from "@torpor/view";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Shows a popover component in code, and asynchronously returns the value selected by the user.
|
|
5
|
+
* @param popover The popover component
|
|
6
|
+
* @param props The properties for the popover component
|
|
7
|
+
* @param target The target element to show the popover in
|
|
8
|
+
* @returns The value selected by the user
|
|
9
|
+
*/
|
|
10
|
+
export default function showPopoverPrompt(
|
|
11
|
+
popover: Component,
|
|
12
|
+
props?: Record<PropertyKey, any>,
|
|
13
|
+
target?: Element,
|
|
14
|
+
): Promise<void> {
|
|
15
|
+
return new Promise((resolve) => {
|
|
16
|
+
// Add the promise as the callback
|
|
17
|
+
const allProps = Object.assign({}, props, { callback: resolve });
|
|
18
|
+
|
|
19
|
+
// Create the popover
|
|
20
|
+
// TODO: This needs to clean up after itself, like the use methods do
|
|
21
|
+
popover(target || document.body, null, allProps);
|
|
22
|
+
});
|
|
23
|
+
}
|
package/src/Popover.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import Popover from "./Popover/Popover.torp";
|
|
2
|
+
import PopoverContentAnchored from "./Popover/PopoverContentAnchored.torp";
|
|
3
|
+
import PopoverContentContextual from "./Popover/PopoverContentContextual.torp";
|
|
4
|
+
import PopoverContentStatic from "./Popover/PopoverContentStatic.torp";
|
|
5
|
+
import PopoverOverlay from "./Popover/PopoverOverlay.torp";
|
|
6
|
+
import PopoverTrigger from "./Popover/PopoverTrigger.torp";
|
|
7
|
+
import PopoverTriggerContextual from "./Popover/PopoverTriggerContextual.torp";
|
|
8
|
+
import PopoverTriggerHover from "./Popover/PopoverTriggerHover.torp";
|
|
9
|
+
import clickPopover from "./Popover/mountClickPopover";
|
|
10
|
+
import contextPopover from "./Popover/mountContextPopover";
|
|
11
|
+
import hoverPopover from "./Popover/mountHoverPopover";
|
|
12
|
+
import showPopover from "./Popover/showPopover";
|
|
13
|
+
import showPopoverPrompt from "./Popover/showPopoverPrompt";
|
|
14
|
+
|
|
15
|
+
export {
|
|
16
|
+
Popover,
|
|
17
|
+
PopoverOverlay,
|
|
18
|
+
PopoverTrigger,
|
|
19
|
+
PopoverTriggerHover,
|
|
20
|
+
PopoverTriggerContextual,
|
|
21
|
+
PopoverContentAnchored,
|
|
22
|
+
PopoverContentContextual,
|
|
23
|
+
PopoverContentStatic,
|
|
24
|
+
showPopover,
|
|
25
|
+
showPopoverPrompt,
|
|
26
|
+
clickPopover,
|
|
27
|
+
contextPopover,
|
|
28
|
+
hoverPopover,
|
|
29
|
+
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import getId from
|
|
2
|
-
import { TabGroupContextName, type TabGroupContext } from
|
|
1
|
+
import getId from "../utils/getId";
|
|
2
|
+
import { TabGroupContextName, type TabGroupContext } from "./TabGroupTypes";
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Content for a TabGroup, which can be revealed or hidden by an associated TabTrigger.
|
|
@@ -11,7 +11,7 @@ export default function TabContent($props: any) {
|
|
|
11
11
|
|
|
12
12
|
const context = $context[TabGroupContextName] as TabGroupContext;
|
|
13
13
|
if (!context) {
|
|
14
|
-
throw new Error(
|
|
14
|
+
throw new Error("TabContent must be contained within a TabGroup");
|
|
15
15
|
}
|
|
16
16
|
let $state = $context[TabGroupContextName].registerItem($props.index, $props.value);
|
|
17
17
|
|
|
@@ -21,7 +21,7 @@ export default function TabContent($props: any) {
|
|
|
21
21
|
@render {
|
|
22
22
|
<div
|
|
23
23
|
id={$props.id}
|
|
24
|
-
class=
|
|
24
|
+
class={`torp-tab-content ${$props.class ?? ""}`.trim()}
|
|
25
25
|
role="tabpanel"
|
|
26
26
|
aria-labelledby={$state.headerId}
|
|
27
27
|
data-orientation={$props.orientation}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { TabGroupContextName, type TabGroupContext, type ItemState } from
|
|
1
|
+
import { TabGroupContextName, type TabGroupContext, type ItemState } from "./TabGroupTypes";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* A list of interactive headings coupled with layered sections of content that
|
|
@@ -19,26 +19,18 @@ import { TabGroupContextName, type TabGroupContext, type ItemState } from './Tab
|
|
|
19
19
|
* </TabGroup>
|
|
20
20
|
* ```
|
|
21
21
|
*
|
|
22
|
-
* @prop {
|
|
23
|
-
* @prop {
|
|
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
24
|
* @prop {string | string[]} value -- The value of the active TabContent
|
|
25
25
|
* @prop {boolean} disabled -- If set to true, the user cannot interact with any items in the TabGroup
|
|
26
26
|
* @prop {(value: string | string[]) => void} [onchange] -- Raised when the active item has changed
|
|
27
27
|
*/
|
|
28
28
|
export default function TabGroup($props: any) {
|
|
29
|
-
$props.orientation ||=
|
|
30
|
-
$props.activation ||=
|
|
31
|
-
$props.value ||=
|
|
29
|
+
$props.orientation ||= "horizontal";
|
|
30
|
+
$props.activation ||= "automatic";
|
|
31
|
+
$props.value ||= "";
|
|
32
32
|
|
|
33
|
-
|
|
34
|
-
value: string;
|
|
35
|
-
itemStates: ItemState[];
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
let $state: State = $watch({
|
|
39
|
-
value: $props.value,
|
|
40
|
-
itemStates: []
|
|
41
|
-
})
|
|
33
|
+
let itemStates: ItemState[] = [];
|
|
42
34
|
|
|
43
35
|
// This is used to assign automatic indexes to items where needed
|
|
44
36
|
let nextHeaderIndex = 0;
|
|
@@ -55,37 +47,40 @@ export default function TabGroup($props: any) {
|
|
|
55
47
|
activation: $props.activation,
|
|
56
48
|
};
|
|
57
49
|
$context[TabGroupContextName] = context;
|
|
58
|
-
|
|
59
50
|
$run(() => {
|
|
60
51
|
// HACK: This should probably be done with setters?
|
|
61
|
-
let newValue = $
|
|
52
|
+
let newValue = $props.value;
|
|
62
53
|
|
|
63
54
|
// Ensure the value passed in was a string (in case e.g. the user has set values to ints)
|
|
64
55
|
newValue = newValue?.toString();
|
|
65
|
-
for (let item of
|
|
56
|
+
for (let item of itemStates) {
|
|
66
57
|
item.active = item.value === newValue;
|
|
67
58
|
}
|
|
68
59
|
|
|
69
60
|
// Set the value
|
|
70
|
-
$
|
|
61
|
+
$props.value = newValue;
|
|
71
62
|
});
|
|
72
63
|
|
|
73
64
|
$run(() => {
|
|
74
|
-
|
|
65
|
+
itemStates.forEach((s) => s.parentDisabled = $props.disabled);
|
|
75
66
|
});
|
|
76
67
|
|
|
77
68
|
// Dispatch the change event when the value changes
|
|
78
|
-
|
|
69
|
+
let firstChange = true;
|
|
79
70
|
$run(() => {
|
|
80
|
-
|
|
81
|
-
|
|
71
|
+
const ev = $props.onchange;
|
|
72
|
+
const value = $props.value;
|
|
73
|
+
if (firstChange) {
|
|
74
|
+
firstChange = false;
|
|
75
|
+
} else if (ev) {
|
|
76
|
+
ev(value);
|
|
82
77
|
}
|
|
83
|
-
})
|
|
78
|
+
});
|
|
84
79
|
|
|
85
80
|
// TODO:
|
|
86
81
|
//$mount(() => {
|
|
87
|
-
// if ($
|
|
88
|
-
// $
|
|
82
|
+
// if ($props.value === "" && itemStates.length) {
|
|
83
|
+
// $props.value = itemStates[0].value;
|
|
89
84
|
// }
|
|
90
85
|
//})
|
|
91
86
|
|
|
@@ -117,8 +112,8 @@ export default function TabGroup($props: any) {
|
|
|
117
112
|
|
|
118
113
|
function getItemState(index: number, value: string | undefined): ItemState {
|
|
119
114
|
// If we already have an item at this index, just return it
|
|
120
|
-
if (
|
|
121
|
-
return
|
|
115
|
+
if (itemStates.length > index) {
|
|
116
|
+
return itemStates[index];
|
|
122
117
|
}
|
|
123
118
|
|
|
124
119
|
// If a value wasn't set for the item, set it to the index
|
|
@@ -130,16 +125,19 @@ export default function TabGroup($props: any) {
|
|
|
130
125
|
const newItemState: ItemState = $watch({
|
|
131
126
|
index,
|
|
132
127
|
value,
|
|
133
|
-
active: value === $
|
|
134
|
-
headerId:
|
|
135
|
-
contentId:
|
|
128
|
+
active: value === $props.value,
|
|
129
|
+
headerId: "",
|
|
130
|
+
contentId: "",
|
|
136
131
|
disabled: false,
|
|
137
132
|
parentDisabled: false,
|
|
133
|
+
get dataState() {
|
|
134
|
+
return this.active ? "active" : "inactive";
|
|
135
|
+
},
|
|
138
136
|
setFocused: () => {},
|
|
139
137
|
orientation: $props.orientation,
|
|
140
138
|
activation: $props.activation
|
|
141
139
|
});
|
|
142
|
-
|
|
140
|
+
itemStates.splice(index, 0, newItemState);
|
|
143
141
|
|
|
144
142
|
return newItemState;
|
|
145
143
|
}
|
|
@@ -147,18 +145,18 @@ export default function TabGroup($props: any) {
|
|
|
147
145
|
// TODO: I think this should be done by value
|
|
148
146
|
function removeItem(itemIndex: number) {
|
|
149
147
|
// Remove the item and re-index
|
|
150
|
-
let index =
|
|
148
|
+
let index = itemStates.findIndex((item) => item.index === itemIndex);
|
|
151
149
|
if (index !== -1) {
|
|
152
|
-
|
|
150
|
+
itemStates.splice(itemIndex, 1);
|
|
153
151
|
}
|
|
154
|
-
|
|
152
|
+
itemStates.forEach((item, i) => (item.index = i));
|
|
155
153
|
}
|
|
156
154
|
|
|
157
155
|
function triggerItem(toggleValue: string) {
|
|
158
|
-
for (let item of
|
|
156
|
+
for (let item of itemStates) {
|
|
159
157
|
// The item is active if it has the supplied value
|
|
160
158
|
if (item.value === toggleValue) {
|
|
161
|
-
$
|
|
159
|
+
$props.value = item.value;
|
|
162
160
|
break;
|
|
163
161
|
}
|
|
164
162
|
}
|
|
@@ -166,49 +164,49 @@ export default function TabGroup($props: any) {
|
|
|
166
164
|
|
|
167
165
|
function handleHeaderKey(headerIndex: number, e: KeyboardEvent) {
|
|
168
166
|
switch (e.key) {
|
|
169
|
-
case
|
|
170
|
-
case
|
|
171
|
-
if ($props.orientation ===
|
|
167
|
+
case "Left":
|
|
168
|
+
case "ArrowLeft": {
|
|
169
|
+
if ($props.orientation === "horizontal" && headerIndex > 0) {
|
|
172
170
|
e.preventDefault();
|
|
173
|
-
|
|
171
|
+
itemStates[headerIndex - 1].setFocused();
|
|
174
172
|
}
|
|
175
173
|
break;
|
|
176
174
|
}
|
|
177
|
-
case
|
|
178
|
-
case
|
|
179
|
-
if ($props.orientation ===
|
|
175
|
+
case "Up":
|
|
176
|
+
case "ArrowUp": {
|
|
177
|
+
if ($props.orientation === "vertical" && headerIndex > 0) {
|
|
180
178
|
e.preventDefault();
|
|
181
|
-
|
|
179
|
+
itemStates[headerIndex - 1].setFocused();
|
|
182
180
|
}
|
|
183
181
|
break;
|
|
184
182
|
}
|
|
185
|
-
case
|
|
186
|
-
case
|
|
187
|
-
if ($props.orientation ===
|
|
183
|
+
case "Right":
|
|
184
|
+
case "ArrowRight": {
|
|
185
|
+
if ($props.orientation === "horizontal" && headerIndex < itemStates.length - 1) {
|
|
188
186
|
e.preventDefault();
|
|
189
|
-
|
|
187
|
+
itemStates[headerIndex + 1].setFocused();
|
|
190
188
|
}
|
|
191
189
|
break;
|
|
192
190
|
}
|
|
193
|
-
case
|
|
194
|
-
case
|
|
195
|
-
if ($props.orientation ===
|
|
191
|
+
case "Down":
|
|
192
|
+
case "ArrowDown": {
|
|
193
|
+
if ($props.orientation === "vertical" && headerIndex < itemStates.length - 1) {
|
|
196
194
|
e.preventDefault();
|
|
197
|
-
|
|
195
|
+
itemStates[headerIndex + 1].setFocused();
|
|
198
196
|
}
|
|
199
197
|
break;
|
|
200
198
|
}
|
|
201
|
-
case
|
|
199
|
+
case "Home": {
|
|
202
200
|
if (headerIndex > 0) {
|
|
203
201
|
e.preventDefault();
|
|
204
|
-
|
|
202
|
+
itemStates[0].setFocused();
|
|
205
203
|
}
|
|
206
204
|
break;
|
|
207
205
|
}
|
|
208
|
-
case
|
|
209
|
-
if (headerIndex <
|
|
206
|
+
case "End": {
|
|
207
|
+
if (headerIndex < itemStates.length - 1) {
|
|
210
208
|
e.preventDefault();
|
|
211
|
-
|
|
209
|
+
itemStates[itemStates.length - 1].setFocused();
|
|
212
210
|
}
|
|
213
211
|
break;
|
|
214
212
|
}
|
|
@@ -217,7 +215,8 @@ export default function TabGroup($props: any) {
|
|
|
217
215
|
|
|
218
216
|
@render {
|
|
219
217
|
<div
|
|
220
|
-
|
|
218
|
+
id={$props.id}
|
|
219
|
+
class={`torp-tab-group ${$props.class ?? ""}`.trim()}
|
|
221
220
|
data-orientation={$props.orientation}
|
|
222
221
|
>
|
|
223
222
|
<:slot />
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { TabGroupContextName, TabItemContextName, type TabGroupContext, type TabItemContext } from
|
|
1
|
+
import { TabGroupContextName, TabItemContextName, type TabGroupContext, type TabItemContext } from "./TabGroupTypes";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* The header for a tab item, which contains the TabTrigger.
|
|
5
5
|
*
|
|
6
|
-
* The structure of
|
|
6
|
+
* The structure of a TabHeader is:
|
|
7
7
|
*
|
|
8
8
|
* ```
|
|
9
9
|
* <TabHeader>
|
|
@@ -11,12 +11,12 @@ import { TabGroupContextName, TabItemContextName, type TabGroupContext, type Tab
|
|
|
11
11
|
* </TabHeader>
|
|
12
12
|
* ```
|
|
13
13
|
*
|
|
14
|
-
* @prop {
|
|
14
|
+
* @prop {"h1" | "h2" | "h3" | "h4" | "h5" | "h6"} as -- The type of HTML header to use
|
|
15
15
|
*/
|
|
16
16
|
export default function TabHeader($props: any) {
|
|
17
17
|
const context = $context[TabGroupContextName] as TabGroupContext;
|
|
18
18
|
if (!context) {
|
|
19
|
-
throw new Error(
|
|
19
|
+
throw new Error("TabHeader must be contained within a TabGroup");
|
|
20
20
|
}
|
|
21
21
|
let $state = $context[TabGroupContextName].registerHeader($props.index, $props.value);
|
|
22
22
|
|
|
@@ -37,10 +37,12 @@ export default function TabHeader($props: any) {
|
|
|
37
37
|
@render {
|
|
38
38
|
@// TODO: $props.as
|
|
39
39
|
<h4
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
40
|
+
id={$props.id}
|
|
41
|
+
:class={[
|
|
42
|
+
"torp-tab-header",
|
|
43
|
+
$props.class,
|
|
44
|
+
$state.orientation
|
|
45
|
+
]}
|
|
44
46
|
data-state={$state.dataState}
|
|
45
47
|
data-disabled={$state.dataDisabled}
|
|
46
48
|
>
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { TabGroupContextName, TabItemContextName, type TabGroupContext, type TabItemContext } from
|
|
1
|
+
import { TabGroupContextName, TabItemContextName, type TabGroupContext, type TabItemContext } from "./TabGroupTypes";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* A component that holds the TabHeaders of a TabGroup.
|
|
@@ -19,7 +19,7 @@ export default function TabList($props: any) {
|
|
|
19
19
|
// Get stuff out of the TabGroupContext
|
|
20
20
|
const context = $context[TabGroupContextName] as TabGroupContext;
|
|
21
21
|
if (!context) {
|
|
22
|
-
throw new Error(
|
|
22
|
+
throw new Error("TabList must be contained within a TabGroup");
|
|
23
23
|
}
|
|
24
24
|
// TODO: const { orientation } = context;
|
|
25
25
|
const orientation = context.orientation;
|
|
@@ -35,7 +35,8 @@ export default function TabList($props: any) {
|
|
|
35
35
|
|
|
36
36
|
@render {
|
|
37
37
|
<div
|
|
38
|
-
|
|
38
|
+
id={$props.id}
|
|
39
|
+
class={`torp-tab-list ${$props.class ?? ""}`.trim()}
|
|
39
40
|
role="tablist"
|
|
40
41
|
aria-label={$props.ariaLabel}
|
|
41
42
|
aria-orientation={orientation}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import getId from
|
|
2
|
-
import { TabItemContextName, type TabItemContext } from
|
|
1
|
+
import getId from "../utils/getId";
|
|
2
|
+
import { TabItemContextName, type TabItemContext } from "./TabGroupTypes";
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* A header button for a tab item, which can be used to reveal an associated TabContent.
|
|
@@ -12,7 +12,7 @@ export default function TabTrigger() {
|
|
|
12
12
|
// Get stuff out of the TabItemContext
|
|
13
13
|
const context = $context[TabItemContextName] as TabItemContext;
|
|
14
14
|
if (!context) {
|
|
15
|
-
throw new Error(
|
|
15
|
+
throw new Error("TabTrigger must be contained within a TabHeader");
|
|
16
16
|
}
|
|
17
17
|
let $state = $context[TabItemContextName].state;
|
|
18
18
|
|
|
@@ -25,7 +25,7 @@ export default function TabTrigger() {
|
|
|
25
25
|
// This function is called by the parent TabGroup to focus this item's button
|
|
26
26
|
function setFocused() {
|
|
27
27
|
button.focus();
|
|
28
|
-
if ($state.activation ===
|
|
28
|
+
if ($state.activation === "automatic") {
|
|
29
29
|
context.triggerItem($state.value);
|
|
30
30
|
}
|
|
31
31
|
}
|
|
@@ -45,7 +45,7 @@ export default function TabTrigger() {
|
|
|
45
45
|
<button
|
|
46
46
|
type="button"
|
|
47
47
|
id={$props.id}
|
|
48
|
-
class=
|
|
48
|
+
class={`torp-tab-trigger ${$props.class ?? ""}`.trim()}
|
|
49
49
|
role="tab"
|
|
50
50
|
tabindex="-1"
|
|
51
51
|
disabled={$state.disabled || $state.parentDisabled}
|
|
@@ -55,7 +55,7 @@ export default function TabTrigger() {
|
|
|
55
55
|
data-state={$state.dataState}
|
|
56
56
|
data-disabled={$state.disabled}
|
|
57
57
|
data-orientation={$state.orientation}
|
|
58
|
-
|
|
58
|
+
&ref={button}
|
|
59
59
|
onclick={handleClick}
|
|
60
60
|
onkeydown={handleKey}
|
|
61
61
|
>
|
|
@@ -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
|
+
}
|