@surreal-ui/catalog 0.9.1 → 0.10.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/dist/{chunk-G2FSYMIB.js → chunk-NROGLREK.js} +761 -4
- package/dist/components.css +5 -0
- package/dist/index.js +1 -1
- package/dist/lazy.js +1 -1
- package/dist/react/editor.d.ts +129 -1
- package/dist/react/editor.js +529 -31
- package/dist/sources/bar-chart/react.tsx +15 -13
- package/dist/sources/braid/react.tsx +174 -0
- package/dist/sources/donut-chart/react.tsx +65 -0
- package/dist/sources/line-chart/react.tsx +79 -0
- package/dist/sources/lineage/react.tsx +169 -0
- package/dist/sources/sparkline/react.tsx +12 -13
- package/dist/sources/weave/react.tsx +165 -0
- package/llms.txt +5 -0
- package/package.json +3 -3
- package/skills/surreal-use/references/components.md +5 -0
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
// Generated by packages/catalog/scripts/sync-providers.ts from @pixerate/editor 0.322.0 (braid). Do not edit.
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import { uiComponents } from '@pixerate/editor/chart';
|
|
4
|
+
import { createLinkFocus } from '@pixerate/editor/tools';
|
|
5
|
+
import { useRoleColors } from '../../packages/catalog/src/runtime/role-colors';
|
|
6
|
+
|
|
7
|
+
const ROLES = ['surface', 'ink', 'accent'] as const;
|
|
8
|
+
const COMPONENT = uiComponents().find((c) => c.id === 'braid')!;
|
|
9
|
+
const VISUALLY_HIDDEN: React.CSSProperties = {
|
|
10
|
+
position: 'absolute',
|
|
11
|
+
width: 1,
|
|
12
|
+
height: 1,
|
|
13
|
+
overflow: 'hidden',
|
|
14
|
+
clip: 'rect(0 0 0 0)',
|
|
15
|
+
whiteSpace: 'nowrap'
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export interface BraidProps {
|
|
19
|
+
/** What the strands twist into, labelled at the braid's start. */
|
|
20
|
+
root?: string;
|
|
21
|
+
/** The strands, each labelled at the braid's open end. */
|
|
22
|
+
strands: { name: string; value?: number }[];
|
|
23
|
+
/** Whole twists along the braid (default 2.5). */
|
|
24
|
+
turns?: number;
|
|
25
|
+
/** Accessible name (default: built from the data). */
|
|
26
|
+
title?: string;
|
|
27
|
+
/** Controlled focus; leave out for the component to keep its own. */
|
|
28
|
+
focus?: string | null;
|
|
29
|
+
onFocusChange?: (focus: string | null) => void;
|
|
30
|
+
/** Draw-on progress, 0–1 (1 is the picture at rest). Leave out to draw on once when shown (not under reduced motion). */
|
|
31
|
+
progress?: number;
|
|
32
|
+
/** Draw on when shown (default true). Ignored when `progress` is given. */
|
|
33
|
+
animate?: boolean;
|
|
34
|
+
/** Length of the draw-on in ms (default 1600). */
|
|
35
|
+
duration?: number;
|
|
36
|
+
/** Width in CSS px. */
|
|
37
|
+
width?: number;
|
|
38
|
+
/** Height in CSS px. */
|
|
39
|
+
height?: number;
|
|
40
|
+
className?: string;
|
|
41
|
+
style?: React.CSSProperties;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Strands twisted into one root, passing over and under, weighted by value and labelled at the open end; draws on along the braid, and focusing a strand lights it. (editor `braid`, coloured by the aesthetic's roles.) */
|
|
45
|
+
export function Braid({
|
|
46
|
+
root,
|
|
47
|
+
strands,
|
|
48
|
+
turns,
|
|
49
|
+
title,
|
|
50
|
+
focus,
|
|
51
|
+
onFocusChange,
|
|
52
|
+
progress,
|
|
53
|
+
animate,
|
|
54
|
+
duration,
|
|
55
|
+
width,
|
|
56
|
+
height,
|
|
57
|
+
className,
|
|
58
|
+
style
|
|
59
|
+
}: BraidProps) {
|
|
60
|
+
const ref = React.useRef<HTMLSpanElement>(null);
|
|
61
|
+
const colors = useRoleColors(ref, ROLES);
|
|
62
|
+
// Fills its container: render at the container's width, and again when it changes.
|
|
63
|
+
const [measured, setMeasured] = React.useState<number | undefined>(undefined);
|
|
64
|
+
React.useEffect(() => {
|
|
65
|
+
const el = ref.current?.parentElement;
|
|
66
|
+
if (!el || width !== undefined) return;
|
|
67
|
+
const observer = new ResizeObserver(([entry]) =>
|
|
68
|
+
setMeasured(Math.max(280, Math.round(entry!.contentRect.width)))
|
|
69
|
+
);
|
|
70
|
+
observer.observe(el);
|
|
71
|
+
return () => observer.disconnect();
|
|
72
|
+
}, [width]);
|
|
73
|
+
const w = width ?? measured;
|
|
74
|
+
const h = height ?? 240;
|
|
75
|
+
const data = React.useMemo(
|
|
76
|
+
() =>
|
|
77
|
+
Object.fromEntries(
|
|
78
|
+
Object.entries({ root, strands, turns, title }).filter(([, v]) => v !== undefined)
|
|
79
|
+
),
|
|
80
|
+
[root, strands, turns, title]
|
|
81
|
+
);
|
|
82
|
+
// Draws on once when shown, frame by frame (the render is frame-pure), unless controlled or reduced.
|
|
83
|
+
const controlled = progress !== undefined;
|
|
84
|
+
const [played, setPlayed] = React.useState(controlled || animate === false ? 1 : 0);
|
|
85
|
+
React.useEffect(() => {
|
|
86
|
+
if (controlled || animate === false) return;
|
|
87
|
+
if (
|
|
88
|
+
typeof matchMedia === 'function' &&
|
|
89
|
+
matchMedia('(prefers-reduced-motion: reduce)').matches
|
|
90
|
+
) {
|
|
91
|
+
setPlayed(1);
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
let frame = 0;
|
|
95
|
+
const start = performance.now();
|
|
96
|
+
const length = Math.max(1, duration ?? 1600);
|
|
97
|
+
const tick = (now: number) => {
|
|
98
|
+
const p = Math.min(1, (now - start) / length);
|
|
99
|
+
setPlayed(p);
|
|
100
|
+
if (p < 1) frame = requestAnimationFrame(tick);
|
|
101
|
+
};
|
|
102
|
+
frame = requestAnimationFrame(tick);
|
|
103
|
+
return () => cancelAnimationFrame(frame);
|
|
104
|
+
}, [controlled, animate, duration]);
|
|
105
|
+
const p = Math.min(1, Math.max(0, progress ?? played));
|
|
106
|
+
// Linked focus: hover or arrow keys focus a key; its linked marks stay lit and the rest dim.
|
|
107
|
+
// Keyed on the data's contents, so a parent passing new arrays with the same items keeps the focus.
|
|
108
|
+
const dataKey = React.useMemo(() => JSON.stringify(data), [data]);
|
|
109
|
+
const model = React.useMemo(
|
|
110
|
+
() => createLinkFocus(COMPONENT.interaction!(JSON.parse(dataKey) as unknown)),
|
|
111
|
+
[dataKey]
|
|
112
|
+
);
|
|
113
|
+
const [ownFocus, setOwnFocus] = React.useState<string | null>(null);
|
|
114
|
+
const onFocusChangeRef = React.useRef(onFocusChange);
|
|
115
|
+
onFocusChangeRef.current = onFocusChange;
|
|
116
|
+
React.useEffect(() => {
|
|
117
|
+
setOwnFocus(model.getState().focus);
|
|
118
|
+
return model.subscribe((state) => {
|
|
119
|
+
setOwnFocus(state.focus);
|
|
120
|
+
onFocusChangeRef.current?.(state.focus);
|
|
121
|
+
});
|
|
122
|
+
}, [model]);
|
|
123
|
+
React.useEffect(() => {
|
|
124
|
+
if (focus !== undefined && (focus === null || model.order.includes(focus))) model.focus(focus);
|
|
125
|
+
}, [model, focus]);
|
|
126
|
+
const wanted = focus !== undefined ? focus : ownFocus;
|
|
127
|
+
const f = wanted !== null && model.order.includes(wanted) ? wanted : null;
|
|
128
|
+
const svg = React.useMemo(
|
|
129
|
+
() =>
|
|
130
|
+
w === undefined
|
|
131
|
+
? ''
|
|
132
|
+
: COMPONENT.render(data, {
|
|
133
|
+
width: w,
|
|
134
|
+
height: h,
|
|
135
|
+
theme: colors?.scheme ?? 'light',
|
|
136
|
+
...(colors ? { colors: colors.css } : {}),
|
|
137
|
+
progress: p,
|
|
138
|
+
focus: f
|
|
139
|
+
}),
|
|
140
|
+
[data, w, h, colors, p, f]
|
|
141
|
+
);
|
|
142
|
+
return (
|
|
143
|
+
<span
|
|
144
|
+
ref={ref}
|
|
145
|
+
className={['sui-provider', className].filter(Boolean).join(' ')}
|
|
146
|
+
style={{
|
|
147
|
+
display: 'inline-block',
|
|
148
|
+
position: 'relative',
|
|
149
|
+
width: w ?? '100%',
|
|
150
|
+
height: h,
|
|
151
|
+
...style
|
|
152
|
+
}}
|
|
153
|
+
tabIndex={0}
|
|
154
|
+
onKeyDown={(event) => {
|
|
155
|
+
if (model.handleKey(event)) event.preventDefault();
|
|
156
|
+
}}
|
|
157
|
+
onPointerOver={(event) => {
|
|
158
|
+
const key = (event.target as Element)
|
|
159
|
+
.closest('[data-link-focus]')
|
|
160
|
+
?.getAttribute('data-link-focus');
|
|
161
|
+
if (key && !key.includes(' ')) model.focus(key);
|
|
162
|
+
}}
|
|
163
|
+
onPointerLeave={() => model.clear()}
|
|
164
|
+
onBlur={(event) => {
|
|
165
|
+
if (!event.currentTarget.contains(event.relatedTarget as Node | null)) model.clear();
|
|
166
|
+
}}
|
|
167
|
+
>
|
|
168
|
+
<span style={{ display: 'block' }} dangerouslySetInnerHTML={{ __html: svg }} />
|
|
169
|
+
<span aria-live="polite" style={VISUALLY_HIDDEN}>
|
|
170
|
+
{f === null ? '' : model.describe(f)}
|
|
171
|
+
</span>
|
|
172
|
+
</span>
|
|
173
|
+
);
|
|
174
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// Generated by packages/catalog/scripts/sync-providers.ts from @pixerate/editor 0.322.0 (donut-chart). Do not edit.
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import { uiComponents } from '@pixerate/editor/chart';
|
|
4
|
+
import { useRoleColors } from '../../packages/catalog/src/runtime/role-colors';
|
|
5
|
+
|
|
6
|
+
const ROLES = ['surface', 'ink', 'accent'] as const;
|
|
7
|
+
const COMPONENT = uiComponents().find((c) => c.id === 'donut-chart')!;
|
|
8
|
+
|
|
9
|
+
export interface DonutChartProps {
|
|
10
|
+
/** Parts of the whole, largest first reads best. */
|
|
11
|
+
slices: { label: string; value: number }[];
|
|
12
|
+
/** Title drawn above the chart and used as its accessible name. */
|
|
13
|
+
title?: string;
|
|
14
|
+
/** Written after every value, e.g. "%". */
|
|
15
|
+
unit?: string;
|
|
16
|
+
/** Width in CSS px. */
|
|
17
|
+
width?: number;
|
|
18
|
+
/** Height in CSS px. */
|
|
19
|
+
height?: number;
|
|
20
|
+
className?: string;
|
|
21
|
+
style?: React.CSSProperties;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Parts of a whole as slices of a ring, one colour per slice, with a legend. (editor `donut-chart`, coloured by the aesthetic's roles.) */
|
|
25
|
+
export function DonutChart({
|
|
26
|
+
slices,
|
|
27
|
+
title,
|
|
28
|
+
unit,
|
|
29
|
+
width,
|
|
30
|
+
height,
|
|
31
|
+
className,
|
|
32
|
+
style
|
|
33
|
+
}: DonutChartProps) {
|
|
34
|
+
const ref = React.useRef<HTMLSpanElement>(null);
|
|
35
|
+
const colors = useRoleColors(ref, ROLES);
|
|
36
|
+
const w = width ?? 196;
|
|
37
|
+
const h = height ?? 140;
|
|
38
|
+
const data = React.useMemo(
|
|
39
|
+
() =>
|
|
40
|
+
Object.fromEntries(
|
|
41
|
+
Object.entries({ slices, title, unit }).filter(([, v]) => v !== undefined)
|
|
42
|
+
),
|
|
43
|
+
[slices, title, unit]
|
|
44
|
+
);
|
|
45
|
+
const svg = React.useMemo(
|
|
46
|
+
() =>
|
|
47
|
+
w === undefined
|
|
48
|
+
? ''
|
|
49
|
+
: COMPONENT.render(data, {
|
|
50
|
+
width: w,
|
|
51
|
+
height: h,
|
|
52
|
+
theme: colors?.scheme ?? 'light',
|
|
53
|
+
...(colors ? { colors: colors.css } : {})
|
|
54
|
+
}),
|
|
55
|
+
[data, w, h, colors]
|
|
56
|
+
);
|
|
57
|
+
return (
|
|
58
|
+
<span
|
|
59
|
+
ref={ref}
|
|
60
|
+
className={['sui-provider', className].filter(Boolean).join(' ')}
|
|
61
|
+
style={{ display: 'inline-block', width: w, height: h, ...style }}
|
|
62
|
+
dangerouslySetInnerHTML={{ __html: svg }}
|
|
63
|
+
/>
|
|
64
|
+
);
|
|
65
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// Generated by packages/catalog/scripts/sync-providers.ts from @pixerate/editor 0.322.0 (line-chart). Do not edit.
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import { uiComponents } from '@pixerate/editor/chart';
|
|
4
|
+
import { useRoleColors } from '../../packages/catalog/src/runtime/role-colors';
|
|
5
|
+
|
|
6
|
+
const ROLES = ['surface', 'ink', 'ink-muted', 'border', 'accent'] as const;
|
|
7
|
+
const COMPONENT = uiComponents().find((c) => c.id === 'line-chart')!;
|
|
8
|
+
|
|
9
|
+
export interface LineChartProps {
|
|
10
|
+
/** Labels along the x axis, in order. */
|
|
11
|
+
categories: string[];
|
|
12
|
+
/** One line per series, one value per category. */
|
|
13
|
+
series: { name: string; values: number[] }[];
|
|
14
|
+
/** Title drawn above the chart and used as its accessible name. */
|
|
15
|
+
title?: string;
|
|
16
|
+
/** Written after every tick and value, e.g. "%" or " kWh". */
|
|
17
|
+
unit?: string;
|
|
18
|
+
/** Width in CSS px. */
|
|
19
|
+
width?: number;
|
|
20
|
+
/** Height in CSS px. */
|
|
21
|
+
height?: number;
|
|
22
|
+
className?: string;
|
|
23
|
+
style?: React.CSSProperties;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Lines showing how values change across ordered categories (time), one colour per series, with a value axis and a legend for several series. (editor `line-chart`, coloured by the aesthetic's roles.) */
|
|
27
|
+
export function LineChart({
|
|
28
|
+
categories,
|
|
29
|
+
series,
|
|
30
|
+
title,
|
|
31
|
+
unit,
|
|
32
|
+
width,
|
|
33
|
+
height,
|
|
34
|
+
className,
|
|
35
|
+
style
|
|
36
|
+
}: LineChartProps) {
|
|
37
|
+
const ref = React.useRef<HTMLSpanElement>(null);
|
|
38
|
+
const colors = useRoleColors(ref, ROLES);
|
|
39
|
+
// Fills its container: render at the container's width, and again when it changes.
|
|
40
|
+
const [measured, setMeasured] = React.useState<number | undefined>(undefined);
|
|
41
|
+
React.useEffect(() => {
|
|
42
|
+
const el = ref.current?.parentElement;
|
|
43
|
+
if (!el || width !== undefined) return;
|
|
44
|
+
const observer = new ResizeObserver(([entry]) =>
|
|
45
|
+
setMeasured(Math.max(200, Math.round(entry!.contentRect.width)))
|
|
46
|
+
);
|
|
47
|
+
observer.observe(el);
|
|
48
|
+
return () => observer.disconnect();
|
|
49
|
+
}, [width]);
|
|
50
|
+
const w = width ?? measured;
|
|
51
|
+
const h = height ?? 240;
|
|
52
|
+
const data = React.useMemo(
|
|
53
|
+
() =>
|
|
54
|
+
Object.fromEntries(
|
|
55
|
+
Object.entries({ categories, series, title, unit }).filter(([, v]) => v !== undefined)
|
|
56
|
+
),
|
|
57
|
+
[categories, series, title, unit]
|
|
58
|
+
);
|
|
59
|
+
const svg = React.useMemo(
|
|
60
|
+
() =>
|
|
61
|
+
w === undefined
|
|
62
|
+
? ''
|
|
63
|
+
: COMPONENT.render(data, {
|
|
64
|
+
width: w,
|
|
65
|
+
height: h,
|
|
66
|
+
theme: colors?.scheme ?? 'light',
|
|
67
|
+
...(colors ? { colors: colors.css } : {})
|
|
68
|
+
}),
|
|
69
|
+
[data, w, h, colors]
|
|
70
|
+
);
|
|
71
|
+
return (
|
|
72
|
+
<span
|
|
73
|
+
ref={ref}
|
|
74
|
+
className={['sui-provider', className].filter(Boolean).join(' ')}
|
|
75
|
+
style={{ display: 'inline-block', width: w ?? '100%', height: h, ...style }}
|
|
76
|
+
dangerouslySetInnerHTML={{ __html: svg }}
|
|
77
|
+
/>
|
|
78
|
+
);
|
|
79
|
+
}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
// Generated by packages/catalog/scripts/sync-providers.ts from @pixerate/editor 0.322.0 (lineage). Do not edit.
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import { uiComponents } from '@pixerate/editor/chart';
|
|
4
|
+
import { createLinkFocus } from '@pixerate/editor/tools';
|
|
5
|
+
import { useRoleColors } from '../../packages/catalog/src/runtime/role-colors';
|
|
6
|
+
|
|
7
|
+
const ROLES = ['surface', 'ink', 'ink-muted', 'accent'] as const;
|
|
8
|
+
const COMPONENT = uiComponents().find((c) => c.id === 'lineage')!;
|
|
9
|
+
const VISUALLY_HIDDEN: React.CSSProperties = {
|
|
10
|
+
position: 'absolute',
|
|
11
|
+
width: 1,
|
|
12
|
+
height: 1,
|
|
13
|
+
overflow: 'hidden',
|
|
14
|
+
clip: 'rect(0 0 0 0)',
|
|
15
|
+
whiteSpace: 'nowrap'
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export interface LineageProps {
|
|
19
|
+
/** The root everything descends from, e.g. a team or a product. */
|
|
20
|
+
root: string;
|
|
21
|
+
/** The row it fans out to, in order. */
|
|
22
|
+
items: { label: string }[];
|
|
23
|
+
/** Accessible name (default: built from the data). */
|
|
24
|
+
title?: string;
|
|
25
|
+
/** Controlled focus; leave out for the component to keep its own. */
|
|
26
|
+
focus?: string | null;
|
|
27
|
+
onFocusChange?: (focus: string | null) => void;
|
|
28
|
+
/** Draw-on progress, 0–1 (1 is the picture at rest). Leave out to draw on once when shown (not under reduced motion). */
|
|
29
|
+
progress?: number;
|
|
30
|
+
/** Draw on when shown (default true). Ignored when `progress` is given. */
|
|
31
|
+
animate?: boolean;
|
|
32
|
+
/** Length of the draw-on in ms (default 1600). */
|
|
33
|
+
duration?: number;
|
|
34
|
+
/** Width in CSS px. */
|
|
35
|
+
width?: number;
|
|
36
|
+
/** Height in CSS px. */
|
|
37
|
+
height?: number;
|
|
38
|
+
className?: string;
|
|
39
|
+
style?: React.CSSProperties;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** One root fanned out to a row of items with rounded connectors that never cross, labels sized to fit their columns; draws on from the root, and focusing an item lights its branch. (editor `lineage`, coloured by the aesthetic's roles.) */
|
|
43
|
+
export function Lineage({
|
|
44
|
+
root,
|
|
45
|
+
items,
|
|
46
|
+
title,
|
|
47
|
+
focus,
|
|
48
|
+
onFocusChange,
|
|
49
|
+
progress,
|
|
50
|
+
animate,
|
|
51
|
+
duration,
|
|
52
|
+
width,
|
|
53
|
+
height,
|
|
54
|
+
className,
|
|
55
|
+
style
|
|
56
|
+
}: LineageProps) {
|
|
57
|
+
const ref = React.useRef<HTMLSpanElement>(null);
|
|
58
|
+
const colors = useRoleColors(ref, ROLES);
|
|
59
|
+
// Fills its container: render at the container's width, and again when it changes.
|
|
60
|
+
const [measured, setMeasured] = React.useState<number | undefined>(undefined);
|
|
61
|
+
React.useEffect(() => {
|
|
62
|
+
const el = ref.current?.parentElement;
|
|
63
|
+
if (!el || width !== undefined) return;
|
|
64
|
+
const observer = new ResizeObserver(([entry]) =>
|
|
65
|
+
setMeasured(Math.max(240, Math.round(entry!.contentRect.width)))
|
|
66
|
+
);
|
|
67
|
+
observer.observe(el);
|
|
68
|
+
return () => observer.disconnect();
|
|
69
|
+
}, [width]);
|
|
70
|
+
const w = width ?? measured;
|
|
71
|
+
const h = height ?? 240;
|
|
72
|
+
const data = React.useMemo(
|
|
73
|
+
() =>
|
|
74
|
+
Object.fromEntries(Object.entries({ root, items, title }).filter(([, v]) => v !== undefined)),
|
|
75
|
+
[root, items, title]
|
|
76
|
+
);
|
|
77
|
+
// Draws on once when shown, frame by frame (the render is frame-pure), unless controlled or reduced.
|
|
78
|
+
const controlled = progress !== undefined;
|
|
79
|
+
const [played, setPlayed] = React.useState(controlled || animate === false ? 1 : 0);
|
|
80
|
+
React.useEffect(() => {
|
|
81
|
+
if (controlled || animate === false) return;
|
|
82
|
+
if (
|
|
83
|
+
typeof matchMedia === 'function' &&
|
|
84
|
+
matchMedia('(prefers-reduced-motion: reduce)').matches
|
|
85
|
+
) {
|
|
86
|
+
setPlayed(1);
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
let frame = 0;
|
|
90
|
+
const start = performance.now();
|
|
91
|
+
const length = Math.max(1, duration ?? 1600);
|
|
92
|
+
const tick = (now: number) => {
|
|
93
|
+
const p = Math.min(1, (now - start) / length);
|
|
94
|
+
setPlayed(p);
|
|
95
|
+
if (p < 1) frame = requestAnimationFrame(tick);
|
|
96
|
+
};
|
|
97
|
+
frame = requestAnimationFrame(tick);
|
|
98
|
+
return () => cancelAnimationFrame(frame);
|
|
99
|
+
}, [controlled, animate, duration]);
|
|
100
|
+
const p = Math.min(1, Math.max(0, progress ?? played));
|
|
101
|
+
// Linked focus: hover or arrow keys focus a key; its linked marks stay lit and the rest dim.
|
|
102
|
+
// Keyed on the data's contents, so a parent passing new arrays with the same items keeps the focus.
|
|
103
|
+
const dataKey = React.useMemo(() => JSON.stringify(data), [data]);
|
|
104
|
+
const model = React.useMemo(
|
|
105
|
+
() => createLinkFocus(COMPONENT.interaction!(JSON.parse(dataKey) as unknown)),
|
|
106
|
+
[dataKey]
|
|
107
|
+
);
|
|
108
|
+
const [ownFocus, setOwnFocus] = React.useState<string | null>(null);
|
|
109
|
+
const onFocusChangeRef = React.useRef(onFocusChange);
|
|
110
|
+
onFocusChangeRef.current = onFocusChange;
|
|
111
|
+
React.useEffect(() => {
|
|
112
|
+
setOwnFocus(model.getState().focus);
|
|
113
|
+
return model.subscribe((state) => {
|
|
114
|
+
setOwnFocus(state.focus);
|
|
115
|
+
onFocusChangeRef.current?.(state.focus);
|
|
116
|
+
});
|
|
117
|
+
}, [model]);
|
|
118
|
+
React.useEffect(() => {
|
|
119
|
+
if (focus !== undefined && (focus === null || model.order.includes(focus))) model.focus(focus);
|
|
120
|
+
}, [model, focus]);
|
|
121
|
+
const wanted = focus !== undefined ? focus : ownFocus;
|
|
122
|
+
const f = wanted !== null && model.order.includes(wanted) ? wanted : null;
|
|
123
|
+
const svg = React.useMemo(
|
|
124
|
+
() =>
|
|
125
|
+
w === undefined
|
|
126
|
+
? ''
|
|
127
|
+
: COMPONENT.render(data, {
|
|
128
|
+
width: w,
|
|
129
|
+
height: h,
|
|
130
|
+
theme: colors?.scheme ?? 'light',
|
|
131
|
+
...(colors ? { colors: colors.css } : {}),
|
|
132
|
+
progress: p,
|
|
133
|
+
focus: f
|
|
134
|
+
}),
|
|
135
|
+
[data, w, h, colors, p, f]
|
|
136
|
+
);
|
|
137
|
+
return (
|
|
138
|
+
<span
|
|
139
|
+
ref={ref}
|
|
140
|
+
className={['sui-provider', className].filter(Boolean).join(' ')}
|
|
141
|
+
style={{
|
|
142
|
+
display: 'inline-block',
|
|
143
|
+
position: 'relative',
|
|
144
|
+
width: w ?? '100%',
|
|
145
|
+
height: h,
|
|
146
|
+
...style
|
|
147
|
+
}}
|
|
148
|
+
tabIndex={0}
|
|
149
|
+
onKeyDown={(event) => {
|
|
150
|
+
if (model.handleKey(event)) event.preventDefault();
|
|
151
|
+
}}
|
|
152
|
+
onPointerOver={(event) => {
|
|
153
|
+
const key = (event.target as Element)
|
|
154
|
+
.closest('[data-link-focus]')
|
|
155
|
+
?.getAttribute('data-link-focus');
|
|
156
|
+
if (key && !key.includes(' ')) model.focus(key);
|
|
157
|
+
}}
|
|
158
|
+
onPointerLeave={() => model.clear()}
|
|
159
|
+
onBlur={(event) => {
|
|
160
|
+
if (!event.currentTarget.contains(event.relatedTarget as Node | null)) model.clear();
|
|
161
|
+
}}
|
|
162
|
+
>
|
|
163
|
+
<span style={{ display: 'block' }} dangerouslySetInnerHTML={{ __html: svg }} />
|
|
164
|
+
<span aria-live="polite" style={VISUALLY_HIDDEN}>
|
|
165
|
+
{f === null ? '' : model.describe(f)}
|
|
166
|
+
</span>
|
|
167
|
+
</span>
|
|
168
|
+
);
|
|
169
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Generated by packages/catalog/scripts/sync-providers.ts from @pixerate/editor 0.
|
|
1
|
+
// Generated by packages/catalog/scripts/sync-providers.ts from @pixerate/editor 0.322.0 (sparkline). Do not edit.
|
|
2
2
|
import * as React from 'react';
|
|
3
3
|
import { uiComponents } from '@pixerate/editor/chart';
|
|
4
4
|
import { useRoleColors } from '../../packages/catalog/src/runtime/role-colors';
|
|
@@ -25,22 +25,21 @@ export function Sparkline({ values, label, width, height, className, style }: Sp
|
|
|
25
25
|
const colors = useRoleColors(ref, ROLES);
|
|
26
26
|
const w = width ?? 240;
|
|
27
27
|
const h = height ?? 48;
|
|
28
|
+
const data = React.useMemo(
|
|
29
|
+
() => Object.fromEntries(Object.entries({ values, label }).filter(([, v]) => v !== undefined)),
|
|
30
|
+
[values, label]
|
|
31
|
+
);
|
|
28
32
|
const svg = React.useMemo(
|
|
29
33
|
() =>
|
|
30
34
|
w === undefined
|
|
31
35
|
? ''
|
|
32
|
-
: COMPONENT.render(
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
{
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
theme: colors?.scheme ?? 'light',
|
|
40
|
-
...(colors ? { colors: colors.css } : {})
|
|
41
|
-
}
|
|
42
|
-
),
|
|
43
|
-
[values, label, w, h, colors]
|
|
36
|
+
: COMPONENT.render(data, {
|
|
37
|
+
width: w,
|
|
38
|
+
height: h,
|
|
39
|
+
theme: colors?.scheme ?? 'light',
|
|
40
|
+
...(colors ? { colors: colors.css } : {})
|
|
41
|
+
}),
|
|
42
|
+
[data, w, h, colors]
|
|
44
43
|
);
|
|
45
44
|
return (
|
|
46
45
|
<span
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
// Generated by packages/catalog/scripts/sync-providers.ts from @pixerate/editor 0.322.0 (weave). Do not edit.
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import { uiComponents } from '@pixerate/editor/chart';
|
|
4
|
+
import { createLinkFocus } from '@pixerate/editor/tools';
|
|
5
|
+
import { useRoleColors } from '../../packages/catalog/src/runtime/role-colors';
|
|
6
|
+
|
|
7
|
+
const ROLES = ['surface', 'surface-raised', 'ink', 'ink-muted', 'border', 'accent'] as const;
|
|
8
|
+
const COMPONENT = uiComponents().find((c) => c.id === 'weave')!;
|
|
9
|
+
const VISUALLY_HIDDEN: React.CSSProperties = {
|
|
10
|
+
position: 'absolute',
|
|
11
|
+
width: 1,
|
|
12
|
+
height: 1,
|
|
13
|
+
overflow: 'hidden',
|
|
14
|
+
clip: 'rect(0 0 0 0)',
|
|
15
|
+
whiteSpace: 'nowrap'
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export interface WeaveProps {
|
|
19
|
+
/** Items along the top, each with the tags it has; tags along the bottom in order of first appearance (at most 24). */
|
|
20
|
+
items: { label: string; tags: string[] }[];
|
|
21
|
+
/** Accessible name (default: built from the data). */
|
|
22
|
+
title?: string;
|
|
23
|
+
/** Controlled focus; leave out for the component to keep its own. */
|
|
24
|
+
focus?: string | null;
|
|
25
|
+
onFocusChange?: (focus: string | null) => void;
|
|
26
|
+
/** Draw-on progress, 0–1 (1 is the picture at rest). Leave out to draw on once when shown (not under reduced motion). */
|
|
27
|
+
progress?: number;
|
|
28
|
+
/** Draw on when shown (default true). Ignored when `progress` is given. */
|
|
29
|
+
animate?: boolean;
|
|
30
|
+
/** Length of the draw-on in ms (default 1600). */
|
|
31
|
+
duration?: number;
|
|
32
|
+
/** Width in CSS px. */
|
|
33
|
+
width?: number;
|
|
34
|
+
/** Height in CSS px. */
|
|
35
|
+
height?: number;
|
|
36
|
+
className?: string;
|
|
37
|
+
style?: React.CSSProperties;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Items along the top joined to the tags they share along the bottom, many to many; focusing an item lights its tags, and focusing a tag lights its items. (editor `weave`, coloured by the aesthetic's roles.) */
|
|
41
|
+
export function Weave({
|
|
42
|
+
items,
|
|
43
|
+
title,
|
|
44
|
+
focus,
|
|
45
|
+
onFocusChange,
|
|
46
|
+
progress,
|
|
47
|
+
animate,
|
|
48
|
+
duration,
|
|
49
|
+
width,
|
|
50
|
+
height,
|
|
51
|
+
className,
|
|
52
|
+
style
|
|
53
|
+
}: WeaveProps) {
|
|
54
|
+
const ref = React.useRef<HTMLSpanElement>(null);
|
|
55
|
+
const colors = useRoleColors(ref, ROLES);
|
|
56
|
+
// Fills its container: render at the container's width, and again when it changes.
|
|
57
|
+
const [measured, setMeasured] = React.useState<number | undefined>(undefined);
|
|
58
|
+
React.useEffect(() => {
|
|
59
|
+
const el = ref.current?.parentElement;
|
|
60
|
+
if (!el || width !== undefined) return;
|
|
61
|
+
const observer = new ResizeObserver(([entry]) =>
|
|
62
|
+
setMeasured(Math.max(280, Math.round(entry!.contentRect.width)))
|
|
63
|
+
);
|
|
64
|
+
observer.observe(el);
|
|
65
|
+
return () => observer.disconnect();
|
|
66
|
+
}, [width]);
|
|
67
|
+
const w = width ?? measured;
|
|
68
|
+
const h = height ?? 240;
|
|
69
|
+
const data = React.useMemo(
|
|
70
|
+
() => Object.fromEntries(Object.entries({ items, title }).filter(([, v]) => v !== undefined)),
|
|
71
|
+
[items, title]
|
|
72
|
+
);
|
|
73
|
+
// Draws on once when shown, frame by frame (the render is frame-pure), unless controlled or reduced.
|
|
74
|
+
const controlled = progress !== undefined;
|
|
75
|
+
const [played, setPlayed] = React.useState(controlled || animate === false ? 1 : 0);
|
|
76
|
+
React.useEffect(() => {
|
|
77
|
+
if (controlled || animate === false) return;
|
|
78
|
+
if (
|
|
79
|
+
typeof matchMedia === 'function' &&
|
|
80
|
+
matchMedia('(prefers-reduced-motion: reduce)').matches
|
|
81
|
+
) {
|
|
82
|
+
setPlayed(1);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
let frame = 0;
|
|
86
|
+
const start = performance.now();
|
|
87
|
+
const length = Math.max(1, duration ?? 1600);
|
|
88
|
+
const tick = (now: number) => {
|
|
89
|
+
const p = Math.min(1, (now - start) / length);
|
|
90
|
+
setPlayed(p);
|
|
91
|
+
if (p < 1) frame = requestAnimationFrame(tick);
|
|
92
|
+
};
|
|
93
|
+
frame = requestAnimationFrame(tick);
|
|
94
|
+
return () => cancelAnimationFrame(frame);
|
|
95
|
+
}, [controlled, animate, duration]);
|
|
96
|
+
const p = Math.min(1, Math.max(0, progress ?? played));
|
|
97
|
+
// Linked focus: hover or arrow keys focus a key; its linked marks stay lit and the rest dim.
|
|
98
|
+
// Keyed on the data's contents, so a parent passing new arrays with the same items keeps the focus.
|
|
99
|
+
const dataKey = React.useMemo(() => JSON.stringify(data), [data]);
|
|
100
|
+
const model = React.useMemo(
|
|
101
|
+
() => createLinkFocus(COMPONENT.interaction!(JSON.parse(dataKey) as unknown)),
|
|
102
|
+
[dataKey]
|
|
103
|
+
);
|
|
104
|
+
const [ownFocus, setOwnFocus] = React.useState<string | null>(null);
|
|
105
|
+
const onFocusChangeRef = React.useRef(onFocusChange);
|
|
106
|
+
onFocusChangeRef.current = onFocusChange;
|
|
107
|
+
React.useEffect(() => {
|
|
108
|
+
setOwnFocus(model.getState().focus);
|
|
109
|
+
return model.subscribe((state) => {
|
|
110
|
+
setOwnFocus(state.focus);
|
|
111
|
+
onFocusChangeRef.current?.(state.focus);
|
|
112
|
+
});
|
|
113
|
+
}, [model]);
|
|
114
|
+
React.useEffect(() => {
|
|
115
|
+
if (focus !== undefined && (focus === null || model.order.includes(focus))) model.focus(focus);
|
|
116
|
+
}, [model, focus]);
|
|
117
|
+
const wanted = focus !== undefined ? focus : ownFocus;
|
|
118
|
+
const f = wanted !== null && model.order.includes(wanted) ? wanted : null;
|
|
119
|
+
const svg = React.useMemo(
|
|
120
|
+
() =>
|
|
121
|
+
w === undefined
|
|
122
|
+
? ''
|
|
123
|
+
: COMPONENT.render(data, {
|
|
124
|
+
width: w,
|
|
125
|
+
height: h,
|
|
126
|
+
theme: colors?.scheme ?? 'light',
|
|
127
|
+
...(colors ? { colors: colors.css } : {}),
|
|
128
|
+
progress: p,
|
|
129
|
+
focus: f
|
|
130
|
+
}),
|
|
131
|
+
[data, w, h, colors, p, f]
|
|
132
|
+
);
|
|
133
|
+
return (
|
|
134
|
+
<span
|
|
135
|
+
ref={ref}
|
|
136
|
+
className={['sui-provider', className].filter(Boolean).join(' ')}
|
|
137
|
+
style={{
|
|
138
|
+
display: 'inline-block',
|
|
139
|
+
position: 'relative',
|
|
140
|
+
width: w ?? '100%',
|
|
141
|
+
height: h,
|
|
142
|
+
...style
|
|
143
|
+
}}
|
|
144
|
+
tabIndex={0}
|
|
145
|
+
onKeyDown={(event) => {
|
|
146
|
+
if (model.handleKey(event)) event.preventDefault();
|
|
147
|
+
}}
|
|
148
|
+
onPointerOver={(event) => {
|
|
149
|
+
const key = (event.target as Element)
|
|
150
|
+
.closest('[data-link-focus]')
|
|
151
|
+
?.getAttribute('data-link-focus');
|
|
152
|
+
if (key && !key.includes(' ')) model.focus(key);
|
|
153
|
+
}}
|
|
154
|
+
onPointerLeave={() => model.clear()}
|
|
155
|
+
onBlur={(event) => {
|
|
156
|
+
if (!event.currentTarget.contains(event.relatedTarget as Node | null)) model.clear();
|
|
157
|
+
}}
|
|
158
|
+
>
|
|
159
|
+
<span style={{ display: 'block' }} dangerouslySetInnerHTML={{ __html: svg }} />
|
|
160
|
+
<span aria-live="polite" style={VISUALLY_HIDDEN}>
|
|
161
|
+
{f === null ? '' : model.describe(f)}
|
|
162
|
+
</span>
|
|
163
|
+
</span>
|
|
164
|
+
);
|
|
165
|
+
}
|