@nurvus/ui 0.1.0-alpha.11 → 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.
- package/README.md +20 -9
- package/dist/index.js +1 -1
- package/dist/nurvus.css +15 -0
- package/dist/respawn.js +2 -0
- package/dist/respawn.js.LICENSE.txt +5 -0
- package/dist/theme.js +1 -0
- package/dist/themes/all.css +231 -0
- package/dist/themes/apollo-white.css +33 -0
- package/dist/themes/apollo-white.json +52 -0
- package/dist/themes/arcane.css +33 -0
- package/dist/themes/arcane.json +52 -0
- package/dist/themes/dopamine.css +33 -0
- package/dist/themes/dopamine.json +52 -0
- package/dist/themes/nether-neon.css +33 -0
- package/dist/themes/nether-neon.json +52 -0
- package/dist/themes/nether.css +33 -0
- package/dist/themes/nether.json +52 -0
- package/dist/themes/outworld.css +33 -0
- package/dist/themes/outworld.json +52 -0
- package/dist/themes/roles.json +29 -0
- package/dist/themes/tokyo-neon.css +33 -0
- package/dist/themes/tokyo-neon.json +52 -0
- package/dist/types/components/Button/Button.d.ts +19 -0
- package/dist/types/components/ButtonGroup/ButtonGroup.d.ts +12 -0
- package/dist/types/components/FullPageModal/FullPageModal.d.ts +15 -0
- package/dist/types/components/HistoryList/HistoryList.d.ts +40 -0
- package/dist/types/components/Layout/Layout.d.ts +27 -0
- package/dist/types/components/Link/Link.d.ts +9 -0
- package/dist/types/components/Menu/Menu.d.ts +35 -0
- package/dist/types/components/NavList/NavList.d.ts +43 -0
- package/dist/types/components/Panel/Panel.d.ts +19 -0
- package/dist/types/components/Progress/Progress.d.ts +15 -0
- package/dist/types/components/Table/Table.d.ts +25 -0
- package/dist/types/components/Tag/Tag.d.ts +15 -0
- package/dist/types/components/Text/Text.d.ts +28 -0
- package/dist/types/components/Title/Title.d.ts +288 -0
- package/dist/types/constants.d.ts +5 -0
- package/dist/types/index.d.ts +32 -0
- package/dist/types/navList.d.ts +7 -0
- package/dist/types/respawn/Button.d.ts +16 -0
- package/dist/types/respawn/ButtonGroup.d.ts +8 -0
- package/dist/types/respawn/FullPageModal.d.ts +32 -0
- package/dist/types/respawn/HistoryList.d.ts +36 -0
- package/dist/types/respawn/Layout.d.ts +22 -0
- package/dist/types/respawn/Link.d.ts +12 -0
- package/dist/types/respawn/Menu.d.ts +33 -0
- package/dist/types/respawn/NavList.d.ts +43 -0
- package/dist/types/respawn/Panel.d.ts +15 -0
- package/dist/types/respawn/Progress.d.ts +15 -0
- package/dist/types/respawn/Table.d.ts +22 -0
- package/dist/types/respawn/Tag.d.ts +11 -0
- package/dist/types/respawn/Text.d.ts +19 -0
- package/dist/types/respawn/Title.d.ts +14 -0
- package/dist/types/respawn/index.d.ts +35 -0
- package/dist/types/respawn/spawn.d.ts +50 -0
- package/dist/types/respawn/update.d.ts +13 -0
- package/dist/types/spacing.d.ts +8 -0
- package/dist/types/theme.d.ts +32 -0
- package/dist/types/themes/index.d.ts +45 -0
- package/package.json +40 -12
- package/src/components/Button/Button.md +5 -5
- package/src/components/Button/Button.tsx +44 -0
- package/src/components/ButtonGroup/ButtonGroup.tsx +23 -0
- package/src/components/FullPageModal/FullPageModal.scss +9 -5
- package/src/components/FullPageModal/FullPageModal.tsx +74 -0
- package/src/components/HistoryList/{HistoryList.jsx → HistoryList.tsx} +45 -58
- package/src/components/Layout/Layout.md +4 -4
- package/src/components/Layout/Layout.scss +14 -47
- package/src/components/Layout/Layout.tsx +56 -0
- package/src/components/Link/Link.scss +9 -9
- package/src/components/Link/Link.tsx +23 -0
- package/src/components/Menu/Menu.tsx +90 -0
- package/src/components/NavList/NavList.md +39 -0
- package/src/components/NavList/NavList.scss +149 -0
- package/src/components/NavList/NavList.tsx +129 -0
- package/src/components/Panel/{Panel.jsx → Panel.tsx} +17 -26
- package/src/components/Progress/Progress.md +5 -5
- package/src/components/Progress/Progress.tsx +36 -0
- package/src/components/Table/Table.tsx +59 -0
- package/src/components/Tag/{Tag.jsx → Tag.tsx} +13 -18
- package/src/components/Text/Text.md +13 -0
- package/src/components/Text/Text.scss +6 -0
- package/src/components/Text/Text.tsx +54 -0
- package/src/components/Title/Title.tsx +42 -0
- package/src/constants.ts +6 -0
- package/src/css.ts +17 -0
- package/src/global.d.ts +3 -0
- package/src/index.ts +42 -0
- package/src/navList.ts +9 -0
- package/src/respawn/Button.ts +31 -0
- package/src/respawn/ButtonGroup.ts +19 -0
- package/src/respawn/FullPageModal.ts +125 -0
- package/src/respawn/HistoryList.ts +100 -0
- package/src/respawn/Layout.ts +44 -0
- package/src/respawn/Link.ts +24 -0
- package/src/respawn/Menu.ts +86 -0
- package/src/respawn/NavList.ts +126 -0
- package/src/respawn/Panel.ts +36 -0
- package/src/respawn/Progress.ts +29 -0
- package/src/respawn/Table.ts +54 -0
- package/src/respawn/Tag.ts +25 -0
- package/src/respawn/Text.ts +35 -0
- package/src/respawn/Title.ts +30 -0
- package/src/respawn/index.ts +59 -0
- package/src/respawn/spawn.ts +155 -0
- package/src/respawn/update.ts +49 -0
- package/src/spacing.ts +23 -0
- package/src/styles/theme.scss +6 -197
- package/src/styles/type.json +35 -5
- package/src/theme.ts +92 -0
- package/src/themes/css/all.css +231 -0
- package/src/themes/css/apollo-white.css +33 -0
- package/src/themes/css/arcane.css +33 -0
- package/src/themes/css/dopamine.css +33 -0
- package/src/themes/css/nether-neon.css +33 -0
- package/src/themes/css/nether.css +33 -0
- package/src/themes/css/outworld.css +33 -0
- package/src/themes/css/tokyo-neon.css +33 -0
- package/src/themes/index.ts +61 -0
- package/src/themes/roles.json +1 -1
- package/src/app.js +0 -29
- package/src/components/Button/Button.jsx +0 -59
- package/src/components/ButtonGroup/ButtonGroup.jsx +0 -28
- package/src/components/FullPageModal/FullPageModal.jsx +0 -106
- package/src/components/Layout/Layout.jsx +0 -68
- package/src/components/Link/Link.jsx +0 -39
- package/src/components/Menu/Menu.jsx +0 -120
- package/src/components/Progress/Progress.jsx +0 -56
- package/src/components/Table/Table.jsx +0 -94
- package/src/components/Text/Text.jsx +0 -54
- package/src/components/Title/Title.jsx +0 -48
- package/src/constants.js +0 -3
- package/src/index.js +0 -20
- package/src/theme.js +0 -64
- package/src/themes/index.js +0 -22
package/src/index.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// Core CSS: the primitives, the type roles and the default theme (Apollo
|
|
2
|
+
// White), injected once. Other themes are opt-in files:
|
|
3
|
+
// import '@nurvus/ui/themes/tokyo-neon.css'; (or themes/all.css)
|
|
4
|
+
import './styles/theme.scss';
|
|
5
|
+
|
|
6
|
+
export {
|
|
7
|
+
themes, DEFAULT_THEME, THEME_STORAGE_KEY, THEME_CHANGE_EVENT,
|
|
8
|
+
getTheme, currentTheme, setTheme, initTheme,
|
|
9
|
+
} from './theme';
|
|
10
|
+
export type { SetThemeOptions } from './theme';
|
|
11
|
+
export { roles as themeRoles } from './themes';
|
|
12
|
+
export type { Theme, ThemeRole, ThemeRoleInfo } from './themes';
|
|
13
|
+
|
|
14
|
+
// Components, and the types of their props.
|
|
15
|
+
export { default as Button } from './components/Button/Button';
|
|
16
|
+
export type { ButtonProps, ButtonSize, ButtonType } from './components/Button/Button';
|
|
17
|
+
export { default as ButtonGroup } from './components/ButtonGroup/ButtonGroup';
|
|
18
|
+
export type { ButtonGroupProps } from './components/ButtonGroup/ButtonGroup';
|
|
19
|
+
export { default as Link } from './components/Link/Link';
|
|
20
|
+
export type { LinkProps } from './components/Link/Link';
|
|
21
|
+
export { default as Progress } from './components/Progress/Progress';
|
|
22
|
+
export type { ProgressProps, ProgressSize, ProgressStatus } from './components/Progress/Progress';
|
|
23
|
+
export { default as Layout } from './components/Layout/Layout';
|
|
24
|
+
export type { LayoutProps, Spacing, LegacySpacing } from './components/Layout/Layout';
|
|
25
|
+
export { default as Menu } from './components/Menu/Menu';
|
|
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';
|
|
29
|
+
export { default as FullPageModal } from './components/FullPageModal/FullPageModal';
|
|
30
|
+
export type { FullPageModalProps } from './components/FullPageModal/FullPageModal';
|
|
31
|
+
export { default as HistoryList } from './components/HistoryList/HistoryList';
|
|
32
|
+
export type { HistoryListProps, HistoryStep } from './components/HistoryList/HistoryList';
|
|
33
|
+
export { default as Panel } from './components/Panel/Panel';
|
|
34
|
+
export type { PanelProps, PanelPadding } from './components/Panel/Panel';
|
|
35
|
+
export { default as Table } from './components/Table/Table';
|
|
36
|
+
export type { TableProps, TableColumn, TableRow } from './components/Table/Table';
|
|
37
|
+
export { default as Tag } from './components/Tag/Tag';
|
|
38
|
+
export type { TagProps, TagTone } from './components/Tag/Tag';
|
|
39
|
+
export { default as Text } from './components/Text/Text';
|
|
40
|
+
export type { TextProps, TextVariant, TextTone, TextWeight } from './components/Text/Text';
|
|
41
|
+
export { default as Title } from './components/Title/Title';
|
|
42
|
+
export type { TitleProps, TitleLevel, TitleType } from './components/Title/Title';
|
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,31 @@
|
|
|
1
|
+
import classNames from 'classnames';
|
|
2
|
+
import CONSTANT from '../constants';
|
|
3
|
+
import { spawn, Child, ElementProps, Listener } from './spawn';
|
|
4
|
+
import { updatable, NurvusElement } from './update';
|
|
5
|
+
import '../components/Button/Button.scss';
|
|
6
|
+
|
|
7
|
+
export type ButtonSize = 'extra-large' | 'large' | 'medium' | 'small' | 'mini';
|
|
8
|
+
export type ButtonType = 'primary' | 'secondary';
|
|
9
|
+
|
|
10
|
+
export interface ButtonProps extends ElementProps {
|
|
11
|
+
/** Size of the button. */
|
|
12
|
+
size?: ButtonSize;
|
|
13
|
+
/** Look: primary (filled with the accent) or secondary (quiet). Not the HTML type attribute. */
|
|
14
|
+
type?: ButtonType;
|
|
15
|
+
/** Called on a click with the event and the button. */
|
|
16
|
+
onClick?: Listener<MouseEvent>;
|
|
17
|
+
/** Content to render inside the button. */
|
|
18
|
+
children?: Child;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** A `<button>`. Any other prop (onClick, disabled, aria-*) goes on it. Same markup as the React Button. */
|
|
22
|
+
export default function Button(props: ButtonProps = {}): NurvusElement<HTMLButtonElement, ButtonProps> {
|
|
23
|
+
return updatable(({ className, children, size = 'medium', type = 'primary', ...rest }: ButtonProps) => spawn<HTMLButtonElement>({
|
|
24
|
+
className: classNames(className, 'button', CONSTANT.CLASS_PREFIX),
|
|
25
|
+
'data-size': size,
|
|
26
|
+
'data-type': type,
|
|
27
|
+
...rest,
|
|
28
|
+
tag: 'button',
|
|
29
|
+
children,
|
|
30
|
+
}), props);
|
|
31
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import classNames from 'classnames';
|
|
2
|
+
import CONSTANT from '../constants';
|
|
3
|
+
import { spawn, Child, ElementProps } from './spawn';
|
|
4
|
+
import { updatable, NurvusElement } from './update';
|
|
5
|
+
import '../components/ButtonGroup/ButtonGroup.scss';
|
|
6
|
+
|
|
7
|
+
export interface ButtonGroupProps extends ElementProps {
|
|
8
|
+
/** The Buttons to join. */
|
|
9
|
+
children?: Child;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** Joins Buttons into one row: square corners where they meet, rounded at the two outer ends. */
|
|
13
|
+
export default function ButtonGroup(props: ButtonGroupProps = {}): NurvusElement<HTMLDivElement, ButtonGroupProps> {
|
|
14
|
+
return updatable(({ className, ...rest }: ButtonGroupProps) => spawn<HTMLDivElement>({
|
|
15
|
+
className: classNames(className, 'button-group', CONSTANT.CLASS_PREFIX),
|
|
16
|
+
...rest,
|
|
17
|
+
tag: 'div',
|
|
18
|
+
}), props);
|
|
19
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import classNames from 'classnames';
|
|
2
|
+
import CONSTANT from '../constants';
|
|
3
|
+
import { spawn, Child, ElementProps } from './spawn';
|
|
4
|
+
import { NurvusElement } from './update';
|
|
5
|
+
import Layout from './Layout';
|
|
6
|
+
import Title from './Title';
|
|
7
|
+
import '../components/FullPageModal/FullPageModal.scss';
|
|
8
|
+
|
|
9
|
+
export interface FullPageModalProps extends ElementProps {
|
|
10
|
+
/** Shows the modal (animates in), or hides it (animates out). */
|
|
11
|
+
visible?: boolean;
|
|
12
|
+
/** Title in the modal's header. */
|
|
13
|
+
title?: Child;
|
|
14
|
+
/** Buttons at the right of the header, e.g. Close. */
|
|
15
|
+
headerActions?: Child;
|
|
16
|
+
/** The modal's content. */
|
|
17
|
+
children?: Child;
|
|
18
|
+
/** Appends the modal to this element when it is made, e.g. document.body. */
|
|
19
|
+
mountEl?: Element;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Shows content over the whole screen. Returns the modal's outer element
|
|
24
|
+
* (`<div class="portal nurvus">`, the one React puts into `<body>` itself):
|
|
25
|
+
* mount it on document.body, or pass `mountEl: document.body`.
|
|
26
|
+
*
|
|
27
|
+
* const modal = FullPageModal({ title: 'Settings', mountEl: document.body, children: … });
|
|
28
|
+
* modal.update({ visible: true }); // animates in
|
|
29
|
+
* modal.update({ visible: false }); // animates out, then hides
|
|
30
|
+
*
|
|
31
|
+
* Unlike the other components, update() changes this element in place and
|
|
32
|
+
* returns the same one, so the in and out animations can run on it. The
|
|
33
|
+
* attributes go through the same steps as the React FullPageModal:
|
|
34
|
+
* closed portal data-visible="false", modal data-visible="false" (display: none), no mask
|
|
35
|
+
* opening portal "true", a mask, modal data-visible="true" data-animate="reveal-in"
|
|
36
|
+
* closing portal "false", mask and modal data-animate="reveal-out"
|
|
37
|
+
* closed after the out animation ends.
|
|
38
|
+
*/
|
|
39
|
+
export default function FullPageModal(props: FullPageModalProps = {}): NurvusElement<HTMLDivElement, FullPageModalProps> {
|
|
40
|
+
const { mountEl, ...initial } = props;
|
|
41
|
+
let current: FullPageModalProps = initial;
|
|
42
|
+
// The React component's two states.
|
|
43
|
+
let isVisible = Boolean(current.visible);
|
|
44
|
+
let animationState = '';
|
|
45
|
+
|
|
46
|
+
const portal = spawn<HTMLDivElement>({ className: `portal ${CONSTANT.CLASS_PREFIX}` }) as NurvusElement<HTMLDivElement, FullPageModalProps>;
|
|
47
|
+
let mask: HTMLElement | null = null;
|
|
48
|
+
let modal: HTMLElement;
|
|
49
|
+
|
|
50
|
+
// Draws the attributes for the current state.
|
|
51
|
+
const sync = () => {
|
|
52
|
+
portal.setAttribute('data-visible', String(Boolean(current.visible)));
|
|
53
|
+
if (isVisible) {
|
|
54
|
+
if (!mask) mask = spawn({ className: 'mask' });
|
|
55
|
+
mask.setAttribute('data-animate', animationState);
|
|
56
|
+
if (mask.parentNode !== portal) portal.insertBefore(mask, modal);
|
|
57
|
+
} else if (mask) {
|
|
58
|
+
mask.remove();
|
|
59
|
+
mask = null;
|
|
60
|
+
}
|
|
61
|
+
modal.setAttribute('data-visible', String(isVisible));
|
|
62
|
+
modal.setAttribute('data-animate', animationState);
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
// When an animation ends: settle the visibility (React's clearAnimation).
|
|
66
|
+
const clearAnimation = () => {
|
|
67
|
+
const visible = Boolean(current.visible);
|
|
68
|
+
if (animationState && visible !== isVisible) {
|
|
69
|
+
isVisible = visible;
|
|
70
|
+
animationState = '';
|
|
71
|
+
sync();
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
// When `visible` changes: start the animation (React's effect on [visible]).
|
|
76
|
+
const start = (visible: boolean) => {
|
|
77
|
+
const before = animationState;
|
|
78
|
+
if (visible !== isVisible) animationState = visible ? 'reveal-in' : 'reveal-out';
|
|
79
|
+
if (visible && !before) isVisible = true;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const buildModal = () => {
|
|
83
|
+
const { children, className, headerActions, title, visible: _visible, mountEl: _mountEl, ...rest } = current;
|
|
84
|
+
const el = spawn({
|
|
85
|
+
className: classNames(className, 'modal', CONSTANT.CLASS_PREFIX),
|
|
86
|
+
...rest,
|
|
87
|
+
tag: 'div',
|
|
88
|
+
children: [
|
|
89
|
+
spawn({
|
|
90
|
+
className: 'header',
|
|
91
|
+
children: Layout({
|
|
92
|
+
display: 'flex',
|
|
93
|
+
alignItems: 'center',
|
|
94
|
+
justifyContent: 'space-between',
|
|
95
|
+
children: [Title({ children: title }), spawn({ className: 'header-actions', children: headerActions })],
|
|
96
|
+
}),
|
|
97
|
+
}),
|
|
98
|
+
children,
|
|
99
|
+
],
|
|
100
|
+
});
|
|
101
|
+
el.addEventListener('animationend', clearAnimation);
|
|
102
|
+
return el;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
modal = buildModal();
|
|
106
|
+
portal.appendChild(modal);
|
|
107
|
+
sync();
|
|
108
|
+
|
|
109
|
+
portal.update = (changes) => {
|
|
110
|
+
const was = Boolean(current.visible);
|
|
111
|
+
current = { ...current, ...changes };
|
|
112
|
+
if (Object.keys(changes).some((key) => key !== 'visible')) {
|
|
113
|
+
const next = buildModal();
|
|
114
|
+
modal.replaceWith(next);
|
|
115
|
+
modal = next;
|
|
116
|
+
}
|
|
117
|
+
const visible = Boolean(current.visible);
|
|
118
|
+
if (visible !== was) start(visible);
|
|
119
|
+
sync();
|
|
120
|
+
return portal;
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
if (mountEl) mountEl.appendChild(portal);
|
|
124
|
+
return portal;
|
|
125
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import classNames from 'classnames';
|
|
2
|
+
import CONSTANT from '../constants';
|
|
3
|
+
import { spawn, Child, ElementProps } from './spawn';
|
|
4
|
+
import { updatable, NurvusElement } from './update';
|
|
5
|
+
import '../components/HistoryList/HistoryList.scss';
|
|
6
|
+
|
|
7
|
+
export interface HistoryStep {
|
|
8
|
+
/** Unique id. */
|
|
9
|
+
id: string | number;
|
|
10
|
+
/** What the step did. */
|
|
11
|
+
label: Child;
|
|
12
|
+
/** A second line. */
|
|
13
|
+
detail?: Child;
|
|
14
|
+
/** When it happened, as a timestamp in ms. */
|
|
15
|
+
time?: number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface HistoryListProps extends ElementProps {
|
|
19
|
+
/** Every step, OLDEST first. */
|
|
20
|
+
steps: HistoryStep[];
|
|
21
|
+
/** How many steps are applied: step N (1-based) is current when N === index; 0 = everything undone. */
|
|
22
|
+
index: number;
|
|
23
|
+
/** Called with the step number to jump to (0 = the start row). */
|
|
24
|
+
onJump?: (step: number) => void;
|
|
25
|
+
/** Label of the bottom row, the state before any step. */
|
|
26
|
+
startLabel?: Child;
|
|
27
|
+
/** Second line of the bottom row. */
|
|
28
|
+
startDetail?: Child;
|
|
29
|
+
/** Formats a step's `time` for its row. Defaults to the locale time with seconds. */
|
|
30
|
+
formatTime?: (time: number) => Child;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const defaultFormatTime = (time: number) => new Date(time).toLocaleTimeString([], {
|
|
34
|
+
hour: '2-digit',
|
|
35
|
+
minute: '2-digit',
|
|
36
|
+
second: '2-digit',
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Undo history as a list, newest first, with the current step marked. Like
|
|
41
|
+
* the React one it only shows what it is given: the app owns the history.
|
|
42
|
+
* A click calls onJump(step); the app then moves the current step with
|
|
43
|
+
* list.update({ index: step }).
|
|
44
|
+
*
|
|
45
|
+
* With no steps the React HistoryList renders nothing; this one returns an
|
|
46
|
+
* empty, hidden <ol> instead, so there is an element to update later.
|
|
47
|
+
*/
|
|
48
|
+
export default function HistoryList(props: HistoryListProps): NurvusElement<HTMLOListElement, HistoryListProps> {
|
|
49
|
+
return updatable(({
|
|
50
|
+
className, formatTime = defaultFormatTime, index, onJump, startDetail = 'Before any changes', startLabel = 'Start', steps, ...rest
|
|
51
|
+
}: HistoryListProps) => {
|
|
52
|
+
const renderRow = (number: number, label: Child, detail: Child, time: number | null | undefined) => {
|
|
53
|
+
const isCurrent = number === index;
|
|
54
|
+
const isFuture = number > index;
|
|
55
|
+
let title = 'Jump to this step (undo to here)';
|
|
56
|
+
if (isCurrent) title = 'Current state';
|
|
57
|
+
else if (isFuture) title = 'Jump to this step (redo to here)';
|
|
58
|
+
|
|
59
|
+
return spawn({
|
|
60
|
+
tag: 'li',
|
|
61
|
+
children: spawn({
|
|
62
|
+
tag: 'button',
|
|
63
|
+
type: 'button',
|
|
64
|
+
className: 'history-list-item',
|
|
65
|
+
'data-current': isCurrent,
|
|
66
|
+
'data-future': isFuture,
|
|
67
|
+
'aria-current': isCurrent ? 'step' : undefined,
|
|
68
|
+
title,
|
|
69
|
+
events: { click: () => { if (onJump) onJump(number); } },
|
|
70
|
+
children: [
|
|
71
|
+
spawn({ tag: 'span', className: 'history-list-index', children: number }),
|
|
72
|
+
spawn({
|
|
73
|
+
tag: 'span',
|
|
74
|
+
className: 'history-list-text',
|
|
75
|
+
children: [
|
|
76
|
+
spawn({ tag: 'span', className: 'history-list-label', children: label }),
|
|
77
|
+
detail ? spawn({ tag: 'span', className: 'history-list-detail', children: detail }) : null,
|
|
78
|
+
],
|
|
79
|
+
}),
|
|
80
|
+
time ? spawn({ tag: 'span', className: 'history-list-time', children: formatTime(time) }) : null,
|
|
81
|
+
],
|
|
82
|
+
}),
|
|
83
|
+
});
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const list = steps || [];
|
|
87
|
+
const rows = list.map((step, i) => ({ step, number: i + 1 })).reverse();
|
|
88
|
+
return spawn<HTMLOListElement>({
|
|
89
|
+
className: classNames(className, 'history-list', CONSTANT.CLASS_PREFIX),
|
|
90
|
+
'aria-label': 'History, newest first',
|
|
91
|
+
...rest,
|
|
92
|
+
hidden: list.length ? rest.hidden : true,
|
|
93
|
+
tag: 'ol',
|
|
94
|
+
children: list.length ? [
|
|
95
|
+
...rows.map(({ step, number }) => renderRow(number, step.label, step.detail, step.time)),
|
|
96
|
+
renderRow(0, startLabel, startDetail, null),
|
|
97
|
+
] : null,
|
|
98
|
+
});
|
|
99
|
+
}, props);
|
|
100
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import classNames from 'classnames';
|
|
2
|
+
import CONSTANT from '../constants';
|
|
3
|
+
import { spawn, Child, ElementProps } from './spawn';
|
|
4
|
+
import { updatable, NurvusElement } from './update';
|
|
5
|
+
import '../components/Layout/Layout.scss';
|
|
6
|
+
|
|
7
|
+
// Shared with the React Layout, so both snap and warn the same way.
|
|
8
|
+
import { Spacing, LegacySpacing, toGrid } from '../spacing';
|
|
9
|
+
|
|
10
|
+
export type { Spacing, LegacySpacing };
|
|
11
|
+
|
|
12
|
+
export interface LayoutProps extends ElementProps {
|
|
13
|
+
/** `block` stacks the children top to bottom; `flex` puts them in a row. */
|
|
14
|
+
display?: 'block' | 'flex';
|
|
15
|
+
/** With `display: 'flex'`: `column` stacks the children instead of a row. */
|
|
16
|
+
flexDirection?: 'row' | 'column';
|
|
17
|
+
/** Space between the children, in px, from the 4px grid: 0, 2, 4, 8, 12, 16, 24, 32, 48 or 64. The old values (10, 13, 20, 30, 40) still work: they snap to the grid, with a console warning. */
|
|
18
|
+
itemSpacing?: Spacing | LegacySpacing;
|
|
19
|
+
/** With `display: 'flex'`: how the children line up across the row (CSS `align-items`). */
|
|
20
|
+
alignItems?: 'flex-start' | 'center' | 'flex-end';
|
|
21
|
+
/** With `display: 'flex'`: how the children spread along the row (CSS `justify-content`). */
|
|
22
|
+
justifyContent?: 'flex-start' | 'center' | 'flex-end' | 'space-between';
|
|
23
|
+
/** `block` makes every child `display: block` (e.g. links stacked one per line). */
|
|
24
|
+
itemDisplay?: 'block';
|
|
25
|
+
/** What to lay out. */
|
|
26
|
+
children?: Child;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Stacks its children in a column (`block`) or a row (`flex`) with even space between them. */
|
|
30
|
+
export default function Layout(props: LayoutProps = {}): NurvusElement<HTMLDivElement, LayoutProps> {
|
|
31
|
+
return updatable(({
|
|
32
|
+
alignItems, className, display = 'block', flexDirection, justifyContent, itemDisplay, itemSpacing = 16, ...rest
|
|
33
|
+
}: LayoutProps) => spawn<HTMLDivElement>({
|
|
34
|
+
className: classNames(className, 'layout', CONSTANT.CLASS_PREFIX),
|
|
35
|
+
'data-align-items': alignItems,
|
|
36
|
+
'data-display': display,
|
|
37
|
+
'data-flex-direction': flexDirection,
|
|
38
|
+
'data-item-display': itemDisplay,
|
|
39
|
+
'data-item-spacing': toGrid(itemSpacing),
|
|
40
|
+
'data-justify-content': justifyContent,
|
|
41
|
+
...rest,
|
|
42
|
+
tag: 'div',
|
|
43
|
+
}), props);
|
|
44
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import classNames from 'classnames';
|
|
2
|
+
import CONSTANT from '../constants';
|
|
3
|
+
import { spawn, Child, ElementProps, Listener } from './spawn';
|
|
4
|
+
import { updatable, NurvusElement } from './update';
|
|
5
|
+
import '../components/Link/Link.scss';
|
|
6
|
+
|
|
7
|
+
export interface LinkProps extends ElementProps {
|
|
8
|
+
/** Where it goes. */
|
|
9
|
+
href?: string;
|
|
10
|
+
/** Called on a click with the event and the link. */
|
|
11
|
+
onClick?: Listener<MouseEvent>;
|
|
12
|
+
/** Content to render. */
|
|
13
|
+
children?: Child;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** A link in the theme's accent color. Any other prop (href, target…) goes on the `<a>`. */
|
|
17
|
+
export default function Link(props: LinkProps = {}): NurvusElement<HTMLAnchorElement, LinkProps> {
|
|
18
|
+
return updatable(({ children, className, ...rest }: LinkProps) => spawn<HTMLAnchorElement>({
|
|
19
|
+
className: classNames(className, 'link', CONSTANT.CLASS_PREFIX),
|
|
20
|
+
...rest,
|
|
21
|
+
tag: 'a',
|
|
22
|
+
children,
|
|
23
|
+
}), props);
|
|
24
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import classNames from 'classnames';
|
|
2
|
+
import CONSTANT from '../constants';
|
|
3
|
+
import { spawn, Child, ElementProps, Style } from './spawn';
|
|
4
|
+
import { updatable, NurvusElement } from './update';
|
|
5
|
+
import '../components/Menu/Menu.scss';
|
|
6
|
+
|
|
7
|
+
export interface MenuItem {
|
|
8
|
+
/** What the row shows (unless itemRenderer draws it). */
|
|
9
|
+
label?: Child;
|
|
10
|
+
/** Child rows, shown indented under this one. */
|
|
11
|
+
items?: MenuItem[];
|
|
12
|
+
/** Custom class name for this row. */
|
|
13
|
+
className?: string;
|
|
14
|
+
/** Inline style for this row. */
|
|
15
|
+
style?: Style;
|
|
16
|
+
/** Anything else your itemRenderer needs. */
|
|
17
|
+
[key: string]: any;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface MenuProps extends ElementProps {
|
|
21
|
+
/** The rows to render; each can have child `items`. */
|
|
22
|
+
items?: MenuItem[];
|
|
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: MenuItem) => Child;
|
|
27
|
+
/** Called on a row click with the event and that row's path, after the row is marked active. */
|
|
28
|
+
onClick?: (event: MouseEvent, path: number[]) => void;
|
|
29
|
+
/** Deepest level that still indents. Deeper rows stay at that indent. */
|
|
30
|
+
maxIndentDepth?: number;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* A nested list of rows, like a layers panel. Rows indent by depth and the
|
|
35
|
+
* active one is highlighted. A click marks that row active (in place: the
|
|
36
|
+
* element stays the same) and calls onClick(event, path). To set the active
|
|
37
|
+
* row from code: menu.update({ activePath: [1] }).
|
|
38
|
+
*/
|
|
39
|
+
export default function Menu(props: MenuProps = {}): NurvusElement<HTMLDivElement, MenuProps> {
|
|
40
|
+
return updatable(({
|
|
41
|
+
activePath = [], className, itemRenderer, items, maxIndentDepth = 6, onClick, children: _children, ...rest
|
|
42
|
+
}: MenuProps, keep) => {
|
|
43
|
+
const rows: { el: HTMLElement; path: string }[] = [];
|
|
44
|
+
const markActive = (path: number[]) => {
|
|
45
|
+
const key = path.join();
|
|
46
|
+
rows.forEach((row) => row.el.setAttribute('data-active', String(row.path === key)));
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const renderItems = (list: MenuItem[] | undefined, active: number[], path: number[] = []): HTMLElement[] => {
|
|
50
|
+
if (!list) return [];
|
|
51
|
+
return list.flatMap((item, i) => {
|
|
52
|
+
const { className: itemClassName, label, style } = item;
|
|
53
|
+
const type = path.length > 1 ? 'group' : '';
|
|
54
|
+
const isActive = active.length === 1 && active[0] === i;
|
|
55
|
+
const [first, ...remainingPath] = active;
|
|
56
|
+
const nextPath = [...path, i];
|
|
57
|
+
// Clamped so a deep tree cannot indent the label out of a narrow panel (see Menu.scss).
|
|
58
|
+
const depth = Math.min(path.length, maxIndentDepth);
|
|
59
|
+
const el = spawn({
|
|
60
|
+
className: classNames(itemClassName, 'menu-item', CONSTANT.CLASS_PREFIX),
|
|
61
|
+
'data-active': isActive,
|
|
62
|
+
'data-type': type,
|
|
63
|
+
style: { '--nurvus-menu-depth': String(depth), ...style },
|
|
64
|
+
events: {
|
|
65
|
+
click: (event) => {
|
|
66
|
+
event.stopPropagation();
|
|
67
|
+
keep({ activePath: nextPath });
|
|
68
|
+
markActive(nextPath);
|
|
69
|
+
if (onClick) onClick(event, nextPath);
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
children: itemRenderer ? itemRenderer(item) : label,
|
|
73
|
+
});
|
|
74
|
+
rows.push({ el, path: nextPath.join() });
|
|
75
|
+
return [el, ...renderItems(item.items, first === i ? remainingPath : [], nextPath)];
|
|
76
|
+
});
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
return spawn<HTMLDivElement>({
|
|
80
|
+
className: classNames(className, 'menu', CONSTANT.CLASS_PREFIX),
|
|
81
|
+
...rest,
|
|
82
|
+
tag: 'div',
|
|
83
|
+
children: renderItems(items, activePath),
|
|
84
|
+
});
|
|
85
|
+
}, props);
|
|
86
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import classNames from 'classnames';
|
|
2
|
+
import CONSTANT from '../constants';
|
|
3
|
+
import { spawn, Child, ElementProps } from './spawn';
|
|
4
|
+
import { updatable, NurvusElement } from './update';
|
|
5
|
+
import '../components/Panel/Panel.scss';
|
|
6
|
+
|
|
7
|
+
export type PanelPadding = 'none' | 'small' | 'medium';
|
|
8
|
+
|
|
9
|
+
export interface PanelProps extends ElementProps {
|
|
10
|
+
/** Heading at the top of the panel. */
|
|
11
|
+
title?: Child;
|
|
12
|
+
/** Footer line under a hairline, in caption type. */
|
|
13
|
+
caption?: Child;
|
|
14
|
+
/** Inner spacing: none (e.g. a full-bleed image), small (12px) or medium (16px). */
|
|
15
|
+
padding?: PanelPadding;
|
|
16
|
+
/** The panel's content. */
|
|
17
|
+
children?: Child;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** A card: the theme's surface, a hairline border and 12px corners, with an optional title and caption. */
|
|
21
|
+
export default function Panel(props: PanelProps = {}): NurvusElement<HTMLElement, PanelProps> {
|
|
22
|
+
return updatable(({ title, caption, padding = 'medium', children, className, ...rest }: PanelProps) => spawn({
|
|
23
|
+
className: classNames(className, 'panel', CONSTANT.CLASS_PREFIX),
|
|
24
|
+
'data-padding': padding,
|
|
25
|
+
...rest,
|
|
26
|
+
tag: 'section',
|
|
27
|
+
// Same tests as the React Panel, so the same props give the same parts.
|
|
28
|
+
children: [
|
|
29
|
+
(title || children) && spawn({
|
|
30
|
+
className: 'panel-body',
|
|
31
|
+
children: [title && spawn({ tag: 'h3', className: 'panel-title', children: title }), children],
|
|
32
|
+
}),
|
|
33
|
+
caption && spawn({ className: 'panel-caption', children: caption }),
|
|
34
|
+
],
|
|
35
|
+
}), props);
|
|
36
|
+
}
|