menhir 0.1.3 → 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 +8 -1
- package/dist/core/createControllableSignal.js +11 -2
- package/dist/core/listValue.js +2 -7
- package/dist/floating/dismissal/escape.js +2 -1
- 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/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/source/core/createControllableSignal.d.ts +9 -1
- package/dist/source/core/createControllableSignal.js +7 -2
- package/dist/source/core/listValue.js +4 -10
- package/dist/source/floating/dismissal/escape.d.ts +2 -1
- package/dist/source/floating/dismissal/escape.js +2 -1
- package/dist/source/floating/dismissal/reason.d.ts +5 -1
- 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/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/package.json +8 -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,36 @@
|
|
|
1
|
+
import { warnUnlessButton } from "../../core/button.js";
|
|
2
|
+
import { mergeElementProps } from "../../core/mergeElementProps.js";
|
|
3
|
+
import { renderElement } from "../../core/renderElement.js";
|
|
4
|
+
import { useNavigationMenuItemContext } from "../item/NavigationMenuItemContext.js";
|
|
5
|
+
import { isDev, spread, template } from "@solidjs/web";
|
|
6
|
+
import { merge, omit } from "solid-js";
|
|
7
|
+
// src/navigation-menu/trigger/NavigationMenuTrigger.tsx
|
|
8
|
+
var _tmpl$ = /* @__PURE__ */ template(`<button>`);
|
|
9
|
+
/**
|
|
10
|
+
* The button that shows and hides its Item's Panel: a
|
|
11
|
+
* `<button type="button" aria-expanded>`. A press toggles the Panel, and
|
|
12
|
+
* the pointer resting on it opens it. Place it inside
|
|
13
|
+
* `NavigationMenu.Item`, before `NavigationMenu.Panel`.
|
|
14
|
+
*/
|
|
15
|
+
function NavigationMenuTrigger(props) {
|
|
16
|
+
const item = useNavigationMenuItemContext();
|
|
17
|
+
const ownProps = merge(item.attributes, {
|
|
18
|
+
type: "button",
|
|
19
|
+
get "aria-expanded"() {
|
|
20
|
+
return item.open() ? "true" : "false";
|
|
21
|
+
},
|
|
22
|
+
"aria-controls": item.panelId,
|
|
23
|
+
onClick: (event) => item.toggle(event),
|
|
24
|
+
ref: (element) => {
|
|
25
|
+
item.trigger = element;
|
|
26
|
+
if (isDev) warnUnlessButton(element, "NavigationMenu.Trigger");
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
const elementProps = mergeElementProps(ownProps, omit(props, "render"));
|
|
30
|
+
return renderElement(props, elementProps, (p) => (() => {
|
|
31
|
+
var _el$ = _tmpl$();
|
|
32
|
+
spread(_el$, p, false);
|
|
33
|
+
return _el$;
|
|
34
|
+
})());
|
|
35
|
+
}
|
|
36
|
+
export { NavigationMenuTrigger };
|
|
@@ -20,9 +20,17 @@ import type { Accessor } from "solid-js";
|
|
|
20
20
|
* does nothing. Set from event handlers, not from a component body or a
|
|
21
21
|
* memo. A set may pass `details`, such as why a popup closed, which
|
|
22
22
|
* `onChange` receives after the value.
|
|
23
|
+
*
|
|
24
|
+
* The third item, `requested`, reads untracked the value set last in this
|
|
25
|
+
* turn, or `value` if nothing was: for a handler that builds on a set made
|
|
26
|
+
* earlier in the same turn, which `value` shows only after the flush.
|
|
23
27
|
*/
|
|
24
28
|
export declare function createControllableSignal<T, Details = void>(options: {
|
|
25
29
|
readonly value?: T;
|
|
26
30
|
readonly defaultValue: T;
|
|
27
31
|
onChange?: (value: T, details: Details) => void;
|
|
28
|
-
}): [
|
|
32
|
+
}): [
|
|
33
|
+
value: Accessor<T>,
|
|
34
|
+
setValue: (next: T, details: Details) => void,
|
|
35
|
+
requested: () => T
|
|
36
|
+
];
|
|
@@ -21,6 +21,10 @@ import { createSignal, onFlush, untrack } from "solid-js";
|
|
|
21
21
|
* does nothing. Set from event handlers, not from a component body or a
|
|
22
22
|
* memo. A set may pass `details`, such as why a popup closed, which
|
|
23
23
|
* `onChange` receives after the value.
|
|
24
|
+
*
|
|
25
|
+
* The third item, `requested`, reads untracked the value set last in this
|
|
26
|
+
* turn, or `value` if nothing was: for a handler that builds on a set made
|
|
27
|
+
* earlier in the same turn, which `value` shows only after the flush.
|
|
24
28
|
*/
|
|
25
29
|
export function createControllableSignal(options) {
|
|
26
30
|
// A writable derived signal that reads the default once, untracked, so a
|
|
@@ -52,8 +56,9 @@ export function createControllableSignal(options) {
|
|
|
52
56
|
// Reads lag writes, so a set is compared with the last value set in this
|
|
53
57
|
// turn, if any, not with what `value` still reads.
|
|
54
58
|
let pending;
|
|
59
|
+
const requested = () => (pending ? pending[0] : untrack(value));
|
|
55
60
|
const setValue = (next, details) => {
|
|
56
|
-
if (Object.is(next,
|
|
61
|
+
if (Object.is(next, requested()))
|
|
57
62
|
return;
|
|
58
63
|
// The part's own write is an update of its own, after the pending
|
|
59
64
|
// flush: written inline, it would be held with any async work that
|
|
@@ -71,5 +76,5 @@ export function createControllableSignal(options) {
|
|
|
71
76
|
pending = undefined;
|
|
72
77
|
});
|
|
73
78
|
};
|
|
74
|
-
return [value, setValue];
|
|
79
|
+
return [value, setValue, requested];
|
|
75
80
|
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { onFlush } from "solid-js";
|
|
2
1
|
import { createControllableSignal } from "./createControllableSignal.js";
|
|
3
2
|
/**
|
|
4
3
|
* A part's value that lists items and that the consumer may control, as an
|
|
@@ -20,7 +19,7 @@ import { createControllableSignal } from "./createControllableSignal.js";
|
|
|
20
19
|
* Sets in one turn build on each other, before the flush.
|
|
21
20
|
*/
|
|
22
21
|
export function createListValue(options) {
|
|
23
|
-
const [value, setValue] = createControllableSignal({
|
|
22
|
+
const [value, setValue, requested] = createControllableSignal({
|
|
24
23
|
get value() {
|
|
25
24
|
return options.value;
|
|
26
25
|
},
|
|
@@ -29,22 +28,17 @@ export function createListValue(options) {
|
|
|
29
28
|
},
|
|
30
29
|
onChange: (value) => options.onChange?.(value),
|
|
31
30
|
});
|
|
32
|
-
// The value asked for in this turn, if any: reads lag writes, so a second
|
|
33
|
-
// set before the flush builds on this, not on what `value` still reads.
|
|
34
|
-
let pending;
|
|
35
31
|
return {
|
|
36
32
|
has: (item) => value().includes(item),
|
|
37
33
|
set: (item, included) => {
|
|
38
|
-
const current = pending ?? value();
|
|
39
34
|
let next = included ? [item] : [];
|
|
40
35
|
if (options.multiple) {
|
|
41
|
-
|
|
36
|
+
// Reads lag writes, so a second set before the flush builds on the
|
|
37
|
+
// first, not on what `value` still reads.
|
|
38
|
+
next = requested().filter((other) => other !== item);
|
|
42
39
|
if (included)
|
|
43
40
|
next.push(item);
|
|
44
41
|
}
|
|
45
|
-
if (!pending)
|
|
46
|
-
onFlush(() => (pending = undefined));
|
|
47
|
-
pending = next;
|
|
48
42
|
setValue(next);
|
|
49
43
|
},
|
|
50
44
|
};
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import type { OpenChangeDetails } from "./reason.ts";
|
|
2
2
|
/**
|
|
3
3
|
* Escape for a popup the browser does not close on Escape, a `manual`
|
|
4
|
-
* popover such as Tooltip's
|
|
4
|
+
* popover such as Tooltip's, or for content in the page's flow, such as
|
|
5
|
+
* Navigation Menu's Panels. While `open`, an Escape key calls `onDismiss`
|
|
5
6
|
* with the reason `escape-key`:
|
|
6
7
|
*
|
|
7
8
|
* ```ts
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { createEffect } from "solid-js";
|
|
2
2
|
/**
|
|
3
3
|
* Escape for a popup the browser does not close on Escape, a `manual`
|
|
4
|
-
* popover such as Tooltip's
|
|
4
|
+
* popover such as Tooltip's, or for content in the page's flow, such as
|
|
5
|
+
* Navigation Menu's Panels. While `open`, an Escape key calls `onDismiss`
|
|
5
6
|
* with the reason `escape-key`:
|
|
6
7
|
*
|
|
7
8
|
* ```ts
|
|
@@ -16,8 +16,12 @@
|
|
|
16
16
|
* keyboard (Dialog's `Close`).
|
|
17
17
|
* - `trigger-focus`: the trigger received keyboard focus, or lost it
|
|
18
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`.
|
|
19
23
|
*/
|
|
20
|
-
export type OpenChangeReason = "escape-key" | "outside-press" | "trigger-press" | "close-request" | "trigger-hover" | "close-press" | "trigger-focus";
|
|
24
|
+
export type OpenChangeReason = "escape-key" | "outside-press" | "trigger-press" | "close-request" | "trigger-hover" | "close-press" | "trigger-focus" | "focus-out" | "link-press";
|
|
21
25
|
/**
|
|
22
26
|
* The second argument of `onOpenChange`. `event` is the event that caused
|
|
23
27
|
* the change: the key or pointer event when there was one, else the
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { type ListItem, type ListNavigation, type ListNavigationOptions } from "./listNavigation.ts";
|
|
2
|
+
/** An item named in `aria-activedescendant`, which needs an `id`. */
|
|
3
|
+
export interface ActiveDescendantItem extends ListItem {
|
|
4
|
+
readonly id: string;
|
|
5
|
+
}
|
|
6
|
+
/** What `createActiveDescendantList` returns. */
|
|
7
|
+
export interface ActiveDescendantList<T extends ActiveDescendantItem> extends ListNavigation<T> {
|
|
8
|
+
/** The active item's `id`, for `aria-activedescendant`, if any. */
|
|
9
|
+
activeId(): string | undefined;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* List navigation where focus stays on another element, such as a
|
|
13
|
+
* combobox's input, which names the active item in `aria-activedescendant`.
|
|
14
|
+
* The item part takes `props.id ?? createUniqueId()` and registers once
|
|
15
|
+
* mounted, so the server HTML names no item:
|
|
16
|
+
*
|
|
17
|
+
* ```ts
|
|
18
|
+
* const list = createActiveDescendantList<Entry>({
|
|
19
|
+
* get open() { return open(); },
|
|
20
|
+
* });
|
|
21
|
+
* // On the input:
|
|
22
|
+
* const inputProps = {
|
|
23
|
+
* get "aria-activedescendant"() { return list.activeId(); },
|
|
24
|
+
* onKeyDown: (event) => list.onKeyDown(event),
|
|
25
|
+
* };
|
|
26
|
+
* // In the item part:
|
|
27
|
+
* onSettled(() => list.register(item));
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export declare function createActiveDescendantList<T extends ActiveDescendantItem>(options: ListNavigationOptions<T>): ActiveDescendantList<T>;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { createListNavigation, } from "./listNavigation.js";
|
|
2
|
+
/**
|
|
3
|
+
* List navigation where focus stays on another element, such as a
|
|
4
|
+
* combobox's input, which names the active item in `aria-activedescendant`.
|
|
5
|
+
* The item part takes `props.id ?? createUniqueId()` and registers once
|
|
6
|
+
* mounted, so the server HTML names no item:
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* const list = createActiveDescendantList<Entry>({
|
|
10
|
+
* get open() { return open(); },
|
|
11
|
+
* });
|
|
12
|
+
* // On the input:
|
|
13
|
+
* const inputProps = {
|
|
14
|
+
* get "aria-activedescendant"() { return list.activeId(); },
|
|
15
|
+
* onKeyDown: (event) => list.onKeyDown(event),
|
|
16
|
+
* };
|
|
17
|
+
* // In the item part:
|
|
18
|
+
* onSettled(() => list.register(item));
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export function createActiveDescendantList(options) {
|
|
22
|
+
const list = createListNavigation(options);
|
|
23
|
+
return { ...list, activeId: () => list.active()?.id };
|
|
24
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/** An item of a list, as list navigation knows it. */
|
|
2
|
+
export interface ListItem {
|
|
3
|
+
readonly element: HTMLElement;
|
|
4
|
+
/**
|
|
5
|
+
* The item's text, for typeahead (#98) and a Select's value, when its
|
|
6
|
+
* content is not its label. Without it, the element's text content.
|
|
7
|
+
*/
|
|
8
|
+
readonly text?: string;
|
|
9
|
+
}
|
|
10
|
+
/** The options of `createListNavigation`, as getters and functions. */
|
|
11
|
+
export interface ListNavigationOptions<T extends ListItem> {
|
|
12
|
+
/**
|
|
13
|
+
* Whether the list's popup is open. While it is closed, focus does not
|
|
14
|
+
* move and, once it has closed, no item is active. Left out for a list
|
|
15
|
+
* that is always shown.
|
|
16
|
+
*/
|
|
17
|
+
readonly open?: boolean;
|
|
18
|
+
/** Up and Down move in a vertical list (the default), Left and Right in a horizontal one. */
|
|
19
|
+
readonly orientation?: "horizontal" | "vertical";
|
|
20
|
+
/** Whether the keys wrap from the last item to the first and back (default `false`). */
|
|
21
|
+
readonly wrap?: boolean;
|
|
22
|
+
/** Whether the keys and the pointer reach this item (by default, every item). */
|
|
23
|
+
reachable?(item: T): boolean;
|
|
24
|
+
/**
|
|
25
|
+
* The key that points into the active item: Right in a vertical list
|
|
26
|
+
* (Left right to left), Down in a horizontal one. Opens a submenu.
|
|
27
|
+
*/
|
|
28
|
+
onArrowIn?(item: T, event: KeyboardEvent): void;
|
|
29
|
+
/**
|
|
30
|
+
* The key that points out of the list: Left in a vertical list (Right
|
|
31
|
+
* right to left), Up in a horizontal one. Closes a submenu.
|
|
32
|
+
*/
|
|
33
|
+
onArrowOut?(item: T | undefined, event: KeyboardEvent): void;
|
|
34
|
+
}
|
|
35
|
+
/** What `createListNavigation` returns, in both ways of holding focus. */
|
|
36
|
+
export interface ListNavigation<T extends ListItem> {
|
|
37
|
+
/** Adds a mounted item; returns what removes it. */
|
|
38
|
+
register(item: T): () => void;
|
|
39
|
+
/** The items the keys reach, in document order. */
|
|
40
|
+
items(): T[];
|
|
41
|
+
/** The active item, if any. */
|
|
42
|
+
active(): T | undefined;
|
|
43
|
+
/**
|
|
44
|
+
* Whether `item` is the active one. A move re-runs it for the item that
|
|
45
|
+
* was active and the one that is, not for every item.
|
|
46
|
+
*/
|
|
47
|
+
isActive(item: T): boolean;
|
|
48
|
+
/**
|
|
49
|
+
* Makes `item` active as the keyboard does: it is focused, in the roving
|
|
50
|
+
* form, and scrolled into view. An item the keys do not reach is ignored;
|
|
51
|
+
* `undefined` leaves no item active.
|
|
52
|
+
*/
|
|
53
|
+
move(item: T | undefined): void;
|
|
54
|
+
/**
|
|
55
|
+
* Arrow keys, Home and End, from one of the items or from the element
|
|
56
|
+
* that hands them over; returns whether it used the key.
|
|
57
|
+
*/
|
|
58
|
+
onKeyDown(event: KeyboardEvent): boolean;
|
|
59
|
+
/** The pointer moving on an item makes it active, without scrolling. */
|
|
60
|
+
onPointerMove(item: T, event: PointerEvent): void;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* The items of a list in document order, the active item, the keys and the
|
|
64
|
+
* pointer: what both ways of holding focus share. `focusItem` is the way's
|
|
65
|
+
* own: the roving form focuses the item. Nothing runs at import time, nor,
|
|
66
|
+
* until a key or the pointer, on the server.
|
|
67
|
+
*/
|
|
68
|
+
export declare function createListNavigation<T extends ListItem>(options: ListNavigationOptions<T>, focusItem?: (item: T) => void): ListNavigation<T>;
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { createEffect, createMemo, createSignal } from "solid-js";
|
|
2
|
+
// Document order, which can differ from the order items registered in.
|
|
3
|
+
const byPosition = (a, b) => a.element.compareDocumentPosition(b.element) &
|
|
4
|
+
Node.DOCUMENT_POSITION_FOLLOWING
|
|
5
|
+
? -1
|
|
6
|
+
: 1;
|
|
7
|
+
/**
|
|
8
|
+
* The items of a list in document order, the active item, the keys and the
|
|
9
|
+
* pointer: what both ways of holding focus share. `focusItem` is the way's
|
|
10
|
+
* own: the roving form focuses the item. Nothing runs at import time, nor,
|
|
11
|
+
* until a key or the pointer, on the server.
|
|
12
|
+
*/
|
|
13
|
+
export function createListNavigation(options, focusItem) {
|
|
14
|
+
const [registered, setRegistered] = createSignal([]);
|
|
15
|
+
// Sorted again only when an item registers or leaves.
|
|
16
|
+
const ordered = createMemo(() => [...registered()].sort(byPosition));
|
|
17
|
+
const reachable = (item) => options.reachable?.(item) ?? true;
|
|
18
|
+
const [active, setActive] = createSignal();
|
|
19
|
+
// Each item's own active signal, so a move sets two of them, and only the
|
|
20
|
+
// two items that read them run again. Made on the item's first read.
|
|
21
|
+
const activeSignals = new WeakMap();
|
|
22
|
+
const activeSignal = (item) => {
|
|
23
|
+
let signal = activeSignals.get(item);
|
|
24
|
+
if (!signal) {
|
|
25
|
+
const [read, write] = createSignal(false);
|
|
26
|
+
activeSignals.set(item, (signal = [read, write]));
|
|
27
|
+
}
|
|
28
|
+
return signal;
|
|
29
|
+
};
|
|
30
|
+
// The active item as written, where `active()` lags until the flush.
|
|
31
|
+
let activeItem;
|
|
32
|
+
const activate = (item) => {
|
|
33
|
+
if (activeItem)
|
|
34
|
+
activeSignal(activeItem)[1](false);
|
|
35
|
+
if (item)
|
|
36
|
+
activeSignal(item)[1](true);
|
|
37
|
+
activeItem = item;
|
|
38
|
+
setActive(() => item);
|
|
39
|
+
};
|
|
40
|
+
// As the keyboard shows an item: focused, in the roving form, and
|
|
41
|
+
// scrolled into view at the near edge, so one already in view does not
|
|
42
|
+
// move.
|
|
43
|
+
const focusAndScroll = (item) => {
|
|
44
|
+
focusItem?.(item);
|
|
45
|
+
item.element.scrollIntoView({ block: "nearest", inline: "nearest" });
|
|
46
|
+
};
|
|
47
|
+
const list = {
|
|
48
|
+
register: (item) => {
|
|
49
|
+
setRegistered((items) => [...items, item]);
|
|
50
|
+
return () => {
|
|
51
|
+
setRegistered((items) => items.filter((other) => other !== item));
|
|
52
|
+
if (activeItem === item)
|
|
53
|
+
activate(undefined);
|
|
54
|
+
};
|
|
55
|
+
},
|
|
56
|
+
items: () => ordered().filter(reachable),
|
|
57
|
+
active,
|
|
58
|
+
isActive: (item) => activeSignal(item)[0](),
|
|
59
|
+
move: (item) => {
|
|
60
|
+
if (item && !reachable(item))
|
|
61
|
+
return;
|
|
62
|
+
activate(item);
|
|
63
|
+
// While the popup is closed, the item takes no focus and does not
|
|
64
|
+
// scroll, though an exit animation may still show it: it is focused
|
|
65
|
+
// and scrolled into view once the popup opens (below).
|
|
66
|
+
if (item && options.open !== false)
|
|
67
|
+
focusAndScroll(item);
|
|
68
|
+
},
|
|
69
|
+
onKeyDown: (event) => {
|
|
70
|
+
// Only a key from one of its items, or from the element that hands
|
|
71
|
+
// it over, such as a combobox's input: a nested list, such as a
|
|
72
|
+
// submenu in an item, takes its own keys, which bubble through it.
|
|
73
|
+
const { target } = event;
|
|
74
|
+
if (target !== event.currentTarget &&
|
|
75
|
+
!registered().some((item) => item.element === target))
|
|
76
|
+
return false;
|
|
77
|
+
if (event.altKey || event.ctrlKey || event.metaKey || event.shiftKey)
|
|
78
|
+
return false;
|
|
79
|
+
const items = list.items();
|
|
80
|
+
const itemCount = items.length;
|
|
81
|
+
const activeIndex = items.indexOf(activeItem);
|
|
82
|
+
// Right to left, Left points forward. `direction` inherits, so an
|
|
83
|
+
// item's own is its list's.
|
|
84
|
+
const directionSource = (activeItem ?? items[0])?.element;
|
|
85
|
+
const [leftKey, rightKey] = directionSource && getComputedStyle(directionSource).direction === "rtl"
|
|
86
|
+
? ["ArrowRight", "ArrowLeft"]
|
|
87
|
+
: ["ArrowLeft", "ArrowRight"];
|
|
88
|
+
const [backKey, forwardKey, outKey, intoKey] = options.orientation === "horizontal"
|
|
89
|
+
? [leftKey, rightKey, "ArrowUp", "ArrowDown"]
|
|
90
|
+
: ["ArrowUp", "ArrowDown", leftKey, rightKey];
|
|
91
|
+
// Into the active item, or out of the list: the component's to act
|
|
92
|
+
// on, such as a submenu.
|
|
93
|
+
const { onArrowIn, onArrowOut } = options;
|
|
94
|
+
if (event.key === intoKey && activeItem && onArrowIn) {
|
|
95
|
+
onArrowIn(activeItem, event);
|
|
96
|
+
return true;
|
|
97
|
+
}
|
|
98
|
+
if (event.key === outKey && onArrowOut) {
|
|
99
|
+
onArrowOut(activeItem, event);
|
|
100
|
+
return true;
|
|
101
|
+
}
|
|
102
|
+
const toIndex = event.key === backKey
|
|
103
|
+
? (activeIndex < 0 ? itemCount : activeIndex) - 1
|
|
104
|
+
: event.key === forwardKey
|
|
105
|
+
? activeIndex + 1
|
|
106
|
+
: event.key === "Home"
|
|
107
|
+
? 0
|
|
108
|
+
: event.key === "End"
|
|
109
|
+
? itemCount - 1
|
|
110
|
+
: undefined;
|
|
111
|
+
if (toIndex === undefined || !itemCount)
|
|
112
|
+
return false;
|
|
113
|
+
event.preventDefault();
|
|
114
|
+
list.move(items[options.wrap
|
|
115
|
+
? (toIndex + itemCount) % itemCount
|
|
116
|
+
: Math.min(Math.max(toIndex, 0), itemCount - 1)]);
|
|
117
|
+
return true;
|
|
118
|
+
},
|
|
119
|
+
onPointerMove: (item, event) => {
|
|
120
|
+
// A finger that scrolls the list would make each item it crosses
|
|
121
|
+
// active; a tap activates an item through the component's click.
|
|
122
|
+
// A closed popup still shown, as during an exit animation, is left
|
|
123
|
+
// alone, as `move` leaves it.
|
|
124
|
+
if (options.open === false || event.pointerType === "touch")
|
|
125
|
+
return;
|
|
126
|
+
if (activeItem === item)
|
|
127
|
+
return;
|
|
128
|
+
if (!reachable(item))
|
|
129
|
+
return;
|
|
130
|
+
activate(item);
|
|
131
|
+
focusItem?.(item);
|
|
132
|
+
},
|
|
133
|
+
};
|
|
134
|
+
createEffect(() => options.open, (open) => {
|
|
135
|
+
// Set again on each opening, by the component.
|
|
136
|
+
if (open === false) {
|
|
137
|
+
activate(undefined);
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
// Once the popup is shown: after its own `showPopover()` in this
|
|
141
|
+
// flush, whichever effect runs first, as `focus/initialFocus.ts`.
|
|
142
|
+
queueMicrotask(() => {
|
|
143
|
+
if (activeItem)
|
|
144
|
+
focusAndScroll(activeItem);
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
return list;
|
|
148
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type ListItem, type ListNavigation, type ListNavigationOptions } from "./listNavigation.ts";
|
|
2
|
+
/**
|
|
3
|
+
* List navigation where focus moves to the active item (roving `tabindex`),
|
|
4
|
+
* as in a menu. The item part registers once mounted, sets `tabindex` to 0
|
|
5
|
+
* on the active item and -1 on the others, and hands its key and pointer
|
|
6
|
+
* events over:
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* const list = createRovingList<Entry>({
|
|
10
|
+
* get open() { return open(); },
|
|
11
|
+
* reachable: (item) => true,
|
|
12
|
+
* });
|
|
13
|
+
* // In the item part:
|
|
14
|
+
* onSettled(() => list.register(item));
|
|
15
|
+
* const ownProps = {
|
|
16
|
+
* get tabindex() { return list.isActive(item) ? 0 : -1; },
|
|
17
|
+
* get "data-active"() { return list.isActive(item) ? "" : undefined; },
|
|
18
|
+
* onKeyDown: (event) => list.onKeyDown(event),
|
|
19
|
+
* onPointerMove: (event) => list.onPointerMove(item, event),
|
|
20
|
+
* };
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export declare function createRovingList<T extends ListItem>(options: ListNavigationOptions<T>): ListNavigation<T>;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { createListNavigation, } from "./listNavigation.js";
|
|
2
|
+
/**
|
|
3
|
+
* List navigation where focus moves to the active item (roving `tabindex`),
|
|
4
|
+
* as in a menu. The item part registers once mounted, sets `tabindex` to 0
|
|
5
|
+
* on the active item and -1 on the others, and hands its key and pointer
|
|
6
|
+
* events over:
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* const list = createRovingList<Entry>({
|
|
10
|
+
* get open() { return open(); },
|
|
11
|
+
* reachable: (item) => true,
|
|
12
|
+
* });
|
|
13
|
+
* // In the item part:
|
|
14
|
+
* onSettled(() => list.register(item));
|
|
15
|
+
* const ownProps = {
|
|
16
|
+
* get tabindex() { return list.isActive(item) ? 0 : -1; },
|
|
17
|
+
* get "data-active"() { return list.isActive(item) ? "" : undefined; },
|
|
18
|
+
* onKeyDown: (event) => list.onKeyDown(event),
|
|
19
|
+
* onPointerMove: (event) => list.onPointerMove(item, event),
|
|
20
|
+
* };
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export function createRovingList(options) {
|
|
24
|
+
return createListNavigation(options, (item) => item.element.focus({ preventScroll: true }));
|
|
25
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** An item typeahead can move to: list navigation's item (#97). */
|
|
2
|
+
export interface TypeaheadItem {
|
|
3
|
+
readonly element: Element;
|
|
4
|
+
/** The item's label, when its content is not; else its text content. */
|
|
5
|
+
readonly text?: string;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* The item that `typed` moves to, from `active`, or `undefined`:
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* const item = findMatch(list.items(), list.active(), "ba");
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export declare function findMatch<T extends TypeaheadItem>(items: readonly T[], active: T | undefined, typed: string): T | undefined;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// Made on the first key, never at import time (PLAN.md section 6, rule 2),
|
|
2
|
+
// and kept for the page: it holds no state.
|
|
3
|
+
let collator;
|
|
4
|
+
/**
|
|
5
|
+
* The item that `typed` moves to, from `active`, or `undefined`:
|
|
6
|
+
*
|
|
7
|
+
* ```ts
|
|
8
|
+
* const item = findMatch(list.items(), list.active(), "ba");
|
|
9
|
+
* ```
|
|
10
|
+
*/
|
|
11
|
+
export function findMatch(items, active, typed) {
|
|
12
|
+
const characters = [...typed];
|
|
13
|
+
// One character, or the same one repeated: the search cycles through
|
|
14
|
+
// the items that start with it.
|
|
15
|
+
const cycles = characters.every((character) => character === characters[0]);
|
|
16
|
+
const search = cycles ? characters[0] : typed;
|
|
17
|
+
const count = items.length;
|
|
18
|
+
const index = items.indexOf(active);
|
|
19
|
+
// One character searches after the active item, a longer string from it
|
|
20
|
+
// on, so the item the first character reached stays while it matches.
|
|
21
|
+
const start = index < 0 ? 0 : index + (cycles ? 1 : 0);
|
|
22
|
+
for (let offset = 0; offset < count; offset++) {
|
|
23
|
+
const item = items[(start + offset) % count];
|
|
24
|
+
// Read now, so a label that changed is matched as it is.
|
|
25
|
+
const text = (item.text ?? item.element.textContent ?? "").trimStart();
|
|
26
|
+
// Case and accents ignored.
|
|
27
|
+
collator ??= new Intl.Collator(undefined, { sensitivity: "base" });
|
|
28
|
+
if (!collator.compare(text.slice(0, search.length), search))
|
|
29
|
+
return item;
|
|
30
|
+
}
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { type TypeaheadItem } from "./match.ts";
|
|
2
|
+
/** What `createTypeahead` returns. */
|
|
3
|
+
export interface Typeahead {
|
|
4
|
+
/**
|
|
5
|
+
* A key from one of the items, or from the element that hands them over;
|
|
6
|
+
* returns whether it used the key.
|
|
7
|
+
*/
|
|
8
|
+
onKeyDown(event: KeyboardEvent): boolean;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Typing characters moves to the item whose text starts with them
|
|
12
|
+
* (`findMatch`). Characters typed less than a second apart make one string;
|
|
13
|
+
* Space joins a string being typed. The component hands over list
|
|
14
|
+
* navigation's items, active item and move, and calls the handler from its
|
|
15
|
+
* items' key events, before its own Space activation:
|
|
16
|
+
*
|
|
17
|
+
* ```ts
|
|
18
|
+
* const typeahead = createTypeahead<Entry>({
|
|
19
|
+
* get open() { return open(); },
|
|
20
|
+
* get items() { return list.items(); },
|
|
21
|
+
* get active() { return list.active(); },
|
|
22
|
+
* move: (item) => list.move(item),
|
|
23
|
+
* });
|
|
24
|
+
* // In the item part:
|
|
25
|
+
* onKeyDown: (event) => {
|
|
26
|
+
* if (typeahead.onKeyDown(event) || list.onKeyDown(event)) return;
|
|
27
|
+
* if (event.key === " " || event.key === "Enter") activate(event);
|
|
28
|
+
* },
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* The handler cancels each key it uses. Closing the popup (`open` turning
|
|
32
|
+
* false) and disposal clear the string and its timer; opening does not, so
|
|
33
|
+
* a closed Select's trigger may open on the first key and the string goes
|
|
34
|
+
* on. Nothing runs at import time, nor, until a key, on the server.
|
|
35
|
+
*/
|
|
36
|
+
export declare function createTypeahead<T extends TypeaheadItem>(options: {
|
|
37
|
+
readonly open?: boolean;
|
|
38
|
+
readonly items: readonly T[];
|
|
39
|
+
readonly active: T | undefined;
|
|
40
|
+
move(item: T): void;
|
|
41
|
+
}): Typeahead;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { createEffect, onCleanup } from "solid-js";
|
|
2
|
+
import { findMatch } from "./match.js";
|
|
3
|
+
// How long after the last key the typed string resets: what Chromium's
|
|
4
|
+
// native <select> waits (README.md, Decisions).
|
|
5
|
+
const resetDelay = 1000;
|
|
6
|
+
/**
|
|
7
|
+
* Typing characters moves to the item whose text starts with them
|
|
8
|
+
* (`findMatch`). Characters typed less than a second apart make one string;
|
|
9
|
+
* Space joins a string being typed. The component hands over list
|
|
10
|
+
* navigation's items, active item and move, and calls the handler from its
|
|
11
|
+
* items' key events, before its own Space activation:
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* const typeahead = createTypeahead<Entry>({
|
|
15
|
+
* get open() { return open(); },
|
|
16
|
+
* get items() { return list.items(); },
|
|
17
|
+
* get active() { return list.active(); },
|
|
18
|
+
* move: (item) => list.move(item),
|
|
19
|
+
* });
|
|
20
|
+
* // In the item part:
|
|
21
|
+
* onKeyDown: (event) => {
|
|
22
|
+
* if (typeahead.onKeyDown(event) || list.onKeyDown(event)) return;
|
|
23
|
+
* if (event.key === " " || event.key === "Enter") activate(event);
|
|
24
|
+
* },
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* The handler cancels each key it uses. Closing the popup (`open` turning
|
|
28
|
+
* false) and disposal clear the string and its timer; opening does not, so
|
|
29
|
+
* a closed Select's trigger may open on the first key and the string goes
|
|
30
|
+
* on. Nothing runs at import time, nor, until a key, on the server.
|
|
31
|
+
*/
|
|
32
|
+
export function createTypeahead(options) {
|
|
33
|
+
let typed = "";
|
|
34
|
+
let timer;
|
|
35
|
+
const reset = () => {
|
|
36
|
+
clearTimeout(timer);
|
|
37
|
+
typed = "";
|
|
38
|
+
};
|
|
39
|
+
// Not on opening: a closed Select opens on the first key, and the string
|
|
40
|
+
// goes on.
|
|
41
|
+
createEffect(() => options.open, (open) => {
|
|
42
|
+
if (open === false)
|
|
43
|
+
reset();
|
|
44
|
+
});
|
|
45
|
+
onCleanup(reset);
|
|
46
|
+
return {
|
|
47
|
+
onKeyDown: (event) => {
|
|
48
|
+
const { key, target } = event;
|
|
49
|
+
const items = options.items;
|
|
50
|
+
// Only a key from one of its items, or from the element whose handler
|
|
51
|
+
// calls this, such as a closed Select's trigger: a nested list, such
|
|
52
|
+
// as a submenu in an item, takes its own keys, which bubble through
|
|
53
|
+
// it. As list navigation's check, except that `items` holds only the
|
|
54
|
+
// items the keys reach: a focused item they do not reach hands its
|
|
55
|
+
// key over from its own handler, as the target, and is taken all the
|
|
56
|
+
// same. A printable character is a key of one character, astral ones
|
|
57
|
+
// too; with Control, Alt or Meta it is a shortcut. Space is part of a
|
|
58
|
+
// string being typed, and else the part's. While an input method
|
|
59
|
+
// composes, its text arrives as events of its own.
|
|
60
|
+
if ((target !== event.currentTarget &&
|
|
61
|
+
!items.some((item) => item.element === target)) ||
|
|
62
|
+
event.isComposing ||
|
|
63
|
+
event.ctrlKey ||
|
|
64
|
+
event.altKey ||
|
|
65
|
+
event.metaKey ||
|
|
66
|
+
!/^.$/u.test(key) ||
|
|
67
|
+
(key === " " && !typed))
|
|
68
|
+
return false;
|
|
69
|
+
// So that a key typed into the string does nothing else.
|
|
70
|
+
event.preventDefault();
|
|
71
|
+
typed += key;
|
|
72
|
+
clearTimeout(timer);
|
|
73
|
+
timer = setTimeout(reset, resetDelay);
|
|
74
|
+
const active = options.active;
|
|
75
|
+
const match = findMatch(items, active, typed);
|
|
76
|
+
if (match && match !== active)
|
|
77
|
+
options.move(match);
|
|
78
|
+
return true;
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * as NavigationMenu from "./index.parts.ts";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * as NavigationMenu from "./index.parts.js";
|