@nurvus/ui 0.1.0-alpha.15 → 0.1.0-alpha.17
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 +17 -1
- package/dist/icons.svg +74 -0
- package/dist/index.js +1 -1
- package/dist/nurvus.css +15 -10
- package/dist/respawn.js +1 -1
- package/dist/types/anchor.d.ts +37 -0
- package/dist/types/breakpoints.d.ts +14 -0
- package/dist/types/components/Anchor/Anchor.d.ts +30 -0
- package/dist/types/components/Dropdown/Dropdown.d.ts +31 -0
- package/dist/types/components/FullPageModal/FullPageModal.d.ts +11 -2
- package/dist/types/components/Grid/Grid.d.ts +26 -0
- package/dist/types/components/Loader/Loader.d.ts +21 -0
- package/dist/types/components/Menu/Menu.d.ts +61 -19
- package/dist/types/components/Progress/Progress.d.ts +19 -5
- package/dist/types/components/Stack/Stack.d.ts +26 -0
- package/dist/types/dialog.d.ts +12 -0
- package/dist/types/dropdown.d.ts +9 -0
- package/dist/types/icons.d.ts +29 -0
- package/dist/types/index.d.ts +13 -1
- package/dist/types/loader.d.ts +28 -0
- package/dist/types/logo.d.ts +5 -0
- package/dist/types/menu.d.ts +43 -14
- package/dist/types/progress.d.ts +11 -0
- package/dist/types/respawn/Anchor.d.ts +28 -0
- package/dist/types/respawn/Dropdown.d.ts +27 -0
- package/dist/types/respawn/FullPageModal.d.ts +7 -1
- package/dist/types/respawn/Grid.d.ts +21 -0
- package/dist/types/respawn/Loader.d.ts +18 -0
- package/dist/types/respawn/Menu.d.ts +47 -15
- package/dist/types/respawn/Progress.d.ts +18 -6
- package/dist/types/respawn/Stack.d.ts +21 -0
- package/dist/types/respawn/index.d.ts +13 -1
- package/package.json +1 -3
- package/src/anchor.ts +127 -0
- package/src/breakpoints.ts +28 -0
- package/src/components/Anchor/Anchor.md +59 -0
- package/src/components/Anchor/Anchor.scss +65 -0
- package/src/components/Anchor/Anchor.tsx +105 -0
- package/src/components/Button/Button.scss +20 -20
- 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/FullPageModal/FullPageModal.md +20 -17
- package/src/components/FullPageModal/FullPageModal.scss +5 -0
- package/src/components/FullPageModal/FullPageModal.tsx +37 -3
- package/src/components/Grid/Grid.md +37 -0
- package/src/components/Grid/Grid.scss +37 -0
- package/src/components/Grid/Grid.tsx +54 -0
- package/src/components/HistoryList/HistoryList.scss +2 -2
- package/src/components/Loader/Loader.md +30 -0
- package/src/components/Loader/Loader.scss +42 -0
- package/src/components/Loader/Loader.tsx +68 -0
- package/src/components/Menu/Menu.md +67 -3
- package/src/components/Menu/Menu.scss +126 -18
- package/src/components/Menu/Menu.tsx +159 -40
- package/src/components/Progress/Progress.md +27 -26
- package/src/components/Progress/Progress.scss +41 -36
- package/src/components/Progress/Progress.tsx +50 -6
- package/src/components/Select/Select.scss +20 -19
- package/src/components/Stack/Stack.md +43 -0
- package/src/components/Stack/Stack.scss +36 -0
- package/src/components/Stack/Stack.tsx +52 -0
- package/src/components/Table/Table.scss +1 -1
- package/src/components/Tag/Tag.scss +2 -2
- package/src/components/Title/Title.scss +6 -6
- package/src/components/Tree/Tree.md +1 -1
- package/src/css.ts +5 -0
- package/src/dialog.ts +47 -0
- package/src/dropdown.ts +24 -0
- package/src/icons.ts +31 -0
- package/src/index.ts +13 -1
- package/src/loader.ts +43 -0
- package/src/logo.ts +12 -0
- package/src/menu.ts +91 -21
- package/src/progress.ts +21 -0
- package/src/respawn/Anchor.ts +126 -0
- package/src/respawn/Dropdown.ts +90 -0
- package/src/respawn/FullPageModal.ts +30 -3
- package/src/respawn/Grid.ts +41 -0
- package/src/respawn/Loader.ts +77 -0
- package/src/respawn/Menu.ts +144 -37
- package/src/respawn/Progress.ts +56 -13
- package/src/respawn/Stack.ts +37 -0
- package/src/respawn/index.ts +13 -1
- package/src/styles/breakpoints.scss +3 -0
- package/src/styles/theme.scss +55 -55
- package/src/styles/type.json +9 -9
|
@@ -11,9 +11,15 @@ export interface FullPageModalProps extends ElementProps {
|
|
|
11
11
|
children?: Child;
|
|
12
12
|
/** Appends the modal to this element when it is made, e.g. document.body. */
|
|
13
13
|
mountEl?: Element;
|
|
14
|
+
/** Called when someone presses Esc. Close the modal in it: modal.update({ visible: false }). */
|
|
15
|
+
onClose?: () => void;
|
|
16
|
+
/** Names the modal and its title: the title gets the id "<id>-title", and the modal is labelled by it. */
|
|
17
|
+
id?: string;
|
|
14
18
|
}
|
|
15
19
|
/**
|
|
16
|
-
* Shows content over the whole screen
|
|
20
|
+
* Shows content over the whole screen: a modal dialog, like the React one
|
|
21
|
+
* (focus moves in when it opens and back when it closes, Tab stays inside,
|
|
22
|
+
* Esc calls onClose). Returns the modal's outer element
|
|
17
23
|
* (`<div class="portal nurvus">`, the one React puts into `<body>` itself):
|
|
18
24
|
* mount it on document.body, or pass `mountEl: document.body`.
|
|
19
25
|
*
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Spacing } from '../spacing';
|
|
2
|
+
import { Responsive } from '../breakpoints';
|
|
3
|
+
import { Child, ElementProps } from './spawn';
|
|
4
|
+
import { NurvusElement } from './update';
|
|
5
|
+
export type GridColumns = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12;
|
|
6
|
+
export interface GridProps extends ElementProps {
|
|
7
|
+
/** How many equal columns: 1 to 12. Can change with the width: { base: 1, md: 2, lg: 3 }. */
|
|
8
|
+
columns?: Responsive<GridColumns>;
|
|
9
|
+
/** Instead of a count: as many columns as fit, each at least this wide (e.g. "15rem" or 240). They fill the row. */
|
|
10
|
+
minItemWidth?: string | number;
|
|
11
|
+
/** Space between cells, from the 4px grid: 0, 2, 4, 8, 12, 16, 24, 32, 48 or 64. */
|
|
12
|
+
gap?: Spacing;
|
|
13
|
+
/** Space between rows when it differs from gap. */
|
|
14
|
+
rowGap?: Spacing;
|
|
15
|
+
/** How cells line up in their row. Defaults to stretch (equal heights). */
|
|
16
|
+
align?: 'start' | 'center' | 'end' | 'stretch';
|
|
17
|
+
/** What to lay out. */
|
|
18
|
+
children?: Child;
|
|
19
|
+
}
|
|
20
|
+
/** Columns: a count that changes at the breakpoints, or as many as fit. The same markup as the React Grid. */
|
|
21
|
+
export default function Grid(props?: GridProps): NurvusElement<HTMLDivElement, GridProps>;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ElementProps } from './spawn';
|
|
2
|
+
import { NurvusElement } from './update';
|
|
3
|
+
export interface LoaderProps extends ElementProps {
|
|
4
|
+
/** Width and height of the mark. Defaults to 2em (twice the text size). A number is pixels. */
|
|
5
|
+
size?: number | string;
|
|
6
|
+
/** What is loading, for screen readers ("Loading photos"). Defaults to "Loading". */
|
|
7
|
+
label?: string;
|
|
8
|
+
/** Shows the label beside the mark, not only to screen readers. */
|
|
9
|
+
showLabel?: boolean;
|
|
10
|
+
/** Fills the segments with the logo's gradients (for big loaders); their ids start with this. Without it, one color each. */
|
|
11
|
+
id?: string;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Shows that something is loading: the NURVUS mark with a light running round
|
|
15
|
+
* it, the same markup as the React Loader. Screen readers hear the label once
|
|
16
|
+
* (role="status"). Remove it when the thing has loaded.
|
|
17
|
+
*/
|
|
18
|
+
export default function Loader(props?: LoaderProps): NurvusElement<HTMLDivElement, LoaderProps>;
|
|
@@ -1,26 +1,34 @@
|
|
|
1
|
+
import { MenuKind } from '../menu';
|
|
2
|
+
import { TreeProps } from './Tree';
|
|
1
3
|
import { Child, ElementProps } from './spawn';
|
|
2
4
|
import { NurvusElement } from './update';
|
|
5
|
+
export type { MenuKind };
|
|
3
6
|
export interface MenuItem {
|
|
4
|
-
/** What the
|
|
7
|
+
/** What the item says. */
|
|
5
8
|
label: Child;
|
|
6
|
-
/**
|
|
7
|
-
href
|
|
9
|
+
/** Makes the item a link to this address. The item whose href equals `current` is marked as the current page. */
|
|
10
|
+
href?: string;
|
|
11
|
+
/** Makes the item a button that calls this (an action: "Duplicate", "Sign out"). */
|
|
12
|
+
onSelect?: (event: MouseEvent) => void;
|
|
13
|
+
/** Shows the item but stops it working (actions only). */
|
|
14
|
+
disabled?: boolean;
|
|
8
15
|
/** Draws a hairline above this item, to split a section into groups. */
|
|
9
16
|
divider?: boolean;
|
|
10
17
|
}
|
|
11
18
|
export interface MenuSection {
|
|
12
19
|
/** Stable id: names the section's open state and links its toggle to its list. Defaults to the title. */
|
|
13
20
|
id?: string;
|
|
14
|
-
/** The section heading. */
|
|
15
|
-
title
|
|
21
|
+
/** The section heading. Optional for actions. */
|
|
22
|
+
title?: string;
|
|
16
23
|
/** Makes the heading a link too (e.g. to the section's overview page). */
|
|
17
24
|
href?: string;
|
|
18
|
-
/** The section's
|
|
25
|
+
/** The section's items. */
|
|
19
26
|
items: MenuItem[];
|
|
20
27
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
28
|
+
/** What navigation and actions take. */
|
|
29
|
+
interface MenuBaseProps extends ElementProps {
|
|
30
|
+
/** Navigation and actions: the sections, in order. */
|
|
31
|
+
sections?: MenuSection[];
|
|
24
32
|
/** The href of the page being shown: its link is marked, and its section opens. */
|
|
25
33
|
current?: string;
|
|
26
34
|
/** Which sections start open: true (all), false (none) or a map of section id -> open. Defaults to all. */
|
|
@@ -29,15 +37,39 @@ export interface MenuProps extends ElementProps {
|
|
|
29
37
|
onToggle?: (id: string, open: boolean) => void;
|
|
30
38
|
/** Called when any link is clicked, with its href (e.g. to close a drawer). */
|
|
31
39
|
onNavigate?: (href: string, event: MouseEvent) => void;
|
|
32
|
-
/**
|
|
40
|
+
/** What screen readers call the menu ("Pages", "Actions"). */
|
|
33
41
|
label?: string;
|
|
34
|
-
/** Prefix for the ids
|
|
42
|
+
/** Prefix for the ids inside (each toggle names its list). Defaults to "menu". */
|
|
35
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';
|
|
46
|
+
}
|
|
47
|
+
export interface MenuProps extends MenuBaseProps {
|
|
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. */
|
|
49
|
+
kind?: MenuKind;
|
|
50
|
+
/** Tree: the rows, each with optional child `items`. */
|
|
51
|
+
items?: TreeProps['items'];
|
|
52
|
+
/** Tree: the picked row as a path of indexes. */
|
|
53
|
+
activePath?: number[];
|
|
54
|
+
/** Tree: draws a row's content instead of its label. */
|
|
55
|
+
itemRenderer?: TreeProps['itemRenderer'];
|
|
56
|
+
/** Tree: the deepest level that still indents. */
|
|
57
|
+
maxIndentDepth?: number;
|
|
58
|
+
}
|
|
59
|
+
/** Menu's props for navigation and actions. */
|
|
60
|
+
export interface MenuListProps extends MenuBaseProps {
|
|
61
|
+
/** 'nav' or 'actions'; left out, it follows the items. */
|
|
62
|
+
kind?: 'nav' | 'actions';
|
|
36
63
|
}
|
|
64
|
+
/** Menu kind: 'tree': Tree's props. */
|
|
65
|
+
export type MenuTreeProps = TreeProps & {
|
|
66
|
+
kind: 'tree';
|
|
67
|
+
};
|
|
37
68
|
/**
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
69
|
+
* One component for lists of things to go to, do or pick, the same markup as
|
|
70
|
+
* the React Menu. The items decide what it is, and screen readers hear it so:
|
|
71
|
+
* links make navigation (sections open and close in place; update({ current })
|
|
72
|
+
* marks another page), actions make a menu of commands (one Tab stop, the
|
|
73
|
+
* arrow keys move), kind: 'tree' makes a Tree.
|
|
42
74
|
*/
|
|
43
75
|
export default function Menu(props?: MenuProps): NurvusElement<HTMLElement, MenuProps>;
|
|
@@ -1,15 +1,27 @@
|
|
|
1
|
+
import { ProgressStatus } from '../progress';
|
|
2
|
+
import { ElementProps } from './spawn';
|
|
1
3
|
import { NurvusElement } from './update';
|
|
2
4
|
export type ProgressSize = 'small' | 'medium' | 'large';
|
|
3
|
-
export type ProgressStatus
|
|
4
|
-
export interface ProgressProps {
|
|
5
|
+
export type { ProgressStatus };
|
|
6
|
+
export interface ProgressProps extends ElementProps {
|
|
5
7
|
/** How far along, 0 to 100. */
|
|
6
8
|
percent?: number;
|
|
7
9
|
/** Bar height. */
|
|
8
10
|
size?: ProgressSize;
|
|
9
|
-
/** The bar color: active (accent), success, warning or critical (danger). */
|
|
11
|
+
/** The bar color: active (accent), success, warning or critical (danger). With a label or showValue, a status also shows as an icon and a word. */
|
|
10
12
|
status?: ProgressStatus;
|
|
11
|
-
/**
|
|
12
|
-
|
|
13
|
+
/** What is in progress ("Uploading"): shown above the bar, and its name for screen readers. */
|
|
14
|
+
label?: string;
|
|
15
|
+
/** Keeps the label for screen readers only. */
|
|
16
|
+
hideLabel?: boolean;
|
|
17
|
+
/** Shows the percent (and the status, as an icon and a word) at the right, above the bar. */
|
|
18
|
+
showValue?: boolean;
|
|
19
|
+
/** What screen readers say for the value, instead of "40%" (e.g. "3 of 8 files"). */
|
|
20
|
+
valueText?: string;
|
|
13
21
|
}
|
|
14
|
-
/**
|
|
22
|
+
/**
|
|
23
|
+
* A progress bar, the same markup as the React one: a progressbar with its
|
|
24
|
+
* label and value for screen readers, and the status as an icon and a word
|
|
25
|
+
* when it has a label or showValue. bar.update({ percent: 60 }) moves it.
|
|
26
|
+
*/
|
|
15
27
|
export default function Progress(props?: ProgressProps): NurvusElement<HTMLDivElement, ProgressProps>;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Spacing } from '../spacing';
|
|
2
|
+
import { Responsive } from '../breakpoints';
|
|
3
|
+
import { Child, ElementProps } from './spawn';
|
|
4
|
+
import { NurvusElement } from './update';
|
|
5
|
+
export type StackDirection = 'column' | 'row';
|
|
6
|
+
export interface StackProps extends ElementProps {
|
|
7
|
+
/** column (top to bottom, the default) or row. Can change with the width: { base: 'column', md: 'row' }. */
|
|
8
|
+
direction?: Responsive<StackDirection>;
|
|
9
|
+
/** Space between the children, from the 4px grid: 0, 2, 4, 8, 12, 16, 24, 32, 48 or 64. */
|
|
10
|
+
gap?: Spacing;
|
|
11
|
+
/** How the children line up across the stack. */
|
|
12
|
+
align?: 'start' | 'center' | 'end' | 'stretch' | 'baseline';
|
|
13
|
+
/** How the children spread along the stack. */
|
|
14
|
+
justify?: 'start' | 'center' | 'end' | 'between';
|
|
15
|
+
/** Lets a row wrap onto more lines when it runs out of room. */
|
|
16
|
+
wrap?: boolean;
|
|
17
|
+
/** What to lay out. */
|
|
18
|
+
children?: Child;
|
|
19
|
+
}
|
|
20
|
+
/** A column or a row with even space between the children, the same markup as the React Stack. */
|
|
21
|
+
export default function Stack(props?: StackProps): NurvusElement<HTMLDivElement, StackProps>;
|
|
@@ -21,8 +21,20 @@ export { ICONS, ICON_NAMES } from '../icons';
|
|
|
21
21
|
export type { IconName, IconNode } from '../icons';
|
|
22
22
|
export { default as Tree } from './Tree';
|
|
23
23
|
export type { TreeProps, TreeItem } from './Tree';
|
|
24
|
+
export { default as Stack } from './Stack';
|
|
25
|
+
export type { StackProps, StackDirection } from './Stack';
|
|
26
|
+
export { default as Grid } from './Grid';
|
|
27
|
+
export type { GridProps, GridColumns } from './Grid';
|
|
28
|
+
export { BREAKPOINTS, isAtLeast } from '../breakpoints';
|
|
29
|
+
export type { Breakpoint, Responsive } from '../breakpoints';
|
|
30
|
+
export { default as Loader } from './Loader';
|
|
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';
|
|
24
36
|
export { default as Menu } from './Menu';
|
|
25
|
-
export type { MenuProps, MenuItem, MenuSection } from './Menu';
|
|
37
|
+
export type { MenuProps, MenuListProps, MenuTreeProps, MenuItem, MenuSection, MenuKind } from './Menu';
|
|
26
38
|
export { default as Select } from './Select';
|
|
27
39
|
export type { SelectProps, SelectOption, SelectOptionState } from './Select';
|
|
28
40
|
export { default as FullPageModal } from './FullPageModal';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nurvus/ui",
|
|
3
|
-
"version": "0.1.0-alpha.
|
|
3
|
+
"version": "0.1.0-alpha.17",
|
|
4
4
|
"description": "NURVUS Design System",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/types/index.d.ts",
|
|
@@ -68,8 +68,6 @@
|
|
|
68
68
|
"@fontsource-variable/inter": "^5.3.0",
|
|
69
69
|
"@fontsource/jetbrains-mono": "^5.3.0",
|
|
70
70
|
"@lezer/highlight": "^1.2.5",
|
|
71
|
-
"@material-ui/core": "^4.12.3",
|
|
72
|
-
"@material-ui/icons": "^4.11.2",
|
|
73
71
|
"@types/react": "^17.0.94",
|
|
74
72
|
"@types/react-dom": "^17.0.26",
|
|
75
73
|
"@vitejs/plugin-react": "^6.3.0",
|
package/src/anchor.ts
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
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
|
+
}
|
|
53
|
+
|
|
54
|
+
/** The rail for rows at these levels. */
|
|
55
|
+
export function anchorRail(levels: number[]): AnchorRail {
|
|
56
|
+
const n = levels.length;
|
|
57
|
+
if (!n) return { d: '', width: 0, height: 0, length: 0, spans: [] };
|
|
58
|
+
const x = levels.map(xAt);
|
|
59
|
+
const parts: string[] = [];
|
|
60
|
+
const spans: [number, number][] = [];
|
|
61
|
+
let length = 0;
|
|
62
|
+
let y = END;
|
|
63
|
+
parts.push(`M${round(x[0])} ${y}`);
|
|
64
|
+
for (let i = 0; i < n; i += 1) {
|
|
65
|
+
const bottom = (i + 1) * ANCHOR_ROW;
|
|
66
|
+
const bendsBelow = i < n - 1 && x[i + 1] !== x[i];
|
|
67
|
+
const straightEnd = i === n - 1 ? bottom - END : bendsBelow ? bottom - BEND : bottom;
|
|
68
|
+
const start = length;
|
|
69
|
+
length += straightEnd - y;
|
|
70
|
+
parts.push(`L${round(x[i])} ${straightEnd}`);
|
|
71
|
+
spans.push([round(start), round(length)]);
|
|
72
|
+
y = straightEnd;
|
|
73
|
+
if (bendsBelow) {
|
|
74
|
+
const p0 = [x[i], bottom - BEND]; const p1 = [x[i], bottom]; const p2 = [x[i + 1], bottom]; const p3 = [x[i + 1], bottom + BEND];
|
|
75
|
+
parts.push(`C${round(x[i])} ${bottom} ${round(x[i + 1])} ${bottom} ${round(x[i + 1])} ${bottom + BEND}`);
|
|
76
|
+
length += cubicLength(p0, p1, p2, p3);
|
|
77
|
+
y = bottom + BEND;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
const width = Math.max(...x) + X0;
|
|
81
|
+
return { d: parts.join(''), width: round(width), height: n * ANCHOR_ROW, length: round(length), spans };
|
|
82
|
+
}
|
|
83
|
+
|
|
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] };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** The id an "#id" href points at, or null for other links. */
|
|
92
|
+
export const anchorTarget = (href: string): string | null => (href.startsWith('#') && href.length > 1 ? decodeURIComponent(href.slice(1)) : null);
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Which row is in view: the last section whose top has passed `offset` pixels
|
|
96
|
+
* below the top of the scroll area (the first one before any has), and the
|
|
97
|
+
* last one once the area is scrolled to its end.
|
|
98
|
+
*/
|
|
99
|
+
export function anchorActive(items: AnchorItem[], offset: number, scrollRoot?: Element | null): number {
|
|
100
|
+
const ids = items.map((item) => anchorTarget(item.href));
|
|
101
|
+
const top = scrollRoot ? scrollRoot.getBoundingClientRect().top : 0;
|
|
102
|
+
const atEnd = scrollRoot
|
|
103
|
+
? scrollRoot.scrollTop + scrollRoot.clientHeight >= scrollRoot.scrollHeight - 2
|
|
104
|
+
: window.innerHeight + window.scrollY >= document.documentElement.scrollHeight - 2;
|
|
105
|
+
let active = 0;
|
|
106
|
+
ids.forEach((id, i) => {
|
|
107
|
+
const el = id ? document.getElementById(id) : null;
|
|
108
|
+
if (el && el.getBoundingClientRect().top - top <= offset) active = i;
|
|
109
|
+
});
|
|
110
|
+
if (atEnd) {
|
|
111
|
+
for (let i = ids.length - 1; i >= 0; i -= 1) if (ids[i] && document.getElementById(ids[i] as string)) return i;
|
|
112
|
+
}
|
|
113
|
+
return active;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Scrolls a section into view (smoothly unless people asked for less motion) and moves focus to it. */
|
|
117
|
+
export function anchorGo(href: string, updateHash: boolean): boolean {
|
|
118
|
+
const id = anchorTarget(href);
|
|
119
|
+
const target = id ? document.getElementById(id) : null;
|
|
120
|
+
if (!target) return false;
|
|
121
|
+
const still = typeof window.matchMedia === 'function' && window.matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
122
|
+
target.scrollIntoView({ behavior: still ? 'auto' : 'smooth', block: 'start' });
|
|
123
|
+
if (!target.hasAttribute('tabindex')) target.setAttribute('tabindex', '-1');
|
|
124
|
+
target.focus({ preventScroll: true });
|
|
125
|
+
if (updateHash && typeof history !== 'undefined') history.replaceState(null, '', href);
|
|
126
|
+
return true;
|
|
127
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// The breakpoints: where layouts change as the screen widens. In em, so they
|
|
2
|
+
// follow the reader's text size (at the default 16px: 640, 768, 1024, 1280).
|
|
3
|
+
// Shared by Grid, Stack and your own code (no React here).
|
|
4
|
+
|
|
5
|
+
export type Breakpoint = 'sm' | 'md' | 'lg' | 'xl';
|
|
6
|
+
|
|
7
|
+
/** Each breakpoint's min-width: from this width up, the `sm`/`md`/`lg`/`xl` value applies. */
|
|
8
|
+
export const BREAKPOINTS: Record<Breakpoint, string> = { sm: '40em', md: '48em', lg: '64em', xl: '80em' };
|
|
9
|
+
|
|
10
|
+
/** A value for every width (`base`), changing from a breakpoint up: { base: 1, md: 2, lg: 3 }. */
|
|
11
|
+
export type Responsive<T> = T | ({ base?: T } & Partial<Record<Breakpoint, T>>);
|
|
12
|
+
|
|
13
|
+
/** `true` once the window is at least as wide as `breakpoint` (for your own code). */
|
|
14
|
+
export const isAtLeast = (breakpoint: Breakpoint): boolean =>
|
|
15
|
+
typeof window !== 'undefined' && !!window.matchMedia && window.matchMedia(`(min-width: ${BREAKPOINTS[breakpoint]})`).matches;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The data attributes for a responsive value: data-<name>="base value" and
|
|
19
|
+
* data-<name>-md="…" for each breakpoint given. The CSS reads them.
|
|
20
|
+
*/
|
|
21
|
+
export function responsiveData<T>(name: string, value: Responsive<T> | undefined): Record<string, T | undefined> {
|
|
22
|
+
if (value === undefined || value === null) return {};
|
|
23
|
+
if (typeof value !== 'object') return { [`data-${name}`]: value as T };
|
|
24
|
+
const v = value as { base?: T } & Partial<Record<Breakpoint, T>>;
|
|
25
|
+
const out: Record<string, T | undefined> = { [`data-${name}`]: v.base };
|
|
26
|
+
(Object.keys(BREAKPOINTS) as Breakpoint[]).forEach((bp) => { if (v[bp] !== undefined) out[`data-${name}-${bp}`] = v[bp]; });
|
|
27
|
+
return out;
|
|
28
|
+
}
|
|
@@ -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
|
+
```
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// Styles for Anchor (React and Respawn: same markup). Rows are exactly 1.75rem
|
|
2
|
+
// tall so the rail (drawn from the items, src/anchor.ts) lines up with them.
|
|
3
|
+
@import "../../styles/variables.scss";
|
|
4
|
+
|
|
5
|
+
.#{$cls-prefix}.anchor {
|
|
6
|
+
font-size: 0.8125rem;
|
|
7
|
+
line-height: 1.25rem;
|
|
8
|
+
|
|
9
|
+
.anchor-title {
|
|
10
|
+
margin: 0 0 var(--space--8);
|
|
11
|
+
color: var(--nv-text);
|
|
12
|
+
font-size: 0.8125rem;
|
|
13
|
+
font-weight: 600;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
.anchor-body { position: relative; }
|
|
17
|
+
|
|
18
|
+
.anchor-rail {
|
|
19
|
+
position: absolute;
|
|
20
|
+
top: 0;
|
|
21
|
+
left: 0;
|
|
22
|
+
overflow: visible;
|
|
23
|
+
fill: none;
|
|
24
|
+
stroke-linecap: round;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
.anchor-track { stroke: var(--nv-border-strong); stroke-width: 1; }
|
|
28
|
+
|
|
29
|
+
// The highlight: the same path, a dash over the section in view, sliding.
|
|
30
|
+
.anchor-thumb {
|
|
31
|
+
stroke: var(--nv-accent);
|
|
32
|
+
stroke-width: 2;
|
|
33
|
+
transition: stroke-dasharray 0.35s cubic-bezier(0.4, 0, 0.2, 1), stroke-dashoffset 0.35s cubic-bezier(0.4, 0, 0.2, 1);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
.anchor-list {
|
|
37
|
+
margin: 0;
|
|
38
|
+
padding: 0;
|
|
39
|
+
list-style: none;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
li { padding-left: calc(var(--anchor-level, 0) * 0.75rem + 1rem); }
|
|
43
|
+
|
|
44
|
+
.anchor-link {
|
|
45
|
+
display: block;
|
|
46
|
+
height: 1.75rem;
|
|
47
|
+
overflow: hidden;
|
|
48
|
+
border-radius: 0.25rem;
|
|
49
|
+
color: var(--nv-text-muted);
|
|
50
|
+
line-height: 1.75rem;
|
|
51
|
+
text-decoration: none;
|
|
52
|
+
text-overflow: ellipsis;
|
|
53
|
+
white-space: nowrap;
|
|
54
|
+
transition: color 0.2s ease;
|
|
55
|
+
|
|
56
|
+
&:hover { color: var(--nv-text); }
|
|
57
|
+
&:focus-visible { outline: 2px solid var(--nv-accent); outline-offset: -2px; }
|
|
58
|
+
&[data-active="true"] { color: var(--nv-text); font-weight: 500; }
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
@media (prefers-reduced-motion: reduce) {
|
|
63
|
+
.#{$cls-prefix}.anchor .anchor-thumb,
|
|
64
|
+
.#{$cls-prefix}.anchor .anchor-link { transition: none; }
|
|
65
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import classNames from 'classnames';
|
|
3
|
+
import CONSTANT from '../../constants';
|
|
4
|
+
import { AnchorItem, anchorRail, anchorDash, anchorActive, anchorGo } from '../../anchor';
|
|
5
|
+
import './Anchor.scss';
|
|
6
|
+
|
|
7
|
+
export type { AnchorItem };
|
|
8
|
+
|
|
9
|
+
export interface AnchorProps extends Omit<React.HTMLAttributes<HTMLElement>, 'title'> {
|
|
10
|
+
/** The sections, in page order: each an "#id" href, a label and a level (1, 2…). */
|
|
11
|
+
items: AnchorItem[];
|
|
12
|
+
/** The heading above the list. */
|
|
13
|
+
title?: string;
|
|
14
|
+
/** Prefix for the ids inside (the title is "<id>-title"). Defaults to "anchor". */
|
|
15
|
+
id?: string;
|
|
16
|
+
/** How far below the top a section's heading must be scrolled to count as "in view", in pixels (room for a sticky header). */
|
|
17
|
+
offset?: number;
|
|
18
|
+
/** The section in view, set by you (its href). Left out, the Anchor follows the scrolling itself. */
|
|
19
|
+
current?: string;
|
|
20
|
+
/** Scroll-spy inside this element instead of the page: an element or a CSS selector. */
|
|
21
|
+
scrollRoot?: Element | string;
|
|
22
|
+
/** Writes the section's #id in the address bar when one is picked. Turn off for hash routers. */
|
|
23
|
+
updateHash?: boolean;
|
|
24
|
+
/** Called when a row is picked, with its href. Call event.preventDefault() to handle it yourself. */
|
|
25
|
+
onNavigate?: (href: string, event: React.MouseEvent<HTMLAnchorElement>) => void;
|
|
26
|
+
/** Custom class name. */
|
|
27
|
+
className?: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const rootOf = (scrollRoot?: Element | string): Element | null => (typeof scrollRoot === 'string' ? document.querySelector(scrollRoot) : scrollRoot || null);
|
|
31
|
+
|
|
32
|
+
/**
|
|
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.
|
|
37
|
+
*/
|
|
38
|
+
export default function Anchor({
|
|
39
|
+
items,
|
|
40
|
+
title = 'On this page',
|
|
41
|
+
id = 'anchor',
|
|
42
|
+
offset = 96,
|
|
43
|
+
current,
|
|
44
|
+
scrollRoot,
|
|
45
|
+
updateHash = true,
|
|
46
|
+
onNavigate,
|
|
47
|
+
className,
|
|
48
|
+
...restProps
|
|
49
|
+
}: AnchorProps) {
|
|
50
|
+
const [spied, setSpied] = React.useState(0);
|
|
51
|
+
const controlled = current !== undefined;
|
|
52
|
+
const active = controlled ? Math.max(0, items.findIndex((item) => item.href === current)) : spied;
|
|
53
|
+
const rail = anchorRail(items.map((item) => item.level || 1));
|
|
54
|
+
const dash = anchorDash(rail, active);
|
|
55
|
+
|
|
56
|
+
// Follow the scrolling (unless the section in view is set from outside).
|
|
57
|
+
const list = React.useRef(items);
|
|
58
|
+
list.current = items;
|
|
59
|
+
const hrefs = items.map((item) => item.href).join(' ');
|
|
60
|
+
React.useEffect(() => {
|
|
61
|
+
if (controlled || typeof window === 'undefined') return undefined;
|
|
62
|
+
const root = rootOf(scrollRoot);
|
|
63
|
+
let frame = 0;
|
|
64
|
+
const update = () => { cancelAnimationFrame(frame); frame = requestAnimationFrame(() => setSpied(anchorActive(list.current, offset, root))); };
|
|
65
|
+
update();
|
|
66
|
+
const target: EventTarget = root || window;
|
|
67
|
+
target.addEventListener('scroll', update, { passive: true });
|
|
68
|
+
window.addEventListener('resize', update);
|
|
69
|
+
return () => { cancelAnimationFrame(frame); target.removeEventListener('scroll', update); window.removeEventListener('resize', update); };
|
|
70
|
+
}, [controlled, hrefs, offset, scrollRoot]);
|
|
71
|
+
|
|
72
|
+
const go = (href: string, i: number) => (event: React.MouseEvent<HTMLAnchorElement>) => {
|
|
73
|
+
if (onNavigate) onNavigate(href, event);
|
|
74
|
+
if (event.defaultPrevented) return;
|
|
75
|
+
if (anchorGo(href, updateHash)) { event.preventDefault(); if (!controlled) setSpied(i); }
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
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>
|
|
81
|
+
<div className="anchor-body">
|
|
82
|
+
<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
|
+
<path className="anchor-track" d={ rail.d } />
|
|
84
|
+
<path className="anchor-thumb" d={ rail.d } style={ { strokeDasharray: dash.dasharray, strokeDashoffset: dash.dashoffset } } />
|
|
85
|
+
</svg>
|
|
86
|
+
<ul className="anchor-list">
|
|
87
|
+
{ items.map((item, i) => (
|
|
88
|
+
<li key={ item.href } style={ { '--anchor-level': String((item.level || 1) - 1) } as React.CSSProperties }>
|
|
89
|
+
<a
|
|
90
|
+
className="anchor-link"
|
|
91
|
+
href={ item.href }
|
|
92
|
+
title={ item.label }
|
|
93
|
+
data-active={ i === active }
|
|
94
|
+
aria-current={ i === active ? 'location' : undefined }
|
|
95
|
+
onClick={ go(item.href, i) }
|
|
96
|
+
>
|
|
97
|
+
{ item.label }
|
|
98
|
+
</a>
|
|
99
|
+
</li>
|
|
100
|
+
)) }
|
|
101
|
+
</ul>
|
|
102
|
+
</div>
|
|
103
|
+
</nav>
|
|
104
|
+
);
|
|
105
|
+
}
|