@nurvus/ui 0.1.0-alpha.12 → 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 -4
- 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 -1
- 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 -1
- 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 -0
- package/src/index.ts +5 -1
- 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 -1
- 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
|
@@ -1,68 +1,150 @@
|
|
|
1
1
|
@import "../../styles/variables.scss";
|
|
2
|
-
|
|
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
|
+
$menu-speed: 0.2s;
|
|
3
6
|
|
|
4
7
|
.#{$cls-prefix}.menu {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
// Rows are FLAT siblings in the DOM (see Menu.jsx) — nesting is purely visual,
|
|
8
|
-
// produced by padding-left. Menu.jsx stamps `--nurvus-menu-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.menu.
|
|
12
|
-
// ---------------------------------------------------------------------------
|
|
13
|
-
--nurvus-menu-indent-base: #{$--space--8};
|
|
14
|
-
--nurvus-menu-indent-step: #{$--space--12};
|
|
15
|
-
|
|
16
|
-
background: var(--nv-bg);
|
|
17
|
-
color: var(--nv-text);
|
|
18
|
-
padding: $--space--4;
|
|
19
|
-
|
|
20
|
-
// Allow the menu to shrink inside a flex/grid parent instead of forcing its
|
|
21
|
-
// intrinsic width and pushing the row content out.
|
|
22
|
-
min-width: 0;
|
|
8
|
+
font-size: 14px;
|
|
9
|
+
line-height: 20px;
|
|
23
10
|
|
|
24
|
-
.menu-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
11
|
+
.menu-section + .menu-section { margin-top: var(--space--8); }
|
|
12
|
+
|
|
13
|
+
.menu-heading {
|
|
14
|
+
display: flex;
|
|
15
|
+
align-items: center;
|
|
16
|
+
gap: var(--space--4);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
.menu-title {
|
|
20
|
+
flex: 1;
|
|
21
|
+
min-width: 0;
|
|
22
|
+
padding: var(--space--4) 10px;
|
|
23
|
+
border: 0;
|
|
24
|
+
border-radius: 6px;
|
|
25
|
+
background: none;
|
|
33
26
|
color: var(--nv-text);
|
|
27
|
+
font: inherit;
|
|
28
|
+
font-weight: 600;
|
|
29
|
+
text-align: left;
|
|
30
|
+
text-decoration: none;
|
|
34
31
|
cursor: pointer;
|
|
35
32
|
|
|
36
|
-
&:hover {
|
|
37
|
-
|
|
38
|
-
|
|
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. Down when the section is closed (there is
|
|
39
|
+
// more below), up when it is open; it turns half a circle between the two.
|
|
40
|
+
.menu-toggle {
|
|
41
|
+
display: inline-flex;
|
|
42
|
+
align-items: center;
|
|
43
|
+
justify-content: center;
|
|
44
|
+
|
|
45
|
+
&::after {
|
|
46
|
+
content: '';
|
|
47
|
+
width: 6px;
|
|
48
|
+
height: 6px;
|
|
49
|
+
margin: 0 var(--space--4);
|
|
50
|
+
border-right: 1.5px solid currentColor;
|
|
51
|
+
border-bottom: 1.5px solid currentColor;
|
|
52
|
+
transform: translateY(2px) rotate(225deg);
|
|
53
|
+
transition: transform $menu-speed ease;
|
|
39
54
|
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// The icon-only toggle beside a heading that is a link.
|
|
58
|
+
.menu-toggle:not(.menu-title) {
|
|
59
|
+
flex: none;
|
|
60
|
+
width: 24px;
|
|
61
|
+
height: 24px;
|
|
62
|
+
padding: 0;
|
|
63
|
+
border: 0;
|
|
64
|
+
border-radius: 6px;
|
|
65
|
+
background: none;
|
|
66
|
+
color: var(--nv-text-muted);
|
|
67
|
+
cursor: pointer;
|
|
68
|
+
|
|
69
|
+
&::after { margin: 0; }
|
|
70
|
+
&:hover { background: var(--nv-surface-hover); color: var(--nv-text); }
|
|
71
|
+
&:focus-visible { outline: 2px solid var(--nv-accent); outline-offset: -2px; }
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// A heading with no page of its own: the whole row is the toggle.
|
|
75
|
+
.menu-title.menu-toggle { justify-content: space-between; }
|
|
76
|
+
|
|
77
|
+
.menu-section[data-open="false"] .menu-toggle::after { transform: translateY(-2px) rotate(45deg); }
|
|
78
|
+
|
|
79
|
+
.menu-items {
|
|
80
|
+
display: grid;
|
|
81
|
+
grid-template-rows: 1fr;
|
|
82
|
+
transition: grid-template-rows $menu-speed ease;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
.menu-section[data-open="false"] .menu-items { grid-template-rows: 0fr; }
|
|
86
|
+
|
|
87
|
+
ul {
|
|
88
|
+
min-height: 0;
|
|
89
|
+
overflow: hidden;
|
|
90
|
+
// Room on the left for the current-page bar, which sits outside the item.
|
|
91
|
+
margin: 0 0 0 -12px;
|
|
92
|
+
padding: 0 0 0 12px; // no vertical padding: it would stay when the row closes to 0
|
|
93
|
+
list-style: none;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
li:first-child { margin-top: 2px; }
|
|
97
|
+
li:last-child { margin-bottom: var(--space--8); }
|
|
98
|
+
|
|
99
|
+
// Closed: out of sight and out of the tab order once the animation ends.
|
|
100
|
+
.menu-section[data-open="false"] ul {
|
|
101
|
+
visibility: hidden;
|
|
102
|
+
transition: visibility 0s $menu-speed;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
li + li { margin-top: 2px; }
|
|
106
|
+
|
|
107
|
+
li[data-divider] {
|
|
108
|
+
margin-top: var(--space--8);
|
|
109
|
+
padding-top: var(--space--8);
|
|
110
|
+
border-top: 1px solid var(--nv-border);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
.menu-item {
|
|
114
|
+
position: relative;
|
|
115
|
+
display: block;
|
|
116
|
+
padding: var(--space--4) 10px var(--space--4) 20px;
|
|
117
|
+
border-radius: 6px;
|
|
118
|
+
color: var(--nv-text-muted);
|
|
119
|
+
text-decoration: none;
|
|
120
|
+
|
|
121
|
+
&:hover { background: var(--nv-surface-hover); color: var(--nv-text); }
|
|
122
|
+
&:focus-visible { outline: 2px solid var(--nv-accent); outline-offset: -2px; }
|
|
40
123
|
|
|
41
|
-
// Active row: 2px accent rail + a tinted wash, both from the theme's
|
|
42
|
-
// accent, so every theme keeps the same "you are here" language.
|
|
43
124
|
&[data-active="true"] {
|
|
44
125
|
background: var(--nv-accent-soft);
|
|
45
126
|
color: var(--nv-text);
|
|
46
|
-
|
|
47
|
-
&::before {
|
|
48
|
-
content: '';
|
|
49
|
-
position: absolute;
|
|
50
|
-
left: 0;
|
|
51
|
-
top: 0;
|
|
52
|
-
height: 100%;
|
|
53
|
-
width: $--space--2;
|
|
54
|
-
border-radius: $--radius--2;
|
|
55
|
-
background: var(--nv-accent);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
&:hover {
|
|
59
|
-
background: color-mix(in srgb, var(--nv-accent) 22%, transparent);
|
|
60
|
-
}
|
|
127
|
+
font-weight: 500;
|
|
61
128
|
}
|
|
62
129
|
|
|
63
|
-
//
|
|
64
|
-
|
|
65
|
-
|
|
130
|
+
// The current page's marker: its own small rounded bar beside the item
|
|
131
|
+
// (as GitHub Primer's NavList draws it), not a border on the rounded item.
|
|
132
|
+
&[data-active="true"]::before {
|
|
133
|
+
content: '';
|
|
134
|
+
position: absolute;
|
|
135
|
+
top: 50%;
|
|
136
|
+
left: -8px;
|
|
137
|
+
width: 4px;
|
|
138
|
+
height: 18px;
|
|
139
|
+
border-radius: 2px;
|
|
140
|
+
background: var(--nv-accent);
|
|
141
|
+
transform: translateY(-50%);
|
|
66
142
|
}
|
|
67
143
|
}
|
|
144
|
+
|
|
145
|
+
@media (prefers-reduced-motion: reduce) {
|
|
146
|
+
.menu-items,
|
|
147
|
+
.menu-toggle::after,
|
|
148
|
+
.menu-section[data-open="false"] ul { transition: none; }
|
|
149
|
+
}
|
|
68
150
|
}
|
|
@@ -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
|
|
8
|
-
label
|
|
9
|
-
/**
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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<
|
|
20
|
-
/** The
|
|
21
|
-
|
|
22
|
-
/** The
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
|
|
26
|
-
/** Called
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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
|
-
*
|
|
39
|
-
*
|
|
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
|
-
|
|
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
|
-
|
|
45
|
-
|
|
46
|
-
maxIndentDepth = 6,
|
|
47
|
-
onClick,
|
|
48
|
-
children: _children,
|
|
49
|
-
...props
|
|
67
|
+
onKeyDown,
|
|
68
|
+
...restProps
|
|
50
69
|
}: MenuProps) {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
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
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
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
|
-
<
|
|
87
|
-
{
|
|
88
|
-
|
|
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
|
+
}
|