@nurvus/ui 0.1.0-alpha.16 → 0.1.0-alpha.18
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/README.md +13 -0
- package/dist/icons.svg +2 -0
- package/dist/index.js +1 -1
- package/dist/nurvus.css +6 -1
- package/dist/respawn.js +1 -1
- package/dist/types/anchor.d.ts +45 -0
- package/dist/types/components/Anchor/Anchor.d.ts +33 -0
- package/dist/types/components/Dropdown/Dropdown.d.ts +31 -0
- package/dist/types/components/Kbd/Kbd.d.ts +16 -0
- package/dist/types/components/Menu/Menu.d.ts +8 -1
- package/dist/types/components/NavToggle/NavToggle.d.ts +27 -0
- package/dist/types/components/Tooltip/Tooltip.d.ts +27 -0
- package/dist/types/dropdown.d.ts +9 -0
- package/dist/types/icons.d.ts +2 -0
- package/dist/types/index.d.ts +11 -0
- package/dist/types/respawn/Anchor.d.ts +30 -0
- package/dist/types/respawn/Dropdown.d.ts +27 -0
- package/dist/types/respawn/Kbd.d.ts +10 -0
- package/dist/types/respawn/Menu.d.ts +2 -0
- package/dist/types/respawn/NavToggle.d.ts +22 -0
- package/dist/types/respawn/Tooltip.d.ts +20 -0
- package/dist/types/respawn/index.d.ts +11 -0
- package/dist/types/tooltip.d.ts +4 -0
- package/package.json +1 -1
- package/src/anchor.ts +150 -0
- package/src/components/Anchor/Anchor.md +59 -0
- package/src/components/Anchor/Anchor.scss +88 -0
- package/src/components/Anchor/Anchor.tsx +114 -0
- package/src/components/Dropdown/Dropdown.md +48 -0
- package/src/components/Dropdown/Dropdown.scss +67 -0
- package/src/components/Dropdown/Dropdown.tsx +110 -0
- package/src/components/Kbd/Kbd.md +14 -0
- package/src/components/Kbd/Kbd.scss +30 -0
- package/src/components/Kbd/Kbd.tsx +28 -0
- package/src/components/Menu/Menu.md +1 -1
- package/src/components/Menu/Menu.scss +78 -0
- package/src/components/Menu/Menu.tsx +15 -3
- package/src/components/NavToggle/NavToggle.md +51 -0
- package/src/components/NavToggle/NavToggle.scss +162 -0
- package/src/components/NavToggle/NavToggle.tsx +68 -0
- package/src/components/Tooltip/Tooltip.md +19 -0
- package/src/components/Tooltip/Tooltip.scss +75 -0
- package/src/components/Tooltip/Tooltip.tsx +69 -0
- package/src/css.ts +5 -0
- package/src/dropdown.ts +24 -0
- package/src/icons.ts +3 -0
- package/src/index.ts +11 -0
- package/src/respawn/Anchor.ts +135 -0
- package/src/respawn/Dropdown.ts +90 -0
- package/src/respawn/Kbd.ts +22 -0
- package/src/respawn/Menu.ts +10 -7
- package/src/respawn/NavToggle.ts +78 -0
- package/src/respawn/Tooltip.ts +63 -0
- package/src/respawn/index.ts +11 -0
- package/src/tooltip.ts +14 -0
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export interface AnchorItem {
|
|
2
|
+
/** "#id" of the section (or any link). */
|
|
3
|
+
href: string;
|
|
4
|
+
/** What the row says. */
|
|
5
|
+
label: string;
|
|
6
|
+
/** 1 for a top-level section, 2 for one inside it, and so on. Defaults to 1. */
|
|
7
|
+
level?: number;
|
|
8
|
+
}
|
|
9
|
+
export declare const ANCHOR_ROW = 28;
|
|
10
|
+
export declare const ANCHOR_INDENT = 12;
|
|
11
|
+
export interface AnchorRail {
|
|
12
|
+
/** The path, in units (1 unit = 1px at the default text size). */
|
|
13
|
+
d: string;
|
|
14
|
+
width: number;
|
|
15
|
+
height: number;
|
|
16
|
+
/** The whole path's length. */
|
|
17
|
+
length: number;
|
|
18
|
+
/** Each row's stretch along the path: [start, end]. */
|
|
19
|
+
spans: [number, number][];
|
|
20
|
+
/** Where the line starts and ends (a dot marks each). */
|
|
21
|
+
caps: [[number, number], [number, number]];
|
|
22
|
+
}
|
|
23
|
+
/** The rail for rows at these levels. */
|
|
24
|
+
export declare function anchorRail(levels: number[]): AnchorRail;
|
|
25
|
+
/** The dash that lights rows `from` to `to` (the sections in view): stroke-dasharray and stroke-dashoffset. */
|
|
26
|
+
export declare function anchorDash(rail: AnchorRail, from: number, to?: number): {
|
|
27
|
+
dasharray: string;
|
|
28
|
+
dashoffset: number;
|
|
29
|
+
};
|
|
30
|
+
/** The id an "#id" href points at, or null for other links. */
|
|
31
|
+
export declare const anchorTarget: (href: string) => string | null;
|
|
32
|
+
/**
|
|
33
|
+
* Which row is in view: the last section whose top has passed `offset` pixels
|
|
34
|
+
* below the top of the scroll area (the first one before any has), and the
|
|
35
|
+
* last one once the area is scrolled to its end.
|
|
36
|
+
*/
|
|
37
|
+
export declare function anchorActive(items: AnchorItem[], offset: number, scrollRoot?: Element | null): number;
|
|
38
|
+
/**
|
|
39
|
+
* The sections in view: [first, last]. A section runs from its heading to the
|
|
40
|
+
* next one; it is in view when any of it is between `offset` pixels from the
|
|
41
|
+
* top of the scroll area and the bottom. The first one is the one being read.
|
|
42
|
+
*/
|
|
43
|
+
export declare function anchorRange(items: AnchorItem[], offset: number, scrollRoot?: Element | null): [number, number];
|
|
44
|
+
/** Scrolls a section into view (smoothly unless people asked for less motion) and moves focus to it. */
|
|
45
|
+
export declare function anchorGo(href: string, updateHash: boolean): boolean;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { AnchorItem } from '../../anchor';
|
|
3
|
+
export type { AnchorItem };
|
|
4
|
+
export interface AnchorProps extends Omit<React.HTMLAttributes<HTMLElement>, 'title'> {
|
|
5
|
+
/** The sections, in page order: each an "#id" href, a label and a level (1, 2…). */
|
|
6
|
+
items: AnchorItem[];
|
|
7
|
+
/** The heading above the list. */
|
|
8
|
+
title?: string;
|
|
9
|
+
/** Prefix for the ids inside (the title is "<id>-title"). Defaults to "anchor". */
|
|
10
|
+
id?: string;
|
|
11
|
+
/** How far below the top a section's heading must be scrolled to count as "in view", in pixels (room for a sticky header). */
|
|
12
|
+
offset?: number;
|
|
13
|
+
/** The section in view, set by you (its href). Left out, the Anchor follows the scrolling itself. */
|
|
14
|
+
current?: string;
|
|
15
|
+
/** Scroll-spy inside this element instead of the page: an element or a CSS selector. */
|
|
16
|
+
scrollRoot?: Element | string;
|
|
17
|
+
/** Writes the section's #id in the address bar when one is picked. Turn off for hash routers. */
|
|
18
|
+
updateHash?: boolean;
|
|
19
|
+
/** Called when a row is picked, with its href. Call event.preventDefault() to handle it yourself. */
|
|
20
|
+
onNavigate?: (href: string, event: React.MouseEvent<HTMLAnchorElement>) => void;
|
|
21
|
+
/** A soft wash behind the row being read, fading to the right. */
|
|
22
|
+
fade?: boolean;
|
|
23
|
+
/** Custom class name. */
|
|
24
|
+
className?: string;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* "On this page": the sections of a long page, the ones in view marked as you
|
|
28
|
+
* scroll. A rail runs down beside the rows from a dot to a dot, bending in and
|
|
29
|
+
* out with the nesting, and a highlight slides along it over the sections in
|
|
30
|
+
* view (their names brighten; the first is the one being read). Picking a row
|
|
31
|
+
* scrolls to the section and moves focus there.
|
|
32
|
+
*/
|
|
33
|
+
export default function Anchor({ items, title, id, offset, current, scrollRoot, updateHash, onNavigate, fade, className, ...restProps }: AnchorProps): React.ReactElement;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { ButtonSize, ButtonType } from '../Button/Button';
|
|
3
|
+
import { MenuItem, MenuSection } from '../Menu/Menu';
|
|
4
|
+
export interface DropdownProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'onSelect'> {
|
|
5
|
+
/** What the button says (text, or an Icon with a label for an icon-only button). */
|
|
6
|
+
label: React.ReactNode;
|
|
7
|
+
/** The actions, in groups (each may have a title). */
|
|
8
|
+
sections?: MenuSection[];
|
|
9
|
+
/** Or the actions as one flat list. */
|
|
10
|
+
items?: MenuItem[];
|
|
11
|
+
/** Which edge of the button the menu lines up with. */
|
|
12
|
+
align?: 'start' | 'end';
|
|
13
|
+
/** The button's look. Defaults to secondary. */
|
|
14
|
+
type?: ButtonType;
|
|
15
|
+
/** The button's size. */
|
|
16
|
+
size?: ButtonSize;
|
|
17
|
+
/** Stops the button working. */
|
|
18
|
+
disabled?: boolean;
|
|
19
|
+
/** Names the button and the menu ("<id>-button", "<id>-menu"). */
|
|
20
|
+
id: string;
|
|
21
|
+
/** Custom class name. */
|
|
22
|
+
className?: string;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* A button that opens a menu of actions (the WAI-ARIA menu button pattern).
|
|
26
|
+
* A click, Enter, Space or ↓ opens it on its first item, ↑ on its last; inside,
|
|
27
|
+
* the Menu's keys work (↑ ↓ Home End, type a letter); choosing an action runs
|
|
28
|
+
* it, closes the menu and puts focus back on the button; Esc does the same
|
|
29
|
+
* without choosing; Tab or a click outside closes it.
|
|
30
|
+
*/
|
|
31
|
+
export default function Dropdown({ label, sections, items, align, type, size, disabled, id, className, ...restProps }: DropdownProps): React.ReactElement;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
export interface KbdProps extends React.HTMLAttributes<HTMLElement> {
|
|
3
|
+
/** The keys of a shortcut, in order: ['⌘', 'B']. Each is drawn as a key cap. */
|
|
4
|
+
keys?: string[];
|
|
5
|
+
/** One key, when there is only one (instead of keys). */
|
|
6
|
+
children?: React.ReactNode;
|
|
7
|
+
/** Custom class name. */
|
|
8
|
+
className?: string;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* A keyboard key, or a shortcut of several (⌘ B), drawn as key caps. The HTML
|
|
12
|
+
* way: a <kbd> for the shortcut holding a <kbd> per key, so screen readers and
|
|
13
|
+
* search engines know it is keyboard input. Sized in em, so it matches the
|
|
14
|
+
* text around it.
|
|
15
|
+
*/
|
|
16
|
+
export default function Kbd({ keys, children, className, ...restProps }: KbdProps): React.ReactElement;
|
|
@@ -43,6 +43,13 @@ export interface MenuProps extends Omit<React.HTMLAttributes<HTMLElement>, 'onTo
|
|
|
43
43
|
onToggle?: (id: string, open: boolean) => void;
|
|
44
44
|
/** Navigation: called when any link is clicked, with its href (e.g. to close a drawer). */
|
|
45
45
|
onNavigate?: (href: string, event: React.MouseEvent<HTMLAnchorElement>) => void;
|
|
46
|
+
/**
|
|
47
|
+
* Navigation: where each section's chevron sits. 'start' (the default): before
|
|
48
|
+
* the title, pointing right when closed and down when open, with a guide line
|
|
49
|
+
* down the open section's items (as Material UI draws it). 'end': after the
|
|
50
|
+
* title, down when closed and up when open.
|
|
51
|
+
*/
|
|
52
|
+
chevron?: 'start' | 'end';
|
|
46
53
|
/** Tree: the rows, each with optional child `items`. */
|
|
47
54
|
items?: TreeItem[];
|
|
48
55
|
/** Tree: the picked row as a path of indexes, e.g. [2, 0] = the first child of the third row. */
|
|
@@ -55,7 +62,7 @@ export interface MenuProps extends Omit<React.HTMLAttributes<HTMLElement>, 'onTo
|
|
|
55
62
|
maxIndentDepth?: number;
|
|
56
63
|
/** What screen readers call the menu ("Pages", "Actions", "Layers"). */
|
|
57
64
|
label?: string;
|
|
58
|
-
/** Prefix for the ids inside (each toggle names its list). Defaults to "menu". */
|
|
65
|
+
/** Prefix for the ids inside (each toggle names its list). Defaults to "menu". A menu of actions also carries it (so a button can point at it with aria-controls). */
|
|
59
66
|
id?: string;
|
|
60
67
|
/** Custom class name. */
|
|
61
68
|
className?: string;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
export type NavToggleMorph = 'slide' | 'cross' | 'squeeze' | 'elastic' | 'spin' | 'arrow' | 'chevron' | 'collapse';
|
|
3
|
+
export interface NavToggleProps extends Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, 'type'> {
|
|
4
|
+
/** Whether the navigation it controls is showing. */
|
|
5
|
+
open: boolean;
|
|
6
|
+
/** 'hamburger' (three bars, for a menu that covers the page) or 'sidebar' (a window with a side pane, for a sidebar beside the page). */
|
|
7
|
+
variant?: 'hamburger' | 'sidebar';
|
|
8
|
+
/** Hamburger: how the bars change while open. 'slide' (default): a cross, the middle bar sliding out to the left. */
|
|
9
|
+
morph?: NavToggleMorph;
|
|
10
|
+
/** Hamburger: three bars of one length, or 'asymmetric' (long, short, middle) that line up when pointed at or open. */
|
|
11
|
+
lines?: 'even' | 'asymmetric';
|
|
12
|
+
/** What it shows and hides, for its name: "Show navigation" / "Hide navigation". */
|
|
13
|
+
label?: string;
|
|
14
|
+
/** The id of the navigation it controls (aria-controls). */
|
|
15
|
+
controls?: string;
|
|
16
|
+
/** Custom class name. */
|
|
17
|
+
className?: string;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* The button that shows and hides navigation. Two looks: the hamburger, whose
|
|
21
|
+
* bars move into another shape while the navigation is open (eight morphs),
|
|
22
|
+
* and the sidebar icon, a window whose side pane fills while the sidebar shows
|
|
23
|
+
* and empties as its divider slides away. The shapes move, they aren't
|
|
24
|
+
* swapped; instantly for reduced motion. It says what it does ("Hide
|
|
25
|
+
* navigation") and whether it is open (aria-expanded).
|
|
26
|
+
*/
|
|
27
|
+
export default function NavToggle({ open, variant, morph, lines, label, controls, className, ...restProps }: NavToggleProps): React.ReactElement;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
export interface TooltipProps {
|
|
3
|
+
/** What the tooltip says. */
|
|
4
|
+
content: React.ReactNode;
|
|
5
|
+
/** A keyboard shortcut to show after it, as key caps: ['⌘', 'B']. */
|
|
6
|
+
shortcut?: string[];
|
|
7
|
+
/** The tooltip's id: the control is described by it (aria-describedby). */
|
|
8
|
+
id: string;
|
|
9
|
+
/** Below the control (default) or above it. */
|
|
10
|
+
placement?: 'bottom' | 'top';
|
|
11
|
+
/** Which edge it lines up with: centered (default), or the control's start or end edge (near the screen's edges). */
|
|
12
|
+
align?: 'center' | 'start' | 'end';
|
|
13
|
+
/** How long the pointer must rest on the control before it shows, in ms. Keyboard focus shows it at once. */
|
|
14
|
+
delay?: number;
|
|
15
|
+
/** The one control it describes. */
|
|
16
|
+
children: React.ReactElement;
|
|
17
|
+
/** Custom class name. */
|
|
18
|
+
className?: string;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* A short label for a control, shown while the pointer rests on it or it has
|
|
22
|
+
* keyboard focus; optionally with its keyboard shortcut as key caps. It meets
|
|
23
|
+
* WCAG 1.4.13: Esc hides it without moving focus, the pointer can move onto
|
|
24
|
+
* it, and it stays until the pointer or focus leaves. Screen readers get it as
|
|
25
|
+
* the control's description.
|
|
26
|
+
*/
|
|
27
|
+
export default function Tooltip({ content, shortcut, id, placement, align, delay, children, className }: TooltipProps): React.ReactElement;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** The menu's items, in order. */
|
|
2
|
+
export declare const menuItems: (root: HTMLElement) => HTMLElement[];
|
|
3
|
+
/** Moves focus to the first or last item (and makes it the menu's Tab stop). */
|
|
4
|
+
export declare function focusItem(root: HTMLElement, which: 'first' | 'last'): void;
|
|
5
|
+
/**
|
|
6
|
+
* Keys on the button: ↓ (or Enter / Space, which click it) opens the menu on
|
|
7
|
+
* its first item, ↑ on its last. Returns which item to focus, or null.
|
|
8
|
+
*/
|
|
9
|
+
export declare function triggerKey(event: KeyboardEvent): 'first' | 'last' | null;
|
package/dist/types/icons.d.ts
CHANGED
|
@@ -63,9 +63,11 @@ declare const DEFS: {
|
|
|
63
63
|
heart: IconNode[];
|
|
64
64
|
grid: IconNode[];
|
|
65
65
|
list: IconNode[];
|
|
66
|
+
'align-left': IconNode[];
|
|
66
67
|
link: IconNode[];
|
|
67
68
|
sun: IconNode[];
|
|
68
69
|
moon: IconNode[];
|
|
70
|
+
contrast: IconNode[];
|
|
69
71
|
palette: IconNode[];
|
|
70
72
|
sparkles: IconNode[];
|
|
71
73
|
zap: IconNode[];
|
package/dist/types/index.d.ts
CHANGED
|
@@ -26,6 +26,17 @@ export { default as Loader } from './components/Loader/Loader';
|
|
|
26
26
|
export type { LoaderProps } from './components/Loader/Loader';
|
|
27
27
|
export { default as Tree } from './components/Tree/Tree';
|
|
28
28
|
export type { TreeProps, TreeItem } from './components/Tree/Tree';
|
|
29
|
+
export { default as Anchor } from './components/Anchor/Anchor';
|
|
30
|
+
export type { AnchorProps, AnchorItem } from './components/Anchor/Anchor';
|
|
31
|
+
export { default as Dropdown } from './components/Dropdown/Dropdown';
|
|
32
|
+
export type { DropdownProps } from './components/Dropdown/Dropdown';
|
|
33
|
+
export { default as Kbd } from './components/Kbd/Kbd';
|
|
34
|
+
export type { KbdProps } from './components/Kbd/Kbd';
|
|
35
|
+
export { default as Tooltip } from './components/Tooltip/Tooltip';
|
|
36
|
+
export type { TooltipProps } from './components/Tooltip/Tooltip';
|
|
37
|
+
export { default as NavToggle } from './components/NavToggle/NavToggle';
|
|
38
|
+
export type { NavToggleProps, NavToggleMorph } from './components/NavToggle/NavToggle';
|
|
39
|
+
export { modKey } from './tooltip';
|
|
29
40
|
export { default as Menu } from './components/Menu/Menu';
|
|
30
41
|
export type { MenuProps, MenuListProps, MenuTreeProps, MenuItem, MenuSection, MenuKind } from './components/Menu/Menu';
|
|
31
42
|
export { default as Select } from './components/Select/Select';
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { AnchorItem } from '../anchor';
|
|
2
|
+
import { ElementProps } from './spawn';
|
|
3
|
+
import { NurvusElement } from './update';
|
|
4
|
+
export type { AnchorItem };
|
|
5
|
+
export interface AnchorProps extends ElementProps {
|
|
6
|
+
/** The sections, in page order: each an "#id" href, a label and a level (1, 2…). */
|
|
7
|
+
items: AnchorItem[];
|
|
8
|
+
/** The heading above the list. */
|
|
9
|
+
title?: string;
|
|
10
|
+
/** Prefix for the ids inside (the title is "<id>-title"). Defaults to "anchor". */
|
|
11
|
+
id?: string;
|
|
12
|
+
/** How far below the top a section's heading must be scrolled to count as "in view", in pixels. */
|
|
13
|
+
offset?: number;
|
|
14
|
+
/** The section in view, set by you (its href). Left out, the Anchor follows the scrolling itself. */
|
|
15
|
+
current?: string;
|
|
16
|
+
/** Scroll-spy inside this element instead of the page: an element or a CSS selector. */
|
|
17
|
+
scrollRoot?: Element | string;
|
|
18
|
+
/** Writes the section's #id in the address bar when one is picked. Turn off for hash routers. */
|
|
19
|
+
updateHash?: boolean;
|
|
20
|
+
/** Called when a row is picked, with its href. Call event.preventDefault() to handle it yourself. */
|
|
21
|
+
onNavigate?: (href: string, event: MouseEvent) => void;
|
|
22
|
+
/** A soft wash behind the row being read, fading to the right. */
|
|
23
|
+
fade?: boolean;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* "On this page", the same markup as the React Anchor: the rail bends with the
|
|
27
|
+
* nesting and its highlight slides to the section in view, in place (the
|
|
28
|
+
* element stays the same while you scroll).
|
|
29
|
+
*/
|
|
30
|
+
export default function Anchor(props: AnchorProps): NurvusElement<HTMLElement, AnchorProps>;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { ButtonSize, ButtonType } from './Button';
|
|
2
|
+
import { MenuItem, MenuSection } from './Menu';
|
|
3
|
+
import { Child, ElementProps } from './spawn';
|
|
4
|
+
import { NurvusElement } from './update';
|
|
5
|
+
export interface DropdownProps extends ElementProps {
|
|
6
|
+
/** What the button says (text, or an Icon with a label for an icon-only button). */
|
|
7
|
+
label: Child;
|
|
8
|
+
/** The actions, in groups (each may have a title). */
|
|
9
|
+
sections?: MenuSection[];
|
|
10
|
+
/** Or the actions as one flat list. */
|
|
11
|
+
items?: MenuItem[];
|
|
12
|
+
/** Which edge of the button the menu lines up with. */
|
|
13
|
+
align?: 'start' | 'end';
|
|
14
|
+
/** The button's look. Defaults to secondary. */
|
|
15
|
+
type?: ButtonType;
|
|
16
|
+
/** The button's size. */
|
|
17
|
+
size?: ButtonSize;
|
|
18
|
+
/** Stops the button working. */
|
|
19
|
+
disabled?: boolean;
|
|
20
|
+
/** Names the button and the menu ("<id>-button", "<id>-menu"). */
|
|
21
|
+
id: string;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* A button that opens a menu of actions, the same markup and keys as the React
|
|
25
|
+
* Dropdown. Opening and closing change it in place (the element stays the same).
|
|
26
|
+
*/
|
|
27
|
+
export default function Dropdown(props: DropdownProps): NurvusElement<HTMLDivElement, DropdownProps>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Child, ElementProps } from './spawn';
|
|
2
|
+
import { NurvusElement } from './update';
|
|
3
|
+
export interface KbdProps extends ElementProps {
|
|
4
|
+
/** The keys of a shortcut, in order: ['⌘', 'B']. Each is drawn as a key cap. */
|
|
5
|
+
keys?: string[];
|
|
6
|
+
/** One key, when there is only one (instead of keys). */
|
|
7
|
+
children?: Child;
|
|
8
|
+
}
|
|
9
|
+
/** A keyboard key or shortcut as key caps: the same markup as the React Kbd. */
|
|
10
|
+
export default function Kbd(props?: KbdProps): NurvusElement<HTMLElement, KbdProps>;
|
|
@@ -41,6 +41,8 @@ interface MenuBaseProps extends ElementProps {
|
|
|
41
41
|
label?: string;
|
|
42
42
|
/** Prefix for the ids inside (each toggle names its list). Defaults to "menu". */
|
|
43
43
|
id?: string;
|
|
44
|
+
/** Navigation: the chevron before the title ('start', the default: right when closed, down when open, a guide line down the items) or after it ('end'). */
|
|
45
|
+
chevron?: 'start' | 'end';
|
|
44
46
|
}
|
|
45
47
|
export interface MenuProps extends MenuBaseProps {
|
|
46
48
|
/** What the menu is: 'nav' (links), 'actions' (commands) or 'tree' (rows to pick from, with Tree's props). Left out, it follows the props. */
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { ElementProps } from './spawn';
|
|
2
|
+
import { NurvusElement } from './update';
|
|
3
|
+
export type NavToggleMorph = 'slide' | 'cross' | 'squeeze' | 'elastic' | 'spin' | 'arrow' | 'chevron' | 'collapse';
|
|
4
|
+
export interface NavToggleProps extends ElementProps {
|
|
5
|
+
/** Whether the navigation it controls is showing. */
|
|
6
|
+
open: boolean;
|
|
7
|
+
/** 'hamburger' (three bars) or 'sidebar' (a window with a side pane). */
|
|
8
|
+
variant?: 'hamburger' | 'sidebar';
|
|
9
|
+
/** Hamburger: how the bars change while open. */
|
|
10
|
+
morph?: NavToggleMorph;
|
|
11
|
+
/** Hamburger: bars of one length, or 'asymmetric'. */
|
|
12
|
+
lines?: 'even' | 'asymmetric';
|
|
13
|
+
/** What it shows and hides, for its name. */
|
|
14
|
+
label?: string;
|
|
15
|
+
/** The id of the navigation it controls (aria-controls). */
|
|
16
|
+
controls?: string;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* The show/hide navigation button, the same markup as the React NavToggle.
|
|
20
|
+
* toggle.update({ open }) changes it in place, so the shapes animate.
|
|
21
|
+
*/
|
|
22
|
+
export default function NavToggle(props: NavToggleProps): NurvusElement<HTMLButtonElement, NavToggleProps>;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Child, ElementProps } from './spawn';
|
|
2
|
+
import { NurvusElement } from './update';
|
|
3
|
+
export interface TooltipProps extends ElementProps {
|
|
4
|
+
/** What the tooltip says. */
|
|
5
|
+
content: Child;
|
|
6
|
+
/** A keyboard shortcut to show after it, as key caps: ['⌘', 'B']. */
|
|
7
|
+
shortcut?: string[];
|
|
8
|
+
/** The tooltip's id: the control is described by it (aria-describedby). */
|
|
9
|
+
id: string;
|
|
10
|
+
/** Below the control (default) or above it. */
|
|
11
|
+
placement?: 'bottom' | 'top';
|
|
12
|
+
/** Which edge it lines up with: centered (default), or the control's start or end edge. */
|
|
13
|
+
align?: 'center' | 'start' | 'end';
|
|
14
|
+
/** How long the pointer must rest on the control before it shows, in ms. */
|
|
15
|
+
delay?: number;
|
|
16
|
+
/** The one control it describes (an element: it gets aria-describedby). */
|
|
17
|
+
children: HTMLElement;
|
|
18
|
+
}
|
|
19
|
+
/** A short label for a control, the same markup and behavior as the React Tooltip; it opens and closes in place. */
|
|
20
|
+
export default function Tooltip(props: TooltipProps): NurvusElement<HTMLSpanElement, TooltipProps>;
|
|
@@ -29,6 +29,17 @@ export { BREAKPOINTS, isAtLeast } from '../breakpoints';
|
|
|
29
29
|
export type { Breakpoint, Responsive } from '../breakpoints';
|
|
30
30
|
export { default as Loader } from './Loader';
|
|
31
31
|
export type { LoaderProps } from './Loader';
|
|
32
|
+
export { default as Anchor } from './Anchor';
|
|
33
|
+
export type { AnchorProps, AnchorItem } from './Anchor';
|
|
34
|
+
export { default as Dropdown } from './Dropdown';
|
|
35
|
+
export type { DropdownProps } from './Dropdown';
|
|
36
|
+
export { default as Kbd } from './Kbd';
|
|
37
|
+
export type { KbdProps } from './Kbd';
|
|
38
|
+
export { default as Tooltip } from './Tooltip';
|
|
39
|
+
export type { TooltipProps } from './Tooltip';
|
|
40
|
+
export { default as NavToggle } from './NavToggle';
|
|
41
|
+
export type { NavToggleProps, NavToggleMorph } from './NavToggle';
|
|
42
|
+
export { modKey } from '../tooltip';
|
|
32
43
|
export { default as Menu } from './Menu';
|
|
33
44
|
export type { MenuProps, MenuListProps, MenuTreeProps, MenuItem, MenuSection, MenuKind } from './Menu';
|
|
34
45
|
export { default as Select } from './Select';
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/** True when focus on `el` came from the keyboard (the browser shows a focus ring). */
|
|
2
|
+
export declare const keyboardFocus: (el: EventTarget | null) => boolean;
|
|
3
|
+
/** The platform's modifier for shortcuts: ⌘ on Apple devices, Ctrl elsewhere. */
|
|
4
|
+
export declare const modKey: () => "\u2318" | "Ctrl";
|
package/package.json
CHANGED
package/src/anchor.ts
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
// Anchor rules shared by the React and the Respawn Anchor (no React here):
|
|
2
|
+
// the rail's drawing and which section is in view.
|
|
3
|
+
//
|
|
4
|
+
// The rail is drawn from the items alone (no measuring), so it is the same on
|
|
5
|
+
// the server, in React and in Respawn: every row is 1.75rem (28 units at the
|
|
6
|
+
// default 16px), each level indents 0.75rem (12 units), and between rows at
|
|
7
|
+
// different levels the line bends with a short S-curve. The highlight is the
|
|
8
|
+
// same path with a dash over the active row's stretch; moving the dash slides
|
|
9
|
+
// it along the curves.
|
|
10
|
+
|
|
11
|
+
export interface AnchorItem {
|
|
12
|
+
/** "#id" of the section (or any link). */
|
|
13
|
+
href: string;
|
|
14
|
+
/** What the row says. */
|
|
15
|
+
label: string;
|
|
16
|
+
/** 1 for a top-level section, 2 for one inside it, and so on. Defaults to 1. */
|
|
17
|
+
level?: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export const ANCHOR_ROW = 28;
|
|
21
|
+
export const ANCHOR_INDENT = 12;
|
|
22
|
+
const X0 = 1.5; // the line's x at level 1 (inside a 3-unit gutter)
|
|
23
|
+
const BEND = 7; // half the height of an S-curve
|
|
24
|
+
const END = 5; // the line stops this far inside the first and last rows
|
|
25
|
+
|
|
26
|
+
const xAt = (level: number) => X0 + (Math.max(1, level) - 1) * ANCHOR_INDENT;
|
|
27
|
+
|
|
28
|
+
/** Length of a cubic Bézier, by sampling (deterministic). */
|
|
29
|
+
function cubicLength(p0: number[], p1: number[], p2: number[], p3: number[]): number {
|
|
30
|
+
let len = 0;
|
|
31
|
+
let prev = p0;
|
|
32
|
+
for (let i = 1; i <= 24; i += 1) {
|
|
33
|
+
const t = i / 24; const u = 1 - t;
|
|
34
|
+
const pt = [0, 1].map((k) => u * u * u * p0[k] + 3 * u * u * t * p1[k] + 3 * u * t * t * p2[k] + t * t * t * p3[k]);
|
|
35
|
+
len += Math.hypot(pt[0] - prev[0], pt[1] - prev[1]);
|
|
36
|
+
prev = pt;
|
|
37
|
+
}
|
|
38
|
+
return len;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const round = (n: number) => Math.round(n * 100) / 100;
|
|
42
|
+
|
|
43
|
+
export interface AnchorRail {
|
|
44
|
+
/** The path, in units (1 unit = 1px at the default text size). */
|
|
45
|
+
d: string;
|
|
46
|
+
width: number;
|
|
47
|
+
height: number;
|
|
48
|
+
/** The whole path's length. */
|
|
49
|
+
length: number;
|
|
50
|
+
/** Each row's stretch along the path: [start, end]. */
|
|
51
|
+
spans: [number, number][];
|
|
52
|
+
/** Where the line starts and ends (a dot marks each). */
|
|
53
|
+
caps: [[number, number], [number, number]];
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** The rail for rows at these levels. */
|
|
57
|
+
export function anchorRail(levels: number[]): AnchorRail {
|
|
58
|
+
const n = levels.length;
|
|
59
|
+
if (!n) return { d: '', width: 0, height: 0, length: 0, spans: [], caps: [[0, 0], [0, 0]] };
|
|
60
|
+
const x = levels.map(xAt);
|
|
61
|
+
const parts: string[] = [];
|
|
62
|
+
const spans: [number, number][] = [];
|
|
63
|
+
let length = 0;
|
|
64
|
+
let y = END;
|
|
65
|
+
parts.push(`M${round(x[0])} ${y}`);
|
|
66
|
+
for (let i = 0; i < n; i += 1) {
|
|
67
|
+
const bottom = (i + 1) * ANCHOR_ROW;
|
|
68
|
+
const bendsBelow = i < n - 1 && x[i + 1] !== x[i];
|
|
69
|
+
const straightEnd = i === n - 1 ? bottom - END : bendsBelow ? bottom - BEND : bottom;
|
|
70
|
+
const start = length;
|
|
71
|
+
length += straightEnd - y;
|
|
72
|
+
parts.push(`L${round(x[i])} ${straightEnd}`);
|
|
73
|
+
spans.push([round(start), round(length)]);
|
|
74
|
+
y = straightEnd;
|
|
75
|
+
if (bendsBelow) {
|
|
76
|
+
const p0 = [x[i], bottom - BEND]; const p1 = [x[i], bottom]; const p2 = [x[i + 1], bottom]; const p3 = [x[i + 1], bottom + BEND];
|
|
77
|
+
parts.push(`C${round(x[i])} ${bottom} ${round(x[i + 1])} ${bottom} ${round(x[i + 1])} ${bottom + BEND}`);
|
|
78
|
+
length += cubicLength(p0, p1, p2, p3);
|
|
79
|
+
y = bottom + BEND;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
const width = Math.max(...x) + X0;
|
|
83
|
+
const caps: [[number, number], [number, number]] = [[round(x[0]), END], [round(x[n - 1]), n * ANCHOR_ROW - END]];
|
|
84
|
+
return { d: parts.join(''), width: round(width), height: n * ANCHOR_ROW, length: round(length), spans, caps };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** The dash that lights rows `from` to `to` (the sections in view): stroke-dasharray and stroke-dashoffset. */
|
|
88
|
+
export function anchorDash(rail: AnchorRail, from: number, to: number = from): { dasharray: string; dashoffset: number } {
|
|
89
|
+
const a = rail.spans[from]; const b = rail.spans[Math.max(from, to)];
|
|
90
|
+
if (!a || !b) return { dasharray: `0 ${rail.length + 1}`, dashoffset: 0 };
|
|
91
|
+
return { dasharray: `${round(b[1] - a[0])} ${round(rail.length + 1)}`, dashoffset: -a[0] };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** The id an "#id" href points at, or null for other links. */
|
|
95
|
+
export const anchorTarget = (href: string): string | null => (href.startsWith('#') && href.length > 1 ? decodeURIComponent(href.slice(1)) : null);
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Which row is in view: the last section whose top has passed `offset` pixels
|
|
99
|
+
* below the top of the scroll area (the first one before any has), and the
|
|
100
|
+
* last one once the area is scrolled to its end.
|
|
101
|
+
*/
|
|
102
|
+
export function anchorActive(items: AnchorItem[], offset: number, scrollRoot?: Element | null): number {
|
|
103
|
+
const ids = items.map((item) => anchorTarget(item.href));
|
|
104
|
+
const top = scrollRoot ? scrollRoot.getBoundingClientRect().top : 0;
|
|
105
|
+
const atEnd = scrollRoot
|
|
106
|
+
? scrollRoot.scrollTop + scrollRoot.clientHeight >= scrollRoot.scrollHeight - 2
|
|
107
|
+
: window.innerHeight + window.scrollY >= document.documentElement.scrollHeight - 2;
|
|
108
|
+
let active = 0;
|
|
109
|
+
ids.forEach((id, i) => {
|
|
110
|
+
const el = id ? document.getElementById(id) : null;
|
|
111
|
+
if (el && el.getBoundingClientRect().top - top <= offset) active = i;
|
|
112
|
+
});
|
|
113
|
+
if (atEnd) {
|
|
114
|
+
for (let i = ids.length - 1; i >= 0; i -= 1) if (ids[i] && document.getElementById(ids[i] as string)) return i;
|
|
115
|
+
}
|
|
116
|
+
return active;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* The sections in view: [first, last]. A section runs from its heading to the
|
|
121
|
+
* next one; it is in view when any of it is between `offset` pixels from the
|
|
122
|
+
* top of the scroll area and the bottom. The first one is the one being read.
|
|
123
|
+
*/
|
|
124
|
+
export function anchorRange(items: AnchorItem[], offset: number, scrollRoot?: Element | null): [number, number] {
|
|
125
|
+
const tops = items.map((item) => { const id = anchorTarget(item.href); const el = id ? document.getElementById(id) : null; return el ? el.getBoundingClientRect().top : null; });
|
|
126
|
+
const box = scrollRoot ? scrollRoot.getBoundingClientRect() : { top: 0, bottom: window.innerHeight };
|
|
127
|
+
const end = scrollRoot ? box.top + scrollRoot.scrollHeight - scrollRoot.scrollTop : document.documentElement.scrollHeight - window.scrollY;
|
|
128
|
+
let first = -1; let last = -1;
|
|
129
|
+
tops.forEach((top, i) => {
|
|
130
|
+
if (top === null) return;
|
|
131
|
+
let next = end;
|
|
132
|
+
for (let k = i + 1; k < tops.length; k += 1) if (tops[k] !== null) { next = tops[k] as number; break; }
|
|
133
|
+
if (next > box.top + offset && top < box.bottom) { if (first < 0) first = i; last = i; }
|
|
134
|
+
});
|
|
135
|
+
if (first < 0) { const one = anchorActive(items, offset, scrollRoot); return [one, one]; }
|
|
136
|
+
return [first, last];
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Scrolls a section into view (smoothly unless people asked for less motion) and moves focus to it. */
|
|
140
|
+
export function anchorGo(href: string, updateHash: boolean): boolean {
|
|
141
|
+
const id = anchorTarget(href);
|
|
142
|
+
const target = id ? document.getElementById(id) : null;
|
|
143
|
+
if (!target) return false;
|
|
144
|
+
const still = typeof window.matchMedia === 'function' && window.matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
145
|
+
target.scrollIntoView({ behavior: still ? 'auto' : 'smooth', block: 'start' });
|
|
146
|
+
if (!target.hasAttribute('tabindex')) target.setAttribute('tabindex', '-1');
|
|
147
|
+
target.focus({ preventScroll: true });
|
|
148
|
+
if (updateHash && typeof history !== 'undefined') history.replaceState(null, '', href);
|
|
149
|
+
return true;
|
|
150
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
Use it beside a long page, at the top right. Rows are links (`<nav>`, "On this page"), so Tab reaches each one and Enter goes there: the page scrolls to the section and focus moves to it. The row being read carries `aria-current="location"`, and the highlight slides to it as you scroll (instantly when the system asks for less motion). Long labels end in "…": the full label is the row's tooltip.
|
|
2
|
+
|
|
3
|
+
Scroll the box on the left: the highlight follows, bending in and out with the levels. Click a row to jump.
|
|
4
|
+
```jsx
|
|
5
|
+
import { Anchor, Text, Title } from '@nurvus/ui';
|
|
6
|
+
|
|
7
|
+
const SECTIONS = [
|
|
8
|
+
{ id: 'demo-preview', label: 'Preview', level: 1 },
|
|
9
|
+
{ id: 'demo-install', label: 'Installation', level: 1 },
|
|
10
|
+
{ id: 'demo-usage', label: 'Usage', level: 1 },
|
|
11
|
+
{ id: 'demo-anatomy', label: 'Anatomy', level: 2 },
|
|
12
|
+
{ id: 'demo-examples', label: 'Examples', level: 1 },
|
|
13
|
+
{ id: 'demo-basic', label: 'Basic', level: 2 },
|
|
14
|
+
{ id: 'demo-rail', label: 'Icon rail', level: 2 },
|
|
15
|
+
{ id: 'demo-phone', label: 'On a phone', level: 3 },
|
|
16
|
+
{ id: 'demo-api', label: 'API reference', level: 1 },
|
|
17
|
+
];
|
|
18
|
+
|
|
19
|
+
<div style={ { display: 'grid', gridTemplateColumns: 'minmax(0, 1fr) 12rem', gap: 24 } }>
|
|
20
|
+
<div id="anchor-demo-box" style={ { height: 280, overflow: 'auto', padding: '0 16px', border: '1px solid var(--nv-border)', borderRadius: 12 } }>
|
|
21
|
+
{ SECTIONS.map((s) => (
|
|
22
|
+
<section key={ s.id } style={ { minHeight: 160, paddingTop: 12 } }>
|
|
23
|
+
<Title level={ s.level + 2 } id={ s.id } style={ { scrollMarginTop: 8 } }>{ s.label }</Title>
|
|
24
|
+
<Text tone="muted">The { s.label.toLowerCase() } section.</Text>
|
|
25
|
+
</section>
|
|
26
|
+
)) }
|
|
27
|
+
</div>
|
|
28
|
+
<Anchor
|
|
29
|
+
id="anchor-demo"
|
|
30
|
+
scrollRoot="#anchor-demo-box"
|
|
31
|
+
offset={ 24 }
|
|
32
|
+
updateHash={ false }
|
|
33
|
+
items={ SECTIONS.map((s) => ({ href: `#${s.id}`, label: s.label, level: s.level })) }
|
|
34
|
+
/>
|
|
35
|
+
</div>
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Set the section yourself with `current` (for a page that tracks it already, or a demo).
|
|
39
|
+
```jsx
|
|
40
|
+
import { Anchor, Button, Layout } from '@nurvus/ui';
|
|
41
|
+
|
|
42
|
+
const ITEMS = [
|
|
43
|
+
{ href: '#a', label: 'Overview' },
|
|
44
|
+
{ href: '#b', label: 'Install', level: 2 },
|
|
45
|
+
{ href: '#c', label: 'Configure', level: 2 },
|
|
46
|
+
{ href: '#d', label: 'Deploy' },
|
|
47
|
+
];
|
|
48
|
+
|
|
49
|
+
const Controlled = () => {
|
|
50
|
+
const [at, setAt] = React.useState(0);
|
|
51
|
+
return (
|
|
52
|
+
<Layout display="flex" itemSpacing={ 32 } alignItems="flex-start">
|
|
53
|
+
<Anchor id="anchor-set" current={ ITEMS[at].href } items={ ITEMS } onNavigate={ (href, event) => { event.preventDefault(); setAt(ITEMS.findIndex((i) => i.href === href)); } } />
|
|
54
|
+
<Button size="small" type="secondary" onClick={ () => setAt((at + 1) % ITEMS.length) }>Next section</Button>
|
|
55
|
+
</Layout>
|
|
56
|
+
);
|
|
57
|
+
};
|
|
58
|
+
<Controlled />
|
|
59
|
+
```
|