@nurvus/ui 0.1.0-alpha.13 → 0.1.0-alpha.14
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 +5 -0
- package/dist/index.js +1 -1
- package/dist/nurvus.css +6 -5
- package/dist/respawn.js +1 -1
- package/dist/types/components/Menu/Menu.d.ts +35 -27
- package/dist/types/components/Select/Select.d.ts +55 -0
- package/dist/types/components/Tree/Tree.d.ts +39 -0
- package/dist/types/index.d.ts +5 -3
- package/dist/types/menu.d.ts +24 -0
- package/dist/types/respawn/Menu.d.ts +36 -26
- package/dist/types/respawn/Select.d.ts +54 -0
- package/dist/types/respawn/Tree.d.ts +36 -0
- package/dist/types/respawn/index.d.ts +5 -3
- package/dist/types/select.d.ts +70 -0
- package/dist/types/tree.d.ts +9 -0
- package/package.json +2 -2
- package/src/components/Button/Button.scss +8 -2
- package/src/components/Link/Link.md +11 -2
- package/src/components/Menu/Menu.md +32 -126
- package/src/components/Menu/Menu.scss +133 -51
- package/src/components/Menu/Menu.tsx +118 -67
- package/src/components/Select/Select.md +72 -0
- package/src/components/Select/Select.scss +170 -0
- package/src/components/Select/Select.tsx +196 -0
- package/src/components/Table/Table.md +3 -2
- package/src/components/Tag/Tag.scss +3 -1
- package/src/components/Text/Text.md +1 -1
- package/src/components/Tree/Tree.md +62 -0
- package/src/components/Tree/Tree.scss +73 -0
- package/src/components/Tree/Tree.tsx +125 -0
- package/src/css.ts +2 -1
- package/src/index.ts +5 -3
- package/src/menu.ts +66 -0
- package/src/respawn/Menu.ts +113 -62
- package/src/respawn/Select.ts +212 -0
- package/src/respawn/Tree.ts +116 -0
- package/src/respawn/index.ts +5 -3
- package/src/select.ts +140 -0
- package/src/styles/theme.scss +1 -1
- package/src/styles/type.json +1 -1
- package/src/tree.ts +46 -0
- package/dist/types/components/NavList/NavList.d.ts +0 -43
- package/dist/types/navList.d.ts +0 -7
- package/dist/types/respawn/NavList.d.ts +0 -43
- package/src/components/NavList/NavList.md +0 -39
- package/src/components/NavList/NavList.scss +0 -149
- package/src/components/NavList/NavList.tsx +0 -129
- package/src/navList.ts +0 -9
- package/src/respawn/NavList.ts +0 -126
package/src/select.ts
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
// Select rules shared by the React and the Respawn Select (no React here):
|
|
2
|
+
// the order options are listed in, typing to find one, and what each key does
|
|
3
|
+
// (the WAI-ARIA "select-only combobox" pattern).
|
|
4
|
+
|
|
5
|
+
export interface OptionLike {
|
|
6
|
+
value: string;
|
|
7
|
+
label?: unknown;
|
|
8
|
+
textValue?: string;
|
|
9
|
+
group?: string;
|
|
10
|
+
disabled?: boolean;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** A run of options under one heading ('' = no heading). */
|
|
14
|
+
export interface OptionGroup<O> { name: string; options: { option: O; index: number }[] }
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The options as the list shows them: grouped, groups in the order they first
|
|
18
|
+
* appear. `index` is the place in that order, which the keys move through.
|
|
19
|
+
*/
|
|
20
|
+
export function groupOptions<O extends OptionLike>(options: O[]): { groups: OptionGroup<O>[]; ordered: O[] } {
|
|
21
|
+
const groups: OptionGroup<O>[] = [];
|
|
22
|
+
options.forEach((option) => {
|
|
23
|
+
const name = option.group || '';
|
|
24
|
+
let group = groups.find((g) => g.name === name);
|
|
25
|
+
if (!group) { group = { name, options: [] }; groups.push(group); }
|
|
26
|
+
group.options.push({ option, index: -1 });
|
|
27
|
+
});
|
|
28
|
+
const ordered: O[] = [];
|
|
29
|
+
groups.forEach((g) => g.options.forEach((entry) => { entry.index = ordered.length; ordered.push(entry.option); }));
|
|
30
|
+
return { groups, ordered };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** The text typing matches: textValue, else a plain-text label, else the value. */
|
|
34
|
+
export const optionText = (option: OptionLike): string => (
|
|
35
|
+
option.textValue !== undefined ? option.textValue
|
|
36
|
+
: typeof option.label === 'string' || typeof option.label === 'number' ? String(option.label) : option.value
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
/** The next option that can be picked, `delta` steps away (stops at the ends). */
|
|
40
|
+
export function stepOption(options: OptionLike[], from: number, delta: number): number {
|
|
41
|
+
const dir = delta < 0 ? -1 : 1;
|
|
42
|
+
let at = from;
|
|
43
|
+
let found = from;
|
|
44
|
+
for (let moved = 0; moved < Math.abs(delta);) {
|
|
45
|
+
at += dir;
|
|
46
|
+
if (at < 0 || at >= options.length) break;
|
|
47
|
+
if (!options[at].disabled) { found = at; moved += 1; }
|
|
48
|
+
}
|
|
49
|
+
return found;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** The first (or last) option that can be picked, or -1. */
|
|
53
|
+
export function edgeOption(options: OptionLike[], last = false): number {
|
|
54
|
+
const list = options.map((o, i) => (o.disabled ? -1 : i)).filter((i) => i >= 0);
|
|
55
|
+
if (!list.length) return -1;
|
|
56
|
+
return last ? list[list.length - 1] : list[0];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Typing: the next option (after `from`, going round) whose text starts with
|
|
61
|
+
* what was typed. Typing one letter again and again steps through the options
|
|
62
|
+
* that start with it, as a native select does.
|
|
63
|
+
*/
|
|
64
|
+
export function matchOption(options: OptionLike[], typed: string, from: number): number {
|
|
65
|
+
const query = typed.toLowerCase();
|
|
66
|
+
const same = query.split('').every((c) => c === query[0]);
|
|
67
|
+
const find = (text: string, start: number) => {
|
|
68
|
+
for (let k = 0; k < options.length; k += 1) {
|
|
69
|
+
const i = (start + k) % options.length;
|
|
70
|
+
if (!options[i].disabled && optionText(options[i]).toLowerCase().startsWith(text)) return i;
|
|
71
|
+
}
|
|
72
|
+
return -1;
|
|
73
|
+
};
|
|
74
|
+
// A new search starts at the current option (it may already match); a repeated letter moves on.
|
|
75
|
+
const exact = find(query, query.length > 1 && !same ? Math.max(from, 0) : from + 1);
|
|
76
|
+
if (exact >= 0) return exact;
|
|
77
|
+
return same ? find(query[0], from + 1) : -1;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export type SelectAction =
|
|
81
|
+
| { type: 'open'; active: number }
|
|
82
|
+
| { type: 'move'; active: number }
|
|
83
|
+
| { type: 'pick'; active: number }
|
|
84
|
+
| { type: 'close' };
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* What a key does. Closed: ↓ ↑ Enter Space (and Alt+↓) open the list at the
|
|
88
|
+
* picked option; Home / End open it at the first / last. Open: ↓ ↑ Home End
|
|
89
|
+
* Page Up / Page Down move; Enter, Space, Alt+↑ and Tab pick and close; Esc
|
|
90
|
+
* closes without picking. null: not a key the select uses.
|
|
91
|
+
*/
|
|
92
|
+
export function selectKey(
|
|
93
|
+
key: string,
|
|
94
|
+
altKey: boolean,
|
|
95
|
+
state: { open: boolean; active: number; selected: number },
|
|
96
|
+
options: OptionLike[],
|
|
97
|
+
): SelectAction | null {
|
|
98
|
+
const { open, active, selected } = state;
|
|
99
|
+
const start = selected >= 0 && !options[selected].disabled ? selected : edgeOption(options);
|
|
100
|
+
if (!open) {
|
|
101
|
+
if (['ArrowDown', 'ArrowUp', 'Enter', ' '].includes(key)) return { type: 'open', active: start };
|
|
102
|
+
if (key === 'Home') return { type: 'open', active: edgeOption(options) };
|
|
103
|
+
if (key === 'End') return { type: 'open', active: edgeOption(options, true) };
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
switch (key) {
|
|
107
|
+
case 'ArrowDown': return { type: 'move', active: active < 0 ? start : stepOption(options, active, 1) };
|
|
108
|
+
case 'ArrowUp':
|
|
109
|
+
if (altKey) return { type: 'pick', active };
|
|
110
|
+
return { type: 'move', active: active < 0 ? start : stepOption(options, active, -1) };
|
|
111
|
+
case 'Home': return { type: 'move', active: edgeOption(options) };
|
|
112
|
+
case 'End': return { type: 'move', active: edgeOption(options, true) };
|
|
113
|
+
case 'PageDown': return { type: 'move', active: stepOption(options, Math.max(active, 0), 10) };
|
|
114
|
+
case 'PageUp': return { type: 'move', active: stepOption(options, Math.max(active, 0), -10) };
|
|
115
|
+
case 'Enter':
|
|
116
|
+
case ' ':
|
|
117
|
+
case 'Tab': return { type: 'pick', active };
|
|
118
|
+
case 'Escape': return { type: 'close' };
|
|
119
|
+
default: return null;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Remembers what was typed for half a second (typeahead). While text is pending, Space is part of the search, not a pick. */
|
|
124
|
+
export function typeahead(): { add: (key: string) => string; pending: () => boolean } {
|
|
125
|
+
let text = '';
|
|
126
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
127
|
+
return {
|
|
128
|
+
add: (key: string) => {
|
|
129
|
+
text += key;
|
|
130
|
+
if (timer) clearTimeout(timer);
|
|
131
|
+
timer = setTimeout(() => { text = ''; }, 500);
|
|
132
|
+
return text;
|
|
133
|
+
},
|
|
134
|
+
pending: () => text !== '',
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** A key that types a character (no Ctrl, Cmd or Alt). */
|
|
139
|
+
export const isPrintable = (event: { key: string; ctrlKey: boolean; metaKey: boolean; altKey: boolean }): boolean =>
|
|
140
|
+
event.key.length === 1 && !event.ctrlKey && !event.metaKey && !event.altKey;
|
package/src/styles/theme.scss
CHANGED
|
@@ -247,7 +247,7 @@
|
|
|
247
247
|
}
|
|
248
248
|
|
|
249
249
|
:root {
|
|
250
|
-
--nv-font-sans: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
|
|
250
|
+
--nv-font-sans: 'Inter Variable', 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
|
|
251
251
|
--nv-font-mono: 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
|
|
252
252
|
--nv-type-display-size: clamp(30px, 4vw, 44px);
|
|
253
253
|
--nv-type-display-line: 1.08;
|
package/src/styles/type.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"font": {
|
|
3
|
-
"sans": "'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif",
|
|
3
|
+
"sans": "'Inter Variable', 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif",
|
|
4
4
|
"mono": "'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, Consolas, monospace"
|
|
5
5
|
},
|
|
6
6
|
"roles": {
|
package/src/tree.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// Tree rules shared by the React and the Respawn Tree (no React here): the
|
|
2
|
+
// keys of the WAI-ARIA tree pattern, for rows that sit flat in the DOM with
|
|
3
|
+
// their depth in aria-level.
|
|
4
|
+
|
|
5
|
+
const level = (row: HTMLElement) => Number(row.getAttribute('aria-level')) || 1;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Moves focus like the WAI-ARIA tree pattern, with one row in the tab order
|
|
9
|
+
* at a time (roving tabindex):
|
|
10
|
+
* - Up / Down: the previous / next row; Home / End: the first / last;
|
|
11
|
+
* - Right: the first child row; Left: the parent row;
|
|
12
|
+
* - Enter / Space: selects the row (the same as a click).
|
|
13
|
+
* Returns true when it used the key.
|
|
14
|
+
*/
|
|
15
|
+
export function treeKeydown(event: KeyboardEvent, tree: HTMLElement): boolean {
|
|
16
|
+
if (event.altKey || event.ctrlKey || event.metaKey) return false;
|
|
17
|
+
const rows = Array.from(tree.querySelectorAll<HTMLElement>('[role="treeitem"]'));
|
|
18
|
+
const row = (event.target as HTMLElement).closest<HTMLElement>('[role="treeitem"]');
|
|
19
|
+
// Keys typed into something inside a row (a button in a custom row) are its own.
|
|
20
|
+
if (!row || row !== event.target) return false;
|
|
21
|
+
const at = rows.indexOf(row);
|
|
22
|
+
let next: HTMLElement | undefined;
|
|
23
|
+
|
|
24
|
+
switch (event.key) {
|
|
25
|
+
case 'ArrowDown': next = rows[at + 1]; break;
|
|
26
|
+
case 'ArrowUp': next = rows[at - 1]; break;
|
|
27
|
+
case 'Home': next = rows[0]; break;
|
|
28
|
+
case 'End': next = rows[rows.length - 1]; break;
|
|
29
|
+
case 'ArrowRight': next = rows[at + 1] && level(rows[at + 1]) > level(row) ? rows[at + 1] : undefined; break;
|
|
30
|
+
case 'ArrowLeft':
|
|
31
|
+
for (let i = at - 1; i >= 0; i -= 1) if (level(rows[i]) < level(row)) { next = rows[i]; break; }
|
|
32
|
+
break;
|
|
33
|
+
case 'Enter':
|
|
34
|
+
case ' ':
|
|
35
|
+
row.click();
|
|
36
|
+
event.preventDefault();
|
|
37
|
+
return true;
|
|
38
|
+
default: return false;
|
|
39
|
+
}
|
|
40
|
+
event.preventDefault();
|
|
41
|
+
if (next) {
|
|
42
|
+
rows.forEach((r) => r.setAttribute('tabindex', r === next ? '0' : '-1'));
|
|
43
|
+
next.focus();
|
|
44
|
+
}
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
export interface NavListItem {
|
|
3
|
-
/** What the link says. */
|
|
4
|
-
label: React.ReactNode;
|
|
5
|
-
/** Where it goes. The item whose href equals `current` is marked as the current page. */
|
|
6
|
-
href: string;
|
|
7
|
-
/** Draws a hairline above this item, to split a section into groups. */
|
|
8
|
-
divider?: boolean;
|
|
9
|
-
}
|
|
10
|
-
export interface NavListSection {
|
|
11
|
-
/** Stable id: names the section's open state and links its toggle to its list. Defaults to the title. */
|
|
12
|
-
id?: string;
|
|
13
|
-
/** The section heading. */
|
|
14
|
-
title: React.ReactNode;
|
|
15
|
-
/** Makes the heading a link too (e.g. to the section's overview page). */
|
|
16
|
-
href?: string;
|
|
17
|
-
/** The section's links. */
|
|
18
|
-
items: NavListItem[];
|
|
19
|
-
}
|
|
20
|
-
export interface NavListProps extends Omit<React.HTMLAttributes<HTMLElement>, 'onToggle'> {
|
|
21
|
-
/** The sections, in order. */
|
|
22
|
-
sections: NavListSection[];
|
|
23
|
-
/** The href of the page being shown: its link is marked, and its section opens. */
|
|
24
|
-
current?: string;
|
|
25
|
-
/** Which sections start open: true (all), false (none) or a map of section id → open. Defaults to all. */
|
|
26
|
-
defaultOpen?: boolean | Record<string, boolean>;
|
|
27
|
-
/** Called when a section opens or closes (e.g. to remember it). */
|
|
28
|
-
onToggle?: (id: string, open: boolean) => void;
|
|
29
|
-
/** Called when any link is clicked, with its href (e.g. to close a drawer). */
|
|
30
|
-
onNavigate?: (href: string, event: React.MouseEvent<HTMLAnchorElement>) => void;
|
|
31
|
-
/** Accessible name of the navigation landmark. */
|
|
32
|
-
label?: string;
|
|
33
|
-
/** Prefix for the ids that link each toggle to its list. Defaults to "nav-list". */
|
|
34
|
-
id?: string;
|
|
35
|
-
/** Custom class name. */
|
|
36
|
-
className?: string;
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Site navigation: sections of links that open and close (an accordion, with
|
|
40
|
-
* a smooth animation), and the current page marked by a bar beside it.
|
|
41
|
-
* A section opens by itself when the current page is in it.
|
|
42
|
-
*/
|
|
43
|
-
export default function NavList({ sections, current, defaultOpen, onToggle, onNavigate, label, id, className, ...restProps }: NavListProps): React.ReactElement;
|
package/dist/types/navList.d.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
/** A section's id: its own, or its title as a slug ("Get started" -> "get-started"). */
|
|
2
|
-
export declare const sectionId: (section: {
|
|
3
|
-
id?: string;
|
|
4
|
-
title: unknown;
|
|
5
|
-
}) => string;
|
|
6
|
-
/** Whether a section starts open: true / false for all, or a map of id -> open (missing = open). */
|
|
7
|
-
export declare const startsOpen: (defaultOpen: boolean | Record<string, boolean> | undefined, id: string) => boolean;
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { Child, ElementProps } from './spawn';
|
|
2
|
-
import { NurvusElement } from './update';
|
|
3
|
-
export interface NavListItem {
|
|
4
|
-
/** What the link says. */
|
|
5
|
-
label: Child;
|
|
6
|
-
/** Where it goes. The item whose href equals `current` is marked as the current page. */
|
|
7
|
-
href: string;
|
|
8
|
-
/** Draws a hairline above this item, to split a section into groups. */
|
|
9
|
-
divider?: boolean;
|
|
10
|
-
}
|
|
11
|
-
export interface NavListSection {
|
|
12
|
-
/** Stable id: names the section's open state and links its toggle to its list. Defaults to the title. */
|
|
13
|
-
id?: string;
|
|
14
|
-
/** The section heading. */
|
|
15
|
-
title: string;
|
|
16
|
-
/** Makes the heading a link too (e.g. to the section's overview page). */
|
|
17
|
-
href?: string;
|
|
18
|
-
/** The section's links. */
|
|
19
|
-
items: NavListItem[];
|
|
20
|
-
}
|
|
21
|
-
export interface NavListProps extends ElementProps {
|
|
22
|
-
/** The sections, in order. */
|
|
23
|
-
sections: NavListSection[];
|
|
24
|
-
/** The href of the page being shown: its link is marked, and its section opens. */
|
|
25
|
-
current?: string;
|
|
26
|
-
/** Which sections start open: true (all), false (none) or a map of section id -> open. Defaults to all. */
|
|
27
|
-
defaultOpen?: boolean | Record<string, boolean>;
|
|
28
|
-
/** Called when a section opens or closes (e.g. to remember it). */
|
|
29
|
-
onToggle?: (id: string, open: boolean) => void;
|
|
30
|
-
/** Called when any link is clicked, with its href (e.g. to close a drawer). */
|
|
31
|
-
onNavigate?: (href: string, event: MouseEvent) => void;
|
|
32
|
-
/** Accessible name of the navigation landmark. */
|
|
33
|
-
label?: string;
|
|
34
|
-
/** Prefix for the ids that link each toggle to its list. Defaults to "nav-list". */
|
|
35
|
-
id?: string;
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* Site navigation: sections of links that open and close (an accordion, with
|
|
39
|
-
* a smooth animation), and the current page marked by a bar beside it. A
|
|
40
|
-
* click on a toggle opens or closes its section in place (the element stays
|
|
41
|
-
* the same); update({ current }) marks another page and opens its section.
|
|
42
|
-
*/
|
|
43
|
-
export default function NavList(props: NavListProps): NurvusElement<HTMLElement, NavListProps>;
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
Sections open and close with the chevron (or the whole heading, when it isn't a link). The current page is marked with a bar beside it, and its section opens by itself.
|
|
2
|
-
```jsx
|
|
3
|
-
import { NavList } from '@nurvus/ui';
|
|
4
|
-
|
|
5
|
-
<div style={ { maxWidth: 260 } }>
|
|
6
|
-
<NavList
|
|
7
|
-
current="#/settings/billing"
|
|
8
|
-
onNavigate={ (href, event) => event.preventDefault() }
|
|
9
|
-
sections={ [
|
|
10
|
-
{ title: 'Account', href: '#/account', items: [
|
|
11
|
-
{ label: 'Profile', href: '#/account/profile' },
|
|
12
|
-
{ label: 'Security', href: '#/account/security' },
|
|
13
|
-
] },
|
|
14
|
-
{ title: 'Settings', items: [
|
|
15
|
-
{ label: 'General', href: '#/settings/general' },
|
|
16
|
-
{ label: 'Billing', href: '#/settings/billing' },
|
|
17
|
-
{ label: 'Danger zone', href: '#/settings/danger', divider: true },
|
|
18
|
-
] },
|
|
19
|
-
] }
|
|
20
|
-
/>
|
|
21
|
-
</div>
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
Start with some sections closed: `defaultOpen` takes a map of section ids (the title as a slug, or the section's own `id`).
|
|
25
|
-
```jsx
|
|
26
|
-
import { NavList } from '@nurvus/ui';
|
|
27
|
-
|
|
28
|
-
<div style={ { maxWidth: 260 } }>
|
|
29
|
-
<NavList
|
|
30
|
-
id="closed-example"
|
|
31
|
-
defaultOpen={ { reports: false } }
|
|
32
|
-
onNavigate={ (href, event) => event.preventDefault() }
|
|
33
|
-
sections={ [
|
|
34
|
-
{ title: 'Projects', items: [{ label: 'Website', href: '#/p/web' }, { label: 'App', href: '#/p/app' }] },
|
|
35
|
-
{ title: 'Reports', items: [{ label: 'Weekly', href: '#/r/week' }, { label: 'Monthly', href: '#/r/month' }] },
|
|
36
|
-
] }
|
|
37
|
-
/>
|
|
38
|
-
</div>
|
|
39
|
-
```
|
|
@@ -1,149 +0,0 @@
|
|
|
1
|
-
@import "../../styles/variables.scss";
|
|
2
|
-
|
|
3
|
-
// Site navigation. Sections open and close by animating the list's row
|
|
4
|
-
// (grid-template-rows 1fr <-> 0fr): smooth, no height measuring, any length.
|
|
5
|
-
$nav-list-speed: 0.2s;
|
|
6
|
-
|
|
7
|
-
.#{$cls-prefix}.nav-list {
|
|
8
|
-
font-size: 14px;
|
|
9
|
-
line-height: 20px;
|
|
10
|
-
|
|
11
|
-
.nav-list-section + .nav-list-section { margin-top: var(--space--8); }
|
|
12
|
-
|
|
13
|
-
.nav-list-heading {
|
|
14
|
-
display: flex;
|
|
15
|
-
align-items: center;
|
|
16
|
-
gap: var(--space--4);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
.nav-list-title {
|
|
20
|
-
flex: 1;
|
|
21
|
-
min-width: 0;
|
|
22
|
-
padding: var(--space--4) 10px;
|
|
23
|
-
border: 0;
|
|
24
|
-
border-radius: 6px;
|
|
25
|
-
background: none;
|
|
26
|
-
color: var(--nv-text);
|
|
27
|
-
font: inherit;
|
|
28
|
-
font-weight: 600;
|
|
29
|
-
text-align: left;
|
|
30
|
-
text-decoration: none;
|
|
31
|
-
cursor: pointer;
|
|
32
|
-
|
|
33
|
-
&:hover { background: var(--nv-surface-hover); }
|
|
34
|
-
&:focus-visible { outline: 2px solid var(--nv-accent); outline-offset: -2px; }
|
|
35
|
-
&[data-active="true"] { background: var(--nv-accent-soft); }
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// The chevron: a turned corner, pointing down when open, right when closed.
|
|
39
|
-
.nav-list-toggle {
|
|
40
|
-
display: inline-flex;
|
|
41
|
-
align-items: center;
|
|
42
|
-
justify-content: center;
|
|
43
|
-
|
|
44
|
-
&::after {
|
|
45
|
-
content: '';
|
|
46
|
-
width: 6px;
|
|
47
|
-
height: 6px;
|
|
48
|
-
margin: 0 var(--space--4);
|
|
49
|
-
border-right: 1.5px solid currentColor;
|
|
50
|
-
border-bottom: 1.5px solid currentColor;
|
|
51
|
-
transform: translateY(-2px) rotate(45deg);
|
|
52
|
-
transition: transform $nav-list-speed ease;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
// The icon-only toggle beside a heading that is a link.
|
|
57
|
-
.nav-list-toggle:not(.nav-list-title) {
|
|
58
|
-
flex: none;
|
|
59
|
-
width: 24px;
|
|
60
|
-
height: 24px;
|
|
61
|
-
padding: 0;
|
|
62
|
-
border: 0;
|
|
63
|
-
border-radius: 6px;
|
|
64
|
-
background: none;
|
|
65
|
-
color: var(--nv-text-muted);
|
|
66
|
-
cursor: pointer;
|
|
67
|
-
|
|
68
|
-
&::after { margin: 0; }
|
|
69
|
-
&:hover { background: var(--nv-surface-hover); color: var(--nv-text); }
|
|
70
|
-
&:focus-visible { outline: 2px solid var(--nv-accent); outline-offset: -2px; }
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// A heading with no page of its own: the whole row is the toggle.
|
|
74
|
-
.nav-list-title.nav-list-toggle { justify-content: space-between; }
|
|
75
|
-
|
|
76
|
-
.nav-list-section[data-open="false"] .nav-list-toggle::after { transform: rotate(-45deg); }
|
|
77
|
-
|
|
78
|
-
.nav-list-items {
|
|
79
|
-
display: grid;
|
|
80
|
-
grid-template-rows: 1fr;
|
|
81
|
-
transition: grid-template-rows $nav-list-speed ease;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
.nav-list-section[data-open="false"] .nav-list-items { grid-template-rows: 0fr; }
|
|
85
|
-
|
|
86
|
-
ul {
|
|
87
|
-
min-height: 0;
|
|
88
|
-
overflow: hidden;
|
|
89
|
-
// Room on the left for the current-page bar, which sits outside the item.
|
|
90
|
-
margin: 0 0 0 -12px;
|
|
91
|
-
padding: 0 0 0 12px; // no vertical padding: it would stay when the row closes to 0
|
|
92
|
-
list-style: none;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
li:first-child { margin-top: 2px; }
|
|
96
|
-
li:last-child { margin-bottom: var(--space--8); }
|
|
97
|
-
|
|
98
|
-
// Closed: out of sight and out of the tab order once the animation ends.
|
|
99
|
-
.nav-list-section[data-open="false"] ul {
|
|
100
|
-
visibility: hidden;
|
|
101
|
-
transition: visibility 0s $nav-list-speed;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
li + li { margin-top: 2px; }
|
|
105
|
-
|
|
106
|
-
li[data-divider] {
|
|
107
|
-
margin-top: var(--space--8);
|
|
108
|
-
padding-top: var(--space--8);
|
|
109
|
-
border-top: 1px solid var(--nv-border);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
.nav-list-item {
|
|
113
|
-
position: relative;
|
|
114
|
-
display: block;
|
|
115
|
-
padding: var(--space--4) 10px var(--space--4) 20px;
|
|
116
|
-
border-radius: 6px;
|
|
117
|
-
color: var(--nv-text-muted);
|
|
118
|
-
text-decoration: none;
|
|
119
|
-
|
|
120
|
-
&:hover { background: var(--nv-surface-hover); color: var(--nv-text); }
|
|
121
|
-
&:focus-visible { outline: 2px solid var(--nv-accent); outline-offset: -2px; }
|
|
122
|
-
|
|
123
|
-
&[data-active="true"] {
|
|
124
|
-
background: var(--nv-accent-soft);
|
|
125
|
-
color: var(--nv-text);
|
|
126
|
-
font-weight: 500;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
// The current page's marker: its own small rounded bar beside the item
|
|
130
|
-
// (as GitHub Primer's NavList draws it), not a border on the rounded item.
|
|
131
|
-
&[data-active="true"]::before {
|
|
132
|
-
content: '';
|
|
133
|
-
position: absolute;
|
|
134
|
-
top: 50%;
|
|
135
|
-
left: -8px;
|
|
136
|
-
width: 4px;
|
|
137
|
-
height: 18px;
|
|
138
|
-
border-radius: 2px;
|
|
139
|
-
background: var(--nv-accent);
|
|
140
|
-
transform: translateY(-50%);
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
@media (prefers-reduced-motion: reduce) {
|
|
145
|
-
.nav-list-items,
|
|
146
|
-
.nav-list-toggle::after,
|
|
147
|
-
.nav-list-section[data-open="false"] ul { transition: none; }
|
|
148
|
-
}
|
|
149
|
-
}
|
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import classNames from 'classnames';
|
|
3
|
-
import CONSTANT from '../../constants';
|
|
4
|
-
import { sectionId, startsOpen } from '../../navList';
|
|
5
|
-
import './NavList.scss';
|
|
6
|
-
|
|
7
|
-
export interface NavListItem {
|
|
8
|
-
/** What the link says. */
|
|
9
|
-
label: React.ReactNode;
|
|
10
|
-
/** Where it goes. The item whose href equals `current` is marked as the current page. */
|
|
11
|
-
href: string;
|
|
12
|
-
/** Draws a hairline above this item, to split a section into groups. */
|
|
13
|
-
divider?: boolean;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export interface NavListSection {
|
|
17
|
-
/** Stable id: names the section's open state and links its toggle to its list. Defaults to the title. */
|
|
18
|
-
id?: string;
|
|
19
|
-
/** The section heading. */
|
|
20
|
-
title: React.ReactNode;
|
|
21
|
-
/** Makes the heading a link too (e.g. to the section's overview page). */
|
|
22
|
-
href?: string;
|
|
23
|
-
/** The section's links. */
|
|
24
|
-
items: NavListItem[];
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export interface NavListProps extends Omit<React.HTMLAttributes<HTMLElement>, 'onToggle'> {
|
|
28
|
-
/** The sections, in order. */
|
|
29
|
-
sections: NavListSection[];
|
|
30
|
-
/** The href of the page being shown: its link is marked, and its section opens. */
|
|
31
|
-
current?: string;
|
|
32
|
-
/** Which sections start open: true (all), false (none) or a map of section id → open. Defaults to all. */
|
|
33
|
-
defaultOpen?: boolean | Record<string, boolean>;
|
|
34
|
-
/** Called when a section opens or closes (e.g. to remember it). */
|
|
35
|
-
onToggle?: (id: string, open: boolean) => void;
|
|
36
|
-
/** Called when any link is clicked, with its href (e.g. to close a drawer). */
|
|
37
|
-
onNavigate?: (href: string, event: React.MouseEvent<HTMLAnchorElement>) => void;
|
|
38
|
-
/** Accessible name of the navigation landmark. */
|
|
39
|
-
label?: string;
|
|
40
|
-
/** Prefix for the ids that link each toggle to its list. Defaults to "nav-list". */
|
|
41
|
-
id?: string;
|
|
42
|
-
/** Custom class name. */
|
|
43
|
-
className?: string;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Site navigation: sections of links that open and close (an accordion, with
|
|
48
|
-
* a smooth animation), and the current page marked by a bar beside it.
|
|
49
|
-
* A section opens by itself when the current page is in it.
|
|
50
|
-
*/
|
|
51
|
-
export default function NavList({
|
|
52
|
-
sections,
|
|
53
|
-
current,
|
|
54
|
-
defaultOpen = true,
|
|
55
|
-
onToggle,
|
|
56
|
-
onNavigate,
|
|
57
|
-
label = 'Pages',
|
|
58
|
-
id = 'nav-list',
|
|
59
|
-
className,
|
|
60
|
-
...restProps
|
|
61
|
-
}: NavListProps) {
|
|
62
|
-
// The current page's section starts open, whatever defaultOpen says.
|
|
63
|
-
const [open, setOpen] = React.useState<Record<string, boolean>>(() => {
|
|
64
|
-
const home = sections.find((s) => s.items.some((item) => item.href === current));
|
|
65
|
-
return Object.fromEntries(sections.map((s) => [sectionId(s), s === home || startsOpen(defaultOpen, sectionId(s))]));
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
// Navigating into a closed section opens it, so the current page is always visible.
|
|
69
|
-
React.useEffect(() => {
|
|
70
|
-
const home = sections.find((s) => s.items.some((item) => item.href === current));
|
|
71
|
-
if (home && open[sectionId(home)] === false) setOpen((was) => ({ ...was, [sectionId(home)]: true }));
|
|
72
|
-
}, [current]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
73
|
-
|
|
74
|
-
const toggle = (sid: string) => {
|
|
75
|
-
const next = open[sid] === false;
|
|
76
|
-
setOpen((was) => ({ ...was, [sid]: next }));
|
|
77
|
-
if (onToggle) onToggle(sid, next);
|
|
78
|
-
};
|
|
79
|
-
const navigate = (href: string) => (event: React.MouseEvent<HTMLAnchorElement>) => { if (onNavigate) onNavigate(href, event); };
|
|
80
|
-
|
|
81
|
-
return (
|
|
82
|
-
<nav className={ classNames(className, 'nav-list', CONSTANT.CLASS_PREFIX) } aria-label={ label } { ...restProps }>
|
|
83
|
-
{ sections.map((section) => {
|
|
84
|
-
const sid = sectionId(section);
|
|
85
|
-
const shown = open[sid] !== false;
|
|
86
|
-
const listId = `${id}-${sid}`;
|
|
87
|
-
const toggleProps = {
|
|
88
|
-
type: 'button' as const,
|
|
89
|
-
'aria-expanded': shown,
|
|
90
|
-
'aria-controls': listId,
|
|
91
|
-
onClick: () => toggle(sid),
|
|
92
|
-
};
|
|
93
|
-
return (
|
|
94
|
-
<div key={ sid } className="nav-list-section" data-section={ sid } data-open={ shown }>
|
|
95
|
-
<div className="nav-list-heading">
|
|
96
|
-
{ section.href ? (
|
|
97
|
-
<>
|
|
98
|
-
<a className="nav-list-title" href={ section.href } data-active={ current === section.href } onClick={ navigate(section.href) }>
|
|
99
|
-
{ section.title }
|
|
100
|
-
</a>
|
|
101
|
-
<button className="nav-list-toggle" aria-label={ `${shown ? 'Hide' : 'Show'} ${String(section.title)}` } { ...toggleProps } />
|
|
102
|
-
</>
|
|
103
|
-
) : (
|
|
104
|
-
<button className="nav-list-title nav-list-toggle" { ...toggleProps }>{ section.title }</button>
|
|
105
|
-
) }
|
|
106
|
-
</div>
|
|
107
|
-
<div className="nav-list-items" id={ listId }>
|
|
108
|
-
<ul>
|
|
109
|
-
{ section.items.map((item) => (
|
|
110
|
-
<li key={ item.href } data-divider={ item.divider || undefined }>
|
|
111
|
-
<a
|
|
112
|
-
className="nav-list-item"
|
|
113
|
-
href={ item.href }
|
|
114
|
-
data-active={ current === item.href }
|
|
115
|
-
aria-current={ current === item.href ? 'page' : undefined }
|
|
116
|
-
onClick={ navigate(item.href) }
|
|
117
|
-
>
|
|
118
|
-
{ item.label }
|
|
119
|
-
</a>
|
|
120
|
-
</li>
|
|
121
|
-
)) }
|
|
122
|
-
</ul>
|
|
123
|
-
</div>
|
|
124
|
-
</div>
|
|
125
|
-
);
|
|
126
|
-
}) }
|
|
127
|
-
</nav>
|
|
128
|
-
);
|
|
129
|
-
}
|
package/src/navList.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
// NavList rules shared by the React and the Respawn NavList (no React here).
|
|
2
|
-
|
|
3
|
-
/** A section's id: its own, or its title as a slug ("Get started" -> "get-started"). */
|
|
4
|
-
export const sectionId = (section: { id?: string; title: unknown }): string =>
|
|
5
|
-
section.id || String(section.title).toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, '');
|
|
6
|
-
|
|
7
|
-
/** Whether a section starts open: true / false for all, or a map of id -> open (missing = open). */
|
|
8
|
-
export const startsOpen = (defaultOpen: boolean | Record<string, boolean> | undefined, id: string): boolean =>
|
|
9
|
-
typeof defaultOpen === 'boolean' ? defaultOpen : !defaultOpen || defaultOpen[id] !== false;
|