@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.
Files changed (135) hide show
  1. package/README.md +20 -9
  2. package/dist/index.js +1 -1
  3. package/dist/nurvus.css +15 -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/NavList/NavList.d.ts +43 -0
  31. package/dist/types/components/Panel/Panel.d.ts +19 -0
  32. package/dist/types/components/Progress/Progress.d.ts +15 -0
  33. package/dist/types/components/Table/Table.d.ts +25 -0
  34. package/dist/types/components/Tag/Tag.d.ts +15 -0
  35. package/dist/types/components/Text/Text.d.ts +28 -0
  36. package/dist/types/components/Title/Title.d.ts +288 -0
  37. package/dist/types/constants.d.ts +5 -0
  38. package/dist/types/index.d.ts +32 -0
  39. package/dist/types/navList.d.ts +7 -0
  40. package/dist/types/respawn/Button.d.ts +16 -0
  41. package/dist/types/respawn/ButtonGroup.d.ts +8 -0
  42. package/dist/types/respawn/FullPageModal.d.ts +32 -0
  43. package/dist/types/respawn/HistoryList.d.ts +36 -0
  44. package/dist/types/respawn/Layout.d.ts +22 -0
  45. package/dist/types/respawn/Link.d.ts +12 -0
  46. package/dist/types/respawn/Menu.d.ts +33 -0
  47. package/dist/types/respawn/NavList.d.ts +43 -0
  48. package/dist/types/respawn/Panel.d.ts +15 -0
  49. package/dist/types/respawn/Progress.d.ts +15 -0
  50. package/dist/types/respawn/Table.d.ts +22 -0
  51. package/dist/types/respawn/Tag.d.ts +11 -0
  52. package/dist/types/respawn/Text.d.ts +19 -0
  53. package/dist/types/respawn/Title.d.ts +14 -0
  54. package/dist/types/respawn/index.d.ts +35 -0
  55. package/dist/types/respawn/spawn.d.ts +50 -0
  56. package/dist/types/respawn/update.d.ts +13 -0
  57. package/dist/types/spacing.d.ts +8 -0
  58. package/dist/types/theme.d.ts +32 -0
  59. package/dist/types/themes/index.d.ts +45 -0
  60. package/package.json +40 -12
  61. package/src/components/Button/Button.md +5 -5
  62. package/src/components/Button/Button.tsx +44 -0
  63. package/src/components/ButtonGroup/ButtonGroup.tsx +23 -0
  64. package/src/components/FullPageModal/FullPageModal.scss +9 -5
  65. package/src/components/FullPageModal/FullPageModal.tsx +74 -0
  66. package/src/components/HistoryList/{HistoryList.jsx → HistoryList.tsx} +45 -58
  67. package/src/components/Layout/Layout.md +4 -4
  68. package/src/components/Layout/Layout.scss +14 -47
  69. package/src/components/Layout/Layout.tsx +56 -0
  70. package/src/components/Link/Link.scss +9 -9
  71. package/src/components/Link/Link.tsx +23 -0
  72. package/src/components/Menu/Menu.tsx +90 -0
  73. package/src/components/NavList/NavList.md +39 -0
  74. package/src/components/NavList/NavList.scss +149 -0
  75. package/src/components/NavList/NavList.tsx +129 -0
  76. package/src/components/Panel/{Panel.jsx → Panel.tsx} +17 -26
  77. package/src/components/Progress/Progress.md +5 -5
  78. package/src/components/Progress/Progress.tsx +36 -0
  79. package/src/components/Table/Table.tsx +59 -0
  80. package/src/components/Tag/{Tag.jsx → Tag.tsx} +13 -18
  81. package/src/components/Text/Text.md +13 -0
  82. package/src/components/Text/Text.scss +6 -0
  83. package/src/components/Text/Text.tsx +54 -0
  84. package/src/components/Title/Title.tsx +42 -0
  85. package/src/constants.ts +6 -0
  86. package/src/css.ts +17 -0
  87. package/src/global.d.ts +3 -0
  88. package/src/index.ts +42 -0
  89. package/src/navList.ts +9 -0
  90. package/src/respawn/Button.ts +31 -0
  91. package/src/respawn/ButtonGroup.ts +19 -0
  92. package/src/respawn/FullPageModal.ts +125 -0
  93. package/src/respawn/HistoryList.ts +100 -0
  94. package/src/respawn/Layout.ts +44 -0
  95. package/src/respawn/Link.ts +24 -0
  96. package/src/respawn/Menu.ts +86 -0
  97. package/src/respawn/NavList.ts +126 -0
  98. package/src/respawn/Panel.ts +36 -0
  99. package/src/respawn/Progress.ts +29 -0
  100. package/src/respawn/Table.ts +54 -0
  101. package/src/respawn/Tag.ts +25 -0
  102. package/src/respawn/Text.ts +35 -0
  103. package/src/respawn/Title.ts +30 -0
  104. package/src/respawn/index.ts +59 -0
  105. package/src/respawn/spawn.ts +155 -0
  106. package/src/respawn/update.ts +49 -0
  107. package/src/spacing.ts +23 -0
  108. package/src/styles/theme.scss +6 -197
  109. package/src/styles/type.json +35 -5
  110. package/src/theme.ts +92 -0
  111. package/src/themes/css/all.css +231 -0
  112. package/src/themes/css/apollo-white.css +33 -0
  113. package/src/themes/css/arcane.css +33 -0
  114. package/src/themes/css/dopamine.css +33 -0
  115. package/src/themes/css/nether-neon.css +33 -0
  116. package/src/themes/css/nether.css +33 -0
  117. package/src/themes/css/outworld.css +33 -0
  118. package/src/themes/css/tokyo-neon.css +33 -0
  119. package/src/themes/index.ts +61 -0
  120. package/src/themes/roles.json +1 -1
  121. package/src/app.js +0 -29
  122. package/src/components/Button/Button.jsx +0 -59
  123. package/src/components/ButtonGroup/ButtonGroup.jsx +0 -28
  124. package/src/components/FullPageModal/FullPageModal.jsx +0 -106
  125. package/src/components/Layout/Layout.jsx +0 -68
  126. package/src/components/Link/Link.jsx +0 -39
  127. package/src/components/Menu/Menu.jsx +0 -120
  128. package/src/components/Progress/Progress.jsx +0 -56
  129. package/src/components/Table/Table.jsx +0 -94
  130. package/src/components/Text/Text.jsx +0 -54
  131. package/src/components/Title/Title.jsx +0 -48
  132. package/src/constants.js +0 -3
  133. package/src/index.js +0 -20
  134. package/src/theme.js +0 -64
  135. package/src/themes/index.js +0 -22
@@ -0,0 +1,29 @@
1
+ import classNames from 'classnames';
2
+ import CONSTANT from '../constants';
3
+ import { spawn } from './spawn';
4
+ import { updatable, NurvusElement } from './update';
5
+ import '../components/Progress/Progress.scss';
6
+
7
+ export type ProgressSize = 'small' | 'medium' | 'large';
8
+ export type ProgressStatus = 'active' | 'success' | 'warning' | 'critical';
9
+
10
+ export interface ProgressProps {
11
+ /** How far along, 0 to 100. */
12
+ percent?: number;
13
+ /** Bar height. */
14
+ size?: ProgressSize;
15
+ /** The bar color: active (accent), success, warning or critical (danger). */
16
+ status?: ProgressStatus;
17
+ /** Custom class name. */
18
+ className?: string;
19
+ }
20
+
21
+ /** A progress bar. Like the React one, it takes no other attributes. */
22
+ export default function Progress(props: ProgressProps = {}): NurvusElement<HTMLDivElement, ProgressProps> {
23
+ return updatable(({ className, percent = 0, size = 'medium', status = 'active' }: ProgressProps) => spawn<HTMLDivElement>({
24
+ className: classNames(className, 'progress', CONSTANT.CLASS_PREFIX),
25
+ 'data-size': size,
26
+ 'data-status': status,
27
+ children: spawn({ className: 'bar', style: { width: `${percent}%` } }),
28
+ }), props);
29
+ }
@@ -0,0 +1,54 @@
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/Table/Table.scss';
6
+
7
+ /** A row of data: any object. */
8
+ export type TableRow = Record<string, any>;
9
+
10
+ export interface TableColumn<Row extends TableRow = TableRow> extends ElementProps {
11
+ /** Which field of each row this column shows. */
12
+ dataKey: string;
13
+ /** Header text. Defaults to the dataKey. */
14
+ title?: Child;
15
+ /** Formats a cell: gets the value, the dataKey and the whole row. Returns text, an element or spawn props. */
16
+ render?: (value: any, dataKey: string, row: Row) => Child;
17
+ }
18
+
19
+ export interface TableProps<Row extends TableRow = TableRow> {
20
+ /** The columns, in order. Any other column prop (e.g. style) goes on its `<th>` and `<td>`s. */
21
+ columns: TableColumn<Row>[];
22
+ /** The rows, in order. */
23
+ data: Row[];
24
+ /** Custom class name. */
25
+ className?: string;
26
+ }
27
+
28
+ /** A data table. Columns describe the fields; each can format its cells with `render`. */
29
+ export default function Table<Row extends TableRow = TableRow>(props: TableProps<Row>): NurvusElement<HTMLTableElement, TableProps<Row>> {
30
+ return updatable(({ className, columns = [], data = [] }: TableProps<Row>) => spawn<HTMLTableElement>({
31
+ tag: 'table',
32
+ className: classNames(className, 'table', CONSTANT.CLASS_PREFIX),
33
+ children: [
34
+ spawn({
35
+ tag: 'thead',
36
+ children: spawn({
37
+ tag: 'tr',
38
+ children: columns.map(({ dataKey, title, render, ...cell }) => spawn({ ...cell, tag: 'th', children: title || dataKey })),
39
+ }),
40
+ }),
41
+ spawn({
42
+ tag: 'tbody',
43
+ children: data.map((row, i) => spawn({
44
+ tag: 'tr',
45
+ 'data-even': i % 2 === 0,
46
+ children: columns.map(({ dataKey, render, title, ...cell }) => {
47
+ const value = row[dataKey];
48
+ return spawn({ ...cell, tag: 'td', children: render ? render(value, dataKey, row) : value });
49
+ }),
50
+ })),
51
+ }),
52
+ ],
53
+ }), props);
54
+ }
@@ -0,0 +1,25 @@
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/Tag/Tag.scss';
6
+
7
+ export type TagTone = 'neutral' | 'accent' | 'success' | 'warning' | 'danger' | 'info';
8
+
9
+ export interface TagProps extends ElementProps {
10
+ /** Color: neutral (muted text) or one of the theme's accent / status colors. */
11
+ tone?: TagTone;
12
+ /** The label. */
13
+ children?: Child;
14
+ }
15
+
16
+ /** A small pill for a status or label ("new", "preview", "demo"). */
17
+ export default function Tag(props: TagProps = {}): NurvusElement<HTMLSpanElement, TagProps> {
18
+ return updatable(({ tone = 'neutral', children, className, ...rest }: TagProps) => spawn<HTMLSpanElement>({
19
+ className: classNames(className, 'tag', CONSTANT.CLASS_PREFIX),
20
+ 'data-tone': tone,
21
+ ...rest,
22
+ tag: 'span',
23
+ children,
24
+ }), props);
25
+ }
@@ -0,0 +1,35 @@
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/Text/Text.scss';
6
+
7
+ export type TextVariant = 'display' | 'title' | 'body' | 'caption' | 'mono';
8
+ export type TextTone = 'default' | 'muted' | 'subtle';
9
+ export type TextWeight = 'regular' | 'medium' | 'semibold' | 'bold';
10
+
11
+ export interface TextProps extends ElementProps {
12
+ /** The element to render: 'p' for a paragraph, 'span' inside a line, 'h1'…, 'label'. */
13
+ as?: string;
14
+ /** Type role: display, title, body, caption or mono. Without one it inherits its parent's type. */
15
+ variant?: TextVariant;
16
+ /** Text color from the theme: default (text), muted or subtle. */
17
+ tone?: TextTone;
18
+ /** Font weight, overriding the role's own: regular 400, medium 500, semibold 600, bold 700. */
19
+ weight?: TextWeight;
20
+ /** The text. */
21
+ children?: Child;
22
+ }
23
+
24
+ /** Text in one of the type roles. A paragraph is `Text({ as: 'p', variant: 'body', children: '…' })`. */
25
+ export default function Text(props: TextProps = {}): NurvusElement<HTMLElement, TextProps> {
26
+ return updatable(({ as = 'span', variant, tone, weight, children, className, ...rest }: TextProps) => spawn({
27
+ className: classNames(className, 'text', CONSTANT.CLASS_PREFIX),
28
+ 'data-variant': variant,
29
+ 'data-tone': tone,
30
+ 'data-weight': weight,
31
+ ...rest,
32
+ tag: as,
33
+ children,
34
+ }), props);
35
+ }
@@ -0,0 +1,30 @@
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/Title/Title.scss';
6
+
7
+ export type TitleLevel = 1 | 2 | 3 | 4 | 5 | 6;
8
+ export type TitleType = 'primary' | 'secondary';
9
+
10
+ export interface TitleProps extends ElementProps {
11
+ /** Heading level: 1 renders `<h1>` (24px) down to 6 (`<h6>`). */
12
+ level?: TitleLevel;
13
+ /** `primary` uses the theme's text color; `secondary` the muted one. */
14
+ type?: TitleType;
15
+ /** The heading text. */
16
+ children?: Child;
17
+ }
18
+
19
+ const TAGS = { 1: 'h1', 2: 'h2', 3: 'h3', 4: 'h4', 5: 'h5', 6: 'h6' } as const;
20
+
21
+ /** A heading, `<h1>` to `<h6>`, in the theme's text color. */
22
+ export default function Title(props: TitleProps = {}): NurvusElement<HTMLHeadingElement, TitleProps> {
23
+ return updatable(({ children, className, level = 1, type = 'primary', ...rest }: TitleProps) => spawn<HTMLHeadingElement>({
24
+ className: classNames(className, 'title', CONSTANT.CLASS_PREFIX),
25
+ 'data-type': type,
26
+ ...rest,
27
+ tag: TAGS[level] || 'h6',
28
+ children,
29
+ }), props);
30
+ }
@@ -0,0 +1,59 @@
1
+ // @nurvus/ui/respawn: the NURVUS components as plain DOM, for Respawn and any
2
+ // page without React. Each component is a function that takes the same props
3
+ // as the React one and returns a real element with the same markup, so the
4
+ // same CSS and themes style it:
5
+ //
6
+ // import { Button, setTheme } from '@nurvus/ui/respawn';
7
+ // document.body.append(Button({ children: 'Save', onClick: () => save() }));
8
+ //
9
+ // Every element has update(changes): it rebuilds the element with the changes,
10
+ // swaps it into the page and returns it (see ./update.ts).
11
+ //
12
+ // Core CSS: the primitives, the type roles and the default theme (Apollo
13
+ // White), then every component's styles, injected once, in the same order as
14
+ // the React entry (src/index.ts). Other themes are opt-in files:
15
+ // import '@nurvus/ui/themes/tokyo-neon.css'; (or themes/all.css)
16
+ import '../styles/theme.scss';
17
+
18
+ export {
19
+ themes, DEFAULT_THEME, THEME_STORAGE_KEY, THEME_CHANGE_EVENT,
20
+ getTheme, currentTheme, setTheme, initTheme,
21
+ } from '../theme';
22
+ export type { SetThemeOptions } from '../theme';
23
+ export { roles as themeRoles } from '../themes';
24
+ export type { Theme, ThemeRole, ThemeRoleInfo } from '../themes';
25
+
26
+ // Building blocks: Respawn-style element building, and the update() contract.
27
+ export { spawn } from './spawn';
28
+ export type { Child, ElementProps, Events, Listener, SpawnProps, Style } from './spawn';
29
+ export type { NurvusElement } from './update';
30
+
31
+ // Components, and the types of their props (same order as src/index.ts, so the CSS lands in the same order).
32
+ export { default as Button } from './Button';
33
+ export type { ButtonProps, ButtonSize, ButtonType } from './Button';
34
+ export { default as ButtonGroup } from './ButtonGroup';
35
+ export type { ButtonGroupProps } from './ButtonGroup';
36
+ export { default as Link } from './Link';
37
+ export type { LinkProps } from './Link';
38
+ export { default as Progress } from './Progress';
39
+ export type { ProgressProps, ProgressSize, ProgressStatus } from './Progress';
40
+ export { default as Layout } from './Layout';
41
+ export type { LayoutProps, Spacing, LegacySpacing } from './Layout';
42
+ export { default as Menu } from './Menu';
43
+ export type { MenuProps, MenuItem } from './Menu';
44
+ export { default as NavList } from './NavList';
45
+ export type { NavListProps, NavListItem, NavListSection } from './NavList';
46
+ export { default as FullPageModal } from './FullPageModal';
47
+ export type { FullPageModalProps } from './FullPageModal';
48
+ export { default as HistoryList } from './HistoryList';
49
+ export type { HistoryListProps, HistoryStep } from './HistoryList';
50
+ export { default as Panel } from './Panel';
51
+ export type { PanelProps, PanelPadding } from './Panel';
52
+ export { default as Table } from './Table';
53
+ export type { TableProps, TableColumn, TableRow } from './Table';
54
+ export { default as Tag } from './Tag';
55
+ export type { TagProps, TagTone } from './Tag';
56
+ export { default as Text } from './Text';
57
+ export type { TextProps, TextVariant, TextTone, TextWeight } from './Text';
58
+ export { default as Title } from './Title';
59
+ export type { TitleProps, TitleLevel, TitleType } from './Title';
@@ -0,0 +1,155 @@
1
+ // spawn(): builds real DOM from the same props shape as Respawn's Spawn
2
+ // ({ tag, className, events, style, children, ...attrs }), with no dependency
3
+ // on Respawn. What it adds, so the output matches what React renders:
4
+ // - null, undefined, true and false children render nothing (React's rule);
5
+ // - any DOM node passes through (Respawn's Spawn passes only HTMLElements);
6
+ // - onClick and other on<Event> functions become event listeners, like
7
+ // `events: { click }`; both get (event, el);
8
+ // - style numbers get px the way React adds them (not for opacity, zIndex,
9
+ // flex… or custom properties), and styles go through el.style, as in React;
10
+ // - data-* and aria-* attributes write true/false as "true"/"false"; other
11
+ // attributes treat true as present and false as absent.
12
+
13
+ /** Something to render: text, a DOM node, nested spawn props, or a list of them. */
14
+ export type Child = string | number | boolean | null | undefined | Node | SpawnProps | Child[];
15
+
16
+ /** An event listener: gets the event and the element it is on. */
17
+ export type Listener<E extends Event = Event> = (event: E, el: HTMLElement) => void;
18
+
19
+ /** `events: { click: (event, el) => … }`, as in Respawn's Spawn. */
20
+ export type Events = { [K in keyof HTMLElementEventMap]?: Listener<HTMLElementEventMap[K]> } & {
21
+ [type: string]: Listener<any> | undefined;
22
+ };
23
+
24
+ /** Inline styles: camelCase or kebab-case names, custom properties (--x) too. Numbers get px where CSS needs a unit. */
25
+ export type Style = { [name: string]: string | number | null | undefined };
26
+
27
+ /** What every component's root element accepts besides its own props: HTML attributes and listeners. */
28
+ export interface ElementProps {
29
+ /** Your class name; it comes first: class="yours <component> nurvus". */
30
+ className?: string;
31
+ /** Inline styles, as an object or a CSS string. */
32
+ style?: Style | string;
33
+ /** Event listeners by event type, as in Respawn: `{ click: (event, el) => … }`. */
34
+ events?: Events;
35
+ /** on<Event> listeners, e.g. onClick, onMouseEnter. */
36
+ [handler: `on${string}`]: ((...args: any[]) => void) | undefined;
37
+ /** Any other HTML attribute: id, title, disabled, aria-label, data-*… */
38
+ [attribute: string]: unknown;
39
+ }
40
+
41
+ /** Respawn's props shape: the element to make, its attributes, listeners and children. */
42
+ export interface SpawnProps extends ElementProps {
43
+ /** The element to create. Defaults to 'div'. */
44
+ tag?: string;
45
+ /** What goes inside. A function is called with the element instead. */
46
+ children?: Child | ((el: HTMLElement) => void);
47
+ /** Appends the new element to this one. */
48
+ mountEl?: Element;
49
+ }
50
+
51
+ const SKIP = new Set(['tag', 'children', 'className', 'events', 'style', 'mountEl', 'key', 'ref']);
52
+ const RENAME: Record<string, string> = { htmlFor: 'for', acceptCharset: 'accept-charset', httpEquiv: 'http-equiv' };
53
+ // Attributes whose value is the text "true" / "false" (React writes these as such).
54
+ const BOOLEANISH = new Set(['draggable', 'spellcheck', 'contenteditable']);
55
+ // Style properties React leaves unitless when given a number.
56
+ const UNITLESS = new Set([
57
+ 'animationIterationCount', 'aspectRatio', 'borderImageOutset', 'borderImageSlice', 'borderImageWidth',
58
+ 'columnCount', 'columns', 'fillOpacity', 'flex', 'flexGrow', 'flexShrink', 'floodOpacity', 'fontWeight',
59
+ 'gridArea', 'gridColumn', 'gridColumnEnd', 'gridColumnStart', 'gridRow', 'gridRowEnd', 'gridRowStart',
60
+ 'lineClamp', 'lineHeight', 'opacity', 'order', 'orphans', 'scale', 'stopOpacity', 'strokeDasharray',
61
+ 'strokeDashoffset', 'strokeMiterlimit', 'strokeOpacity', 'strokeWidth', 'tabSize', 'widows', 'zIndex', 'zoom',
62
+ ]);
63
+
64
+ const isNode = (value: unknown): value is Node => typeof Node !== 'undefined' && value instanceof Node;
65
+
66
+ /** 'backgroundColor' -> 'background-color', 'WebkitTransition' -> '-webkit-transition'. */
67
+ const cssName = (name: string) => (name.startsWith('--') || name.includes('-')
68
+ ? name
69
+ : name.replace(/[A-Z]/g, (c) => `-${c.toLowerCase()}`).replace(/^(webkit|moz|ms|o)-/, '-$1-'));
70
+
71
+ function applyStyle(el: HTMLElement, style: Style | string) {
72
+ if (typeof style === 'string') {
73
+ el.setAttribute('style', style);
74
+ return;
75
+ }
76
+ Object.keys(style).forEach((name) => {
77
+ const value = style[name];
78
+ if (value === null || value === undefined || typeof value === 'boolean' || value === '') return;
79
+ const custom = name.startsWith('--');
80
+ const text = typeof value === 'number' && value !== 0 && !custom && !UNITLESS.has(name) ? `${value}px` : String(value).trim();
81
+ el.style.setProperty(cssName(name), text);
82
+ });
83
+ }
84
+
85
+ function applyAttribute(el: HTMLElement, key: string, value: unknown) {
86
+ if (value === null || value === undefined) return;
87
+ // onClick, onMouseEnter… : listeners, like events: { click, mouseenter }.
88
+ if (typeof value === 'function') {
89
+ if (/^on[A-Z]/.test(key)) {
90
+ const type = key === 'onDoubleClick' ? 'dblclick' : key.slice(2).toLowerCase();
91
+ el.addEventListener(type, (event) => value(event, el));
92
+ }
93
+ return;
94
+ }
95
+ if (typeof value === 'object') return;
96
+ const name = RENAME[key] || key;
97
+ const lower = name.toLowerCase();
98
+ if (typeof value === 'boolean') {
99
+ if (lower.startsWith('data-') || lower.startsWith('aria-') || BOOLEANISH.has(lower)) el.setAttribute(name, String(value));
100
+ else if (value) el.setAttribute(name, '');
101
+ return;
102
+ }
103
+ el.setAttribute(name, String(value));
104
+ }
105
+
106
+ /** Appends `child` (any Child) to `parent`. */
107
+ export function append(parent: Node, child: Child) {
108
+ if (child === null || child === undefined || typeof child === 'boolean') return;
109
+ if (Array.isArray(child)) {
110
+ child.forEach((c) => append(parent, c));
111
+ return;
112
+ }
113
+ parent.appendChild(toNode(child));
114
+ }
115
+
116
+ /** One Child as a single DOM node (a list becomes a fragment; nothing becomes an empty fragment). */
117
+ export function toNode(child: Child): Node {
118
+ if (child === null || child === undefined || typeof child === 'boolean') return document.createDocumentFragment();
119
+ if (typeof child === 'string' || typeof child === 'number') return document.createTextNode(String(child));
120
+ if (isNode(child)) return child;
121
+ if (Array.isArray(child)) {
122
+ const fragment = document.createDocumentFragment();
123
+ append(fragment, child);
124
+ return fragment;
125
+ }
126
+ return spawn(child);
127
+ }
128
+
129
+ /**
130
+ * Builds an element from Respawn-style props and returns it.
131
+ *
132
+ * spawn({ tag: 'p', className: 'note', children: ['Saved ', spawn({ tag: 'b', children: 'now' })] })
133
+ * spawn({ tag: 'button', events: { click: (event, el) => el.remove() }, children: 'Remove' })
134
+ *
135
+ * An element passed in comes back as it is, so components and your own
136
+ * elements mix freely in children.
137
+ */
138
+ export function spawn<E extends HTMLElement = HTMLElement>(props: SpawnProps | HTMLElement = {}): E {
139
+ if (isNode(props)) return props as E;
140
+ const { tag = 'div', children, className, events, style, mountEl } = props;
141
+ const el = document.createElement(tag) as E;
142
+ if (className) el.setAttribute('class', className);
143
+ Object.keys(props).forEach((key) => { if (!SKIP.has(key)) applyAttribute(el, key, props[key]); });
144
+ if (style) applyStyle(el, style);
145
+ if (events) {
146
+ Object.keys(events).forEach((type) => {
147
+ const listener = events[type];
148
+ if (listener) el.addEventListener(type, (event) => listener(event, el));
149
+ });
150
+ }
151
+ if (typeof children === 'function') children(el);
152
+ else append(el, children);
153
+ if (mountEl) mountEl.appendChild(el);
154
+ return el;
155
+ }
@@ -0,0 +1,49 @@
1
+ // How a component element updates, in Respawn's style (explicit re-creation):
2
+ //
3
+ // let tag = Tag({ tone: 'neutral', children: 'Draft' });
4
+ // tag = tag.update({ tone: 'success', children: 'Saved' });
5
+ //
6
+ // update(changes) merges the changes into the props the element was made
7
+ // with, builds a new element, swaps it in with replaceWith (what Respawn() does)
8
+ // and returns it. Keep the returned element. An older reference still works:
9
+ // its update() goes to the element that is on the page now.
10
+
11
+ /** An element a component returned: the DOM element itself, plus update(). */
12
+ export type NurvusElement<E extends HTMLElement, P> = E & {
13
+ /** Rebuilds it with `changes` merged into its props, swaps the new one into the page, and returns it. */
14
+ update(changes: Partial<P>): NurvusElement<E, P>;
15
+ };
16
+
17
+ /** Lets a render function save props without rebuilding (e.g. Menu's row click). */
18
+ export type Keep<P> = (changes: Partial<P>) => void;
19
+
20
+ interface Box<E extends HTMLElement, P> {
21
+ props: P;
22
+ el: NurvusElement<E, P> | null;
23
+ }
24
+
25
+ /**
26
+ * Wraps a render function so its element gets update(). A `mountEl` prop
27
+ * (as in Respawn's Spawn) appends the first element to it; updates replace it
28
+ * in place instead.
29
+ */
30
+ export function updatable<E extends HTMLElement, P extends object>(render: (props: P, keep: Keep<P>) => E, initial: P): NurvusElement<E, P> {
31
+ const { mountEl, ...props } = initial as P & { mountEl?: Element };
32
+ const box: Box<E, P> = { props: props as P, el: null };
33
+ const keep: Keep<P> = (changes) => { box.props = { ...box.props, ...changes }; };
34
+ const build = (): NurvusElement<E, P> => {
35
+ const el = render(box.props, keep) as NurvusElement<E, P>;
36
+ el.update = (changes) => {
37
+ keep(changes);
38
+ const previous = box.el;
39
+ const next = build();
40
+ if (previous && previous !== next) previous.replaceWith(next);
41
+ return next;
42
+ };
43
+ box.el = el;
44
+ return el;
45
+ };
46
+ const el = build();
47
+ if (mountEl) mountEl.appendChild(el);
48
+ return el;
49
+ }
package/src/spacing.ts ADDED
@@ -0,0 +1,23 @@
1
+ // Layout's spacing on the 4px grid. No React here, so the React Layout and the
2
+ // Respawn Layout share it.
3
+
4
+ /** A step of the 4px spacing grid (the --space--N tokens). */
5
+ export type Spacing = 0 | 2 | 4 | 8 | 12 | 16 | 24 | 32 | 48 | 64;
6
+ /** Values from before the grid. Still accepted: each snaps to a grid step, with a console warning. */
7
+ export type LegacySpacing = 10 | 13 | 20 | 30 | 40;
8
+
9
+ /** Where each pre-grid value lands: the nearest step, ties going down (20 → 16, 40 → 32). */
10
+ export const LEGACY_SPACING: Record<LegacySpacing, Spacing> = { 10: 8, 13: 12, 20: 16, 30: 32, 40: 32 };
11
+ const warned = new Set<number>();
12
+
13
+ /** The grid step to use for `value`; warns once per pre-grid value. */
14
+ export function toGrid(value: Spacing | LegacySpacing): Spacing {
15
+ const snapped = LEGACY_SPACING[value as LegacySpacing];
16
+ if (snapped === undefined) return value as Spacing;
17
+ if (!warned.has(value)) {
18
+ warned.add(value);
19
+ // eslint-disable-next-line no-console
20
+ console.warn(`@nurvus/ui Layout: itemSpacing={${value}} is off the 4px grid; using ${snapped}. Pass ${snapped} to keep this look.`);
21
+ }
22
+ return snapped;
23
+ }