@nurvus/ui 0.1.0-alpha.17 → 0.1.0-alpha.19

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.
Files changed (48) hide show
  1. package/README.md +15 -3
  2. package/dist/icons.svg +1 -0
  3. package/dist/index.js +1 -1
  4. package/dist/nurvus.css +5 -2
  5. package/dist/respawn.js +1 -1
  6. package/dist/types/anchor.d.ts +10 -2
  7. package/dist/types/components/Anchor/Anchor.d.ts +8 -5
  8. package/dist/types/components/Kbd/Kbd.d.ts +16 -0
  9. package/dist/types/components/Menu/Menu.d.ts +13 -5
  10. package/dist/types/components/NavToggle/NavToggle.d.ts +27 -0
  11. package/dist/types/components/Tooltip/Tooltip.d.ts +27 -0
  12. package/dist/types/icons.d.ts +1 -0
  13. package/dist/types/index.d.ts +7 -0
  14. package/dist/types/menu.d.ts +30 -6
  15. package/dist/types/respawn/Anchor.d.ts +2 -0
  16. package/dist/types/respawn/Kbd.d.ts +10 -0
  17. package/dist/types/respawn/Menu.d.ts +5 -1
  18. package/dist/types/respawn/NavToggle.d.ts +22 -0
  19. package/dist/types/respawn/Tooltip.d.ts +20 -0
  20. package/dist/types/respawn/index.d.ts +7 -0
  21. package/dist/types/tooltip.d.ts +4 -0
  22. package/package.json +1 -1
  23. package/src/anchor.ts +30 -7
  24. package/src/components/Anchor/Anchor.scss +26 -3
  25. package/src/components/Anchor/Anchor.tsx +23 -14
  26. package/src/components/Kbd/Kbd.md +14 -0
  27. package/src/components/Kbd/Kbd.scss +30 -0
  28. package/src/components/Kbd/Kbd.tsx +28 -0
  29. package/src/components/Menu/Menu.md +31 -2
  30. package/src/components/Menu/Menu.scss +24 -0
  31. package/src/components/Menu/Menu.tsx +72 -65
  32. package/src/components/NavToggle/NavToggle.md +51 -0
  33. package/src/components/NavToggle/NavToggle.scss +162 -0
  34. package/src/components/NavToggle/NavToggle.tsx +68 -0
  35. package/src/components/Tooltip/Tooltip.md +19 -0
  36. package/src/components/Tooltip/Tooltip.scss +75 -0
  37. package/src/components/Tooltip/Tooltip.tsx +69 -0
  38. package/src/css.ts +3 -0
  39. package/src/icons.ts +1 -0
  40. package/src/index.ts +7 -0
  41. package/src/menu.ts +58 -12
  42. package/src/respawn/Anchor.ts +26 -17
  43. package/src/respawn/Kbd.ts +22 -0
  44. package/src/respawn/Menu.ts +18 -16
  45. package/src/respawn/NavToggle.ts +78 -0
  46. package/src/respawn/Tooltip.ts +63 -0
  47. package/src/respawn/index.ts +7 -0
  48. package/src/tooltip.ts +14 -0
@@ -17,11 +17,13 @@ export interface AnchorRail {
17
17
  length: number;
18
18
  /** Each row's stretch along the path: [start, end]. */
19
19
  spans: [number, number][];
20
+ /** Where the line starts and ends (a dot marks each). */
21
+ caps: [[number, number], [number, number]];
20
22
  }
21
23
  /** The rail for rows at these levels. */
22
24
  export declare function anchorRail(levels: number[]): AnchorRail;
23
- /** The dash that lights row `i`'s stretch: stroke-dasharray and stroke-dashoffset. */
24
- export declare function anchorDash(rail: AnchorRail, i: number): {
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): {
25
27
  dasharray: string;
26
28
  dashoffset: number;
27
29
  };
@@ -33,5 +35,11 @@ export declare const anchorTarget: (href: string) => string | null;
33
35
  * last one once the area is scrolled to its end.
34
36
  */
35
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];
36
44
  /** Scrolls a section into view (smoothly unless people asked for less motion) and moves focus to it. */
37
45
  export declare function anchorGo(href: string, updateHash: boolean): boolean;
@@ -18,13 +18,16 @@ export interface AnchorProps extends Omit<React.HTMLAttributes<HTMLElement>, 'ti
18
18
  updateHash?: boolean;
19
19
  /** Called when a row is picked, with its href. Call event.preventDefault() to handle it yourself. */
20
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;
21
23
  /** Custom class name. */
22
24
  className?: string;
23
25
  }
24
26
  /**
25
- * "On this page": the sections of a long page, the one being read marked as
26
- * you scroll. A rail runs down beside the rows, bending in and out with the
27
- * nesting, and a highlight slides along it to the section in view. Picking a
28
- * row scrolls to the section and moves focus there.
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.
29
32
  */
30
- export default function Anchor({ items, title, id, offset, current, scrollRoot, updateHash, onNavigate, className, ...restProps }: AnchorProps): React.ReactElement;
33
+ export default function Anchor({ items, title, id, offset, current, scrollRoot, updateHash, onNavigate, fade, className, ...restProps }: AnchorProps): 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;
@@ -13,6 +13,14 @@ export interface MenuItem {
13
13
  disabled?: boolean;
14
14
  /** Draws a hairline above this item, to split a section into groups. */
15
15
  divider?: boolean;
16
+ /**
17
+ * Navigation: child items, which make this item a nested group that opens
18
+ * and closes like a section, as deep as you like (Tree's shape). With an
19
+ * `href` its label is a link too.
20
+ */
21
+ items?: MenuItem[];
22
+ /** Navigation, a nested group: a stable id for its open state. Defaults to its section's id and its label ("settings-advanced"). */
23
+ id?: string;
16
24
  }
17
25
  export interface MenuSection {
18
26
  /** Stable id: names the section's open state and links its toggle to its list. Defaults to the title. */
@@ -35,11 +43,11 @@ export interface MenuProps extends Omit<React.HTMLAttributes<HTMLElement>, 'onTo
35
43
  kind?: MenuKind;
36
44
  /** Navigation and actions: the sections, in order. */
37
45
  sections?: MenuSection[];
38
- /** Navigation: the href of the page being shown. Its link is marked, and its section opens. */
46
+ /** Navigation: the href of the page being shown. Its link is marked, and its section (and nested group) opens. */
39
47
  current?: string;
40
- /** Navigation: which sections start open: true (all), false (none) or a map of section id → open. Defaults to all. */
48
+ /** Navigation: which sections and nested groups start open: true (all), false (none) or a map of id → open. Defaults to all. */
41
49
  defaultOpen?: boolean | Record<string, boolean>;
42
- /** Navigation: called when a section opens or closes (e.g. to remember it). */
50
+ /** Navigation: called when a section or nested group opens or closes (e.g. to remember it). */
43
51
  onToggle?: (id: string, open: boolean) => void;
44
52
  /** Navigation: called when any link is clicked, with its href (e.g. to close a drawer). */
45
53
  onNavigate?: (href: string, event: React.MouseEvent<HTMLAnchorElement>) => void;
@@ -76,8 +84,8 @@ export type MenuTreeProps = TreeProps & {
76
84
  /**
77
85
  * One component for lists of things to go to, do or pick. The items decide
78
86
  * what it is, and screen readers hear it that way:
79
- * - links: site navigation (sections that open and close, the current page
80
- * marked), and Tab goes link by link;
87
+ * - links: site navigation (sections that open and close, nested groups as
88
+ * deep as you like, the current page marked), and Tab goes link by link;
81
89
  * - actions: a menu of commands, one Tab stop, the arrow keys move;
82
90
  * - kind="tree": a tree to pick a row from, like layers (see Tree).
83
91
  * Up and Down move between items in all three.
@@ -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;
@@ -63,6 +63,7 @@ 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[];
@@ -30,6 +30,13 @@ export { default as Anchor } from './components/Anchor/Anchor';
30
30
  export type { AnchorProps, AnchorItem } from './components/Anchor/Anchor';
31
31
  export { default as Dropdown } from './components/Dropdown/Dropdown';
32
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';
33
40
  export { default as Menu } from './components/Menu/Menu';
34
41
  export type { MenuProps, MenuListProps, MenuTreeProps, MenuItem, MenuSection, MenuKind } from './components/Menu/Menu';
35
42
  export { default as Select } from './components/Select/Select';
@@ -8,13 +8,18 @@
8
8
  * "tree", each row "level 2, 3 of 5, selected". Drawn by Tree.
9
9
  */
10
10
  export type MenuKind = 'nav' | 'actions' | 'tree';
11
+ /** An item as the rules see it: a link, and maybe child items (a nested group). */
12
+ interface NavNode {
13
+ id?: string;
14
+ label?: unknown;
15
+ href?: string;
16
+ items?: NavNode[];
17
+ }
11
18
  interface KindProps {
12
19
  kind?: MenuKind;
13
20
  sections?: {
14
21
  href?: string;
15
- items?: {
16
- href?: string;
17
- }[];
22
+ items?: NavNode[];
18
23
  }[];
19
24
  items?: unknown;
20
25
  activePath?: unknown;
@@ -30,6 +35,24 @@ export declare const sectionId: (section: {
30
35
  id?: string;
31
36
  title?: unknown;
32
37
  }) => string;
38
+ /** Whether a navigation item is a nested group: it has child items of its own. */
39
+ export declare const isGroup: (item: NavNode) => boolean;
40
+ /** A nested group's id: its own, or its parent's id and its label as a slug ("settings" + "Advanced" -> "settings-advanced"). */
41
+ export declare const groupId: (item: NavNode, parent: string) => string;
42
+ /** Whether `current` is the link of one of these items, at any depth. */
43
+ export declare const holdsCurrent: (items: NavNode[] | undefined, current: string | undefined) => boolean;
44
+ /** Every section and nested group of a navigation menu, by id, and whether the current page is inside it. */
45
+ export declare function menuGroups(sections: {
46
+ id?: string;
47
+ title?: unknown;
48
+ items?: NavNode[];
49
+ }[], current?: string): [string, boolean][];
50
+ /** Which sections and groups start open: as `defaultOpen` says, and always the ones the current page is in. */
51
+ export declare const menuOpenState: (sections: {
52
+ id?: string;
53
+ title?: unknown;
54
+ items?: NavNode[];
55
+ }[], current: string | undefined, defaultOpen: boolean | Record<string, boolean> | undefined) => Record<string, boolean>;
33
56
  /** Whether a section starts open: true / false for all, or a map of id -> open (missing = open). */
34
57
  export declare const startsOpen: (defaultOpen: boolean | Record<string, boolean> | undefined, id: string) => boolean;
35
58
  /** Makes `row` the one item of an actions menu that Tab reaches (roving tabindex). */
@@ -41,9 +64,10 @@ export declare function menuFocus(event: FocusEvent, menu: HTMLElement): void;
41
64
  * at the ends) and Home / End to the first / last, in every kind.
42
65
  * - Navigation (the WAI-ARIA disclosure navigation pattern, with its
43
66
  * optional keys): Tab still goes link by link. The rows are the section
44
- * titles and the links in open sections; Right on a section opens it, or
45
- * goes to its first link; Left closes it, or goes from a link up to its
46
- * section. Enter follows a link; Enter or Space opens a section.
67
+ * and group titles and the links in open ones; Right on a section or group
68
+ * opens it, or goes to its first row; Left closes it, or goes up to the
69
+ * one it is in. Enter or Space follows a link (Space too, as in the
70
+ * WAI-ARIA navigation tree) and opens a section.
47
71
  * - Actions (the WAI-ARIA menu pattern): one Tab stop that follows the
48
72
  * focus; typing a letter goes to the next item starting with it; Enter or
49
73
  * Space runs the item.
@@ -19,6 +19,8 @@ export interface AnchorProps extends ElementProps {
19
19
  updateHash?: boolean;
20
20
  /** Called when a row is picked, with its href. Call event.preventDefault() to handle it yourself. */
21
21
  onNavigate?: (href: string, event: MouseEvent) => void;
22
+ /** A soft wash behind the row being read, fading to the right. */
23
+ fade?: boolean;
22
24
  }
23
25
  /**
24
26
  * "On this page", the same markup as the React Anchor: the rail bends with the
@@ -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>;
@@ -14,6 +14,10 @@ export interface MenuItem {
14
14
  disabled?: boolean;
15
15
  /** Draws a hairline above this item, to split a section into groups. */
16
16
  divider?: boolean;
17
+ /** Navigation: child items, which make this item a nested group that opens and closes like a section, as deep as you like. With an `href` its label is a link too. */
18
+ items?: MenuItem[];
19
+ /** Navigation, a nested group: a stable id for its open state. Defaults to its section's id and its label ("settings-advanced"). */
20
+ id?: string;
17
21
  }
18
22
  export interface MenuSection {
19
23
  /** Stable id: names the section's open state and links its toggle to its list. Defaults to the title. */
@@ -31,7 +35,7 @@ interface MenuBaseProps extends ElementProps {
31
35
  sections?: MenuSection[];
32
36
  /** The href of the page being shown: its link is marked, and its section opens. */
33
37
  current?: string;
34
- /** Which sections start open: true (all), false (none) or a map of section id -> open. Defaults to all. */
38
+ /** Which sections and nested groups start open: true (all), false (none) or a map of id -> open. Defaults to all. */
35
39
  defaultOpen?: boolean | Record<string, boolean>;
36
40
  /** Called when a section opens or closes (e.g. to remember it). */
37
41
  onToggle?: (id: string, open: boolean) => void;
@@ -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>;
@@ -33,6 +33,13 @@ export { default as Anchor } from './Anchor';
33
33
  export type { AnchorProps, AnchorItem } from './Anchor';
34
34
  export { default as Dropdown } from './Dropdown';
35
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';
36
43
  export { default as Menu } from './Menu';
37
44
  export type { MenuProps, MenuListProps, MenuTreeProps, MenuItem, MenuSection, MenuKind } from './Menu';
38
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nurvus/ui",
3
- "version": "0.1.0-alpha.17",
3
+ "version": "0.1.0-alpha.19",
4
4
  "description": "NURVUS Design System",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/types/index.d.ts",
package/src/anchor.ts CHANGED
@@ -49,12 +49,14 @@ export interface AnchorRail {
49
49
  length: number;
50
50
  /** Each row's stretch along the path: [start, end]. */
51
51
  spans: [number, number][];
52
+ /** Where the line starts and ends (a dot marks each). */
53
+ caps: [[number, number], [number, number]];
52
54
  }
53
55
 
54
56
  /** The rail for rows at these levels. */
55
57
  export function anchorRail(levels: number[]): AnchorRail {
56
58
  const n = levels.length;
57
- if (!n) return { d: '', width: 0, height: 0, length: 0, spans: [] };
59
+ if (!n) return { d: '', width: 0, height: 0, length: 0, spans: [], caps: [[0, 0], [0, 0]] };
58
60
  const x = levels.map(xAt);
59
61
  const parts: string[] = [];
60
62
  const spans: [number, number][] = [];
@@ -78,14 +80,15 @@ export function anchorRail(levels: number[]): AnchorRail {
78
80
  }
79
81
  }
80
82
  const width = Math.max(...x) + X0;
81
- return { d: parts.join(''), width: round(width), height: n * ANCHOR_ROW, length: round(length), spans };
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 };
82
85
  }
83
86
 
84
- /** The dash that lights row `i`'s stretch: stroke-dasharray and stroke-dashoffset. */
85
- export function anchorDash(rail: AnchorRail, i: number): { dasharray: string; dashoffset: number } {
86
- const span = rail.spans[i];
87
- if (!span) return { dasharray: `0 ${rail.length + 1}`, dashoffset: 0 };
88
- return { dasharray: `${round(span[1] - span[0])} ${round(rail.length + 1)}`, dashoffset: -span[0] };
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] };
89
92
  }
90
93
 
91
94
  /** The id an "#id" href points at, or null for other links. */
@@ -113,6 +116,26 @@ export function anchorActive(items: AnchorItem[], offset: number, scrollRoot?: E
113
116
  return active;
114
117
  }
115
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
+
116
139
  /** Scrolls a section into view (smoothly unless people asked for less motion) and moves focus to it. */
117
140
  export function anchorGo(href: string, updateHash: boolean): boolean {
118
141
  const id = anchorTarget(href);
@@ -7,10 +7,13 @@
7
7
  line-height: 1.25rem;
8
8
 
9
9
  .anchor-title {
10
+ display: flex;
11
+ align-items: center;
12
+ gap: var(--space--8);
10
13
  margin: 0 0 var(--space--8);
11
- color: var(--nv-text);
14
+ color: var(--nv-text-muted);
12
15
  font-size: 0.8125rem;
13
- font-weight: 600;
16
+ font-weight: 500;
14
17
  }
15
18
 
16
19
  .anchor-body { position: relative; }
@@ -26,6 +29,15 @@
26
29
 
27
30
  .anchor-track { stroke: var(--nv-border-strong); stroke-width: 1; }
28
31
 
32
+ // A dot where the line starts and one where it ends; lit while the first or
33
+ // last section is in view.
34
+ .anchor-cap {
35
+ fill: var(--nv-border-strong);
36
+ transition: fill 0.35s ease;
37
+
38
+ &[data-lit="true"] { fill: var(--nv-accent); }
39
+ }
40
+
29
41
  // The highlight: the same path, a dash over the section in view, sliding.
30
42
  .anchor-thumb {
31
43
  stroke: var(--nv-accent);
@@ -55,7 +67,18 @@
55
67
 
56
68
  &:hover { color: var(--nv-text); }
57
69
  &:focus-visible { outline: 2px solid var(--nv-accent); outline-offset: -2px; }
58
- &[data-active="true"] { color: var(--nv-text); font-weight: 500; }
70
+ &[data-inview="true"] { color: var(--nv-text); }
71
+ &[data-active="true"] { font-weight: 500; }
72
+ }
73
+
74
+ // fade: a soft wash behind the row being read, fading to the right.
75
+ &[data-fade="true"] .anchor-link {
76
+ margin-left: calc(-1 * var(--space--8));
77
+ padding-left: var(--space--8);
78
+ background: linear-gradient(90deg, color-mix(in srgb, var(--nv-accent) 16%, transparent), transparent 85%) 0 0 / 0% 100% no-repeat;
79
+ transition: color 0.2s ease, background-size 0.35s cubic-bezier(0.4, 0, 0.2, 1);
80
+
81
+ &[data-active="true"] { background-size: 100% 100%; }
59
82
  }
60
83
  }
61
84
 
@@ -1,7 +1,8 @@
1
1
  import React from 'react';
2
2
  import classNames from 'classnames';
3
3
  import CONSTANT from '../../constants';
4
- import { AnchorItem, anchorRail, anchorDash, anchorActive, anchorGo } from '../../anchor';
4
+ import Icon from '../Icon/Icon';
5
+ import { AnchorItem, anchorRail, anchorDash, anchorRange, anchorGo } from '../../anchor';
5
6
  import './Anchor.scss';
6
7
 
7
8
  export type { AnchorItem };
@@ -23,6 +24,8 @@ export interface AnchorProps extends Omit<React.HTMLAttributes<HTMLElement>, 'ti
23
24
  updateHash?: boolean;
24
25
  /** Called when a row is picked, with its href. Call event.preventDefault() to handle it yourself. */
25
26
  onNavigate?: (href: string, event: React.MouseEvent<HTMLAnchorElement>) => void;
27
+ /** A soft wash behind the row being read, fading to the right. */
28
+ fade?: boolean;
26
29
  /** Custom class name. */
27
30
  className?: string;
28
31
  }
@@ -30,10 +33,11 @@ export interface AnchorProps extends Omit<React.HTMLAttributes<HTMLElement>, 'ti
30
33
  const rootOf = (scrollRoot?: Element | string): Element | null => (typeof scrollRoot === 'string' ? document.querySelector(scrollRoot) : scrollRoot || null);
31
34
 
32
35
  /**
33
- * "On this page": the sections of a long page, the one being read marked as
34
- * you scroll. A rail runs down beside the rows, bending in and out with the
35
- * nesting, and a highlight slides along it to the section in view. Picking a
36
- * row scrolls to the section and moves focus there.
36
+ * "On this page": the sections of a long page, the ones in view marked as you
37
+ * scroll. A rail runs down beside the rows from a dot to a dot, bending in and
38
+ * out with the nesting, and a highlight slides along it over the sections in
39
+ * view (their names brighten; the first is the one being read). Picking a row
40
+ * scrolls to the section and moves focus there.
37
41
  */
38
42
  export default function Anchor({
39
43
  items,
@@ -44,14 +48,16 @@ export default function Anchor({
44
48
  scrollRoot,
45
49
  updateHash = true,
46
50
  onNavigate,
51
+ fade = false,
47
52
  className,
48
53
  ...restProps
49
54
  }: AnchorProps) {
50
- const [spied, setSpied] = React.useState(0);
55
+ const [spied, setSpied] = React.useState<[number, number]>([0, 0]);
51
56
  const controlled = current !== undefined;
52
- const active = controlled ? Math.max(0, items.findIndex((item) => item.href === current)) : spied;
57
+ const fixed = Math.max(0, items.findIndex((item) => item.href === current));
58
+ const [from, to] = controlled ? [fixed, fixed] : spied;
53
59
  const rail = anchorRail(items.map((item) => item.level || 1));
54
- const dash = anchorDash(rail, active);
60
+ const dash = anchorDash(rail, from, to);
55
61
 
56
62
  // Follow the scrolling (unless the section in view is set from outside).
57
63
  const list = React.useRef(items);
@@ -61,7 +67,7 @@ export default function Anchor({
61
67
  if (controlled || typeof window === 'undefined') return undefined;
62
68
  const root = rootOf(scrollRoot);
63
69
  let frame = 0;
64
- const update = () => { cancelAnimationFrame(frame); frame = requestAnimationFrame(() => setSpied(anchorActive(list.current, offset, root))); };
70
+ const update = () => { cancelAnimationFrame(frame); frame = requestAnimationFrame(() => setSpied((was) => { const next = anchorRange(list.current, offset, root); return was[0] === next[0] && was[1] === next[1] ? was : next; })); };
65
71
  update();
66
72
  const target: EventTarget = root || window;
67
73
  target.addEventListener('scroll', update, { passive: true });
@@ -72,16 +78,18 @@ export default function Anchor({
72
78
  const go = (href: string, i: number) => (event: React.MouseEvent<HTMLAnchorElement>) => {
73
79
  if (onNavigate) onNavigate(href, event);
74
80
  if (event.defaultPrevented) return;
75
- if (anchorGo(href, updateHash)) { event.preventDefault(); if (!controlled) setSpied(i); }
81
+ if (anchorGo(href, updateHash)) { event.preventDefault(); if (!controlled) setSpied([i, i]); }
76
82
  };
77
83
 
78
84
  return (
79
- <nav className={ classNames(className, 'anchor', CONSTANT.CLASS_PREFIX) } aria-labelledby={ `${id}-title` } { ...restProps }>
80
- <p className="anchor-title" id={ `${id}-title` }>{ title }</p>
85
+ <nav className={ classNames(className, 'anchor', CONSTANT.CLASS_PREFIX) } aria-labelledby={ `${id}-title` } data-fade={ fade || undefined } { ...restProps }>
86
+ <p className="anchor-title"><Icon name="align-left" size="1.125em" /><span id={ `${id}-title` }>{ title }</span></p>
81
87
  <div className="anchor-body">
82
88
  <svg className="anchor-rail" viewBox={ `0 0 ${rail.width} ${rail.height}` } style={ { width: `${rail.width / 16}rem`, height: `${rail.height / 16}rem` } } aria-hidden="true" focusable="false">
83
89
  <path className="anchor-track" d={ rail.d } />
84
90
  <path className="anchor-thumb" d={ rail.d } style={ { strokeDasharray: dash.dasharray, strokeDashoffset: dash.dashoffset } } />
91
+ { items.length > 0 && <circle className="anchor-cap" cx={ rail.caps[0][0] } cy={ rail.caps[0][1] } r="2.25" data-lit={ from === 0 } /> }
92
+ { items.length > 0 && <circle className="anchor-cap" cx={ rail.caps[1][0] } cy={ rail.caps[1][1] } r="2.25" data-lit={ to === items.length - 1 } /> }
85
93
  </svg>
86
94
  <ul className="anchor-list">
87
95
  { items.map((item, i) => (
@@ -90,8 +98,9 @@ export default function Anchor({
90
98
  className="anchor-link"
91
99
  href={ item.href }
92
100
  title={ item.label }
93
- data-active={ i === active }
94
- aria-current={ i === active ? 'location' : undefined }
101
+ data-active={ i === from }
102
+ data-inview={ i >= from && i <= to }
103
+ aria-current={ i === from ? 'location' : undefined }
95
104
  onClick={ go(item.href, i) }
96
105
  >
97
106
  { item.label }
@@ -0,0 +1,14 @@
1
+ Key caps for keyboard shortcuts: in tooltips, menus and guides, so people can see the keys to press. `keys` draws a shortcut; one key can be the child.
2
+
3
+ ```jsx
4
+ import { Kbd, Layout, Text } from '@nurvus/ui';
5
+
6
+ <Layout itemSpacing={ 12 }>
7
+ <Text>Search: <Kbd keys={ ['⌘', 'K'] } /> (Mac) or <Kbd keys={ ['Ctrl', 'K'] } /> (Windows, Linux)</Text>
8
+ <Text>Show or hide the sidebar: <Kbd keys={ ['⌘', 'B'] } /></Text>
9
+ <Text>Close: <Kbd>Esc</Kbd> · Move: <Kbd>↑</Kbd> <Kbd>↓</Kbd> · Pick: <Kbd>Enter</Kbd></Text>
10
+ <Text variant="caption" tone="muted">Sized in em: <Kbd keys={ ['Shift', '?'] } /> follows the text it sits in.</Text>
11
+ </Layout>
12
+ ```
13
+
14
+ A control with a shortcut should also say so to assistive technology: put `aria-keyshortcuts` on it (`aria-keyshortcuts="Meta+B"`), as the docs header's sidebar button does.
@@ -0,0 +1,30 @@
1
+ // Styles for Kbd (React and Respawn: same markup). Key caps in em: they size with the text.
2
+ @import "../../styles/variables.scss";
3
+
4
+ .#{$cls-prefix}.kbd {
5
+ display: inline-flex;
6
+ align-items: center;
7
+ gap: 0.2em;
8
+ font: inherit;
9
+ vertical-align: baseline;
10
+
11
+ .kbd-key {
12
+ display: inline-flex;
13
+ align-items: center;
14
+ justify-content: center;
15
+ box-sizing: border-box;
16
+ min-width: 1.6em;
17
+ height: 1.6em;
18
+ padding: 0 0.4em;
19
+ border: 1px solid var(--nv-border-strong);
20
+ border-bottom-width: 2px;
21
+ border-radius: 0.3em;
22
+ background: var(--nv-surface);
23
+ color: var(--nv-text);
24
+ font-family: var(--nv-font-sans);
25
+ font-size: 0.8em;
26
+ font-weight: 500;
27
+ line-height: 1;
28
+ white-space: nowrap;
29
+ }
30
+ }