@gopherium/godmin 0.8.0 → 0.9.0
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 +1 -1
- package/dist/admin-root.d.ts +1 -1
- package/dist/admin-root.js +4 -2
- package/dist/base.css +5 -0
- package/dist/frame.js +1 -1
- package/dist/index.d.ts +6 -0
- package/dist/index.js +3 -0
- package/dist/keys.d.ts +16 -0
- package/dist/keys.js +56 -0
- package/dist/row-keys.d.ts +32 -0
- package/dist/row-keys.js +130 -0
- package/dist/rows.d.ts +57 -0
- package/dist/rows.js +139 -0
- package/dist/testing.js +9 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -47,7 +47,7 @@ two lines do for you.
|
|
|
47
47
|
|
|
48
48
|
| Entry point | Contents |
|
|
49
49
|
| --- | --- |
|
|
50
|
-
| `@gopherium/godmin` | `AdminRoot`, `Frame`, `Page`, `PageTitle`, `NavScreen`, `ErrorNotice`, `LoadMore`, `LoadingScreen`, `LoadingRows`, `Toaster`, `useToaster`, `useMediaQuery`, `useTokenDocument`, the breakpoints, `SUPPORTED_WPDS` |
|
|
50
|
+
| `@gopherium/godmin` | `AdminRoot`, `Frame`, `Page`, `PageTitle`, `NavScreen`, `ErrorNotice`, `LoadMore`, `LoadingScreen`, `LoadingRows`, `Toaster`, `useToaster`, `RepeatRows`, `RowControls`, `useRowKeys`, `keyFromLabel`, `useMediaQuery`, `useTokenDocument`, the breakpoints, `SUPPORTED_WPDS` |
|
|
51
51
|
| `@gopherium/godmin/base.css` | Cascade layer order, design tokens, host rules, frame and screen styles |
|
|
52
52
|
| `@gopherium/godmin/router` | `useCanvas`, `useFrameLocation`, the `canvas` route static data |
|
|
53
53
|
| `@gopherium/godmin/testing` | `installTestEnvironment`, `renderAdmin`, `setViewport`, `getAnnouncement`, `clearAnnouncements`, `assertElementPatched`, `WPDS_IGNORE_SELECTOR` |
|
package/dist/admin-root.d.ts
CHANGED
|
@@ -10,5 +10,5 @@ export interface AdminRootProps extends ThemeSettings {
|
|
|
10
10
|
* @param props - The theme settings, root element type and children.
|
|
11
11
|
* @returns The host element wrapping the themed children.
|
|
12
12
|
*/
|
|
13
|
-
export declare function AdminRoot({ children, as: Root, ...theme }: AdminRootProps): import("react").JSX.Element;
|
|
13
|
+
export declare function AdminRoot({ children, as: Root, color, ...theme }: AdminRootProps): import("react").JSX.Element;
|
|
14
14
|
export {};
|
package/dist/admin-root.js
CHANGED
|
@@ -2,12 +2,14 @@ import { jsx as _jsx } from "react/jsx-runtime";
|
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
import { ThemeProvider } from '@wordpress/theme';
|
|
4
4
|
import { useEnableWpCompatOverlaySlot } from '@wordpress/ui';
|
|
5
|
+
/** The design system's own primary color, seeded when an application names none. */
|
|
6
|
+
const DEFAULT_COLOR = { primary: '#3858e9' };
|
|
5
7
|
/**
|
|
6
8
|
* Hosts a design system application in its own stacking context.
|
|
7
9
|
* @param props - The theme settings, root element type and children.
|
|
8
10
|
* @returns The host element wrapping the themed children.
|
|
9
11
|
*/
|
|
10
|
-
export function AdminRoot({ children, as: Root = 'div', ...theme }) {
|
|
12
|
+
export function AdminRoot({ children, as: Root = 'div', color = DEFAULT_COLOR, ...theme }) {
|
|
11
13
|
useEnableWpCompatOverlaySlot();
|
|
12
|
-
return (_jsx(Root, { style: { isolation: 'isolate' }, children: _jsx(ThemeProvider, { ...theme, isRoot: true, children: children }) }));
|
|
14
|
+
return (_jsx(Root, { style: { isolation: 'isolate' }, children: _jsx(ThemeProvider, { color: color, ...theme, isRoot: true, children: children }) }));
|
|
13
15
|
}
|
package/dist/base.css
CHANGED
|
@@ -220,6 +220,11 @@ body {
|
|
|
220
220
|
border-radius: var(--wpds-border-radius-sm);
|
|
221
221
|
}
|
|
222
222
|
|
|
223
|
+
.godmin-rows__row:focus-visible {
|
|
224
|
+
outline: var(--wpds-border-width-focus) solid var(--wpds-color-stroke-focus);
|
|
225
|
+
outline-offset: 2px;
|
|
226
|
+
}
|
|
227
|
+
|
|
223
228
|
/* Small viewport overrides live together at the end of the sheet, so they
|
|
224
229
|
always win over the rules above them without extra specificity. */
|
|
225
230
|
@media (max-width: 639px) {
|
package/dist/frame.js
CHANGED
|
@@ -42,7 +42,7 @@ function Rail({ children, brand, menuLabel = 'Open navigation' }) {
|
|
|
42
42
|
* @param props - The rail content, the brand, and the menu button label.
|
|
43
43
|
* @returns The top bar element.
|
|
44
44
|
*/
|
|
45
|
-
function TopBar({ children, brand, menuLabel }) {
|
|
45
|
+
function TopBar({ children, brand, menuLabel, }) {
|
|
46
46
|
const { location, layout } = useContext(FrameContext);
|
|
47
47
|
const [open, setOpen] = useState(false);
|
|
48
48
|
useEffect(() => {
|
package/dist/index.d.ts
CHANGED
|
@@ -3,11 +3,17 @@ export type { AdminRootProps } from './admin-root.js';
|
|
|
3
3
|
export { DENSE_BREAKPOINT, RAIL_BREAKPOINT, SMALL_VIEWPORT } from './breakpoints.js';
|
|
4
4
|
export { Frame } from './frame.js';
|
|
5
5
|
export type { CanvasMode, FrameCanvasProps, FrameRailProps, FrameRootProps, } from './frame.js';
|
|
6
|
+
export { keyFromLabel } from './keys.js';
|
|
7
|
+
export type { KeyOptions, KeyStyle } from './keys.js';
|
|
6
8
|
export { LoadingRows, LoadingScreen } from './loading.js';
|
|
7
9
|
export { NavScreen } from './nav-screen.js';
|
|
8
10
|
export type { NavScreenProps } from './nav-screen.js';
|
|
9
11
|
export { ErrorNotice, LoadMore, Page, PageTitle } from './page.js';
|
|
10
12
|
export type { LoadMoreQuery, PageProps } from './page.js';
|
|
13
|
+
export { useRowKeys } from './row-keys.js';
|
|
14
|
+
export type { RowBounds, RowKeys } from './row-keys.js';
|
|
15
|
+
export { RepeatRows, RowControls } from './rows.js';
|
|
16
|
+
export type { RepeatRowsProps, RowControlsProps, RowLabels } from './rows.js';
|
|
11
17
|
export { Toaster, useToaster } from './toaster.js';
|
|
12
18
|
export type { ToastAction, ToasterHandle, ToasterProps } from './toaster.js';
|
|
13
19
|
export { useMediaQuery } from './use-media-query.js';
|
package/dist/index.js
CHANGED
|
@@ -2,9 +2,12 @@
|
|
|
2
2
|
export { AdminRoot } from './admin-root.js';
|
|
3
3
|
export { DENSE_BREAKPOINT, RAIL_BREAKPOINT, SMALL_VIEWPORT } from './breakpoints.js';
|
|
4
4
|
export { Frame } from './frame.js';
|
|
5
|
+
export { keyFromLabel } from './keys.js';
|
|
5
6
|
export { LoadingRows, LoadingScreen } from './loading.js';
|
|
6
7
|
export { NavScreen } from './nav-screen.js';
|
|
7
8
|
export { ErrorNotice, LoadMore, Page, PageTitle } from './page.js';
|
|
9
|
+
export { useRowKeys } from './row-keys.js';
|
|
10
|
+
export { RepeatRows, RowControls } from './rows.js';
|
|
8
11
|
export { Toaster, useToaster } from './toaster.js';
|
|
9
12
|
export { useMediaQuery } from './use-media-query.js';
|
|
10
13
|
export { useTokenDocument } from './use-token-document.js';
|
package/dist/keys.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** How a key joins its words: camel gives birthDate, kebab gives birth-date. */
|
|
2
|
+
export type KeyStyle = 'camel' | 'kebab';
|
|
3
|
+
/** What turning a label into a key needs besides the label. */
|
|
4
|
+
export interface KeyOptions {
|
|
5
|
+
/** How the key joins its words. */
|
|
6
|
+
style: KeyStyle;
|
|
7
|
+
/** The keys already in use, which the new key must not repeat. */
|
|
8
|
+
taken?: Iterable<string>;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Returns a key made from a label in a style, with the first free number added when the key is taken.
|
|
12
|
+
* @param label - The label the operator typed.
|
|
13
|
+
* @param options - The key style and the keys already in use.
|
|
14
|
+
* @returns The key.
|
|
15
|
+
*/
|
|
16
|
+
export declare function keyFromLabel(label: string, { style, taken }: KeyOptions): string;
|
package/dist/keys.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
/** The word a key starts with when the label gives none, or starts with a digit. */
|
|
3
|
+
const LEAD = 'field';
|
|
4
|
+
/** The plain letters a key spells each listed lowercase Latin letter with, since none of them decomposes. */
|
|
5
|
+
const FOLDS = {
|
|
6
|
+
ß: 'ss', æ: 'ae', œ: 'oe', ø: 'o', ł: 'l', ı: 'i', đ: 'd', ð: 'd', þ: 'th', ħ: 'h', ŧ: 't', ƒ: 'f', ə: 'e', ŋ: 'n',
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Returns a label's lowercase words in a to z, apostrophes, middle dots and invisible characters joining a word.
|
|
10
|
+
* @param label - The label to split.
|
|
11
|
+
* @returns The words in order.
|
|
12
|
+
*/
|
|
13
|
+
function wordsOf(label) {
|
|
14
|
+
const plain = label
|
|
15
|
+
.replace(/[´ʻ]/g, '')
|
|
16
|
+
.replace(/(?<!\p{L})[ºª]|[™℠ℹⓂ]/gu, ' ')
|
|
17
|
+
.replace(/(?<=\p{Nd})\p{No}+/gu, ' ')
|
|
18
|
+
.normalize('NFKD')
|
|
19
|
+
.replace(/[\p{M}\p{Default_Ignorable_Code_Point}]/gu, '')
|
|
20
|
+
.toLowerCase()
|
|
21
|
+
.replace(/['‘’ʼ`·]/g, '')
|
|
22
|
+
.replace(/[ßæœøłıđðþħŧƒəŋ]/g, (letter) => FOLDS[letter]);
|
|
23
|
+
return plain.split(/[^a-z0-9]+/).filter((word) => word !== '');
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Returns the words joined in a key style.
|
|
27
|
+
* @param words - The words to join.
|
|
28
|
+
* @param style - How to join them.
|
|
29
|
+
* @returns The joined key.
|
|
30
|
+
*/
|
|
31
|
+
function joined(words, style) {
|
|
32
|
+
if (style === 'kebab') {
|
|
33
|
+
return words.join('-');
|
|
34
|
+
}
|
|
35
|
+
return words.map((word, at) => (at === 0 ? word : word.charAt(0).toUpperCase() + word.slice(1))).join('');
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Returns a key made from a label in a style, with the first free number added when the key is taken.
|
|
39
|
+
* @param label - The label the operator typed.
|
|
40
|
+
* @param options - The key style and the keys already in use.
|
|
41
|
+
* @returns The key.
|
|
42
|
+
*/
|
|
43
|
+
export function keyFromLabel(label, { style, taken = [] }) {
|
|
44
|
+
const words = wordsOf(label);
|
|
45
|
+
if (words.length === 0 || /^\d/.test(words[0])) {
|
|
46
|
+
words.unshift(LEAD);
|
|
47
|
+
}
|
|
48
|
+
const stem = joined(words, style);
|
|
49
|
+
const used = new Set(taken);
|
|
50
|
+
const joiner = style === 'kebab' ? '-' : '';
|
|
51
|
+
let key = stem;
|
|
52
|
+
for (let number = 2; used.has(key); number++) {
|
|
53
|
+
key = `${stem}${joiner}${number}`;
|
|
54
|
+
}
|
|
55
|
+
return key;
|
|
56
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/** The identities of a list's rows and the operations that change the list, each ignoring a key no row holds. */
|
|
2
|
+
export interface RowKeys<T> {
|
|
3
|
+
/** One key per row, in row order, that a row keeps through every operation and every list still holding it. */
|
|
4
|
+
keys: readonly number[];
|
|
5
|
+
/** Whether an add would append a row now. */
|
|
6
|
+
canAdd: boolean;
|
|
7
|
+
/** Whether a removal would take a row away now. */
|
|
8
|
+
canRemove: boolean;
|
|
9
|
+
/** Appends a row with a fresh key, doing nothing at the most rows allowed. */
|
|
10
|
+
add: (row: T) => void;
|
|
11
|
+
/** Replaces the row holding a key. */
|
|
12
|
+
update: (key: number, row: T) => void;
|
|
13
|
+
/** Moves the row holding a key by an offset, doing nothing past either end. */
|
|
14
|
+
move: (key: number, offset: number) => void;
|
|
15
|
+
/** Takes away the row holding a key, doing nothing at the fewest rows allowed. */
|
|
16
|
+
remove: (key: number) => void;
|
|
17
|
+
}
|
|
18
|
+
/** The fewest rows a removal leaves and the most an add reaches, neither applied to the rows handed in. */
|
|
19
|
+
export interface RowBounds {
|
|
20
|
+
/** The fewest rows a removal leaves, none by default. */
|
|
21
|
+
min?: number;
|
|
22
|
+
/** The most rows an add reaches, no limit by default. */
|
|
23
|
+
max?: number;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Returns a key per row that survives every change, and the operations that change the list by key.
|
|
27
|
+
* @param rows - The rows the list holds, each known by its identity.
|
|
28
|
+
* @param onChange - What to call with a copy of the rows a change leaves, set right away and never in a transition.
|
|
29
|
+
* @param bounds - The fewest rows a removal leaves and the most rows an add reaches.
|
|
30
|
+
* @returns The keys, whether an add or a removal would do anything, and the operations.
|
|
31
|
+
*/
|
|
32
|
+
export declare function useRowKeys<T>(rows: readonly T[], onChange: (rows: T[]) => void, { min, max }?: RowBounds): RowKeys<T>;
|
package/dist/row-keys.js
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
import { useInsertionEffect, useReducer, useRef, useState } from 'react';
|
|
3
|
+
/**
|
|
4
|
+
* Returns the list with the item at one place moved by an offset.
|
|
5
|
+
* @param list - The list to move within.
|
|
6
|
+
* @param at - The place of the item to move.
|
|
7
|
+
* @param offset - How far to move it.
|
|
8
|
+
* @returns The reordered list.
|
|
9
|
+
*/
|
|
10
|
+
function movedIn(list, at, offset) {
|
|
11
|
+
const moved = [...list];
|
|
12
|
+
const [item] = moved.splice(at, 1);
|
|
13
|
+
moved.splice(at + offset, 0, item);
|
|
14
|
+
return moved;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Returns a copy of a list with a key per row, each row taking a key it held before and any other row a fresh one.
|
|
18
|
+
* @param rows - The list to key.
|
|
19
|
+
* @param known - The lists keyed before, the one to trust first leading.
|
|
20
|
+
* @returns The copy and its keys.
|
|
21
|
+
*/
|
|
22
|
+
function keyed(rows, known) {
|
|
23
|
+
const held = new Map();
|
|
24
|
+
for (const list of known) {
|
|
25
|
+
list.rows.forEach((row, at) => held.set(row, [...(held.get(row) ?? []), list.keys[at]]));
|
|
26
|
+
}
|
|
27
|
+
const used = new Set();
|
|
28
|
+
let next = Math.max(0, ...known.map((list) => list.next));
|
|
29
|
+
const keys = rows.map((row) => {
|
|
30
|
+
const key = held.get(row)?.find((candidate) => !used.has(candidate)) ?? next++;
|
|
31
|
+
used.add(key);
|
|
32
|
+
return key;
|
|
33
|
+
});
|
|
34
|
+
return { rows: [...rows], keys, next };
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Reports whether a keyed list holds the same rows in the same order as a list.
|
|
38
|
+
* @param list - The keyed list.
|
|
39
|
+
* @param rows - The list to compare.
|
|
40
|
+
* @returns Whether both hold the same rows.
|
|
41
|
+
*/
|
|
42
|
+
function holds(list, rows) {
|
|
43
|
+
return list.rows.length === rows.length && list.rows.every((row, at) => Object.is(row, rows[at]));
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Returns the rows to show with their keys.
|
|
47
|
+
* @param lists - The rows last shown and last handed to the owner.
|
|
48
|
+
* @param rows - The rows the owner holds now.
|
|
49
|
+
* @param given - The rows the owner handed in when the list was last shown.
|
|
50
|
+
* @returns The rows and their keys.
|
|
51
|
+
*/
|
|
52
|
+
function shownFor({ shown, sent }, rows, given) {
|
|
53
|
+
const trusted = rows === given ? [shown, sent] : [sent, shown];
|
|
54
|
+
return trusted.find((list) => holds(list, rows)) ?? keyed(rows, [sent, shown]);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Returns the operations that change a list by key, each building on the latest rows and handing the owner a copy.
|
|
58
|
+
* @param lists - The rows last shown and last handed to the owner.
|
|
59
|
+
* @param latest - The rows the next operation builds on and the props it answers to.
|
|
60
|
+
* @param rendered - What renders the host again.
|
|
61
|
+
* @returns The operations adding, replacing, moving and removing a row.
|
|
62
|
+
*/
|
|
63
|
+
function operationsOn(lists, latest, rendered) {
|
|
64
|
+
const emit = (held) => {
|
|
65
|
+
lists.current = { ...lists.current, sent: held };
|
|
66
|
+
latest.current = { ...latest.current, held };
|
|
67
|
+
rendered();
|
|
68
|
+
latest.current.onChange([...held.rows]);
|
|
69
|
+
};
|
|
70
|
+
const placeOf = (key) => latest.current.held.keys.indexOf(key);
|
|
71
|
+
return {
|
|
72
|
+
add: (row) => {
|
|
73
|
+
const { rows: before, keys, next } = latest.current.held;
|
|
74
|
+
if (before.length < latest.current.max) {
|
|
75
|
+
emit({ rows: [...before, row], keys: [...keys, next], next: next + 1 });
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
update: (key, row) => {
|
|
79
|
+
const at = placeOf(key);
|
|
80
|
+
const held = latest.current.held;
|
|
81
|
+
if (at >= 0) {
|
|
82
|
+
emit({ ...held, rows: held.rows.map((kept, place) => (place === at ? row : kept)) });
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
move: (key, offset) => {
|
|
86
|
+
const at = placeOf(key);
|
|
87
|
+
const held = latest.current.held;
|
|
88
|
+
if (at >= 0 && at + offset >= 0 && at + offset < held.rows.length) {
|
|
89
|
+
emit({ ...held, rows: movedIn(held.rows, at, offset), keys: movedIn(held.keys, at, offset) });
|
|
90
|
+
}
|
|
91
|
+
},
|
|
92
|
+
remove: (key) => {
|
|
93
|
+
const at = placeOf(key);
|
|
94
|
+
const held = latest.current.held;
|
|
95
|
+
if (at >= 0 && held.rows.length > latest.current.min) {
|
|
96
|
+
const kept = (_, place) => place !== at;
|
|
97
|
+
emit({ ...held, rows: held.rows.filter(kept), keys: held.keys.filter(kept) });
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Returns a key per row that survives every change, and the operations that change the list by key.
|
|
104
|
+
* @param rows - The rows the list holds, each known by its identity.
|
|
105
|
+
* @param onChange - What to call with a copy of the rows a change leaves, set right away and never in a transition.
|
|
106
|
+
* @param bounds - The fewest rows a removal leaves and the most rows an add reaches.
|
|
107
|
+
* @returns The keys, whether an add or a removal would do anything, and the operations.
|
|
108
|
+
*/
|
|
109
|
+
export function useRowKeys(rows, onChange, { min = 0, max = Infinity } = {}) {
|
|
110
|
+
const [first] = useState(() => keyed(rows, []));
|
|
111
|
+
const lists = useRef({ shown: first, sent: first });
|
|
112
|
+
const given = useRef(rows);
|
|
113
|
+
const [, rendered] = useReducer((count) => count + 1, 0);
|
|
114
|
+
const current = shownFor(lists.current, rows, given.current);
|
|
115
|
+
const latest = useRef({ held: current, onChange, min, max });
|
|
116
|
+
const [operations] = useState(() => operationsOn(lists, latest, rendered));
|
|
117
|
+
useInsertionEffect(() => {
|
|
118
|
+
given.current = rows;
|
|
119
|
+
if (current !== lists.current.shown) {
|
|
120
|
+
lists.current = { shown: current, sent: current };
|
|
121
|
+
}
|
|
122
|
+
latest.current = { held: current, onChange, min, max };
|
|
123
|
+
});
|
|
124
|
+
return {
|
|
125
|
+
keys: current.keys,
|
|
126
|
+
canAdd: current.rows.length < max,
|
|
127
|
+
canRemove: current.rows.length > min,
|
|
128
|
+
...operations,
|
|
129
|
+
};
|
|
130
|
+
}
|
package/dist/rows.d.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
import type { RowBounds } from './row-keys.js';
|
|
3
|
+
/** The words a rows editor shows, each supplied by the application. */
|
|
4
|
+
export interface RowLabels {
|
|
5
|
+
/** The button that appends a row. */
|
|
6
|
+
add: string;
|
|
7
|
+
/** The message shown when the list holds no rows. */
|
|
8
|
+
empty: string;
|
|
9
|
+
/** The button that moves a row up. */
|
|
10
|
+
moveUp: string;
|
|
11
|
+
/** The button that moves a row down. */
|
|
12
|
+
moveDown: string;
|
|
13
|
+
/** The button that takes a row away. */
|
|
14
|
+
remove: string;
|
|
15
|
+
}
|
|
16
|
+
/** What the controls of one row need. */
|
|
17
|
+
export interface RowControlsProps {
|
|
18
|
+
/** The row's place in the list. */
|
|
19
|
+
at: number;
|
|
20
|
+
/** How many rows the list holds. */
|
|
21
|
+
count: number;
|
|
22
|
+
/** Whether the row may be taken away, true by default. */
|
|
23
|
+
removable?: boolean;
|
|
24
|
+
/** The words the controls show. */
|
|
25
|
+
labels: Pick<RowLabels, 'moveUp' | 'moveDown' | 'remove'>;
|
|
26
|
+
/** What to call with the offset a move asks for. */
|
|
27
|
+
onMove: (offset: number) => void;
|
|
28
|
+
/** What to call when the row is taken away. */
|
|
29
|
+
onRemove: () => void;
|
|
30
|
+
}
|
|
31
|
+
/** What a rows editor needs. */
|
|
32
|
+
export interface RepeatRowsProps<T> extends RowBounds {
|
|
33
|
+
/** The rows the list holds, each known by its identity. */
|
|
34
|
+
rows: readonly T[];
|
|
35
|
+
/** What to call with a copy of the rows a change leaves, set right away and never in a transition. */
|
|
36
|
+
onChange: (rows: T[]) => void;
|
|
37
|
+
/** Returns the row an added row starts as. */
|
|
38
|
+
blank: () => T;
|
|
39
|
+
/** Renders the inputs of one row, with what to call when the row changes and the row's place in the list. */
|
|
40
|
+
renderRow: (row: T, update: (row: T) => void, at: number) => ReactNode;
|
|
41
|
+
/** Returns the name a row's group is announced by. */
|
|
42
|
+
rowLabel: (at: number) => string;
|
|
43
|
+
/** The words the editor shows. */
|
|
44
|
+
labels: RowLabels;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Renders the buttons that move a row among its siblings and take it away.
|
|
48
|
+
* @param props - The row's place, the list's length, the words and what to call on a change.
|
|
49
|
+
* @returns The controls element.
|
|
50
|
+
*/
|
|
51
|
+
export declare function RowControls({ at, count, removable, labels, onMove, onRemove }: RowControlsProps): import("react").JSX.Element;
|
|
52
|
+
/**
|
|
53
|
+
* Renders one group of inputs per row with controls to add, move and remove rows.
|
|
54
|
+
* @param props - The rows, what to call with a change, how to render a row, the words and the bounds.
|
|
55
|
+
* @returns The rows editor element.
|
|
56
|
+
*/
|
|
57
|
+
export declare function RepeatRows<T>({ rows, onChange, blank, renderRow, rowLabel, labels, min, max, }: RepeatRowsProps<T>): import("react").JSX.Element;
|
package/dist/rows.js
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import { Button, IconButton, Stack, Text } from '@wordpress/ui';
|
|
4
|
+
import { memo, useCallback, useLayoutEffect, useRef, useState } from 'react';
|
|
5
|
+
import { useRowKeys } from './row-keys.js';
|
|
6
|
+
const upIcon = (_jsx("svg", { xmlns: "http://www.w3.org/2000/svg", viewBox: "0 0 24 24", fill: "currentColor", children: _jsx("path", { d: "M12 8l-6 6 1.41 1.41L12 10.83l4.59 4.58L18 14z" }) }));
|
|
7
|
+
const downIcon = (_jsx("svg", { xmlns: "http://www.w3.org/2000/svg", viewBox: "0 0 24 24", fill: "currentColor", children: _jsx("path", { d: "M16.59 8.59L12 13.17 7.41 8.59 6 10l6 6 6-6z" }) }));
|
|
8
|
+
/**
|
|
9
|
+
* Renders the buttons that move a row among its siblings and take it away.
|
|
10
|
+
* @param props - The row's place, the list's length, the words and what to call on a change.
|
|
11
|
+
* @returns The controls element.
|
|
12
|
+
*/
|
|
13
|
+
export function RowControls({ at, count, removable = true, labels, onMove, onRemove }) {
|
|
14
|
+
return (_jsxs(Stack, { direction: "row", gap: "xs", align: "center", children: [_jsx(IconButton, { icon: upIcon, label: labels.moveUp, size: "compact", variant: "minimal", tone: "neutral", disabled: at === 0, onClick: () => onMove(-1) }), _jsx(IconButton, { icon: downIcon, label: labels.moveDown, size: "compact", variant: "minimal", tone: "neutral", disabled: at === count - 1, onClick: () => onMove(1) }), _jsx(Button, { variant: "outline", size: "compact", disabled: !removable, onClick: onRemove, children: labels.remove })] }));
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Returns where focus goes after the rows shown change.
|
|
18
|
+
* @param change - The keys before and after, the row that held focus as it went, the add press and the holder.
|
|
19
|
+
* @returns The key of the row to focus, the add button, or undefined to leave focus alone.
|
|
20
|
+
*/
|
|
21
|
+
function focusTarget({ before, keys, lost, adding, holder }) {
|
|
22
|
+
if (holder === 'none' && lost !== null) {
|
|
23
|
+
return keys[Math.min(before.indexOf(lost), keys.length - 1)] ?? 'add';
|
|
24
|
+
}
|
|
25
|
+
if (holder !== 'add' || !adding) {
|
|
26
|
+
return undefined;
|
|
27
|
+
}
|
|
28
|
+
const fresh = keys.filter((key) => !before.includes(key));
|
|
29
|
+
return fresh.length > 0 ? Math.max(...fresh) : undefined;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Returns what holds focus now.
|
|
33
|
+
* @param adder - The add button.
|
|
34
|
+
* @returns Nothing, the add button, or anything else.
|
|
35
|
+
*/
|
|
36
|
+
function holderOf(adder) {
|
|
37
|
+
const active = document.activeElement;
|
|
38
|
+
if (active === null || active === document.body) {
|
|
39
|
+
return 'none';
|
|
40
|
+
}
|
|
41
|
+
return active === adder ? 'add' : 'other';
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Scrolls the focused element into view when the row holding it changed place.
|
|
45
|
+
* @param before - The keys shown before the change.
|
|
46
|
+
* @param keys - The keys shown now.
|
|
47
|
+
* @param entered - The row that last took focus and the element in it that did.
|
|
48
|
+
*/
|
|
49
|
+
function keepInView(before, keys, entered) {
|
|
50
|
+
if (entered === null || entered.target !== document.activeElement) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
if (before.indexOf(entered.key) !== keys.indexOf(entered.key)) {
|
|
54
|
+
const focused = entered.target;
|
|
55
|
+
focused.scrollIntoView?.({ block: 'nearest' });
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Returns the handlers that record the row groups, the focus inside them and the add press.
|
|
60
|
+
* @param groups - The group of each row shown, by key.
|
|
61
|
+
* @param record - The row that last took focus and the element in it that did.
|
|
62
|
+
* @param lost - The key of the row that held focus as it went.
|
|
63
|
+
* @param adding - Whether the add button was pressed since the last commit.
|
|
64
|
+
* @returns The handlers.
|
|
65
|
+
*/
|
|
66
|
+
function focusHandlers(groups, record, lost, adding) {
|
|
67
|
+
return {
|
|
68
|
+
register: (key, element) => {
|
|
69
|
+
if (element !== null) {
|
|
70
|
+
groups.current.set(key, element);
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
if (record.current?.key === key && record.current.target === document.activeElement) {
|
|
74
|
+
lost.current = key;
|
|
75
|
+
}
|
|
76
|
+
groups.current.delete(key);
|
|
77
|
+
},
|
|
78
|
+
entered: (key, target) => {
|
|
79
|
+
record.current = { key, target };
|
|
80
|
+
},
|
|
81
|
+
pressed: () => {
|
|
82
|
+
adding.current = true;
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Returns what moves focus to a neighbour when the focused row goes, and into the row an add made.
|
|
88
|
+
* @param keys - The keys of the rows shown.
|
|
89
|
+
* @returns The refs and handlers to wire into the rows and the add button.
|
|
90
|
+
*/
|
|
91
|
+
function useRowFocus(keys) {
|
|
92
|
+
const groups = useRef(new Map());
|
|
93
|
+
const adder = useRef(null);
|
|
94
|
+
const record = useRef(null);
|
|
95
|
+
const lost = useRef(null);
|
|
96
|
+
const adding = useRef(false);
|
|
97
|
+
const placed = useRef(keys);
|
|
98
|
+
const [handlers] = useState(() => focusHandlers(groups, record, lost, adding));
|
|
99
|
+
useLayoutEffect(() => {
|
|
100
|
+
const holder = holderOf(adder.current);
|
|
101
|
+
const before = placed.current;
|
|
102
|
+
const target = focusTarget({ before, keys, lost: lost.current, adding: adding.current, holder });
|
|
103
|
+
placed.current = keys;
|
|
104
|
+
lost.current = null;
|
|
105
|
+
adding.current = false;
|
|
106
|
+
if (target === undefined) {
|
|
107
|
+
keepInView(before, keys, record.current);
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
(target === 'add' ? adder.current : groups.current.get(target))?.focus();
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
return { ...handlers, adder };
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Renders the group of one row, its inputs and its controls.
|
|
117
|
+
* @param props - The row, its key and place, the list's length, the words and what changes the list.
|
|
118
|
+
* @returns The row's group element.
|
|
119
|
+
*/
|
|
120
|
+
function RowGroup({ rowKey, row, at, count, removable, labels, renderRow, rowLabel, update, move, remove, register, entered, }) {
|
|
121
|
+
const changed = useCallback((next) => update(rowKey, next), [update, rowKey]);
|
|
122
|
+
const ref = useCallback((element) => register(rowKey, element), [register, rowKey]);
|
|
123
|
+
return (_jsx("div", { ref: ref, role: "group", "aria-label": rowLabel(at), tabIndex: -1, className: "godmin-rows__row", onFocus: (event) => entered(rowKey, event.target), children: _jsxs(Stack, { direction: "column", gap: "xs", children: [renderRow(row, changed, at), _jsx(RowControls, { at: at, count: count, removable: removable, labels: labels, onMove: (offset) => move(rowKey, offset), onRemove: () => remove(rowKey) })] }) }));
|
|
124
|
+
}
|
|
125
|
+
/** The group of one row, rendered again only when its own props change. */
|
|
126
|
+
const Row = memo(RowGroup);
|
|
127
|
+
/**
|
|
128
|
+
* Renders one group of inputs per row with controls to add, move and remove rows.
|
|
129
|
+
* @param props - The rows, what to call with a change, how to render a row, the words and the bounds.
|
|
130
|
+
* @returns The rows editor element.
|
|
131
|
+
*/
|
|
132
|
+
export function RepeatRows({ rows, onChange, blank, renderRow, rowLabel, labels, min, max, }) {
|
|
133
|
+
const { keys, canAdd, canRemove, add, update, move, remove } = useRowKeys(rows, onChange, { min, max });
|
|
134
|
+
const focus = useRowFocus(keys);
|
|
135
|
+
return (_jsxs(Stack, { direction: "column", gap: "md", className: "godmin-rows", children: [rows.length === 0 ? _jsx(Text, { children: labels.empty }) : null, keys.map((key, at) => (_jsx(Row, { rowKey: key, row: rows[at], at: at, count: rows.length, removable: canRemove, labels: labels, renderRow: renderRow, rowLabel: rowLabel, update: update, move: move, remove: remove, register: focus.register, entered: focus.entered }, key))), _jsx("div", { children: _jsx(Button, { ref: focus.adder, variant: "outline", disabled: !canAdd, onClick: () => {
|
|
136
|
+
focus.pressed();
|
|
137
|
+
add(blank());
|
|
138
|
+
}, children: labels.add }) })] }));
|
|
139
|
+
}
|
package/dist/testing.js
CHANGED
|
@@ -129,8 +129,17 @@ function installFeatureDetection() {
|
|
|
129
129
|
function installResizeObserver() {
|
|
130
130
|
if (typeof globalThis.ResizeObserver !== 'function') {
|
|
131
131
|
globalThis.ResizeObserver = class {
|
|
132
|
+
/**
|
|
133
|
+
* Watches an element.
|
|
134
|
+
*/
|
|
132
135
|
observe() { }
|
|
136
|
+
/**
|
|
137
|
+
* Stops watching an element.
|
|
138
|
+
*/
|
|
133
139
|
unobserve() { }
|
|
140
|
+
/**
|
|
141
|
+
* Stops watching every element.
|
|
142
|
+
*/
|
|
134
143
|
disconnect() { }
|
|
135
144
|
};
|
|
136
145
|
}
|
package/package.json
CHANGED