@nurvus/ui 0.1.0-alpha.13 → 0.1.0-alpha.15
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 +8 -0
- package/dist/index.js +1 -1
- package/dist/nurvus.css +7 -5
- package/dist/respawn.js +1 -1
- package/dist/types/components/Icon/Icon.d.ts +20 -0
- 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/icons.d.ts +55 -0
- package/dist/types/index.d.ts +9 -3
- package/dist/types/menu.d.ts +24 -0
- package/dist/types/respawn/Icon.d.ts +22 -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 +9 -3
- package/dist/types/respawn/spawn.d.ts +2 -0
- 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/ButtonGroup/ButtonGroup.md +8 -12
- package/src/components/Icon/Icon.md +22 -0
- package/src/components/Icon/Icon.scss +10 -0
- package/src/components/Icon/Icon.tsx +45 -0
- package/src/components/Layout/Layout.md +10 -18
- package/src/components/Link/Link.md +11 -2
- package/src/components/Menu/Menu.md +34 -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 +3 -5
- 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 +3 -1
- package/src/icons.ts +88 -0
- package/src/index.ts +9 -3
- package/src/menu.ts +66 -0
- package/src/respawn/Icon.ts +47 -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 +9 -3
- package/src/respawn/spawn.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
|
@@ -31,9 +31,7 @@ import { Text } from '@nurvus/ui';
|
|
|
31
31
|
|
|
32
32
|
Using Text Overflow
|
|
33
33
|
```jsx
|
|
34
|
-
import
|
|
35
|
-
import AddIcon from '@material-ui/icons/Add';
|
|
36
|
-
import { Button, Layout, Menu, Text } from '@nurvus/ui';
|
|
34
|
+
import { Button, Icon, Layout, Text } from '@nurvus/ui';
|
|
37
35
|
|
|
38
36
|
const overflowStyle = {
|
|
39
37
|
textOverflow: 'ellipsis',
|
|
@@ -43,8 +41,8 @@ const overflowStyle = {
|
|
|
43
41
|
|
|
44
42
|
<Layout display="flex" alignItems="center" justifyContent="space-between">
|
|
45
43
|
<Text style={ overflowStyle }>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</Text>
|
|
46
|
-
<Button>
|
|
47
|
-
<
|
|
44
|
+
<Button aria-label="Add">
|
|
45
|
+
<Icon name="add" size="1.25em" />
|
|
48
46
|
</Button>
|
|
49
47
|
</Layout>
|
|
50
48
|
```
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
**Tree or Menu?** Tree picks an item in a list on the same page (layers, folders, outline). To take people to other pages, use [Menu](#/Components/Menu): its rows are links in a `<nav>`.
|
|
2
|
+
|
|
3
|
+
A list of rows to pick from, nested like a layers panel. Click a row, or use the keyboard: Tab reaches the active row, ↑ ↓ move, → goes to a child row, ← to its parent, Enter or Space picks.
|
|
4
|
+
```jsx
|
|
5
|
+
import { Tree } from '@nurvus/ui';
|
|
6
|
+
|
|
7
|
+
const items = [
|
|
8
|
+
{ label: 'Header', items: [{ label: 'Logo' }, { label: 'Navigation' }] },
|
|
9
|
+
{ label: 'Content', items: [{ label: 'Hero', items: [{ label: 'Title' }, { label: 'Button' }] }] },
|
|
10
|
+
{ label: 'Footer' },
|
|
11
|
+
];
|
|
12
|
+
|
|
13
|
+
const Layers = () => {
|
|
14
|
+
const [activePath, setActivePath] = React.useState([0, 1]);
|
|
15
|
+
return (
|
|
16
|
+
<div style={ { maxWidth: 280 } }>
|
|
17
|
+
<Tree
|
|
18
|
+
label="Layers"
|
|
19
|
+
activePath={ activePath }
|
|
20
|
+
items={ items }
|
|
21
|
+
onClick={ (e, path) => setActivePath(path) }
|
|
22
|
+
/>
|
|
23
|
+
</div>
|
|
24
|
+
);
|
|
25
|
+
};
|
|
26
|
+
<Layers />
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Draw your own rows with `itemRenderer`, e.g. a layer's name with its show and lock icons.
|
|
30
|
+
```jsx
|
|
31
|
+
import { Icon, Layout, Text, Tree } from '@nurvus/ui';
|
|
32
|
+
|
|
33
|
+
const itemRenderer = ({ label }) => (
|
|
34
|
+
<Layout display="flex" alignItems="center" justifyContent="space-between">
|
|
35
|
+
<Text>{ label }</Text>
|
|
36
|
+
<Layout display="flex" alignItems="center" itemSpacing={ 4 }>
|
|
37
|
+
<Icon name="eye" />
|
|
38
|
+
<Icon name="unlock" />
|
|
39
|
+
</Layout>
|
|
40
|
+
</Layout>
|
|
41
|
+
);
|
|
42
|
+
const items = [
|
|
43
|
+
{ label: 'Item 1', items: [{ label: 'Sub 1', items: [{ label: 'Sub Item 1' }, { label: 'Sub Item 2' }] }, { label: 'Sub 2' }] },
|
|
44
|
+
{ label: 'Item 2' },
|
|
45
|
+
];
|
|
46
|
+
|
|
47
|
+
const MyApp = () => {
|
|
48
|
+
const [activePath, setActivePath] = React.useState([0, 0, 0]);
|
|
49
|
+
return (
|
|
50
|
+
<div style={ { maxWidth: 320 } }>
|
|
51
|
+
<Tree
|
|
52
|
+
label="Layers"
|
|
53
|
+
activePath={ activePath }
|
|
54
|
+
items={ items }
|
|
55
|
+
itemRenderer={ itemRenderer }
|
|
56
|
+
onClick={ (e, path) => setActivePath(path) }
|
|
57
|
+
/>
|
|
58
|
+
</div>
|
|
59
|
+
);
|
|
60
|
+
};
|
|
61
|
+
<MyApp />
|
|
62
|
+
```
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
@import "../../styles/variables.scss";
|
|
2
|
+
@import "../../styles/tokens.scss";
|
|
3
|
+
|
|
4
|
+
.#{$cls-prefix}.tree {
|
|
5
|
+
// ---------------------------------------------------------------------------
|
|
6
|
+
// Indentation is token-driven, not a magic number in JS.
|
|
7
|
+
// Rows are FLAT siblings in the DOM (see Tree.tsx) — nesting is purely visual,
|
|
8
|
+
// produced by padding-left. Tree.tsx stamps `--nurvus-tree-depth` per row and
|
|
9
|
+
// clamps it with the `maxIndentDepth` prop, so a deep tree can never indent
|
|
10
|
+
// the label out of a narrow panel.
|
|
11
|
+
// Consumers retune the whole tree by overriding these two on .nurvus.tree.
|
|
12
|
+
// ---------------------------------------------------------------------------
|
|
13
|
+
--nurvus-tree-indent-base: #{$--space--8};
|
|
14
|
+
--nurvus-tree-indent-step: #{$--space--12};
|
|
15
|
+
|
|
16
|
+
background: var(--nv-bg);
|
|
17
|
+
color: var(--nv-text);
|
|
18
|
+
padding: $--space--4;
|
|
19
|
+
|
|
20
|
+
// Allow the tree to shrink inside a flex/grid parent instead of forcing its
|
|
21
|
+
// intrinsic width and pushing the row content out.
|
|
22
|
+
min-width: 0;
|
|
23
|
+
|
|
24
|
+
.tree-item {
|
|
25
|
+
position: relative;
|
|
26
|
+
min-width: 0; // ...and let row content ellipsis rather than collapse to 0
|
|
27
|
+
padding: $--space--8 $--space--12;
|
|
28
|
+
padding-left: calc(
|
|
29
|
+
var(--nurvus-tree-indent-base) +
|
|
30
|
+
var(--nurvus-tree-depth, 0) * var(--nurvus-tree-indent-step)
|
|
31
|
+
);
|
|
32
|
+
border-radius: $--radius--4;
|
|
33
|
+
color: var(--nv-text);
|
|
34
|
+
cursor: pointer;
|
|
35
|
+
|
|
36
|
+
&:hover {
|
|
37
|
+
background: var(--nv-surface-hover);
|
|
38
|
+
color: var(--nv-text);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
&:focus-visible {
|
|
42
|
+
outline: 2px solid var(--nv-accent);
|
|
43
|
+
outline-offset: -2px;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Active row: 2px accent rail + a tinted wash, both from the theme's
|
|
47
|
+
// accent, so every theme keeps the same "you are here" language.
|
|
48
|
+
&[data-active="true"] {
|
|
49
|
+
background: var(--nv-accent-soft);
|
|
50
|
+
color: var(--nv-text);
|
|
51
|
+
|
|
52
|
+
&::before {
|
|
53
|
+
content: '';
|
|
54
|
+
position: absolute;
|
|
55
|
+
left: 0;
|
|
56
|
+
top: 0;
|
|
57
|
+
height: 100%;
|
|
58
|
+
width: $--space--2;
|
|
59
|
+
border-radius: $--radius--2;
|
|
60
|
+
background: var(--nv-accent);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
&:hover {
|
|
64
|
+
background: color-mix(in srgb, var(--nv-accent) 22%, transparent);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Nested groups read one step quieter than leaf rows.
|
|
69
|
+
&[data-type="group"] {
|
|
70
|
+
color: var(--nv-text-muted);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import classNames from 'classnames';
|
|
3
|
+
import CONSTANT from '../../constants';
|
|
4
|
+
import { treeKeydown } from '../../tree';
|
|
5
|
+
import './Tree.scss';
|
|
6
|
+
|
|
7
|
+
export interface TreeItem {
|
|
8
|
+
/** What the row shows (unless itemRenderer draws it). */
|
|
9
|
+
label?: React.ReactNode;
|
|
10
|
+
/** Child rows, shown indented under this one. */
|
|
11
|
+
items?: TreeItem[];
|
|
12
|
+
/** Custom class name for this row. */
|
|
13
|
+
className?: string;
|
|
14
|
+
/** Inline style for this row. */
|
|
15
|
+
style?: React.CSSProperties;
|
|
16
|
+
/** Anything else your itemRenderer needs. */
|
|
17
|
+
[key: string]: any;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface TreeProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'onClick'> {
|
|
21
|
+
/** The rows to render; each can have child `items`. */
|
|
22
|
+
items?: TreeItem[];
|
|
23
|
+
/** The active row as a path of indexes, e.g. [2, 0] = the first child of the third row. */
|
|
24
|
+
activePath?: number[];
|
|
25
|
+
/** Draws a row's content instead of its label. */
|
|
26
|
+
itemRenderer?: (item: TreeItem) => React.ReactNode;
|
|
27
|
+
/** Called when a row is clicked (or picked with Enter or Space), with the event and that row's path. */
|
|
28
|
+
onClick?: (event: React.MouseEvent<HTMLDivElement>, path: number[]) => void;
|
|
29
|
+
/**
|
|
30
|
+
* Deepest level that still indents. Deeper rows stay at that indent, so the
|
|
31
|
+
* label keeps its room in a narrow panel.
|
|
32
|
+
*/
|
|
33
|
+
maxIndentDepth?: number;
|
|
34
|
+
/** Accessible name of the tree (screen readers say it), e.g. "Layers". */
|
|
35
|
+
label?: string;
|
|
36
|
+
/** Custom class name. */
|
|
37
|
+
className?: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* A nested list of rows to pick from, like a layers panel. Rows indent by
|
|
42
|
+
* depth, the active one is highlighted, and a click reports the row's path.
|
|
43
|
+
* By keyboard (the WAI-ARIA tree pattern): Tab reaches the active row, the
|
|
44
|
+
* arrow keys move between rows, Enter or Space picks one.
|
|
45
|
+
*/
|
|
46
|
+
export default function Tree({
|
|
47
|
+
activePath = [],
|
|
48
|
+
className,
|
|
49
|
+
itemRenderer,
|
|
50
|
+
items,
|
|
51
|
+
maxIndentDepth = 6,
|
|
52
|
+
onClick,
|
|
53
|
+
label = 'Tree',
|
|
54
|
+
onKeyDown,
|
|
55
|
+
children: _children,
|
|
56
|
+
...props
|
|
57
|
+
}: TreeProps) {
|
|
58
|
+
const handleClick = (event: React.MouseEvent<HTMLDivElement>, path: number[]) => {
|
|
59
|
+
event.stopPropagation();
|
|
60
|
+
if (onClick) onClick(event, path);
|
|
61
|
+
};
|
|
62
|
+
// One row is in the tab order: the active one, or else the first.
|
|
63
|
+
const activeKey = activePath.join();
|
|
64
|
+
const tabKey = activeKey && findPath(items, activePath) ? activeKey : '0';
|
|
65
|
+
|
|
66
|
+
const renderItems = (list: TreeItem[] | undefined, active: number[], path: number[] = []): React.ReactNode => {
|
|
67
|
+
if (!list) return null;
|
|
68
|
+
return list.map((item, i) => {
|
|
69
|
+
const { className: itemClassName, label: itemLabel, style } = item;
|
|
70
|
+
const type = path.length > 1 ? 'group' : '';
|
|
71
|
+
const isActive = active.length === 1 && active[0] === i;
|
|
72
|
+
const [first, ...remainingPath] = active;
|
|
73
|
+
const nextPath = [...path, i];
|
|
74
|
+
// Clamped so a deep tree cannot indent the label out of a narrow panel.
|
|
75
|
+
// The px maths lives in Tree.scss against spacing tokens; JS only supplies depth.
|
|
76
|
+
const depth = Math.min(path.length, maxIndentDepth);
|
|
77
|
+
const hasChildren = !!(item.items && item.items.length);
|
|
78
|
+
|
|
79
|
+
return (
|
|
80
|
+
<React.Fragment key={ i }>
|
|
81
|
+
<div
|
|
82
|
+
className={ classNames(itemClassName, 'tree-item', CONSTANT.CLASS_PREFIX) }
|
|
83
|
+
role="treeitem"
|
|
84
|
+
aria-level={ path.length + 1 }
|
|
85
|
+
aria-setsize={ list.length }
|
|
86
|
+
aria-posinset={ i + 1 }
|
|
87
|
+
aria-selected={ isActive }
|
|
88
|
+
aria-expanded={ hasChildren ? true : undefined }
|
|
89
|
+
tabIndex={ nextPath.join() === tabKey ? 0 : -1 }
|
|
90
|
+
data-active={ isActive }
|
|
91
|
+
data-type={ type }
|
|
92
|
+
onClick={ (e) => handleClick(e, nextPath) }
|
|
93
|
+
style={ { '--nurvus-tree-depth': String(depth), ...style } as React.CSSProperties }
|
|
94
|
+
>
|
|
95
|
+
{ itemRenderer ? itemRenderer(item) : itemLabel }
|
|
96
|
+
</div>
|
|
97
|
+
{ renderItems(item.items, first === i ? remainingPath : [], nextPath) }
|
|
98
|
+
</React.Fragment>
|
|
99
|
+
);
|
|
100
|
+
});
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
return (
|
|
104
|
+
<div
|
|
105
|
+
className={ classNames(className, 'tree', CONSTANT.CLASS_PREFIX) }
|
|
106
|
+
role="tree"
|
|
107
|
+
aria-label={ label }
|
|
108
|
+
{ ...props }
|
|
109
|
+
onKeyDown={ (e) => { treeKeydown(e.nativeEvent, e.currentTarget); if (onKeyDown) onKeyDown(e); } }
|
|
110
|
+
>
|
|
111
|
+
{ renderItems(items, activePath) }
|
|
112
|
+
</div>
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Whether `path` names a row in `items`. */
|
|
117
|
+
function findPath(items: TreeItem[] | undefined, path: number[]): boolean {
|
|
118
|
+
let list = items;
|
|
119
|
+
for (let i = 0; i < path.length; i += 1) {
|
|
120
|
+
const item = list && list[path[i]];
|
|
121
|
+
if (!item) return false;
|
|
122
|
+
list = item.items;
|
|
123
|
+
}
|
|
124
|
+
return path.length > 0;
|
|
125
|
+
}
|
package/src/css.ts
CHANGED
|
@@ -7,8 +7,10 @@ import './components/FullPageModal/FullPageModal.scss';
|
|
|
7
7
|
import './components/HistoryList/HistoryList.scss';
|
|
8
8
|
import './components/Layout/Layout.scss';
|
|
9
9
|
import './components/Link/Link.scss';
|
|
10
|
+
import './components/Icon/Icon.scss';
|
|
11
|
+
import './components/Tree/Tree.scss';
|
|
10
12
|
import './components/Menu/Menu.scss';
|
|
11
|
-
import './components/
|
|
13
|
+
import './components/Select/Select.scss';
|
|
12
14
|
import './components/Panel/Panel.scss';
|
|
13
15
|
import './components/Progress/Progress.scss';
|
|
14
16
|
import './components/Table/Table.scss';
|
package/src/icons.ts
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// The NURVUS icons, drawn for this library (no icon font, no third-party set).
|
|
2
|
+
// Shared by the React and the Respawn Icon (no React here).
|
|
3
|
+
//
|
|
4
|
+
// The grid: a 24 x 24 artboard with 2 units of padding (a 20 x 20 live area),
|
|
5
|
+
// 2-unit strokes with round caps and joins, corners of radius 2, circles of
|
|
6
|
+
// radius 9. Every shape is a stroke in currentColor unless it says otherwise,
|
|
7
|
+
// so an icon takes the color of the text around it. Icons are 1em by default:
|
|
8
|
+
// next to 16px text they are 16px, in a 22px title 22px.
|
|
9
|
+
|
|
10
|
+
/** One SVG element of an icon: its tag and attributes. */
|
|
11
|
+
export type IconNode = [tag: 'path' | 'circle' | 'rect', attrs: Record<string, string | number>];
|
|
12
|
+
|
|
13
|
+
const path = (d: string): IconNode => ['path', { d }];
|
|
14
|
+
const circle = (cx: number, cy: number, r: number): IconNode => ['circle', { cx, cy, r }];
|
|
15
|
+
const rect = (x: number, y: number, width: number, height: number, rx = 2): IconNode => ['rect', { x, y, width, height, rx }];
|
|
16
|
+
const ring = circle(12, 12, 9);
|
|
17
|
+
const dot = (x: number, y: number) => path(`M${x} ${y}h.01`);
|
|
18
|
+
|
|
19
|
+
const DEFS = {
|
|
20
|
+
// Navigation
|
|
21
|
+
menu: [path('M4 6h16M4 12h16M4 18h16')],
|
|
22
|
+
close: [path('M6 6l12 12M18 6L6 18')],
|
|
23
|
+
'chevron-down': [path('M6 9l6 6 6-6')],
|
|
24
|
+
'chevron-up': [path('M6 15l6-6 6 6')],
|
|
25
|
+
'chevron-left': [path('M15 6l-6 6 6 6')],
|
|
26
|
+
'chevron-right': [path('M9 6l6 6-6 6')],
|
|
27
|
+
'arrow-left': [path('M19 12H5M11 6l-6 6 6 6')],
|
|
28
|
+
'arrow-right': [path('M5 12h14M13 6l6 6-6 6')],
|
|
29
|
+
'arrow-up': [path('M12 19V5M6 11l6-6 6 6')],
|
|
30
|
+
'arrow-down': [path('M12 5v14M6 13l6 6 6-6')],
|
|
31
|
+
'external-link': [path('M14 4h6v6M20 4l-9 9M18 14v4a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h4')],
|
|
32
|
+
// Actions
|
|
33
|
+
add: [path('M12 5v14M5 12h14')],
|
|
34
|
+
remove: [path('M5 12h14')],
|
|
35
|
+
edit: [path('M4 20h4L19 9a2.83 2.83 0 0 0-4-4L4 16v4z'), path('M13.5 6.5l4 4')],
|
|
36
|
+
copy: [rect(8, 8, 12, 12), path('M16 8V6a2 2 0 0 0-2-2H6a2 2 0 0 0-2 2v8a2 2 0 0 0 2 2h2')],
|
|
37
|
+
delete: [path('M4 7h16M9 7V5a1 1 0 0 1 1-1h4a1 1 0 0 1 1 1v2M6 7l1 12a2 2 0 0 0 2 2h6a2 2 0 0 0 2-2l1-12M10 11v6M14 11v6')],
|
|
38
|
+
download: [path('M12 4v11M7 10l5 5 5-5M4 20h16')],
|
|
39
|
+
upload: [path('M12 15V4M7 9l5-5 5 5M4 20h16')],
|
|
40
|
+
search: [circle(11, 11, 6.5), path('M16 16l4 4')],
|
|
41
|
+
filter: [path('M4 5h16l-6 7.5V19l-4 2v-8.5z')],
|
|
42
|
+
sliders: [path('M4 7h10M18 7h2M4 17h4M12 17h8'), circle(16, 7, 2), circle(10, 17, 2)],
|
|
43
|
+
refresh: [path('M19 12a7 7 0 1 1-7-7'), path('M9.5 2.5L12 5l-2.5 2.5')],
|
|
44
|
+
undo: [path('M9 14L4 9l5-5M4 9h10.5a5.5 5.5 0 0 1 0 11H11')],
|
|
45
|
+
redo: [path('M15 14l5-5-5-5M20 9H9.5a5.5 5.5 0 0 0 0 11H13')],
|
|
46
|
+
// Status
|
|
47
|
+
check: [path('M5 12.5l4.5 4.5L19 7.5')],
|
|
48
|
+
info: [ring, path('M12 11v5'), dot(12, 8)],
|
|
49
|
+
warning: [path('M10.27 4.3L2.6 18a2 2 0 0 0 1.73 3h15.34a2 2 0 0 0 1.73-3L13.73 4.3a2 2 0 0 0-3.46 0z'), path('M12 9.5v4'), dot(12, 17)],
|
|
50
|
+
error: [ring, path('M15 9l-6 6M9 9l6 6')],
|
|
51
|
+
help: [ring, path('M9.5 9.5a2.5 2.5 0 1 1 3.5 2.3c-.7.3-1 .8-1 1.5v.4'), dot(12, 17)],
|
|
52
|
+
// Things
|
|
53
|
+
file: [path('M14 3H7a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V8z'), path('M14 3v5h5')],
|
|
54
|
+
folder: [path('M3 7a2 2 0 0 1 2-2h4l2 2h8a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z')],
|
|
55
|
+
image: [rect(3, 4, 18, 16), circle(9, 10, 1.5), path('M21 15l-5-5L6 20')],
|
|
56
|
+
layers: [path('M12 3l9 5-9 5-9-5z'), path('M3 13l9 5 9-5')],
|
|
57
|
+
eye: [path('M2 12s3.6-6 10-6 10 6 10 6-3.6 6-10 6S2 12 2 12z'), circle(12, 12, 3)],
|
|
58
|
+
'eye-off': [path('M3 3l18 18'), path('M10.6 6.1A9.7 9.7 0 0 1 12 6c6.4 0 10 6 10 6a17 17 0 0 1-2.6 3.4M6.6 6.6C3.9 8.4 2 12 2 12s3.6 6 10 6a9.7 9.7 0 0 0 5.4-1.6'), path('M9.9 9.9a3 3 0 0 0 4.2 4.2')],
|
|
59
|
+
lock: [rect(5, 11, 14, 10), path('M8 11V8a4 4 0 0 1 8 0v3')],
|
|
60
|
+
unlock: [rect(5, 11, 14, 10), path('M8 11V8a4 4 0 0 1 7.75-1.4')],
|
|
61
|
+
link: [path('M10 14a4 4 0 0 0 5.66 0l3-3a4 4 0 0 0-5.66-5.66l-1 1'), path('M14 10a4 4 0 0 0-5.66 0l-3 3a4 4 0 0 0 5.66 5.66l1-1')],
|
|
62
|
+
// Theme
|
|
63
|
+
sun: [circle(12, 12, 4), path('M12 2v2M12 20v2M4.93 4.93l1.41 1.41M17.66 17.66l1.41 1.41M2 12h2M20 12h2M4.93 19.07l1.41-1.41M17.66 6.34l1.41-1.41')],
|
|
64
|
+
moon: [path('M20 14.5A8 8 0 1 1 9.5 4a6.5 6.5 0 0 0 10.5 10.5z')],
|
|
65
|
+
// Code
|
|
66
|
+
code: [path('M8 7l-5 5 5 5M16 7l5 5-5 5')],
|
|
67
|
+
terminal: [path('M4 17l6-5-6-5M12 19h8')],
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
/** Every icon's name. */
|
|
71
|
+
export type IconName = keyof typeof DEFS;
|
|
72
|
+
|
|
73
|
+
/** Every icon: its name and the shapes it is drawn with. */
|
|
74
|
+
export const ICONS: Record<IconName, IconNode[]> = DEFS;
|
|
75
|
+
|
|
76
|
+
/** The names, in the order above (groups: navigation, actions, status, things, theme, code). */
|
|
77
|
+
export const ICON_NAMES = Object.keys(DEFS) as IconName[];
|
|
78
|
+
|
|
79
|
+
let warned = '';
|
|
80
|
+
/** An unknown name draws an empty icon and says so once. */
|
|
81
|
+
export function iconNodes(name: string): IconNode[] {
|
|
82
|
+
const nodes = (ICONS as Record<string, IconNode[]>)[name];
|
|
83
|
+
if (!nodes && warned !== name && typeof console !== 'undefined') {
|
|
84
|
+
warned = name;
|
|
85
|
+
console.warn(`[@nurvus/ui] Icon: there is no icon named "${name}". The names: ${ICON_NAMES.join(', ')}.`);
|
|
86
|
+
}
|
|
87
|
+
return nodes || [];
|
|
88
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -22,10 +22,16 @@ export { default as Progress } from './components/Progress/Progress';
|
|
|
22
22
|
export type { ProgressProps, ProgressSize, ProgressStatus } from './components/Progress/Progress';
|
|
23
23
|
export { default as Layout } from './components/Layout/Layout';
|
|
24
24
|
export type { LayoutProps, Spacing, LegacySpacing } from './components/Layout/Layout';
|
|
25
|
+
export { default as Icon } from './components/Icon/Icon';
|
|
26
|
+
export type { IconProps } from './components/Icon/Icon';
|
|
27
|
+
export { ICONS, ICON_NAMES } from './icons';
|
|
28
|
+
export type { IconName, IconNode } from './icons';
|
|
29
|
+
export { default as Tree } from './components/Tree/Tree';
|
|
30
|
+
export type { TreeProps, TreeItem } from './components/Tree/Tree';
|
|
25
31
|
export { default as Menu } from './components/Menu/Menu';
|
|
26
|
-
export type { MenuProps, MenuItem } from './components/Menu/Menu';
|
|
27
|
-
export { default as
|
|
28
|
-
export type {
|
|
32
|
+
export type { MenuProps, MenuItem, MenuSection } from './components/Menu/Menu';
|
|
33
|
+
export { default as Select } from './components/Select/Select';
|
|
34
|
+
export type { SelectProps, SelectOption, SelectOptionState } from './components/Select/Select';
|
|
29
35
|
export { default as FullPageModal } from './components/FullPageModal/FullPageModal';
|
|
30
36
|
export type { FullPageModalProps } from './components/FullPageModal/FullPageModal';
|
|
31
37
|
export { default as HistoryList } from './components/HistoryList/HistoryList';
|
package/src/menu.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
// Menu rules shared by the React and the Respawn Menu (no React here): the
|
|
2
|
+
// section ids, which sections start open, and the arrow keys.
|
|
3
|
+
|
|
4
|
+
/** A section's id: its own, or its title as a slug ("Get started" -> "get-started"). */
|
|
5
|
+
export const sectionId = (section: { id?: string; title: unknown }): string =>
|
|
6
|
+
section.id || String(section.title).toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, '');
|
|
7
|
+
|
|
8
|
+
/** Whether a section starts open: true / false for all, or a map of id -> open (missing = open). */
|
|
9
|
+
export const startsOpen = (defaultOpen: boolean | Record<string, boolean> | undefined, id: string): boolean =>
|
|
10
|
+
typeof defaultOpen === 'boolean' ? defaultOpen : !defaultOpen || defaultOpen[id] !== false;
|
|
11
|
+
|
|
12
|
+
let warnedOldProps = false;
|
|
13
|
+
/** Menu used to be the nested, selectable list that is Tree now: say so once. */
|
|
14
|
+
export function warnOldMenuProps(props: { sections?: unknown; items?: unknown; activePath?: unknown }): boolean {
|
|
15
|
+
const old = !props.sections && (props.items !== undefined || props.activePath !== undefined);
|
|
16
|
+
if (old && !warnedOldProps && typeof console !== 'undefined') {
|
|
17
|
+
warnedOldProps = true;
|
|
18
|
+
console.warn('[@nurvus/ui] Menu is site navigation now (sections of links). The nested, selectable list it used to be is Tree: import { Tree } from \'@nurvus/ui\'. Drawing a Tree for now.');
|
|
19
|
+
}
|
|
20
|
+
return old;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The arrow keys on a Menu (the WAI-ARIA disclosure navigation pattern's
|
|
25
|
+
* optional keys). Tab still goes link by link; on top of that:
|
|
26
|
+
* - Up / Down: the previous / next row (section titles and the links in open
|
|
27
|
+
* sections), going round at the ends;
|
|
28
|
+
* - Home / End: the first / last row;
|
|
29
|
+
* - Right on a section: opens it, or (when open) goes to its first link;
|
|
30
|
+
* - Left on a section: closes it; Left on a link: goes up to its section.
|
|
31
|
+
* Returns true when it used the key.
|
|
32
|
+
*/
|
|
33
|
+
export function menuKeydown(event: KeyboardEvent, nav: HTMLElement): boolean {
|
|
34
|
+
if (event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) return false;
|
|
35
|
+
const target = event.target as HTMLElement;
|
|
36
|
+
const rows = Array.from(nav.querySelectorAll<HTMLElement>('.menu-title, .menu-item'))
|
|
37
|
+
.filter((row) => !row.closest('.menu-section[data-open="false"] .menu-items'));
|
|
38
|
+
if (!rows.length) return false;
|
|
39
|
+
const section = target.closest<HTMLElement>('.menu-section');
|
|
40
|
+
// The chevron beside a linked title counts as that title's row.
|
|
41
|
+
const onHeading = !!target.closest('.menu-heading');
|
|
42
|
+
const title = section && section.querySelector<HTMLElement>('.menu-title');
|
|
43
|
+
const at = rows.indexOf(onHeading && title ? title : target);
|
|
44
|
+
const focus = (row: HTMLElement | undefined) => { if (row) row.focus(); };
|
|
45
|
+
const toggle = () => { const button = section && section.querySelector<HTMLElement>('.menu-toggle'); if (button) button.click(); };
|
|
46
|
+
const isOpen = !!section && section.getAttribute('data-open') !== 'false';
|
|
47
|
+
|
|
48
|
+
switch (event.key) {
|
|
49
|
+
case 'ArrowDown': focus(rows[(at + 1) % rows.length]); break;
|
|
50
|
+
case 'ArrowUp': focus(rows[at <= 0 ? rows.length - 1 : at - 1]); break;
|
|
51
|
+
case 'Home': focus(rows[0]); break;
|
|
52
|
+
case 'End': focus(rows[rows.length - 1]); break;
|
|
53
|
+
case 'ArrowRight':
|
|
54
|
+
if (!onHeading || !section) return false;
|
|
55
|
+
if (!isOpen) toggle();
|
|
56
|
+
else focus(section.querySelector<HTMLElement>('.menu-item') || undefined);
|
|
57
|
+
break;
|
|
58
|
+
case 'ArrowLeft':
|
|
59
|
+
if (!section) return false;
|
|
60
|
+
if (onHeading) { if (isOpen) toggle(); } else focus(title || undefined);
|
|
61
|
+
break;
|
|
62
|
+
default: return false;
|
|
63
|
+
}
|
|
64
|
+
event.preventDefault();
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import classNames from 'classnames';
|
|
2
|
+
import CONSTANT from '../constants';
|
|
3
|
+
import { iconNodes, IconName } from '../icons';
|
|
4
|
+
import { spawn, ElementProps } from './spawn';
|
|
5
|
+
import { updatable } from './update';
|
|
6
|
+
import '../components/Icon/Icon.scss';
|
|
7
|
+
|
|
8
|
+
const SVG = 'http://www.w3.org/2000/svg';
|
|
9
|
+
|
|
10
|
+
export interface IconProps extends ElementProps {
|
|
11
|
+
/** Which icon (Foundations > Icons lists them all). */
|
|
12
|
+
name: IconName;
|
|
13
|
+
/** Width and height. Defaults to 1em: the icon is as big as the text around it. A number is pixels. */
|
|
14
|
+
size?: number | string;
|
|
15
|
+
/** Says what the icon means, for an icon that stands alone (screen readers read it). Leave it out when text beside it already says it. */
|
|
16
|
+
label?: string;
|
|
17
|
+
/** Line thickness in grid units (the grid is 24). */
|
|
18
|
+
strokeWidth?: number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** The <svg> Icon returns, with update() like every other component. */
|
|
22
|
+
export type IconElement = SVGSVGElement & { update(changes: Partial<IconProps>): IconElement };
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* An icon from the NURVUS set, as an <svg> element: the same markup as the
|
|
26
|
+
* React Icon. 1em by default, in the text's color; decorative unless it has a
|
|
27
|
+
* label. icon.update({ name: 'check' }) swaps it for another.
|
|
28
|
+
*/
|
|
29
|
+
export default function Icon(props: IconProps): IconElement {
|
|
30
|
+
return updatable(({ name, size = '1em', label, strokeWidth = 2, className, ...rest }: IconProps) => spawn<any>({
|
|
31
|
+
tag: 'svg',
|
|
32
|
+
namespace: SVG,
|
|
33
|
+
className: classNames(className, 'icon', CONSTANT.CLASS_PREFIX),
|
|
34
|
+
'data-icon': name,
|
|
35
|
+
viewBox: '0 0 24 24',
|
|
36
|
+
width: size,
|
|
37
|
+
height: size,
|
|
38
|
+
fill: 'none',
|
|
39
|
+
stroke: 'currentColor',
|
|
40
|
+
'stroke-width': strokeWidth,
|
|
41
|
+
'stroke-linecap': 'round',
|
|
42
|
+
'stroke-linejoin': 'round',
|
|
43
|
+
...(label ? { role: 'img', 'aria-label': label } : { 'aria-hidden': true, focusable: 'false' }),
|
|
44
|
+
...rest,
|
|
45
|
+
children: iconNodes(name).map(([tag, attrs]) => spawn({ tag, namespace: SVG, ...attrs })),
|
|
46
|
+
}), props) as unknown as IconElement;
|
|
47
|
+
}
|