menhir 0.1.2 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +6 -0
- package/README.md +9 -2
- package/dist/alert-dialog/index.js +2 -0
- package/dist/alert-dialog/index.parts.js +7 -0
- package/dist/alert-dialog/popup/AlertDialogPopup.js +13 -0
- package/dist/core/createControllableSignal.js +15 -5
- package/dist/core/listValue.js +2 -7
- package/dist/core/slot.js +30 -0
- package/dist/dialog/close/DialogClose.js +33 -0
- package/dist/dialog/description/DialogDescription.js +27 -0
- package/dist/dialog/index.js +2 -0
- package/dist/dialog/index.parts.js +7 -0
- package/dist/dialog/popup/DialogPopup.js +13 -0
- package/dist/dialog/popup/renderDialogPopup.js +71 -0
- package/dist/dialog/root/DialogRoot.js +46 -0
- package/dist/dialog/root/DialogRootContext.js +10 -0
- package/dist/dialog/title/DialogTitle.js +27 -0
- package/dist/dialog/trigger/DialogTrigger.js +33 -0
- package/dist/floating/dismissal/dialogClose.js +67 -0
- package/dist/floating/dismissal/escape.js +44 -0
- package/dist/floating/dismissal/outsidePress.js +52 -0
- package/dist/floating/dismissal/popoverClose.js +74 -0
- package/dist/floating/dismissal/reason.js +0 -0
- package/dist/floating/focus/initialFocus.js +33 -0
- package/dist/floating/focus/returnFocus.js +65 -0
- package/dist/floating/focus/tabWrap.js +55 -0
- package/dist/floating/focus/tabbable.js +50 -0
- package/dist/floating/focus/target.js +0 -0
- package/dist/floating/hover/group.js +36 -0
- package/dist/floating/hover/hover.js +105 -0
- package/dist/floating/hover/safeArea.js +64 -0
- package/dist/floating/list-navigation/activeDescendant.js +29 -0
- package/dist/floating/list-navigation/listNavigation.js +108 -0
- package/dist/floating/list-navigation/roving.js +27 -0
- package/dist/floating/presence/presence.js +57 -0
- package/dist/floating/scroll-lock/scrollLock.js +125 -0
- package/dist/floating/typeahead/match.js +24 -0
- package/dist/floating/typeahead/typeahead.js +56 -0
- package/dist/navigation-menu/index.js +2 -0
- package/dist/navigation-menu/index.parts.js +6 -0
- package/dist/navigation-menu/item/NavigationMenuItem.js +83 -0
- package/dist/navigation-menu/item/NavigationMenuItemContext.js +10 -0
- package/dist/navigation-menu/link/NavigationMenuLink.js +26 -0
- package/dist/navigation-menu/panel/NavigationMenuPanel.js +31 -0
- package/dist/navigation-menu/root/NavigationMenuRoot.js +100 -0
- package/dist/navigation-menu/root/NavigationMenuRootContext.js +10 -0
- package/dist/navigation-menu/trigger/NavigationMenuTrigger.js +36 -0
- package/dist/popover/arrow/PopoverArrow.js +39 -0
- package/dist/popover/close/PopoverClose.js +33 -0
- package/dist/popover/description/PopoverDescription.js +27 -0
- package/dist/popover/index.js +2 -0
- package/dist/popover/index.parts.js +8 -0
- package/dist/popover/popup/PopoverPopup.js +116 -0
- package/dist/popover/popup/PopoverPopupContext.js +10 -0
- package/dist/popover/root/PopoverRoot.js +50 -0
- package/dist/popover/root/PopoverRootContext.js +10 -0
- package/dist/popover/title/PopoverTitle.js +27 -0
- package/dist/popover/trigger/PopoverTrigger.js +44 -0
- package/dist/source/alert-dialog/index.d.ts +1 -0
- package/dist/source/alert-dialog/index.js +1 -0
- package/dist/source/alert-dialog/index.parts.d.ts +6 -0
- package/dist/source/alert-dialog/index.parts.js +7 -0
- package/dist/source/alert-dialog/popup/AlertDialogPopup.d.ts +10 -0
- package/dist/source/alert-dialog/popup/AlertDialogPopup.jsx +11 -0
- package/dist/source/core/createControllableSignal.d.ts +13 -4
- package/dist/source/core/createControllableSignal.js +11 -5
- package/dist/source/core/listValue.js +4 -10
- package/dist/source/core/slot.d.ts +21 -0
- package/dist/source/core/slot.js +28 -0
- package/dist/source/dialog/close/DialogClose.d.ts +11 -0
- package/dist/source/dialog/close/DialogClose.jsx +25 -0
- package/dist/source/dialog/description/DialogDescription.d.ts +10 -0
- package/dist/source/dialog/description/DialogDescription.jsx +26 -0
- package/dist/source/dialog/index.d.ts +1 -0
- package/dist/source/dialog/index.js +1 -0
- package/dist/source/dialog/index.parts.d.ts +6 -0
- package/dist/source/dialog/index.parts.js +6 -0
- package/dist/source/dialog/popup/DialogPopup.d.ts +9 -0
- package/dist/source/dialog/popup/DialogPopup.jsx +11 -0
- package/dist/source/dialog/popup/renderDialogPopup.d.ts +40 -0
- package/dist/source/dialog/popup/renderDialogPopup.jsx +73 -0
- package/dist/source/dialog/root/DialogRoot.d.ts +20 -0
- package/dist/source/dialog/root/DialogRoot.jsx +39 -0
- package/dist/source/dialog/root/DialogRootContext.d.ts +32 -0
- package/dist/source/dialog/root/DialogRootContext.js +14 -0
- package/dist/source/dialog/title/DialogTitle.d.ts +10 -0
- package/dist/source/dialog/title/DialogTitle.jsx +26 -0
- package/dist/source/dialog/trigger/DialogTrigger.d.ts +10 -0
- package/dist/source/dialog/trigger/DialogTrigger.jsx +27 -0
- package/dist/source/floating/dismissal/dialogClose.d.ts +26 -0
- package/dist/source/floating/dismissal/dialogClose.js +69 -0
- package/dist/source/floating/dismissal/escape.d.ts +24 -0
- package/dist/source/floating/dismissal/escape.js +48 -0
- package/dist/source/floating/dismissal/outsidePress.d.ts +26 -0
- package/dist/source/floating/dismissal/outsidePress.js +50 -0
- package/dist/source/floating/dismissal/popoverClose.d.ts +28 -0
- package/dist/source/floating/dismissal/popoverClose.js +82 -0
- package/dist/source/floating/dismissal/reason.d.ts +34 -0
- package/dist/source/floating/dismissal/reason.js +1 -0
- package/dist/source/floating/focus/initialFocus.d.ts +24 -0
- package/dist/source/floating/focus/initialFocus.js +38 -0
- package/dist/source/floating/focus/returnFocus.d.ts +27 -0
- package/dist/source/floating/focus/returnFocus.js +81 -0
- package/dist/source/floating/focus/tabWrap.d.ts +21 -0
- package/dist/source/floating/focus/tabWrap.js +69 -0
- package/dist/source/floating/focus/tabbable.d.ts +16 -0
- package/dist/source/floating/focus/tabbable.js +69 -0
- package/dist/source/floating/focus/target.d.ts +8 -0
- package/dist/source/floating/focus/target.js +1 -0
- package/dist/source/floating/hover/group.d.ts +39 -0
- package/dist/source/floating/hover/group.js +38 -0
- package/dist/source/floating/hover/hover.d.ts +35 -0
- package/dist/source/floating/hover/hover.js +130 -0
- package/dist/source/floating/hover/safeArea.d.ts +23 -0
- package/dist/source/floating/hover/safeArea.js +67 -0
- package/dist/source/floating/list-navigation/activeDescendant.d.ts +30 -0
- package/dist/source/floating/list-navigation/activeDescendant.js +24 -0
- package/dist/source/floating/list-navigation/listNavigation.d.ts +68 -0
- package/dist/source/floating/list-navigation/listNavigation.js +148 -0
- package/dist/source/floating/list-navigation/roving.d.ts +23 -0
- package/dist/source/floating/list-navigation/roving.js +25 -0
- package/dist/source/floating/presence/presence.d.ts +24 -0
- package/dist/source/floating/presence/presence.js +73 -0
- package/dist/source/floating/scroll-lock/scrollLock.d.ts +27 -0
- package/dist/source/floating/scroll-lock/scrollLock.js +151 -0
- package/dist/source/floating/typeahead/match.d.ts +14 -0
- package/dist/source/floating/typeahead/match.js +32 -0
- package/dist/source/floating/typeahead/typeahead.d.ts +41 -0
- package/dist/source/floating/typeahead/typeahead.js +81 -0
- package/dist/source/navigation-menu/index.d.ts +1 -0
- package/dist/source/navigation-menu/index.js +1 -0
- package/dist/source/navigation-menu/index.parts.d.ts +5 -0
- package/dist/source/navigation-menu/index.parts.js +5 -0
- package/dist/source/navigation-menu/item/NavigationMenuItem.d.ts +16 -0
- package/dist/source/navigation-menu/item/NavigationMenuItem.jsx +85 -0
- package/dist/source/navigation-menu/item/NavigationMenuItemContext.d.ts +27 -0
- package/dist/source/navigation-menu/item/NavigationMenuItemContext.js +12 -0
- package/dist/source/navigation-menu/link/NavigationMenuLink.d.ts +12 -0
- package/dist/source/navigation-menu/link/NavigationMenuLink.jsx +18 -0
- package/dist/source/navigation-menu/panel/NavigationMenuPanel.d.ts +11 -0
- package/dist/source/navigation-menu/panel/NavigationMenuPanel.jsx +26 -0
- package/dist/source/navigation-menu/root/NavigationMenuRoot.d.ts +37 -0
- package/dist/source/navigation-menu/root/NavigationMenuRoot.jsx +103 -0
- package/dist/source/navigation-menu/root/NavigationMenuRootContext.d.ts +33 -0
- package/dist/source/navigation-menu/root/NavigationMenuRootContext.js +13 -0
- package/dist/source/navigation-menu/trigger/NavigationMenuTrigger.d.ts +12 -0
- package/dist/source/navigation-menu/trigger/NavigationMenuTrigger.jsx +32 -0
- package/dist/source/popover/arrow/PopoverArrow.d.ts +11 -0
- package/dist/source/popover/arrow/PopoverArrow.jsx +32 -0
- package/dist/source/popover/close/PopoverClose.d.ts +11 -0
- package/dist/source/popover/close/PopoverClose.jsx +25 -0
- package/dist/source/popover/description/PopoverDescription.d.ts +10 -0
- package/dist/source/popover/description/PopoverDescription.jsx +26 -0
- package/dist/source/popover/index.d.ts +1 -0
- package/dist/source/popover/index.js +1 -0
- package/dist/source/popover/index.parts.d.ts +7 -0
- package/dist/source/popover/index.parts.js +7 -0
- package/dist/source/popover/popup/PopoverPopup.d.ts +47 -0
- package/dist/source/popover/popup/PopoverPopup.jsx +120 -0
- package/dist/source/popover/popup/PopoverPopupContext.d.ts +14 -0
- package/dist/source/popover/popup/PopoverPopupContext.js +11 -0
- package/dist/source/popover/root/PopoverRoot.d.ts +20 -0
- package/dist/source/popover/root/PopoverRoot.jsx +42 -0
- package/dist/source/popover/root/PopoverRootContext.d.ts +39 -0
- package/dist/source/popover/root/PopoverRootContext.js +13 -0
- package/dist/source/popover/title/PopoverTitle.d.ts +10 -0
- package/dist/source/popover/title/PopoverTitle.jsx +26 -0
- package/dist/source/popover/trigger/PopoverTrigger.d.ts +12 -0
- package/dist/source/popover/trigger/PopoverTrigger.jsx +40 -0
- package/dist/source/tooltip/arrow/TooltipArrow.d.ts +11 -0
- package/dist/source/tooltip/arrow/TooltipArrow.jsx +32 -0
- package/dist/source/tooltip/group/TooltipGroup.d.ts +20 -0
- package/dist/source/tooltip/group/TooltipGroup.jsx +18 -0
- package/dist/source/tooltip/group/TooltipGroupContext.d.ts +2 -0
- package/dist/source/tooltip/group/TooltipGroupContext.js +5 -0
- package/dist/source/tooltip/index.d.ts +1 -0
- package/dist/source/tooltip/index.js +1 -0
- package/dist/source/tooltip/index.parts.d.ts +5 -0
- package/dist/source/tooltip/index.parts.js +5 -0
- package/dist/source/tooltip/popup/TooltipPopup.d.ts +31 -0
- package/dist/source/tooltip/popup/TooltipPopup.jsx +77 -0
- package/dist/source/tooltip/popup/TooltipPopupContext.d.ts +14 -0
- package/dist/source/tooltip/popup/TooltipPopupContext.js +11 -0
- package/dist/source/tooltip/root/TooltipRoot.d.ts +31 -0
- package/dist/source/tooltip/root/TooltipRoot.jsx +80 -0
- package/dist/source/tooltip/root/TooltipRootContext.d.ts +35 -0
- package/dist/source/tooltip/root/TooltipRootContext.js +13 -0
- package/dist/source/tooltip/trigger/TooltipTrigger.d.ts +12 -0
- package/dist/source/tooltip/trigger/TooltipTrigger.jsx +52 -0
- package/dist/tooltip/arrow/TooltipArrow.js +39 -0
- package/dist/tooltip/group/TooltipGroup.js +26 -0
- package/dist/tooltip/group/TooltipGroupContext.js +4 -0
- package/dist/tooltip/index.js +2 -0
- package/dist/tooltip/index.parts.js +6 -0
- package/dist/tooltip/popup/TooltipPopup.js +86 -0
- package/dist/tooltip/popup/TooltipPopupContext.js +10 -0
- package/dist/tooltip/root/TooltipRoot.js +79 -0
- package/dist/tooltip/root/TooltipRootContext.js +10 -0
- package/dist/tooltip/trigger/TooltipTrigger.js +54 -0
- package/package.json +28 -2
- package/skills/menhir-styling/SKILL.md +35 -0
- package/skills/menhir-styling/looks/shadcn/LICENSE +24 -0
- package/skills/menhir-styling/looks/shadcn/LOOK.md +106 -0
- package/skills/menhir-styling/looks/shadcn/dialog.md +63 -0
- package/skills/menhir-styling/looks/shadcn/popover.md +39 -0
- package/skills/menhir-styling/looks/shadcn/switch.md +26 -0
- package/skills/menhir-styling/looks/shadcn/tabs.md +62 -0
- package/skills/menhir-styling/looks/shadcn/tokens.css +80 -0
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Why a popup opened or closed, the `reason` of `onOpenChange`'s details
|
|
3
|
+
* (PLAN.md section 8, "API conventions"). Later components add reasons;
|
|
4
|
+
* they do not rename these.
|
|
5
|
+
*
|
|
6
|
+
* - `escape-key`: Escape.
|
|
7
|
+
* - `outside-press`: a press outside the popup, on a dialog's backdrop or
|
|
8
|
+
* in a popover's light dismiss.
|
|
9
|
+
* - `trigger-press`: the trigger, pressed by pointer or keyboard.
|
|
10
|
+
* - `close-request`: the browser closed the popup for another reason, such
|
|
11
|
+
* as the back gesture on Android, a `<form method="dialog">` or another
|
|
12
|
+
* popover opening.
|
|
13
|
+
* - `trigger-hover`: the pointer rested on the trigger, or left the trigger
|
|
14
|
+
* and the popup (`floating/hover`).
|
|
15
|
+
* - `close-press`: the popup's own close button, pressed by pointer or
|
|
16
|
+
* keyboard (Dialog's `Close`).
|
|
17
|
+
* - `trigger-focus`: the trigger received keyboard focus, or lost it
|
|
18
|
+
* (Tooltip).
|
|
19
|
+
* - `focus-out`: focus moved to an element outside the component, such as
|
|
20
|
+
* Tab out of a Navigation Menu's `<nav>`.
|
|
21
|
+
* - `link-press`: a link inside the component was activated, by pointer or
|
|
22
|
+
* keyboard, such as a Navigation Menu's `Link`.
|
|
23
|
+
*/
|
|
24
|
+
export type OpenChangeReason = "escape-key" | "outside-press" | "trigger-press" | "close-request" | "trigger-hover" | "close-press" | "trigger-focus" | "focus-out" | "link-press";
|
|
25
|
+
/**
|
|
26
|
+
* The second argument of `onOpenChange`. `event` is the event that caused
|
|
27
|
+
* the change: the key or pointer event when there was one, else the
|
|
28
|
+
* browser's own `cancel`, `close` or `beforetoggle`. There is no `cancel()`:
|
|
29
|
+
* a consumer keeps a popup open by controlling `open`.
|
|
30
|
+
*/
|
|
31
|
+
export interface OpenChangeDetails {
|
|
32
|
+
readonly reason: OpenChangeReason;
|
|
33
|
+
readonly event: Event;
|
|
34
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { FocusTarget } from "./target.ts";
|
|
2
|
+
/**
|
|
3
|
+
* Moves focus to `initialFocus` once the popup has opened, instead of where
|
|
4
|
+
* the browser puts it: the element with `autofocus`, else a dialog's first
|
|
5
|
+
* focusable element, while a popover leaves focus on its trigger. Without
|
|
6
|
+
* `initialFocus` the browser decides.
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* createInitialFocus({
|
|
10
|
+
* get popup() { return dialog; },
|
|
11
|
+
* get open() { return open(); },
|
|
12
|
+
* get initialFocus() { return props.initialFocus; },
|
|
13
|
+
* });
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* A function is called on each opening with the element the browser focused
|
|
17
|
+
* inside the popup, or `null` when it focused none. The browser focuses
|
|
18
|
+
* first, so focus moves twice, in the same task, before anything is painted.
|
|
19
|
+
*/
|
|
20
|
+
export declare function createInitialFocus(options: {
|
|
21
|
+
readonly popup: HTMLElement | undefined;
|
|
22
|
+
readonly open: boolean;
|
|
23
|
+
readonly initialFocus?: FocusTarget;
|
|
24
|
+
}): void;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { createEffect } from "solid-js";
|
|
2
|
+
/**
|
|
3
|
+
* Moves focus to `initialFocus` once the popup has opened, instead of where
|
|
4
|
+
* the browser puts it: the element with `autofocus`, else a dialog's first
|
|
5
|
+
* focusable element, while a popover leaves focus on its trigger. Without
|
|
6
|
+
* `initialFocus` the browser decides.
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* createInitialFocus({
|
|
10
|
+
* get popup() { return dialog; },
|
|
11
|
+
* get open() { return open(); },
|
|
12
|
+
* get initialFocus() { return props.initialFocus; },
|
|
13
|
+
* });
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* A function is called on each opening with the element the browser focused
|
|
17
|
+
* inside the popup, or `null` when it focused none. The browser focuses
|
|
18
|
+
* first, so focus moves twice, in the same task, before anything is painted.
|
|
19
|
+
*/
|
|
20
|
+
export function createInitialFocus(options) {
|
|
21
|
+
createEffect(() => options.open, (open) => {
|
|
22
|
+
const popup = options.popup;
|
|
23
|
+
if (!open || !popup)
|
|
24
|
+
return;
|
|
25
|
+
// After the popup's own `showModal()` or `showPopover()` in this
|
|
26
|
+
// flush, whichever effect runs first. A microtask, not `onFlush`,
|
|
27
|
+
// which is for event handlers: this runs inside the flush.
|
|
28
|
+
queueMicrotask(() => {
|
|
29
|
+
if (!popup.matches("[open], :popover-open"))
|
|
30
|
+
return;
|
|
31
|
+
const active = document.activeElement;
|
|
32
|
+
const target = options.initialFocus;
|
|
33
|
+
(typeof target === "function"
|
|
34
|
+
? target(popup.contains(active) ? active : null)
|
|
35
|
+
: target)?.focus();
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { FocusTarget } from "./target.ts";
|
|
2
|
+
/**
|
|
3
|
+
* Moves focus to `returnFocus` once the popup has closed, instead of where
|
|
4
|
+
* the browser puts it: the element focused before a dialog opened, or a
|
|
5
|
+
* popover's trigger when focus was inside it. Without `returnFocus` the
|
|
6
|
+
* browser decides.
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* createReturnFocus({
|
|
10
|
+
* get popup() { return dialog; },
|
|
11
|
+
* get open() { return open(); },
|
|
12
|
+
* get returnFocus() { return props.returnFocus; },
|
|
13
|
+
* });
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* A focus the user moved out of the popup while it was shown, such as a
|
|
17
|
+
* press on another button that closed a popover, stays where it is. Focus
|
|
18
|
+
* returns once the popup is closed, not when its state turns false: after
|
|
19
|
+
* the flush that closed it, or on its `close` or `toggle` event if it is
|
|
20
|
+
* still shown then, as while an exit animation runs. A popup disposed while
|
|
21
|
+
* open moves no focus.
|
|
22
|
+
*/
|
|
23
|
+
export declare function createReturnFocus(options: {
|
|
24
|
+
readonly popup: HTMLElement | undefined;
|
|
25
|
+
readonly open: boolean;
|
|
26
|
+
readonly returnFocus?: FocusTarget;
|
|
27
|
+
}): void;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { createEffect, onCleanup } from "solid-js";
|
|
2
|
+
/**
|
|
3
|
+
* Moves focus to `returnFocus` once the popup has closed, instead of where
|
|
4
|
+
* the browser puts it: the element focused before a dialog opened, or a
|
|
5
|
+
* popover's trigger when focus was inside it. Without `returnFocus` the
|
|
6
|
+
* browser decides.
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* createReturnFocus({
|
|
10
|
+
* get popup() { return dialog; },
|
|
11
|
+
* get open() { return open(); },
|
|
12
|
+
* get returnFocus() { return props.returnFocus; },
|
|
13
|
+
* });
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* A focus the user moved out of the popup while it was shown, such as a
|
|
17
|
+
* press on another button that closed a popover, stays where it is. Focus
|
|
18
|
+
* returns once the popup is closed, not when its state turns false: after
|
|
19
|
+
* the flush that closed it, or on its `close` or `toggle` event if it is
|
|
20
|
+
* still shown then, as while an exit animation runs. A popup disposed while
|
|
21
|
+
* open moves no focus.
|
|
22
|
+
*/
|
|
23
|
+
export function createReturnFocus(options) {
|
|
24
|
+
// Ends the listening of the last opening, if its popup has not closed.
|
|
25
|
+
// Disposal ends it at once, before the check queued below runs, so a
|
|
26
|
+
// popup disposed while open moves no focus.
|
|
27
|
+
let stop;
|
|
28
|
+
onCleanup(() => stop?.());
|
|
29
|
+
createEffect(() => options.open, (open) => {
|
|
30
|
+
const popup = options.popup;
|
|
31
|
+
if (!open || !popup)
|
|
32
|
+
return;
|
|
33
|
+
// Opened again before the last closing finished.
|
|
34
|
+
stop?.();
|
|
35
|
+
let movedOut = false;
|
|
36
|
+
// The browser's own restore comes once the popup is hidden, so only
|
|
37
|
+
// a focus that moves while it is shown counts.
|
|
38
|
+
const focusin = (event) => {
|
|
39
|
+
if (isShown(popup))
|
|
40
|
+
movedOut = !popup.contains(event.target);
|
|
41
|
+
};
|
|
42
|
+
const finish = () => {
|
|
43
|
+
stop();
|
|
44
|
+
if (movedOut)
|
|
45
|
+
return;
|
|
46
|
+
// Where the browser left focus: nowhere if on the body, or still on
|
|
47
|
+
// an element inside the hidden popup, where nothing took it back.
|
|
48
|
+
const active = document.activeElement;
|
|
49
|
+
const focused = active === document.body || popup.contains(active) ? null : active;
|
|
50
|
+
const target = options.returnFocus;
|
|
51
|
+
(typeof target === "function" ? target(focused) : target)?.focus();
|
|
52
|
+
};
|
|
53
|
+
const closed = (event) => {
|
|
54
|
+
if (event.newState !== "open")
|
|
55
|
+
finish();
|
|
56
|
+
};
|
|
57
|
+
stop = () => {
|
|
58
|
+
stop = undefined;
|
|
59
|
+
document.removeEventListener("focusin", focusin);
|
|
60
|
+
popup.removeEventListener("close", closed);
|
|
61
|
+
popup.removeEventListener("toggle", closed);
|
|
62
|
+
};
|
|
63
|
+
document.addEventListener("focusin", focusin);
|
|
64
|
+
popup.addEventListener("close", closed);
|
|
65
|
+
popup.addEventListener("toggle", closed);
|
|
66
|
+
// On close, and on disposal, which ends the listening first.
|
|
67
|
+
return () => {
|
|
68
|
+
// After the popup's own close in this flush, whichever effect
|
|
69
|
+
// runs first; a popup still shown then finishes on its event. A
|
|
70
|
+
// microtask, not `onFlush`, which is for event handlers: this runs
|
|
71
|
+
// inside the flush.
|
|
72
|
+
queueMicrotask(() => {
|
|
73
|
+
if (stop && !isShown(popup))
|
|
74
|
+
finish();
|
|
75
|
+
});
|
|
76
|
+
};
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
function isShown(popup) {
|
|
80
|
+
return popup.matches("[open], :popover-open");
|
|
81
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tab and Shift+Tab cycle inside a modal `<dialog>` while `open`, as the
|
|
3
|
+
* WAI-ARIA modal dialog pattern asks. The browser lets them leave for its
|
|
4
|
+
* own UI.
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* createTabWrap({
|
|
8
|
+
* get dialog() { return dialog; },
|
|
9
|
+
* get open() { return open(); },
|
|
10
|
+
* });
|
|
11
|
+
* ```
|
|
12
|
+
*
|
|
13
|
+
* Tab from the last tab stop, or from an element after it, moves to the
|
|
14
|
+
* first; Shift+Tab from the first, from an element before it or from the
|
|
15
|
+
* dialog itself, to the last. Elsewhere the browser moves focus. Only the
|
|
16
|
+
* topmost dialog wraps, and a key already handled does not.
|
|
17
|
+
*/
|
|
18
|
+
export declare function createTabWrap(options: {
|
|
19
|
+
readonly dialog: HTMLDialogElement | undefined;
|
|
20
|
+
readonly open: boolean;
|
|
21
|
+
}): void;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { createEffect } from "solid-js";
|
|
2
|
+
import { isSameStop, tabStops } from "./tabbable.js";
|
|
3
|
+
/**
|
|
4
|
+
* Tab and Shift+Tab cycle inside a modal `<dialog>` while `open`, as the
|
|
5
|
+
* WAI-ARIA modal dialog pattern asks. The browser lets them leave for its
|
|
6
|
+
* own UI.
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* createTabWrap({
|
|
10
|
+
* get dialog() { return dialog; },
|
|
11
|
+
* get open() { return open(); },
|
|
12
|
+
* });
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* Tab from the last tab stop, or from an element after it, moves to the
|
|
16
|
+
* first; Shift+Tab from the first, from an element before it or from the
|
|
17
|
+
* dialog itself, to the last. Elsewhere the browser moves focus. Only the
|
|
18
|
+
* topmost dialog wraps, and a key already handled does not.
|
|
19
|
+
*/
|
|
20
|
+
export function createTabWrap(options) {
|
|
21
|
+
createEffect(() => options.open, (open) => {
|
|
22
|
+
const dialog = options.dialog;
|
|
23
|
+
if (!open || !dialog)
|
|
24
|
+
return;
|
|
25
|
+
const keydown = (event) => {
|
|
26
|
+
if (event.key !== "Tab" ||
|
|
27
|
+
event.defaultPrevented ||
|
|
28
|
+
// A nested dialog's key, bubbling through this one.
|
|
29
|
+
dialogOf(event) !== dialog)
|
|
30
|
+
return;
|
|
31
|
+
const stops = tabStops(dialog);
|
|
32
|
+
const forward = !event.shiftKey;
|
|
33
|
+
const edge = forward ? stops.at(-1) : stops[0];
|
|
34
|
+
let active = document.activeElement;
|
|
35
|
+
while (active?.shadowRoot?.activeElement)
|
|
36
|
+
active = active.shadowRoot.activeElement;
|
|
37
|
+
if (!edge ||
|
|
38
|
+
isSameStop(active, edge) ||
|
|
39
|
+
// Focus on an element that is no stop, past the edge.
|
|
40
|
+
(!stops.some((stop) => isSameStop(active, stop)) &&
|
|
41
|
+
edge.compareDocumentPosition(active) &
|
|
42
|
+
(forward
|
|
43
|
+
? Node.DOCUMENT_POSITION_FOLLOWING
|
|
44
|
+
: Node.DOCUMENT_POSITION_PRECEDING))) {
|
|
45
|
+
event.preventDefault();
|
|
46
|
+
(forward ? stops[0] : stops.at(-1))?.focus();
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
// On the window, after the document, where Solid delegates its
|
|
50
|
+
// handlers, so a consumer's `onKeyDown` can handle the key first.
|
|
51
|
+
window.addEventListener("keydown", keydown);
|
|
52
|
+
return () => window.removeEventListener("keydown", keydown);
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* The nearest `<dialog>` around the key's target, across shadow roots: at
|
|
57
|
+
* the window, `target` is retargeted to the outermost shadow host.
|
|
58
|
+
*/
|
|
59
|
+
function dialogOf(event) {
|
|
60
|
+
let node = event.composedPath()[0];
|
|
61
|
+
let dialog;
|
|
62
|
+
while (!(dialog = node.closest("dialog"))) {
|
|
63
|
+
const root = node.getRootNode();
|
|
64
|
+
if (!(root instanceof ShadowRoot))
|
|
65
|
+
break;
|
|
66
|
+
node = root.host;
|
|
67
|
+
}
|
|
68
|
+
return dialog;
|
|
69
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** An element that can take focus. */
|
|
2
|
+
export type FocusableElement = HTMLElement | SVGElement;
|
|
3
|
+
/**
|
|
4
|
+
* The tab stops inside `root`, in the order Tab visits them. Framework-free;
|
|
5
|
+
* reads layout, so call it in the browser, not on the server.
|
|
6
|
+
*
|
|
7
|
+
* A stop is an element whose `tabIndex` is 0 or more, or a `contenteditable`
|
|
8
|
+
* editing host with no `tabindex`, that is not `:disabled`, is visible and
|
|
9
|
+
* is not in an `inert` subtree. A radio group is one stop: its checked
|
|
10
|
+
* radio, else its first. An open shadow root is searched in place of its
|
|
11
|
+
* host's children, and a slot by what is assigned to it, else its fallback.
|
|
12
|
+
* Positive `tabindex` values come first, in ascending order.
|
|
13
|
+
*/
|
|
14
|
+
export declare function tabStops(root: Element): FocusableElement[];
|
|
15
|
+
/** Whether `a` and `b` are one tab stop: the same element, or one radio group. */
|
|
16
|
+
export declare function isSameStop(a: Element | null, b: Element): boolean;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The tab stops inside `root`, in the order Tab visits them. Framework-free;
|
|
3
|
+
* reads layout, so call it in the browser, not on the server.
|
|
4
|
+
*
|
|
5
|
+
* A stop is an element whose `tabIndex` is 0 or more, or a `contenteditable`
|
|
6
|
+
* editing host with no `tabindex`, that is not `:disabled`, is visible and
|
|
7
|
+
* is not in an `inert` subtree. A radio group is one stop: its checked
|
|
8
|
+
* radio, else its first. An open shadow root is searched in place of its
|
|
9
|
+
* host's children, and a slot by what is assigned to it, else its fallback.
|
|
10
|
+
* Positive `tabindex` values come first, in ascending order.
|
|
11
|
+
*/
|
|
12
|
+
export function tabStops(root) {
|
|
13
|
+
const found = [];
|
|
14
|
+
const search = (parent) => {
|
|
15
|
+
const assigned = parent instanceof HTMLSlotElement ? parent.assignedElements() : [];
|
|
16
|
+
for (const element of parent.shadowRoot?.children ??
|
|
17
|
+
(assigned.length ? assigned : parent.children)) {
|
|
18
|
+
// Nothing inside an inert element can take focus.
|
|
19
|
+
if (element.hasAttribute("inert"))
|
|
20
|
+
continue;
|
|
21
|
+
if (isStop(element))
|
|
22
|
+
addStop(found, element);
|
|
23
|
+
search(element);
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
search(root);
|
|
27
|
+
// A stable sort: equal values keep tree order.
|
|
28
|
+
return found.sort((a, b) => order(a) - order(b));
|
|
29
|
+
}
|
|
30
|
+
/** Whether Tab stops at `element`, leaving radio groups aside. */
|
|
31
|
+
function isStop(element) {
|
|
32
|
+
const { tabIndex, isContentEditable } = element;
|
|
33
|
+
return ((tabIndex >= 0
|
|
34
|
+
? // Reported as 0, but a link without href takes no focus.
|
|
35
|
+
element.localName !== "a" || element.hasAttribute("href")
|
|
36
|
+
: // The editing host, not the editable elements inside it.
|
|
37
|
+
isContentEditable &&
|
|
38
|
+
!element.parentElement?.isContentEditable &&
|
|
39
|
+
!element.hasAttribute("tabindex")) &&
|
|
40
|
+
!element.matches(":disabled") &&
|
|
41
|
+
element.checkVisibility({ visibilityProperty: true }));
|
|
42
|
+
}
|
|
43
|
+
/** Adds a stop, keeping one radio per group: the checked one, else the first. */
|
|
44
|
+
function addStop(found, element) {
|
|
45
|
+
const index = found.findIndex((stop) => isSameStop(stop, element));
|
|
46
|
+
if (index < 0)
|
|
47
|
+
found.push(element);
|
|
48
|
+
else if (element.checked)
|
|
49
|
+
found[index] = element;
|
|
50
|
+
}
|
|
51
|
+
/** Whether `a` and `b` are one tab stop: the same element, or one radio group. */
|
|
52
|
+
export function isSameStop(a, b) {
|
|
53
|
+
return (a === b ||
|
|
54
|
+
(isGroupedRadio(a) &&
|
|
55
|
+
isGroupedRadio(b) &&
|
|
56
|
+
a.name === b.name &&
|
|
57
|
+
a.form === b.form));
|
|
58
|
+
}
|
|
59
|
+
function isGroupedRadio(element) {
|
|
60
|
+
return (element instanceof HTMLInputElement &&
|
|
61
|
+
element.type === "radio" &&
|
|
62
|
+
element.name !== "");
|
|
63
|
+
}
|
|
64
|
+
/** Above any positive `tabindex`, for the stops in tree order. */
|
|
65
|
+
const IN_TREE_ORDER = 2 ** 31;
|
|
66
|
+
/** Positive `tabindex` values first, then the rest. */
|
|
67
|
+
function order(element) {
|
|
68
|
+
return element.tabIndex > 0 ? element.tabIndex : IN_TREE_ORDER;
|
|
69
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where focus goes when a popup opens or closes, overriding the browser:
|
|
3
|
+
* an element, or a function called then with the element the browser
|
|
4
|
+
* focused, or `null` when it focused none. `(focused) => focused ?? other`
|
|
5
|
+
* keeps the browser's choice and replaces only a missing one. `undefined`,
|
|
6
|
+
* or a function that returns it, leaves focus where the browser put it.
|
|
7
|
+
*/
|
|
8
|
+
export type FocusTarget = HTMLElement | null | undefined | ((focused: Element | null) => HTMLElement | null | undefined);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/** A popup of a group, as `createHover` registers it. */
|
|
2
|
+
export interface HoverGroupMember {
|
|
3
|
+
/** Closes the popup, because another member opened on `event`. */
|
|
4
|
+
close(event: Event): void;
|
|
5
|
+
}
|
|
6
|
+
/** Triggers whose popups open without a delay once one of them is open. */
|
|
7
|
+
export interface HoverGroup {
|
|
8
|
+
/**
|
|
9
|
+
* The delay before a member opens, given its own `delay`: 0 while the
|
|
10
|
+
* group is open, else its own, or the group's.
|
|
11
|
+
*/
|
|
12
|
+
delay(own: number | undefined): number | undefined;
|
|
13
|
+
/** `member` opened on hover: the member that was open closes. */
|
|
14
|
+
opened(member: HoverGroupMember, event: Event): void;
|
|
15
|
+
/** `member` closed, for any reason. */
|
|
16
|
+
closed(member: HoverGroupMember): void;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* A group of hover popups, such as the tooltips of a toolbar: once one is
|
|
20
|
+
* open, hovering another opens it at once and closes the first. Created by
|
|
21
|
+
* the part that groups triggers, provided through its context, and passed
|
|
22
|
+
* to each popup's `createHover` as `group`:
|
|
23
|
+
*
|
|
24
|
+
* ```ts
|
|
25
|
+
* const group = createHoverGroup({
|
|
26
|
+
* get delay() { return props.delay; },
|
|
27
|
+
* get timeout() { return props.timeout; },
|
|
28
|
+
* });
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* The next member still opens at once within `timeout` milliseconds
|
|
32
|
+
* (default 400) of the last one closing. `delay` is the delay of members
|
|
33
|
+
* that set none. Only popups opened by hover count. It keeps the time of
|
|
34
|
+
* the last closing, not a timer, so it has nothing to clear.
|
|
35
|
+
*/
|
|
36
|
+
export declare function createHoverGroup(options?: {
|
|
37
|
+
readonly delay?: number;
|
|
38
|
+
readonly timeout?: number;
|
|
39
|
+
}): HoverGroup;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A group of hover popups, such as the tooltips of a toolbar: once one is
|
|
3
|
+
* open, hovering another opens it at once and closes the first. Created by
|
|
4
|
+
* the part that groups triggers, provided through its context, and passed
|
|
5
|
+
* to each popup's `createHover` as `group`:
|
|
6
|
+
*
|
|
7
|
+
* ```ts
|
|
8
|
+
* const group = createHoverGroup({
|
|
9
|
+
* get delay() { return props.delay; },
|
|
10
|
+
* get timeout() { return props.timeout; },
|
|
11
|
+
* });
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* The next member still opens at once within `timeout` milliseconds
|
|
15
|
+
* (default 400) of the last one closing. `delay` is the delay of members
|
|
16
|
+
* that set none. Only popups opened by hover count. It keeps the time of
|
|
17
|
+
* the last closing, not a timer, so it has nothing to clear.
|
|
18
|
+
*/
|
|
19
|
+
export function createHoverGroup(options = {}) {
|
|
20
|
+
let current;
|
|
21
|
+
let closedAt = -Infinity;
|
|
22
|
+
return {
|
|
23
|
+
delay: (own) => current || Date.now() - closedAt < (options.timeout ?? 400)
|
|
24
|
+
? 0
|
|
25
|
+
: (own ?? options.delay),
|
|
26
|
+
opened(member, event) {
|
|
27
|
+
if (current !== member)
|
|
28
|
+
current?.close(event);
|
|
29
|
+
current = member;
|
|
30
|
+
},
|
|
31
|
+
closed(member) {
|
|
32
|
+
if (current !== member)
|
|
33
|
+
return;
|
|
34
|
+
current = undefined;
|
|
35
|
+
closedAt = Date.now();
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { OpenChangeDetails } from "../dismissal/reason.ts";
|
|
2
|
+
import type { HoverGroup } from "./group.ts";
|
|
3
|
+
/**
|
|
4
|
+
* Opens a popup when the pointer rests on its trigger for `delay`
|
|
5
|
+
* milliseconds (default 600), and closes it `closeDelay` milliseconds
|
|
6
|
+
* (default 0) after the pointer has left both the trigger and the popup:
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* createHover({
|
|
10
|
+
* get trigger() { return trigger; },
|
|
11
|
+
* get popup() { return popup; },
|
|
12
|
+
* get open() { return open(); },
|
|
13
|
+
* get delay() { return props.delay; },
|
|
14
|
+
* onOpenChange: (open, details) => setOpen(open, details),
|
|
15
|
+
* });
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* Both report `trigger-hover`. The pointer may cross from the trigger to the
|
|
19
|
+
* popup, and back, through the safe area between them (`trackSafeArea`).
|
|
20
|
+
* A mouse or a pen opens it; a touch does not. While `disabled`, hovering
|
|
21
|
+
* opens nothing. In a `group` (`createHoverGroup`), it opens at once while
|
|
22
|
+
* another member is open, and closes that one. A change of `open`, from
|
|
23
|
+
* here or elsewhere, and disposal clear a pending delay; disposal also
|
|
24
|
+
* takes the popup out of its group. Nothing runs on the server.
|
|
25
|
+
*/
|
|
26
|
+
export declare function createHover(options: {
|
|
27
|
+
readonly trigger: HTMLElement | undefined;
|
|
28
|
+
readonly popup: HTMLElement | undefined;
|
|
29
|
+
readonly open: boolean;
|
|
30
|
+
readonly delay?: number;
|
|
31
|
+
readonly closeDelay?: number;
|
|
32
|
+
readonly disabled?: boolean;
|
|
33
|
+
readonly group?: HoverGroup;
|
|
34
|
+
onOpenChange(open: boolean, details: OpenChangeDetails): void;
|
|
35
|
+
}): void;
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { createEffect, createSignal, onCleanup, onSettled } from "solid-js";
|
|
2
|
+
import { trackSafeArea } from "./safeArea.js";
|
|
3
|
+
/**
|
|
4
|
+
* Opens a popup when the pointer rests on its trigger for `delay`
|
|
5
|
+
* milliseconds (default 600), and closes it `closeDelay` milliseconds
|
|
6
|
+
* (default 0) after the pointer has left both the trigger and the popup:
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* createHover({
|
|
10
|
+
* get trigger() { return trigger; },
|
|
11
|
+
* get popup() { return popup; },
|
|
12
|
+
* get open() { return open(); },
|
|
13
|
+
* get delay() { return props.delay; },
|
|
14
|
+
* onOpenChange: (open, details) => setOpen(open, details),
|
|
15
|
+
* });
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* Both report `trigger-hover`. The pointer may cross from the trigger to the
|
|
19
|
+
* popup, and back, through the safe area between them (`trackSafeArea`).
|
|
20
|
+
* A mouse or a pen opens it; a touch does not. While `disabled`, hovering
|
|
21
|
+
* opens nothing. In a `group` (`createHoverGroup`), it opens at once while
|
|
22
|
+
* another member is open, and closes that one. A change of `open`, from
|
|
23
|
+
* here or elsewhere, and disposal clear a pending delay; disposal also
|
|
24
|
+
* takes the popup out of its group. Nothing runs on the server.
|
|
25
|
+
*/
|
|
26
|
+
export function createHover(options) {
|
|
27
|
+
let timer;
|
|
28
|
+
let stopSafeArea;
|
|
29
|
+
// Where the pointer last was over the trigger or the popup.
|
|
30
|
+
let last;
|
|
31
|
+
const cancel = () => {
|
|
32
|
+
clearTimeout(timer);
|
|
33
|
+
timer = undefined;
|
|
34
|
+
stopSafeArea?.();
|
|
35
|
+
stopSafeArea = undefined;
|
|
36
|
+
};
|
|
37
|
+
/** Opens or closes after `delay`, or now if it is 0. */
|
|
38
|
+
const schedule = (open, event, delay) => {
|
|
39
|
+
cancel();
|
|
40
|
+
const set = () => {
|
|
41
|
+
timer = undefined;
|
|
42
|
+
// Disabled, or removed, while the delay was pending.
|
|
43
|
+
if (open && (options.disabled || !options.trigger?.isConnected))
|
|
44
|
+
return;
|
|
45
|
+
if (open)
|
|
46
|
+
options.group?.opened(member, event);
|
|
47
|
+
options.onOpenChange(open, { reason: "trigger-hover", event });
|
|
48
|
+
};
|
|
49
|
+
if (delay > 0)
|
|
50
|
+
timer = setTimeout(set, delay);
|
|
51
|
+
else
|
|
52
|
+
set();
|
|
53
|
+
};
|
|
54
|
+
const close = (event) => schedule(false, event, options.closeDelay ?? 0);
|
|
55
|
+
const member = {
|
|
56
|
+
close: (event) => schedule(false, event, 0),
|
|
57
|
+
};
|
|
58
|
+
onCleanup(() => {
|
|
59
|
+
cancel();
|
|
60
|
+
// Closed with its owner: an open popup that the group still counted
|
|
61
|
+
// would keep the group open, and be closed by the next member.
|
|
62
|
+
options.group?.closed(member);
|
|
63
|
+
});
|
|
64
|
+
// A touch fires pointer events too, which are ignored, as `trackSafeArea`
|
|
65
|
+
// ignores them; the mouse events a browser emulates after it are not
|
|
66
|
+
// pointer events, so they never reach here.
|
|
67
|
+
const listen = (element, other, onEnter) => {
|
|
68
|
+
const enter = (event) => {
|
|
69
|
+
if (event.pointerType === "touch")
|
|
70
|
+
return;
|
|
71
|
+
move(event);
|
|
72
|
+
cancel();
|
|
73
|
+
onEnter(event);
|
|
74
|
+
};
|
|
75
|
+
const move = (event) => {
|
|
76
|
+
if (event.pointerType !== "touch")
|
|
77
|
+
last = event;
|
|
78
|
+
};
|
|
79
|
+
const leave = (event) => {
|
|
80
|
+
if (event.pointerType === "touch")
|
|
81
|
+
return;
|
|
82
|
+
cancel();
|
|
83
|
+
if (!options.open)
|
|
84
|
+
return;
|
|
85
|
+
const to = other();
|
|
86
|
+
// A leave at the pointer's last position is the page moving under a
|
|
87
|
+
// still pointer, such as a scroll, and one with nothing to go to is
|
|
88
|
+
// the pointer leaving the window: no move follows to end a safe area.
|
|
89
|
+
if (to &&
|
|
90
|
+
event.relatedTarget &&
|
|
91
|
+
(event.clientX !== last?.clientX || event.clientY !== last.clientY))
|
|
92
|
+
stopSafeArea = trackSafeArea(event, to, close);
|
|
93
|
+
else
|
|
94
|
+
close(event);
|
|
95
|
+
};
|
|
96
|
+
element.addEventListener("pointerenter", enter);
|
|
97
|
+
element.addEventListener("pointermove", move);
|
|
98
|
+
element.addEventListener("pointerleave", leave);
|
|
99
|
+
return () => {
|
|
100
|
+
element.removeEventListener("pointerenter", enter);
|
|
101
|
+
element.removeEventListener("pointermove", move);
|
|
102
|
+
element.removeEventListener("pointerleave", leave);
|
|
103
|
+
};
|
|
104
|
+
};
|
|
105
|
+
// The trigger usually comes from a ref, assigned while the component
|
|
106
|
+
// renders, after an effect's first compute: it is read once mounted.
|
|
107
|
+
const [mounted, setMounted] = createSignal(false);
|
|
108
|
+
onSettled(() => {
|
|
109
|
+
setMounted(true);
|
|
110
|
+
});
|
|
111
|
+
createEffect(() => mounted() && options.trigger, (trigger) => {
|
|
112
|
+
if (!trigger)
|
|
113
|
+
return;
|
|
114
|
+
return listen(trigger, () => options.popup, (event) => {
|
|
115
|
+
if (options.open || options.disabled)
|
|
116
|
+
return;
|
|
117
|
+
const group = options.group;
|
|
118
|
+
schedule(true, event, (group ? group.delay(options.delay) : options.delay) ?? 600);
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
createEffect(() => options.open, (open) => {
|
|
122
|
+
cancel();
|
|
123
|
+
if (!open)
|
|
124
|
+
options.group?.closed(member);
|
|
125
|
+
const popup = options.popup;
|
|
126
|
+
if (!open || !popup)
|
|
127
|
+
return;
|
|
128
|
+
return listen(popup, () => options.trigger, () => { });
|
|
129
|
+
});
|
|
130
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/** A point in the viewport, as a pointer event gives it. */
|
|
2
|
+
export interface ViewportPoint {
|
|
3
|
+
readonly clientX: number;
|
|
4
|
+
readonly clientY: number;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Follows the pointer once it has left one element on its way to another,
|
|
8
|
+
* such as from a trigger to its popup across the gap between them, and
|
|
9
|
+
* calls `onLeave` with the event once it moves out of the way:
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* trigger.addEventListener("pointerleave", (event) => {
|
|
13
|
+
* stop = trackSafeArea(event, popup, () => close());
|
|
14
|
+
* });
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* The safe area is every point on a line from `exit`, where the pointer
|
|
18
|
+
* left, to the box of `element`, read again on each move. A move outside
|
|
19
|
+
* it, or past the element, ends it, and so does the pointer leaving the
|
|
20
|
+
* window. A touch is ignored. Returns what stops following; call it when
|
|
21
|
+
* the pointer reaches the element. Plain DOM, with no timers and no Solid.
|
|
22
|
+
*/
|
|
23
|
+
export declare function trackSafeArea(exit: ViewportPoint, element: Element, onLeave: (event: PointerEvent) => void): () => void;
|