@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.
Files changed (61) hide show
  1. package/README.md +8 -0
  2. package/dist/index.js +1 -1
  3. package/dist/nurvus.css +7 -5
  4. package/dist/respawn.js +1 -1
  5. package/dist/types/components/Icon/Icon.d.ts +20 -0
  6. package/dist/types/components/Menu/Menu.d.ts +35 -27
  7. package/dist/types/components/Select/Select.d.ts +55 -0
  8. package/dist/types/components/Tree/Tree.d.ts +39 -0
  9. package/dist/types/icons.d.ts +55 -0
  10. package/dist/types/index.d.ts +9 -3
  11. package/dist/types/menu.d.ts +24 -0
  12. package/dist/types/respawn/Icon.d.ts +22 -0
  13. package/dist/types/respawn/Menu.d.ts +36 -26
  14. package/dist/types/respawn/Select.d.ts +54 -0
  15. package/dist/types/respawn/Tree.d.ts +36 -0
  16. package/dist/types/respawn/index.d.ts +9 -3
  17. package/dist/types/respawn/spawn.d.ts +2 -0
  18. package/dist/types/select.d.ts +70 -0
  19. package/dist/types/tree.d.ts +9 -0
  20. package/package.json +2 -2
  21. package/src/components/Button/Button.scss +8 -2
  22. package/src/components/ButtonGroup/ButtonGroup.md +8 -12
  23. package/src/components/Icon/Icon.md +22 -0
  24. package/src/components/Icon/Icon.scss +10 -0
  25. package/src/components/Icon/Icon.tsx +45 -0
  26. package/src/components/Layout/Layout.md +10 -18
  27. package/src/components/Link/Link.md +11 -2
  28. package/src/components/Menu/Menu.md +34 -126
  29. package/src/components/Menu/Menu.scss +133 -51
  30. package/src/components/Menu/Menu.tsx +118 -67
  31. package/src/components/Select/Select.md +72 -0
  32. package/src/components/Select/Select.scss +170 -0
  33. package/src/components/Select/Select.tsx +196 -0
  34. package/src/components/Table/Table.md +3 -2
  35. package/src/components/Tag/Tag.scss +3 -1
  36. package/src/components/Text/Text.md +3 -5
  37. package/src/components/Tree/Tree.md +62 -0
  38. package/src/components/Tree/Tree.scss +73 -0
  39. package/src/components/Tree/Tree.tsx +125 -0
  40. package/src/css.ts +3 -1
  41. package/src/icons.ts +88 -0
  42. package/src/index.ts +9 -3
  43. package/src/menu.ts +66 -0
  44. package/src/respawn/Icon.ts +47 -0
  45. package/src/respawn/Menu.ts +113 -62
  46. package/src/respawn/Select.ts +212 -0
  47. package/src/respawn/Tree.ts +116 -0
  48. package/src/respawn/index.ts +9 -3
  49. package/src/respawn/spawn.ts +5 -3
  50. package/src/select.ts +140 -0
  51. package/src/styles/theme.scss +1 -1
  52. package/src/styles/type.json +1 -1
  53. package/src/tree.ts +46 -0
  54. package/dist/types/components/NavList/NavList.d.ts +0 -43
  55. package/dist/types/navList.d.ts +0 -7
  56. package/dist/types/respawn/NavList.d.ts +0 -43
  57. package/src/components/NavList/NavList.md +0 -39
  58. package/src/components/NavList/NavList.scss +0 -149
  59. package/src/components/NavList/NavList.tsx +0 -129
  60. package/src/navList.ts +0 -9
  61. package/src/respawn/NavList.ts +0 -126
@@ -1,90 +1,141 @@
1
1
  import React from 'react';
2
2
  import classNames from 'classnames';
3
3
  import CONSTANT from '../../constants';
4
+ import { sectionId, startsOpen, menuKeydown, warnOldMenuProps } from '../../menu';
5
+ import Tree from '../Tree/Tree';
4
6
  import './Menu.scss';
5
7
 
6
8
  export interface MenuItem {
7
- /** What the row shows (unless itemRenderer draws it). */
8
- label?: React.ReactNode;
9
- /** Child rows, shown indented under this one. */
10
- items?: MenuItem[];
11
- /** Custom class name for this row. */
12
- className?: string;
13
- /** Inline style for this row. */
14
- style?: React.CSSProperties;
15
- /** Anything else your itemRenderer needs. */
16
- [key: string]: any;
9
+ /** What the link says. */
10
+ label: React.ReactNode;
11
+ /** Where it goes. The item whose href equals `current` is marked as the current page. */
12
+ href: string;
13
+ /** Draws a hairline above this item, to split a section into groups. */
14
+ divider?: boolean;
15
+ }
16
+
17
+ export interface MenuSection {
18
+ /** Stable id: names the section's open state and links its toggle to its list. Defaults to the title. */
19
+ id?: string;
20
+ /** The section heading. */
21
+ title: React.ReactNode;
22
+ /** Makes the heading a link too (e.g. to the section's overview page). */
23
+ href?: string;
24
+ /** The section's links. */
25
+ items: MenuItem[];
17
26
  }
18
27
 
19
- export interface MenuProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'onClick'> {
20
- /** The rows to render; each can have child `items`. */
21
- items?: MenuItem[];
22
- /** The active row as a path of indexes, e.g. [2, 0] = the first child of the third row. */
23
- activePath?: number[];
24
- /** Draws a row's content instead of its label. */
25
- itemRenderer?: (item: MenuItem) => React.ReactNode;
26
- /** Called on a row click with the event and that row's path. */
27
- onClick?: (event: React.MouseEvent<HTMLDivElement>, path: number[]) => void;
28
- /**
29
- * Deepest level that still indents. Deeper rows stay at that indent, so the
30
- * label keeps its room in a narrow panel.
31
- */
32
- maxIndentDepth?: number;
28
+ export interface MenuProps extends Omit<React.HTMLAttributes<HTMLElement>, 'onToggle'> {
29
+ /** The sections, in order. */
30
+ sections: MenuSection[];
31
+ /** The href of the page being shown: its link is marked, and its section opens. */
32
+ current?: string;
33
+ /** Which sections start open: true (all), false (none) or a map of section id → open. Defaults to all. */
34
+ defaultOpen?: boolean | Record<string, boolean>;
35
+ /** Called when a section opens or closes (e.g. to remember it). */
36
+ onToggle?: (id: string, open: boolean) => void;
37
+ /** Called when any link is clicked, with its href (e.g. to close a drawer). */
38
+ onNavigate?: (href: string, event: React.MouseEvent<HTMLAnchorElement>) => void;
39
+ /** Accessible name of the navigation landmark. */
40
+ label?: string;
41
+ /** Prefix for the ids that link each toggle to its list. Defaults to "menu". */
42
+ id?: string;
33
43
  /** Custom class name. */
34
44
  className?: string;
35
45
  }
36
46
 
37
47
  /**
38
- * A nested list of rows, like a layers panel. Rows indent by depth, the
39
- * active one is highlighted, and a click reports the row's path.
48
+ * Site navigation: sections of links that open and close (an accordion, with
49
+ * a smooth animation), and the current page marked by a bar beside it.
50
+ * A section opens by itself when the current page is in it.
40
51
  */
41
- export default function Menu({
42
- activePath = [],
52
+ export default function Menu({ defaultOpen = true, label = 'Pages', ...props }: MenuProps) {
53
+ // The props Menu took before it was navigation (Tree's) still draw a Tree, with a warning.
54
+ if (warnOldMenuProps(props)) return <Tree { ...(props as any) } />;
55
+ return <MenuNav defaultOpen={ defaultOpen } label={ label } { ...props } />;
56
+ }
57
+
58
+ function MenuNav({
59
+ sections = [],
60
+ current,
61
+ defaultOpen,
62
+ onToggle,
63
+ onNavigate,
64
+ label,
65
+ id = 'menu',
43
66
  className,
44
- itemRenderer,
45
- items,
46
- maxIndentDepth = 6,
47
- onClick,
48
- children: _children,
49
- ...props
67
+ onKeyDown,
68
+ ...restProps
50
69
  }: MenuProps) {
51
- const handleClick = (event: React.MouseEvent<HTMLDivElement>, path: number[]) => {
52
- event.stopPropagation();
53
- if (onClick) onClick(event, path);
54
- };
70
+ // The current page's section starts open, whatever defaultOpen says.
71
+ const [open, setOpen] = React.useState<Record<string, boolean>>(() => {
72
+ const home = sections.find((s) => s.items.some((item) => item.href === current));
73
+ return Object.fromEntries(sections.map((s) => [sectionId(s), s === home || startsOpen(defaultOpen, sectionId(s))]));
74
+ });
55
75
 
56
- const renderItems = (list: MenuItem[] | undefined, active: number[], path: number[] = []): React.ReactNode => {
57
- if (!list) return null;
58
- return list.map((item, i) => {
59
- const { className: itemClassName, label, style } = item;
60
- const type = path.length > 1 ? 'group' : '';
61
- const isActive = active.length === 1 && active[0] === i;
62
- const [first, ...remainingPath] = active;
63
- const nextPath = [...path, i];
64
- // Clamped so a deep tree cannot indent the label out of a narrow panel.
65
- // The px maths lives in Menu.scss against spacing tokens; JS only supplies depth.
66
- const depth = Math.min(path.length, maxIndentDepth);
76
+ // Navigating into a closed section opens it, so the current page is always visible.
77
+ React.useEffect(() => {
78
+ const home = sections.find((s) => s.items.some((item) => item.href === current));
79
+ if (home && open[sectionId(home)] === false) setOpen((was) => ({ ...was, [sectionId(home)]: true }));
80
+ }, [current]); // eslint-disable-line react-hooks/exhaustive-deps
67
81
 
68
- return (
69
- <React.Fragment key={ i }>
70
- <div
71
- className={ classNames(itemClassName, 'menu-item', CONSTANT.CLASS_PREFIX) }
72
- data-active={ isActive }
73
- data-type={ type }
74
- onClick={ (e) => handleClick(e, nextPath) }
75
- style={ { '--nurvus-menu-depth': String(depth), ...style } as React.CSSProperties }
76
- >
77
- { itemRenderer ? itemRenderer(item) : label }
78
- </div>
79
- { renderItems(item.items, first === i ? remainingPath : [], nextPath) }
80
- </React.Fragment>
81
- );
82
- });
82
+ const toggle = (sid: string) => {
83
+ const next = open[sid] === false;
84
+ setOpen((was) => ({ ...was, [sid]: next }));
85
+ if (onToggle) onToggle(sid, next);
83
86
  };
87
+ const navigate = (href: string) => (event: React.MouseEvent<HTMLAnchorElement>) => { if (onNavigate) onNavigate(href, event); };
84
88
 
85
89
  return (
86
- <div className={ classNames(className, 'menu', CONSTANT.CLASS_PREFIX) } { ...props }>
87
- { renderItems(items, activePath) }
88
- </div>
90
+ <nav className={ classNames(className, 'menu', CONSTANT.CLASS_PREFIX) }
91
+ aria-label={ label }
92
+ { ...restProps }
93
+ onKeyDown={ (e) => { menuKeydown(e.nativeEvent, e.currentTarget); if (onKeyDown) onKeyDown(e); } }
94
+ >
95
+ { sections.map((section) => {
96
+ const sid = sectionId(section);
97
+ const shown = open[sid] !== false;
98
+ const listId = `${id}-${sid}`;
99
+ const toggleProps = {
100
+ type: 'button' as const,
101
+ 'aria-expanded': shown,
102
+ 'aria-controls': listId,
103
+ onClick: () => toggle(sid),
104
+ };
105
+ return (
106
+ <div key={ sid } className="menu-section" data-section={ sid } data-open={ shown }>
107
+ <div className="menu-heading">
108
+ { section.href ? (
109
+ <>
110
+ <a className="menu-title" href={ section.href } data-active={ current === section.href } onClick={ navigate(section.href) }>
111
+ { section.title }
112
+ </a>
113
+ <button className="menu-toggle" aria-label={ `${shown ? 'Hide' : 'Show'} ${String(section.title)}` } { ...toggleProps } />
114
+ </>
115
+ ) : (
116
+ <button className="menu-title menu-toggle" { ...toggleProps }>{ section.title }</button>
117
+ ) }
118
+ </div>
119
+ <div className="menu-items" id={ listId }>
120
+ <ul>
121
+ { section.items.map((item) => (
122
+ <li key={ item.href } data-divider={ item.divider || undefined }>
123
+ <a
124
+ className="menu-item"
125
+ href={ item.href }
126
+ data-active={ current === item.href }
127
+ aria-current={ current === item.href ? 'page' : undefined }
128
+ onClick={ navigate(item.href) }
129
+ >
130
+ { item.label }
131
+ </a>
132
+ </li>
133
+ )) }
134
+ </ul>
135
+ </div>
136
+ </div>
137
+ );
138
+ }) }
139
+ </nav>
89
140
  );
90
141
  }
@@ -0,0 +1,72 @@
1
+ Click it, or Tab to it and use the keys: ↓ ↑ open the list and move, Home and End jump, typing finds an option by its first letters, Enter or Space picks, Esc closes.
2
+ ```jsx
3
+ import { Layout, Select, Text } from '@nurvus/ui';
4
+
5
+ const Region = () => {
6
+ const [region, setRegion] = React.useState('eu-west');
7
+ return (
8
+ <Layout display="flex" alignItems="center" itemSpacing={ 12 }>
9
+ <Select
10
+ id="region"
11
+ label="Region"
12
+ value={ region }
13
+ onChange={ setRegion }
14
+ options={ [
15
+ { value: 'us-east', label: 'US East', description: 'Virginia' },
16
+ { value: 'us-west', label: 'US West', description: 'Oregon' },
17
+ { value: 'eu-west', label: 'EU West', description: 'Ireland' },
18
+ { value: 'ap-south', label: 'Asia Pacific', description: 'Mumbai', disabled: true },
19
+ ] }
20
+ />
21
+ <Text tone="muted">Deploying to { region }</Text>
22
+ </Layout>
23
+ );
24
+ };
25
+ <Region />
26
+ ```
27
+
28
+ Groups, and options drawn your own way: here a swatch beside each theme, in the theme's own colors. `renderValue` draws the picked one inside the button.
29
+ ```jsx
30
+ import { Select, themes } from '@nurvus/ui';
31
+
32
+ const Swatch = ({ theme }) => (
33
+ <span style={ { width: 16, height: 16, borderRadius: 4, flex: 'none', background: theme.colors.bg, border: `2px solid ${theme.colors['accent-fill']}` } } />
34
+ );
35
+
36
+ const ThemePicker = () => {
37
+ const [id, setId] = React.useState('tokyo-neon');
38
+ const byId = (value) => themes.find((t) => t.id === value);
39
+ return (
40
+ <Select
41
+ id="theme-example"
42
+ label="Theme"
43
+ value={ id }
44
+ onChange={ setId }
45
+ options={ themes.map((t) => ({ value: t.id, label: t.name, description: t.keywords, group: t.mode === 'light' ? 'Light' : 'Dark' })) }
46
+ renderValue={ (option) => <><Swatch theme={ byId(option.value) } />{ option.label }</> }
47
+ renderOption={ (option) => (
48
+ <>
49
+ <Swatch theme={ byId(option.value) } />
50
+ <span className="select-option-text">
51
+ <span>{ option.label }</span>
52
+ <span className="select-option-description">{ option.description }</span>
53
+ </span>
54
+ </>
55
+ ) }
56
+ />
57
+ );
58
+ };
59
+ <ThemePicker />
60
+ ```
61
+
62
+ Nothing picked yet: the placeholder shows until a value is chosen.
63
+ ```jsx
64
+ import { Select } from '@nurvus/ui';
65
+
66
+ <Select
67
+ id="size"
68
+ label="Size"
69
+ placeholder="Pick a size"
70
+ options={ [{ value: 's', label: 'Small' }, { value: 'm', label: 'Medium' }, { value: 'l', label: 'Large' }] }
71
+ />
72
+ ```
@@ -0,0 +1,170 @@
1
+ @import "../../styles/variables.scss";
2
+
3
+ // Pick one value: a button and the list it opens. The list stays in the page
4
+ // (the button's aria-controls points at it) and fades and slides in.
5
+ $select-speed: 0.12s;
6
+
7
+ .#{$cls-prefix}.select {
8
+ position: relative;
9
+ display: inline-block;
10
+ max-width: 100%;
11
+ font-size: 14px;
12
+ line-height: 20px;
13
+
14
+ .select-trigger {
15
+ display: inline-flex;
16
+ align-items: center;
17
+ gap: var(--space--8);
18
+ max-width: 100%;
19
+ min-height: 32px;
20
+ padding: var(--space--4) var(--space--8) var(--space--4) var(--space--12);
21
+ border: 1px solid var(--nv-border-strong);
22
+ border-radius: 8px;
23
+ background: var(--nv-surface);
24
+ color: var(--nv-text);
25
+ cursor: pointer;
26
+ user-select: none;
27
+
28
+ &:hover { background: var(--nv-surface-hover); }
29
+ &:focus-visible { outline: 2px solid var(--nv-accent); outline-offset: 2px; }
30
+ }
31
+
32
+ &[data-disabled] .select-trigger { opacity: 0.5; cursor: not-allowed; }
33
+
34
+ .select-value {
35
+ display: inline-flex;
36
+ align-items: center;
37
+ gap: var(--space--8);
38
+ min-width: 0;
39
+ overflow: hidden;
40
+ white-space: nowrap;
41
+ text-overflow: ellipsis;
42
+
43
+ &[data-placeholder] { color: var(--nv-text-muted); }
44
+ }
45
+
46
+ // The same turned-corner chevron as Menu: down when closed, up when open.
47
+ .select-chevron {
48
+ display: inline-flex;
49
+ flex: none;
50
+ width: 16px;
51
+ justify-content: center;
52
+
53
+ &::after {
54
+ content: '';
55
+ width: 6px;
56
+ height: 6px;
57
+ border-right: 1.5px solid currentColor;
58
+ border-bottom: 1.5px solid currentColor;
59
+ transform: translateY(-2px) rotate(45deg);
60
+ transition: transform $select-speed ease;
61
+ }
62
+ }
63
+
64
+ &[data-open="true"] .select-chevron::after { transform: translateY(2px) rotate(225deg); }
65
+
66
+ .select-list {
67
+ position: absolute;
68
+ z-index: 1000;
69
+ top: calc(100% + var(--space--4));
70
+ left: 0;
71
+ min-width: 100%;
72
+ max-height: 360px;
73
+ overflow: auto;
74
+ padding: var(--space--4);
75
+ border: 1px solid var(--nv-border);
76
+ border-radius: 10px;
77
+ background: var(--nv-surface);
78
+ color: var(--nv-text);
79
+ box-shadow: 0 12px 32px rgb(0 0 0 / 0.24);
80
+ opacity: 0;
81
+ visibility: hidden;
82
+ transform: translateY(-4px) scale(0.98);
83
+ transform-origin: top left;
84
+ transition: opacity $select-speed ease, transform $select-speed ease, visibility 0s $select-speed;
85
+ }
86
+
87
+ &[data-align="end"] .select-list { left: auto; right: 0; transform-origin: top right; }
88
+
89
+ &[data-open="true"] .select-list {
90
+ opacity: 1;
91
+ visibility: visible;
92
+ transform: none;
93
+ transition: opacity $select-speed ease, transform $select-speed ease, visibility 0s 0s;
94
+ }
95
+
96
+ .select-group + .select-group,
97
+ .select-option + .select-group {
98
+ margin-top: var(--space--4);
99
+ padding-top: var(--space--4);
100
+ border-top: 1px solid var(--nv-border);
101
+ }
102
+
103
+ .select-group-label {
104
+ padding: var(--space--4) var(--space--8);
105
+ color: var(--nv-text-muted);
106
+ font-size: 12px;
107
+ line-height: 16px;
108
+ font-weight: 600;
109
+ letter-spacing: 0.04em;
110
+ text-transform: uppercase;
111
+ }
112
+
113
+ .select-option {
114
+ position: relative;
115
+ display: flex;
116
+ align-items: center;
117
+ gap: var(--space--8);
118
+ padding: 6px 32px 6px var(--space--8); // room on the right for the check
119
+ border-radius: 6px;
120
+ cursor: pointer;
121
+ white-space: nowrap;
122
+
123
+ // Where the keys (or the pointer) are: a wash and an accent bar, so it
124
+ // reads as focus even though focus stays on the button.
125
+ &[data-active="true"] {
126
+ background: var(--nv-surface-hover);
127
+ box-shadow: inset 2px 0 0 var(--nv-accent);
128
+ }
129
+
130
+ // The picked value: tinted, with a check.
131
+ &[aria-selected="true"] {
132
+ background: var(--nv-accent-soft);
133
+ font-weight: 500;
134
+
135
+ &::after {
136
+ content: '';
137
+ position: absolute;
138
+ top: 50%;
139
+ right: 12px;
140
+ width: 5px;
141
+ height: 10px;
142
+ border-right: 2px solid var(--nv-accent);
143
+ border-bottom: 2px solid var(--nv-accent);
144
+ transform: translateY(-60%) rotate(45deg);
145
+ }
146
+ }
147
+
148
+ &[aria-selected="true"][data-active="true"] { background: color-mix(in srgb, var(--nv-accent) 22%, transparent); }
149
+ &[aria-disabled="true"] { opacity: 0.5; cursor: not-allowed; }
150
+ }
151
+
152
+ .select-option-text {
153
+ display: flex;
154
+ flex-direction: column;
155
+ min-width: 0;
156
+ }
157
+
158
+ .select-option-description {
159
+ color: var(--nv-text-muted);
160
+ font-size: 12px;
161
+ line-height: 16px;
162
+ font-weight: 400;
163
+ }
164
+
165
+ @media (prefers-reduced-motion: reduce) {
166
+ .select-list,
167
+ .select-chevron::after,
168
+ &[data-open="true"] .select-list { transition: none; }
169
+ }
170
+ }
@@ -0,0 +1,196 @@
1
+ import React from 'react';
2
+ import classNames from 'classnames';
3
+ import CONSTANT from '../../constants';
4
+ import { groupOptions, selectKey, matchOption, typeahead, isPrintable, SelectAction } from '../../select';
5
+ import './Select.scss';
6
+
7
+ export interface SelectOption {
8
+ /** The value onChange reports. */
9
+ value: string;
10
+ /** What the option says. */
11
+ label: React.ReactNode;
12
+ /** A second, quieter line under the label. */
13
+ description?: React.ReactNode;
14
+ /** Options with the same group are listed together, under the group as a heading. */
15
+ group?: string;
16
+ /** Shown, but can't be picked. */
17
+ disabled?: boolean;
18
+ /** The text typing matches, when the label isn't plain text. Defaults to the label. */
19
+ textValue?: string;
20
+ }
21
+
22
+ /** Where an option stands while the list is open. */
23
+ export interface SelectOptionState {
24
+ /** It is the picked value. */
25
+ selected: boolean;
26
+ /** The keys or the pointer are on it. */
27
+ active: boolean;
28
+ }
29
+
30
+ export interface SelectProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'onChange' | 'defaultValue' | 'placeholder'> {
31
+ /** The options, in order (grouped ones are listed together under their group). */
32
+ options: SelectOption[];
33
+ /** The picked value. Pass it with onChange to control the select yourself. */
34
+ value?: string;
35
+ /** The value it starts with, when you don't pass value. */
36
+ defaultValue?: string;
37
+ /** Called when an option is picked (not when the same one is picked again). */
38
+ onChange?: (value: string, option: SelectOption) => void;
39
+ /** Accessible name (screen readers say it), e.g. "Theme". */
40
+ label: string;
41
+ /** Shown while nothing is picked. */
42
+ placeholder?: React.ReactNode;
43
+ /** Draws an option's content instead of its label and description. */
44
+ renderOption?: (option: SelectOption, state: SelectOptionState) => React.ReactNode;
45
+ /** Draws the picked option inside the button instead of its label. */
46
+ renderValue?: (option: SelectOption | undefined) => React.ReactNode;
47
+ /** Which edge of the button the list lines up with. */
48
+ align?: 'start' | 'end';
49
+ /** Can't be opened. */
50
+ disabled?: boolean;
51
+ /** Prefix for the ids that tie the button to its list and options. */
52
+ id?: string;
53
+ /** Custom class name. */
54
+ className?: string;
55
+ }
56
+
57
+ /**
58
+ * Pick one value from a list: a button that opens a list of options (the
59
+ * WAI-ARIA select-only combobox). Focus stays on the button: ↓ ↑ move, Home
60
+ * and End jump, typing finds an option by its first letters, Enter or Space
61
+ * picks, Esc closes. Options can be grouped, and drawn your own way.
62
+ */
63
+ export default function Select({
64
+ options = [],
65
+ value,
66
+ defaultValue,
67
+ onChange,
68
+ label,
69
+ placeholder = 'Choose…',
70
+ renderOption,
71
+ renderValue,
72
+ align = 'start',
73
+ disabled = false,
74
+ id = 'select',
75
+ className,
76
+ ...restProps
77
+ }: SelectProps) {
78
+ const { groups, ordered } = React.useMemo(() => groupOptions(options), [options]);
79
+ const [inner, setInner] = React.useState(defaultValue);
80
+ const chosen = value !== undefined ? value : inner;
81
+ const selected = ordered.findIndex((o) => o.value === chosen);
82
+ const [open, setOpen] = React.useState(false);
83
+ const [active, setActive] = React.useState(-1);
84
+ const typed = React.useRef(typeahead());
85
+ const list = React.useRef<HTMLDivElement>(null);
86
+ const optionId = (index: number) => `${id}-option-${index}`;
87
+
88
+ // Keep the active option in view as the keys move through a long list.
89
+ React.useEffect(() => {
90
+ if (!open || active < 0 || !list.current) return;
91
+ const el = list.current.querySelector<HTMLElement>(`[data-index="${active}"]`);
92
+ if (el && el.scrollIntoView) el.scrollIntoView({ block: 'nearest' });
93
+ }, [open, active]);
94
+
95
+ const pick = (index: number) => {
96
+ const option = ordered[index];
97
+ if (!option || option.disabled) return;
98
+ if (value === undefined) setInner(option.value);
99
+ if (onChange && option.value !== chosen) onChange(option.value, option);
100
+ };
101
+
102
+ const apply = (action: SelectAction) => {
103
+ if (action.type === 'open') { setOpen(true); setActive(action.active); }
104
+ if (action.type === 'move') setActive(action.active);
105
+ if (action.type === 'pick') { pick(action.active); setOpen(false); }
106
+ if (action.type === 'close') setOpen(false);
107
+ };
108
+
109
+ const handleKeyDown = (event: React.KeyboardEvent<HTMLDivElement>) => {
110
+ if (disabled) return;
111
+ const typing = isPrintable(event) && (event.key !== ' ' || typed.current.pending());
112
+ const action = typing ? null : selectKey(event.key, event.altKey, { open, active, selected }, ordered);
113
+ if (action) {
114
+ if (event.key !== 'Tab') event.preventDefault();
115
+ apply(action);
116
+ return;
117
+ }
118
+ if (typing) {
119
+ event.preventDefault();
120
+ const found = matchOption(ordered, typed.current.add(event.key), open ? active : selected);
121
+ if (found >= 0) { setOpen(true); setActive(found); }
122
+ }
123
+ };
124
+
125
+ const toggle = () => {
126
+ if (disabled) return;
127
+ if (open) setOpen(false);
128
+ else apply(selectKey('Enter', false, { open: false, active, selected }, ordered) as SelectAction);
129
+ };
130
+
131
+ const chosenOption = selected >= 0 ? ordered[selected] : undefined;
132
+ const drawOption = (option: SelectOption, index: number) => {
133
+ const state = { selected: index === selected, active: open && index === active };
134
+ return (
135
+ <div
136
+ key={ `${option.value}-${index}` }
137
+ className="select-option"
138
+ role="option"
139
+ id={ optionId(index) }
140
+ data-index={ index }
141
+ aria-selected={ state.selected }
142
+ aria-disabled={ option.disabled || undefined }
143
+ data-active={ state.active }
144
+ onMouseMove={ () => { if (!option.disabled && active !== index) setActive(index); } }
145
+ onClick={ () => { if (!option.disabled) apply({ type: 'pick', active: index }); } }
146
+ >
147
+ { renderOption ? renderOption(option, state) : (
148
+ <span className="select-option-text">
149
+ <span className="select-option-label">{ option.label }</span>
150
+ { option.description !== undefined && <span className="select-option-description">{ option.description }</span> }
151
+ </span>
152
+ ) }
153
+ </div>
154
+ );
155
+ };
156
+
157
+ return (
158
+ <div
159
+ className={ classNames(className, 'select', CONSTANT.CLASS_PREFIX) }
160
+ data-open={ open }
161
+ data-align={ align }
162
+ data-disabled={ disabled || undefined }
163
+ { ...restProps }
164
+ >
165
+ <div
166
+ className="select-trigger"
167
+ role="combobox"
168
+ id={ id }
169
+ tabIndex={ disabled ? -1 : 0 }
170
+ aria-label={ label }
171
+ aria-haspopup="listbox"
172
+ aria-expanded={ open }
173
+ aria-controls={ `${id}-list` }
174
+ aria-activedescendant={ open && active >= 0 ? optionId(active) : undefined }
175
+ aria-disabled={ disabled || undefined }
176
+ onClick={ toggle }
177
+ onKeyDown={ handleKeyDown }
178
+ onBlur={ () => setOpen(false) }
179
+ >
180
+ <span className="select-value" data-placeholder={ chosenOption ? undefined : true }>
181
+ { chosenOption ? (renderValue ? renderValue(chosenOption) : chosenOption.label) : (renderValue ? renderValue(undefined) : placeholder) }
182
+ </span>
183
+ <span className="select-chevron" aria-hidden="true" />
184
+ </div>
185
+ { /* mousedown would move focus off the button and close the list before the click lands */ }
186
+ <div className="select-list" role="listbox" id={ `${id}-list` } aria-label={ label } ref={ list } onMouseDown={ (e) => e.preventDefault() }>
187
+ { groups.map((group, g) => (group.name ? (
188
+ <div key={ group.name } className="select-group" role="group" aria-labelledby={ `${id}-group-${g}` }>
189
+ <div className="select-group-label" id={ `${id}-group-${g}` } role="presentation">{ group.name }</div>
190
+ { group.options.map(({ option, index }) => drawOption(option, index)) }
191
+ </div>
192
+ ) : group.options.map(({ option, index }) => drawOption(option, index)))) }
193
+ </div>
194
+ </div>
195
+ );
196
+ }
@@ -183,10 +183,11 @@ const columns = [
183
183
  textAlign: 'right'
184
184
  },
185
185
  render: (val, key, props) => {
186
- let color = 'rgba(4, 170, 53, 1)';
186
+ // Theme roles, so the colors pass contrast in every theme.
187
+ let color = 'var(--nv-success)';
187
188
 
188
189
  if (val < 0) {
189
- color = 'rgba(239, 2, 9, 1)';
190
+ color = 'var(--nv-danger)';
190
191
  }
191
192
 
192
193
  return (
@@ -9,7 +9,9 @@
9
9
  border: 1px solid color-mix(in srgb, var(--tag-color) 45%, transparent);
10
10
  border-radius: $--radius--full;
11
11
  background: color-mix(in srgb, var(--tag-color) 10%, transparent);
12
- color: var(--tag-color);
12
+ // The tone mixed with a fifth of the text color: on its own tint (and on a
13
+ // Panel's surface) every tone keeps 4.5:1 in every theme.
14
+ color: color-mix(in srgb, var(--tag-color) 80%, var(--nv-text));
13
15
  font-size: 12px;
14
16
  font-weight: 500;
15
17
  line-height: 18px;