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

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 (128) hide show
  1. package/README.md +26 -11
  2. package/dist/index.js +1 -1
  3. package/dist/nurvus.css +14 -0
  4. package/dist/respawn.js +2 -0
  5. package/dist/respawn.js.LICENSE.txt +5 -0
  6. package/dist/theme.js +1 -0
  7. package/dist/themes/all.css +231 -0
  8. package/dist/themes/apollo-white.css +33 -0
  9. package/dist/themes/apollo-white.json +52 -0
  10. package/dist/themes/arcane.css +33 -0
  11. package/dist/themes/arcane.json +52 -0
  12. package/dist/themes/dopamine.css +33 -0
  13. package/dist/themes/dopamine.json +52 -0
  14. package/dist/themes/nether-neon.css +33 -0
  15. package/dist/themes/nether-neon.json +52 -0
  16. package/dist/themes/nether.css +33 -0
  17. package/dist/themes/nether.json +52 -0
  18. package/dist/themes/outworld.css +33 -0
  19. package/dist/themes/outworld.json +52 -0
  20. package/dist/themes/roles.json +29 -0
  21. package/dist/themes/tokyo-neon.css +33 -0
  22. package/dist/themes/tokyo-neon.json +52 -0
  23. package/dist/types/components/Button/Button.d.ts +19 -0
  24. package/dist/types/components/ButtonGroup/ButtonGroup.d.ts +12 -0
  25. package/dist/types/components/FullPageModal/FullPageModal.d.ts +15 -0
  26. package/dist/types/components/HistoryList/HistoryList.d.ts +40 -0
  27. package/dist/types/components/Layout/Layout.d.ts +27 -0
  28. package/dist/types/components/Link/Link.d.ts +9 -0
  29. package/dist/types/components/Menu/Menu.d.ts +35 -0
  30. package/dist/types/components/Panel/Panel.d.ts +19 -0
  31. package/dist/types/components/Progress/Progress.d.ts +15 -0
  32. package/dist/types/components/Table/Table.d.ts +25 -0
  33. package/dist/types/components/Tag/Tag.d.ts +15 -0
  34. package/dist/types/components/Text/Text.d.ts +28 -0
  35. package/dist/types/components/Title/Title.d.ts +288 -0
  36. package/dist/types/constants.d.ts +5 -0
  37. package/dist/types/index.d.ts +30 -0
  38. package/dist/types/respawn/Button.d.ts +16 -0
  39. package/dist/types/respawn/ButtonGroup.d.ts +8 -0
  40. package/dist/types/respawn/FullPageModal.d.ts +32 -0
  41. package/dist/types/respawn/HistoryList.d.ts +36 -0
  42. package/dist/types/respawn/Layout.d.ts +22 -0
  43. package/dist/types/respawn/Link.d.ts +12 -0
  44. package/dist/types/respawn/Menu.d.ts +33 -0
  45. package/dist/types/respawn/Panel.d.ts +15 -0
  46. package/dist/types/respawn/Progress.d.ts +15 -0
  47. package/dist/types/respawn/Table.d.ts +22 -0
  48. package/dist/types/respawn/Tag.d.ts +11 -0
  49. package/dist/types/respawn/Text.d.ts +19 -0
  50. package/dist/types/respawn/Title.d.ts +14 -0
  51. package/dist/types/respawn/index.d.ts +33 -0
  52. package/dist/types/respawn/spawn.d.ts +50 -0
  53. package/dist/types/respawn/update.d.ts +13 -0
  54. package/dist/types/spacing.d.ts +8 -0
  55. package/dist/types/theme.d.ts +32 -0
  56. package/dist/types/themes/index.d.ts +45 -0
  57. package/package.json +42 -12
  58. package/src/components/Button/Button.md +5 -5
  59. package/src/components/Button/Button.tsx +44 -0
  60. package/src/components/ButtonGroup/ButtonGroup.tsx +23 -0
  61. package/src/components/FullPageModal/FullPageModal.scss +9 -5
  62. package/src/components/FullPageModal/FullPageModal.tsx +74 -0
  63. package/src/components/HistoryList/{HistoryList.jsx → HistoryList.tsx} +46 -59
  64. package/src/components/Layout/Layout.md +4 -4
  65. package/src/components/Layout/Layout.scss +14 -47
  66. package/src/components/Layout/Layout.tsx +56 -0
  67. package/src/components/Link/Link.scss +9 -9
  68. package/src/components/Link/Link.tsx +23 -0
  69. package/src/components/Menu/Menu.tsx +90 -0
  70. package/src/components/Panel/{Panel.jsx → Panel.tsx} +18 -27
  71. package/src/components/Progress/Progress.md +5 -5
  72. package/src/components/Progress/Progress.tsx +36 -0
  73. package/src/components/Table/Table.tsx +59 -0
  74. package/src/components/Tag/Tag.tsx +36 -0
  75. package/src/components/Text/Text.md +13 -0
  76. package/src/components/Text/Text.scss +6 -0
  77. package/src/components/Text/Text.tsx +54 -0
  78. package/src/components/Title/Title.tsx +42 -0
  79. package/src/constants.ts +6 -0
  80. package/src/css.ts +16 -0
  81. package/src/global.d.ts +3 -0
  82. package/src/index.ts +40 -0
  83. package/src/respawn/Button.ts +31 -0
  84. package/src/respawn/ButtonGroup.ts +19 -0
  85. package/src/respawn/FullPageModal.ts +125 -0
  86. package/src/respawn/HistoryList.ts +100 -0
  87. package/src/respawn/Layout.ts +44 -0
  88. package/src/respawn/Link.ts +24 -0
  89. package/src/respawn/Menu.ts +86 -0
  90. package/src/respawn/Panel.ts +36 -0
  91. package/src/respawn/Progress.ts +29 -0
  92. package/src/respawn/Table.ts +54 -0
  93. package/src/respawn/Tag.ts +25 -0
  94. package/src/respawn/Text.ts +35 -0
  95. package/src/respawn/Title.ts +30 -0
  96. package/src/respawn/index.ts +57 -0
  97. package/src/respawn/spawn.ts +155 -0
  98. package/src/respawn/update.ts +49 -0
  99. package/src/spacing.ts +23 -0
  100. package/src/styles/theme.scss +6 -197
  101. package/src/styles/type.json +35 -5
  102. package/src/theme.ts +92 -0
  103. package/src/themes/css/all.css +231 -0
  104. package/src/themes/css/apollo-white.css +33 -0
  105. package/src/themes/css/arcane.css +33 -0
  106. package/src/themes/css/dopamine.css +33 -0
  107. package/src/themes/css/nether-neon.css +33 -0
  108. package/src/themes/css/nether.css +33 -0
  109. package/src/themes/css/outworld.css +33 -0
  110. package/src/themes/css/tokyo-neon.css +33 -0
  111. package/src/themes/index.ts +61 -0
  112. package/src/themes/roles.json +1 -1
  113. package/src/app.js +0 -29
  114. package/src/components/Button/Button.jsx +0 -59
  115. package/src/components/ButtonGroup/ButtonGroup.jsx +0 -28
  116. package/src/components/FullPageModal/FullPageModal.jsx +0 -106
  117. package/src/components/Layout/Layout.jsx +0 -68
  118. package/src/components/Link/Link.jsx +0 -39
  119. package/src/components/Menu/Menu.jsx +0 -120
  120. package/src/components/Progress/Progress.jsx +0 -56
  121. package/src/components/Table/Table.jsx +0 -94
  122. package/src/components/Tag/Tag.jsx +0 -41
  123. package/src/components/Text/Text.jsx +0 -54
  124. package/src/components/Title/Title.jsx +0 -48
  125. package/src/constants.js +0 -3
  126. package/src/index.js +0 -20
  127. package/src/theme.js +0 -48
  128. package/src/themes/index.js +0 -22
@@ -16,6 +16,19 @@ Basic Usage
16
16
  <Text>Hi</Text>
17
17
  ```
18
18
 
19
+ Weight: `weight` overrides the role's own. For words that matter inside a sentence, use `<strong>` instead (screen readers know it). More on the **Typography** page.
20
+ ```jsx
21
+ import { Text } from '@nurvus/ui';
22
+
23
+ <div style={ { display: 'grid', gap: 4 } }>
24
+ <Text as="p" variant="body" weight="regular">Regular, 400</Text>
25
+ <Text as="p" variant="body" weight="medium">Medium, 500</Text>
26
+ <Text as="p" variant="body" weight="semibold">Semibold, 600</Text>
27
+ <Text as="p" variant="body" weight="bold">Bold, 700</Text>
28
+ <Text as="p" variant="body">A paragraph with <strong>one strong phrase</strong> and an <em>emphasised</em> one.</Text>
29
+ </div>
30
+ ```
31
+
19
32
  Using Text Overflow
20
33
  ```jsx
21
34
  import RemoveRedEyeIcon from '@material-ui/icons/RemoveRedEye';
@@ -19,4 +19,10 @@
19
19
  &[data-tone="default"] { color: var(--nv-text); }
20
20
  &[data-tone="muted"] { color: var(--nv-text-muted); }
21
21
  &[data-tone="subtle"] { color: var(--nv-text-subtle); }
22
+
23
+ // Weight overrides the role's own (after the variants, so it wins).
24
+ &[data-weight="regular"] { font-weight: 400; }
25
+ &[data-weight="medium"] { font-weight: 500; }
26
+ &[data-weight="semibold"] { font-weight: 600; }
27
+ &[data-weight="bold"] { font-weight: 700; }
22
28
  }
@@ -0,0 +1,54 @@
1
+ import React from 'react';
2
+ import classNames from 'classnames';
3
+ import CONSTANT from '../../constants';
4
+ import './Text.scss';
5
+
6
+ export type TextVariant = 'display' | 'title' | 'body' | 'caption' | 'mono';
7
+ export type TextTone = 'default' | 'muted' | 'subtle';
8
+ export type TextWeight = 'regular' | 'medium' | 'semibold' | 'bold';
9
+
10
+ export interface TextProps extends React.HTMLAttributes<HTMLElement> {
11
+ /** The element to render: 'p' for a paragraph, 'span' inside a line, 'h1'…, 'label'. */
12
+ as?: React.ElementType;
13
+ /**
14
+ * Type role: display (the one big headline), title (section headings),
15
+ * body (running text), caption (labels and notes), mono (values and code).
16
+ */
17
+ variant?: TextVariant;
18
+ /** Text color from the theme: default (text), muted or subtle. */
19
+ tone?: TextTone;
20
+ /** Font weight, overriding the role's own: regular 400, medium 500, semibold 600, bold 700. */
21
+ weight?: TextWeight;
22
+ /** The text. */
23
+ children?: React.ReactNode;
24
+ /** Custom class name. */
25
+ className?: string;
26
+ }
27
+
28
+ /**
29
+ * Text in one of the type roles (display, title, body, caption, mono). The
30
+ * sizes come from the `--nv-type-*` tokens, so every NURVUS page uses the same
31
+ * scale. Without a `variant` it inherits its parent's type. A paragraph is
32
+ * `<Text as="p" variant="body">`.
33
+ */
34
+ export default function Text({
35
+ as: Element = 'span',
36
+ variant,
37
+ tone,
38
+ weight,
39
+ children,
40
+ className,
41
+ ...restProps
42
+ }: TextProps) {
43
+ return (
44
+ <Element
45
+ className={ classNames(className, 'text', CONSTANT.CLASS_PREFIX) }
46
+ data-variant={ variant }
47
+ data-tone={ tone }
48
+ data-weight={ weight }
49
+ { ...restProps }
50
+ >
51
+ { children }
52
+ </Element>
53
+ );
54
+ }
@@ -0,0 +1,42 @@
1
+ import React from 'react';
2
+ import classNames from 'classnames';
3
+ import CONSTANT from '../../constants';
4
+ import './Title.scss';
5
+
6
+ export type TitleLevel = 1 | 2 | 3 | 4 | 5 | 6;
7
+ export type TitleType = 'primary' | 'secondary';
8
+
9
+ export interface TitleProps extends React.HTMLAttributes<HTMLHeadingElement> {
10
+ /** Heading level: 1 renders `<h1>` (24px) down to 6 (`<h6>`). */
11
+ level?: TitleLevel;
12
+ /** `primary` uses the theme's text color; `secondary` the muted one. */
13
+ type?: TitleType;
14
+ /** The heading text. */
15
+ children?: React.ReactNode;
16
+ /** Custom class name. */
17
+ className?: string;
18
+ }
19
+
20
+ const TAGS = { 1: 'h1', 2: 'h2', 3: 'h3', 4: 'h4', 5: 'h5', 6: 'h6' } as const;
21
+
22
+ /**
23
+ * A heading, `<h1>` to `<h6>`, in the theme's text color. For the larger
24
+ * type roles (display, title, caption) use `Text` with a `variant`.
25
+ */
26
+ export default function Title({
27
+ children,
28
+ className,
29
+ level = 1,
30
+ type = 'primary',
31
+ ...restProps
32
+ }: TitleProps) {
33
+ return React.createElement(
34
+ TAGS[level] || 'h6',
35
+ {
36
+ className: classNames(className, 'title', CONSTANT.CLASS_PREFIX),
37
+ 'data-type': type,
38
+ ...restProps
39
+ },
40
+ children
41
+ );
42
+ }
@@ -0,0 +1,6 @@
1
+ /** Every component's class list ends with this: class="my-class tag nurvus". */
2
+ const CONSTANT = {
3
+ CLASS_PREFIX: 'nurvus',
4
+ } as const;
5
+
6
+ export default CONSTANT;
package/src/css.ts ADDED
@@ -0,0 +1,16 @@
1
+ // Build-only entry for dist/nurvus.css: the core CSS as one file (the
2
+ // primitives, the type roles, Apollo White and every component's styles).
3
+ import './styles/theme.scss';
4
+ import './components/Button/Button.scss';
5
+ import './components/ButtonGroup/ButtonGroup.scss';
6
+ import './components/FullPageModal/FullPageModal.scss';
7
+ import './components/HistoryList/HistoryList.scss';
8
+ import './components/Layout/Layout.scss';
9
+ import './components/Link/Link.scss';
10
+ import './components/Menu/Menu.scss';
11
+ import './components/Panel/Panel.scss';
12
+ import './components/Progress/Progress.scss';
13
+ import './components/Table/Table.scss';
14
+ import './components/Tag/Tag.scss';
15
+ import './components/Text/Text.scss';
16
+ import './components/Title/Title.scss';
@@ -0,0 +1,3 @@
1
+ // Style imports are side effects: the build injects or extracts the CSS.
2
+ declare module '*.scss';
3
+ declare module '*.css';
package/src/index.ts ADDED
@@ -0,0 +1,40 @@
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 FullPageModal } from './components/FullPageModal/FullPageModal';
28
+ export type { FullPageModalProps } from './components/FullPageModal/FullPageModal';
29
+ export { default as HistoryList } from './components/HistoryList/HistoryList';
30
+ export type { HistoryListProps, HistoryStep } from './components/HistoryList/HistoryList';
31
+ export { default as Panel } from './components/Panel/Panel';
32
+ export type { PanelProps, PanelPadding } from './components/Panel/Panel';
33
+ export { default as Table } from './components/Table/Table';
34
+ export type { TableProps, TableColumn, TableRow } from './components/Table/Table';
35
+ export { default as Tag } from './components/Tag/Tag';
36
+ export type { TagProps, TagTone } from './components/Tag/Tag';
37
+ export { default as Text } from './components/Text/Text';
38
+ export type { TextProps, TextVariant, TextTone, TextWeight } from './components/Text/Text';
39
+ export { default as Title } from './components/Title/Title';
40
+ export type { TitleProps, TitleLevel, TitleType } from './components/Title/Title';
@@ -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
+ }