@nurvus/ui 0.1.0-alpha.12 → 0.1.0-alpha.13

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.
@@ -0,0 +1,39 @@
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
+ ```
@@ -0,0 +1,149 @@
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
+ }
@@ -0,0 +1,129 @@
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/css.ts CHANGED
@@ -8,6 +8,7 @@ import './components/HistoryList/HistoryList.scss';
8
8
  import './components/Layout/Layout.scss';
9
9
  import './components/Link/Link.scss';
10
10
  import './components/Menu/Menu.scss';
11
+ import './components/NavList/NavList.scss';
11
12
  import './components/Panel/Panel.scss';
12
13
  import './components/Progress/Progress.scss';
13
14
  import './components/Table/Table.scss';
package/src/index.ts CHANGED
@@ -24,6 +24,8 @@ export { default as Layout } from './components/Layout/Layout';
24
24
  export type { LayoutProps, Spacing, LegacySpacing } from './components/Layout/Layout';
25
25
  export { default as Menu } from './components/Menu/Menu';
26
26
  export type { MenuProps, MenuItem } from './components/Menu/Menu';
27
+ export { default as NavList } from './components/NavList/NavList';
28
+ export type { NavListProps, NavListItem, NavListSection } from './components/NavList/NavList';
27
29
  export { default as FullPageModal } from './components/FullPageModal/FullPageModal';
28
30
  export type { FullPageModalProps } from './components/FullPageModal/FullPageModal';
29
31
  export { default as HistoryList } from './components/HistoryList/HistoryList';
package/src/navList.ts ADDED
@@ -0,0 +1,9 @@
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;
@@ -0,0 +1,126 @@
1
+ import classNames from 'classnames';
2
+ import CONSTANT from '../constants';
3
+ import { sectionId, startsOpen } from '../navList';
4
+ import { spawn, Child, ElementProps } from './spawn';
5
+ import { updatable, NurvusElement } from './update';
6
+ import '../components/NavList/NavList.scss';
7
+
8
+ export interface NavListItem {
9
+ /** What the link says. */
10
+ label: Child;
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 NavListSection {
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: string;
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: NavListItem[];
26
+ }
27
+
28
+ export interface NavListProps extends ElementProps {
29
+ /** The sections, in order. */
30
+ sections: NavListSection[];
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: MouseEvent) => 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 "nav-list". */
42
+ id?: string;
43
+ }
44
+
45
+ /**
46
+ * Site navigation: sections of links that open and close (an accordion, with
47
+ * a smooth animation), and the current page marked by a bar beside it. A
48
+ * click on a toggle opens or closes its section in place (the element stays
49
+ * the same); update({ current }) marks another page and opens its section.
50
+ */
51
+ export default function NavList(props: NavListProps): NurvusElement<HTMLElement, NavListProps> {
52
+ return updatable(({
53
+ sections, current, defaultOpen = true, onToggle, onNavigate, label = 'Pages', id = 'nav-list', className, ...rest
54
+ }: NavListProps, keep) => {
55
+ const open: Record<string, boolean> = Object.fromEntries(sections.map((s) => [sectionId(s), startsOpen(defaultOpen, sectionId(s))]));
56
+ // The current page's section always starts open, as in React.
57
+ const home = sections.find((s) => s.items.some((item) => item.href === current));
58
+ if (home) open[sectionId(home)] = true;
59
+
60
+ const navigate = (href: string) => ({ click: (event: MouseEvent) => { if (onNavigate) onNavigate(href, event); } });
61
+
62
+ const renderSection = (section: NavListSection): HTMLElement => {
63
+ const sid = sectionId(section);
64
+ const listId = `${id}-${sid}`;
65
+ const toggles: HTMLElement[] = [];
66
+ const toggle = () => {
67
+ open[sid] = !open[sid];
68
+ keep({ defaultOpen: { ...open } });
69
+ el.setAttribute('data-open', String(open[sid]));
70
+ toggles.forEach((t) => {
71
+ t.setAttribute('aria-expanded', String(open[sid]));
72
+ if (t.hasAttribute('aria-label')) t.setAttribute('aria-label', `${open[sid] ? 'Hide' : 'Show'} ${section.title}`);
73
+ });
74
+ if (onToggle) onToggle(sid, open[sid]);
75
+ };
76
+ const toggleProps = (extra: Record<string, unknown>) => ({
77
+ tag: 'button', type: 'button', 'aria-expanded': open[sid], 'aria-controls': listId, events: { click: toggle }, ...extra,
78
+ });
79
+ const heading = section.href
80
+ ? [
81
+ spawn({ tag: 'a', className: 'nav-list-title', href: section.href, 'data-active': current === section.href, events: navigate(section.href), children: section.title }),
82
+ spawn(toggleProps({ className: 'nav-list-toggle', 'aria-label': `${open[sid] ? 'Hide' : 'Show'} ${section.title}` })),
83
+ ]
84
+ : [spawn(toggleProps({ className: 'nav-list-title nav-list-toggle', children: section.title }))];
85
+ heading.forEach((h) => { if (h.tagName === 'BUTTON') toggles.push(h); });
86
+
87
+ const el: HTMLElement = spawn({
88
+ className: 'nav-list-section',
89
+ 'data-section': sid,
90
+ 'data-open': open[sid],
91
+ children: [
92
+ spawn({ className: 'nav-list-heading', children: heading }),
93
+ spawn({
94
+ className: 'nav-list-items',
95
+ id: listId,
96
+ children: spawn({
97
+ tag: 'ul',
98
+ children: section.items.map((item) => spawn({
99
+ tag: 'li',
100
+ 'data-divider': item.divider || undefined,
101
+ children: spawn({
102
+ tag: 'a',
103
+ className: 'nav-list-item',
104
+ href: item.href,
105
+ 'data-active': current === item.href,
106
+ 'aria-current': current === item.href ? 'page' : undefined,
107
+ events: navigate(item.href),
108
+ children: item.label,
109
+ }),
110
+ })),
111
+ }),
112
+ }),
113
+ ],
114
+ });
115
+ return el;
116
+ };
117
+
118
+ return spawn<HTMLElement>({
119
+ tag: 'nav',
120
+ className: classNames(className, 'nav-list', CONSTANT.CLASS_PREFIX),
121
+ 'aria-label': label,
122
+ ...rest,
123
+ children: sections.map(renderSection),
124
+ });
125
+ }, props);
126
+ }
@@ -41,6 +41,8 @@ export { default as Layout } from './Layout';
41
41
  export type { LayoutProps, Spacing, LegacySpacing } from './Layout';
42
42
  export { default as Menu } from './Menu';
43
43
  export type { MenuProps, MenuItem } from './Menu';
44
+ export { default as NavList } from './NavList';
45
+ export type { NavListProps, NavListItem, NavListSection } from './NavList';
44
46
  export { default as FullPageModal } from './FullPageModal';
45
47
  export type { FullPageModalProps } from './FullPageModal';
46
48
  export { default as HistoryList } from './HistoryList';