@web-canvas-ui/react 0.0.0-stage → 0.1.2

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 (98) hide show
  1. package/Canvas.tsx +142 -0
  2. package/README.md +988 -2
  3. package/__tests__/pure-logic.test.ts +153 -0
  4. package/__tests__/scene-and-node.test.ts +131 -0
  5. package/__tests__/scratch-space.test.ts +30 -0
  6. package/__tests__/textarea-caret.test.ts +96 -0
  7. package/__tests__/utils.test.ts +114 -0
  8. package/canvas-handle.ts +114 -0
  9. package/components/Button/button.ts +57 -0
  10. package/components/Button/index.ts +3 -0
  11. package/components/Button/logic.ts +11 -0
  12. package/components/CanvasView/canvasView.ts +103 -0
  13. package/components/CanvasView/index.ts +7 -0
  14. package/components/Carousel/carousel.ts +107 -0
  15. package/components/Carousel/index.ts +3 -0
  16. package/components/Carousel/logic.ts +36 -0
  17. package/components/Checkbox/checkbox.ts +14 -0
  18. package/components/Checkbox/index.ts +2 -0
  19. package/components/ChoiceGroup/choiceGroup.ts +172 -0
  20. package/components/ChoiceGroup/index.ts +3 -0
  21. package/components/ChoiceGroup/logic.ts +14 -0
  22. package/components/Circle/circle.ts +31 -0
  23. package/components/Circle/index.ts +2 -0
  24. package/components/Collapse/collapse.ts +69 -0
  25. package/components/Collapse/index.ts +3 -0
  26. package/components/Collapse/logic.ts +14 -0
  27. package/components/CustomDraw/customDraw.ts +50 -0
  28. package/components/CustomDraw/index.ts +7 -0
  29. package/components/DatePicker/datePicker.ts +70 -0
  30. package/components/DatePicker/index.ts +2 -0
  31. package/components/Dialog/dialog.ts +102 -0
  32. package/components/Dialog/index.ts +2 -0
  33. package/components/Dropdown/dropdown.ts +125 -0
  34. package/components/Dropdown/index.ts +3 -0
  35. package/components/Dropdown/logic.ts +29 -0
  36. package/components/Grid/grid.ts +15 -0
  37. package/components/Grid/index.ts +2 -0
  38. package/components/GridItem/gridItem.ts +11 -0
  39. package/components/GridItem/index.ts +2 -0
  40. package/components/Group/group.ts +8 -0
  41. package/components/Group/index.ts +2 -0
  42. package/components/Icon/icon.ts +669 -0
  43. package/components/Icon/index.ts +2 -0
  44. package/components/Image/image.ts +69 -0
  45. package/components/Image/index.ts +2 -0
  46. package/components/Input/index.ts +2 -0
  47. package/components/Input/input.ts +139 -0
  48. package/components/Progress/index.ts +2 -0
  49. package/components/Progress/progress.ts +59 -0
  50. package/components/Radio/index.ts +2 -0
  51. package/components/Radio/radio.ts +14 -0
  52. package/components/Rect/index.ts +2 -0
  53. package/components/Rect/rect.ts +35 -0
  54. package/components/Slider/index.ts +3 -0
  55. package/components/Slider/logic.ts +19 -0
  56. package/components/Slider/slider.ts +58 -0
  57. package/components/Switch/index.ts +3 -0
  58. package/components/Switch/logic.ts +8 -0
  59. package/components/Switch/switch.ts +37 -0
  60. package/components/Tabs/index.ts +3 -0
  61. package/components/Tabs/logic.ts +14 -0
  62. package/components/Tabs/tabs.ts +78 -0
  63. package/components/Text/index.ts +2 -0
  64. package/components/Text/text.ts +95 -0
  65. package/components/Textarea/index.ts +2 -0
  66. package/components/Textarea/textarea.ts +16 -0
  67. package/components/Upload/index.ts +2 -0
  68. package/components/Upload/upload.ts +66 -0
  69. package/components/draw-utils.ts +230 -0
  70. package/components/shared.ts +15 -0
  71. package/components/size.ts +20 -0
  72. package/components/types.ts +37 -0
  73. package/components.ts +98 -0
  74. package/engine/__tests__/carousel.test.ts +218 -0
  75. package/engine/__tests__/hit-test.test.ts +282 -0
  76. package/engine/__tests__/layout.test.ts +474 -0
  77. package/engine/carousel.ts +150 -0
  78. package/engine/events.ts +73 -0
  79. package/engine/hit-test.ts +321 -0
  80. package/engine/images.ts +41 -0
  81. package/engine/interaction.ts +442 -0
  82. package/engine/layout.ts +355 -0
  83. package/engine/nodes.ts +166 -0
  84. package/engine/state.ts +75 -0
  85. package/engine/text-controls.ts +122 -0
  86. package/engine/transform.ts +61 -0
  87. package/engine/vnodes.ts +204 -0
  88. package/engine.ts +162 -0
  89. package/image-assets.ts +37 -0
  90. package/index.ts +78 -0
  91. package/input-bridge.ts +285 -0
  92. package/package.json +27 -4
  93. package/renderer.ts +303 -0
  94. package/scene.ts +122 -0
  95. package/theme.css +327 -0
  96. package/theme.scss +234 -0
  97. package/types.ts +256 -0
  98. package/web-canvas-ui-react-0.1.1.tgz +0 -0
@@ -0,0 +1,2 @@
1
+ export { Textarea, drawTextarea } from './textarea';
2
+ export type { TextareaProps, TextareaDrawProps } from './textarea';
@@ -0,0 +1,16 @@
1
+ import type { InputProps, InputDrawProps } from '../Input';
2
+ import { drawInput } from '../Input';
3
+ import { createComponent } from '../shared';
4
+
5
+ export interface TextareaProps extends Omit<InputProps, 'type'> {
6
+ rows?: number;
7
+ }
8
+
9
+ export const Textarea = createComponent<TextareaProps>('textarea');
10
+
11
+ /** Public props plus the engine-injected caret/scroll state consumed by the draw layer. */
12
+ export type TextareaDrawProps = Omit<InputDrawProps, 'type'> & { rows?: number };
13
+
14
+ export function drawTextarea(context: CanvasRenderingContext2D, props: TextareaDrawProps): void {
15
+ drawInput(context, props, true);
16
+ }
@@ -0,0 +1,2 @@
1
+ export { Upload, drawUpload } from './upload';
2
+ export type { UploadProps } from './upload';
@@ -0,0 +1,66 @@
1
+ import type { TransformProps } from '../../types';
2
+ import { fitText, number, string, themeColor } from '../draw-utils';
3
+ import { drawIcon, type IconName } from '../Icon';
4
+ import { createComponent } from '../shared';
5
+
6
+ export interface UploadProps extends TransformProps {
7
+ width?: number;
8
+ height?: number;
9
+ text?: string;
10
+ icon?: IconName;
11
+ iconSize?: number;
12
+ accept?: string;
13
+ multiple?: boolean;
14
+ disabled?: boolean;
15
+ backgroundColor?: string;
16
+ textColor?: string;
17
+ borderColor?: string;
18
+ borderRadius?: number;
19
+ fontSize?: number;
20
+ onChange?: (files: File[]) => void;
21
+ }
22
+
23
+ export const Upload = createComponent<UploadProps>('upload');
24
+
25
+ export function drawUpload(context: CanvasRenderingContext2D, props: UploadProps): void {
26
+ const width = number(props.width, 120);
27
+ const height = number(props.height, 40);
28
+ const radius = number(props.borderRadius, 6);
29
+ context.beginPath();
30
+ context.roundRect(0, 0, width, height, radius);
31
+ context.fillStyle = string(
32
+ props.backgroundColor,
33
+ themeColor(context, '--canvas-ui-button-background', '#1677ff'),
34
+ );
35
+ context.fill();
36
+ if (props.borderColor) {
37
+ context.strokeStyle = string(props.borderColor, '#1677ff');
38
+ context.stroke();
39
+ }
40
+
41
+ const label = string(props.text, 'Choose File');
42
+ const color = string(props.textColor, themeColor(context, '--canvas-ui-button-color', '#fff'));
43
+ const fontSize = number(props.fontSize, 15);
44
+ const iconSize = number(props.iconSize, Math.round(fontSize * 1.1));
45
+ const gap = 6;
46
+
47
+ context.font = `${fontSize}px sans-serif`;
48
+ context.textBaseline = 'middle';
49
+ context.textAlign = 'left';
50
+ const fittedLabel = fitText(context, label, Math.max(0, width - 12 - iconSize - gap));
51
+ // A built-in glyph (defaults to the "upload" arrow; pass `icon` to customise) plus the
52
+ // label, kept as one centred group that never spills outside the button box.
53
+ const textWidth = fittedLabel ? context.measureText(fittedLabel).width : 0;
54
+ const contentWidth = iconSize + (fittedLabel ? gap : 0) + textWidth;
55
+ let cursorX = (width - contentWidth) / 2;
56
+ const centerY = height / 2;
57
+
58
+ context.save();
59
+ context.translate(cursorX, centerY - iconSize / 2);
60
+ drawIcon(context, { name: props.icon ?? 'upload', size: iconSize, color });
61
+ context.restore();
62
+ cursorX += iconSize + (fittedLabel ? gap : 0);
63
+
64
+ context.fillStyle = color;
65
+ context.fillText(fittedLabel, cursorX, centerY);
66
+ }
@@ -0,0 +1,230 @@
1
+ export function number(value: unknown, fallback: number): number {
2
+ return typeof value === 'number' && Number.isFinite(value) ? value : fallback;
3
+ }
4
+
5
+ export function string(value: unknown, fallback: string): string {
6
+ return typeof value === 'string' ? value : fallback;
7
+ }
8
+
9
+ /** Measures rendered text width; drawing passes a context, layout passes `measureTextWidth`. */
10
+ export type TextMeasurer = (text: string) => number;
11
+
12
+ /** Baseline-to-baseline multiplier, shared by Text drawing and the pass that sizes it. */
13
+ export const TEXT_LINE_HEIGHT = 1.2;
14
+
15
+ const ELLIPSIS = '…';
16
+
17
+ // CJK ideographs, kana and Hangul syllables: each of them is one em wide, and a run of them may be
18
+ // broken between any two characters - unlike a Latin word, which moves to the next line whole.
19
+ const WIDE_CHARACTER = /[\u2e80-\u9fff\uac00-\ud7af]/u;
20
+
21
+ function byContext(context: CanvasRenderingContext2D): TextMeasurer {
22
+ return (text) => context.measureText(text).width;
23
+ }
24
+
25
+ function breakIntoLines(measure: TextMeasurer, text: string, width: number): string[] {
26
+ const lines: string[] = [];
27
+ for (const paragraph of text.split('\n')) {
28
+ let line = '';
29
+ for (const word of paragraph.split(/(\s+)/).filter(Boolean)) {
30
+ const candidate = line + word;
31
+ if (line && measure(candidate) <= width) {
32
+ line = candidate;
33
+ continue;
34
+ }
35
+ if (!line && /^\s+$/.test(word)) continue;
36
+ if (!line && measure(word) <= width) {
37
+ line = word;
38
+ continue;
39
+ }
40
+ if (line && !WIDE_CHARACTER.test(word)) {
41
+ // A Latin word that no longer fits is moved down whole; a CJK run instead keeps
42
+ // filling this line, because that is what the browser does to the text of a
43
+ // textarea, and a caret offset is resolved against those very line breaks.
44
+ lines.push(line.trimEnd());
45
+ line = '';
46
+ }
47
+ for (const character of Array.from(word)) {
48
+ if (line && measure(line + character) > width) {
49
+ lines.push(line.trimEnd());
50
+ line = '';
51
+ }
52
+ line += character;
53
+ }
54
+ }
55
+ lines.push(line);
56
+ }
57
+ return lines;
58
+ }
59
+
60
+ /** Longest prefix of `text` that still fits `maxWidth` once the ellipsis is appended to it. */
61
+ function ellipsizedTo(measure: TextMeasurer, text: string, maxWidth: number): string {
62
+ const characters = Array.from(text);
63
+ let low = 0;
64
+ let high = characters.length;
65
+ while (low < high) {
66
+ const middle = Math.ceil((low + high) / 2);
67
+ const candidate = characters.slice(0, middle).join('') + ELLIPSIS;
68
+ if (measure(candidate) <= maxWidth) low = middle;
69
+ else high = middle - 1;
70
+ }
71
+ return characters.slice(0, low).join('') + ELLIPSIS;
72
+ }
73
+
74
+ function truncateTo(measure: TextMeasurer, text: string, maxWidth: number): string {
75
+ if (!text) return text;
76
+ if (measure(text) <= maxWidth) return text;
77
+ return ellipsizedTo(measure, text, maxWidth);
78
+ }
79
+
80
+ function clampToLines(
81
+ measure: TextMeasurer,
82
+ text: string,
83
+ width: number,
84
+ maxLines: number,
85
+ ): string[] {
86
+ const lines = breakIntoLines(measure, text, width);
87
+ if (lines.length <= maxLines) return lines;
88
+ const visible = lines.slice(0, maxLines);
89
+ // Shorten the last visible line rather than dropping it: the ellipsis has to borrow room from
90
+ // the very line it terminates.
91
+ visible[maxLines - 1] = ellipsizedTo(measure, visible[maxLines - 1], width);
92
+ return visible;
93
+ }
94
+
95
+ /**
96
+ * Wraps `text` into lines at most `width` wide, keeping at most `maxLines` of them; a clamped
97
+ * block ends with an ellipsis instead of silently losing the rest. `maxLines` <= 0 means "no
98
+ * limit"; any positive limit keeps at least one line. Callers supply the measurer so that drawing
99
+ * (live context) and layout ({@link measureTextWidth}) share one algorithm and therefore one result.
100
+ */
101
+ export function measureLines(
102
+ measure: TextMeasurer,
103
+ text: string,
104
+ width: number,
105
+ maxLines = 0,
106
+ ): string[] {
107
+ if (!(width > 0)) return [text];
108
+ if (!(maxLines > 0)) return breakIntoLines(measure, text, width);
109
+ return clampToLines(measure, text, width, Math.max(1, Math.round(maxLines)));
110
+ }
111
+
112
+ export function wrapText(context: CanvasRenderingContext2D, text: string, width: number): string[] {
113
+ return breakIntoLines(byContext(context), text, width);
114
+ }
115
+
116
+ /**
117
+ * Truncates `text` with a trailing ellipsis so it fits within `maxWidth` for the
118
+ * context's current font. Display-only labels (buttons, tabs, dropdown rows…) use
119
+ * this to stay inside their box; editable controls clip instead.
120
+ */
121
+ export function fitText(context: CanvasRenderingContext2D, text: string, maxWidth: number): string {
122
+ return truncateTo(byContext(context), text, maxWidth);
123
+ }
124
+
125
+ let cachedMeasureContext: CanvasRenderingContext2D | null = null;
126
+ let measureContextResolved = false;
127
+ // Assigning `font` re-resolves the font list, which is the expensive half of a measurement. Runs of
128
+ // calls almost always share one font, so the assignment is skipped while the string is unchanged.
129
+ let appliedMeasureFont = '';
130
+
131
+ function getMeasureContext(): CanvasRenderingContext2D | null {
132
+ if (!measureContextResolved) {
133
+ measureContextResolved = true;
134
+ if (typeof document !== 'undefined') {
135
+ cachedMeasureContext = document.createElement('canvas').getContext('2d');
136
+ }
137
+ }
138
+ return cachedMeasureContext;
139
+ }
140
+
141
+ /**
142
+ * Measures the rendered width of `text` for a given CSS `font` string using a
143
+ * shared offscreen context. Draw, hit-test and layout code must all go through
144
+ * this helper so their width calculations stay byte-for-byte identical.
145
+ * Falls back to a deterministic estimate when no canvas is available (SSR/tests).
146
+ */
147
+ export function measureTextWidth(text: string, font: string): number {
148
+ const context = getMeasureContext();
149
+ if (!context) return estimateTextWidth(text, font);
150
+ if (appliedMeasureFont !== font) {
151
+ context.font = font;
152
+ appliedMeasureFont = font;
153
+ }
154
+ return context.measureText(text).width;
155
+ }
156
+
157
+ function estimateTextWidth(text: string, font: string): number {
158
+ const fontSize = parseFloat(font) || 14;
159
+ return Array.from(text).reduce((total, character) => {
160
+ const isWide = WIDE_CHARACTER.test(character);
161
+ return total + fontSize * (isWide ? 1 : 0.62);
162
+ }, 0);
163
+ }
164
+
165
+ let cachedScrollbarWidth: number | null = null;
166
+
167
+ /**
168
+ * Width a classic vertical scrollbar occupies, 0 on platforms that float scrollbars over the
169
+ * content. Measured once from a throwaway scroller and cached, the same way the measurement context
170
+ * is; without a document (SSR, unit tests) there is nothing to reserve.
171
+ */
172
+ export function verticalScrollbarWidth(): number {
173
+ if (cachedScrollbarWidth !== null) return cachedScrollbarWidth;
174
+ cachedScrollbarWidth = 0;
175
+ const body = typeof document === 'undefined' ? null : document.body;
176
+ if (!body) return cachedScrollbarWidth;
177
+ const host = document.createElement('div');
178
+ host.style.position = 'absolute';
179
+ host.style.top = '-9999px';
180
+ host.style.width = '100px';
181
+ host.style.height = '100px';
182
+ host.style.overflowY = 'scroll';
183
+ const inner = document.createElement('div');
184
+ inner.style.width = '10px';
185
+ inner.style.height = '10px';
186
+ host.appendChild(inner);
187
+ body.appendChild(host);
188
+ cachedScrollbarWidth = Math.max(0, host.offsetWidth - host.clientWidth);
189
+ host.remove();
190
+ return cachedScrollbarWidth;
191
+ }
192
+
193
+ interface CachedThemeColor {
194
+ generation: number;
195
+ value: string;
196
+ }
197
+
198
+ // Reads of CSS custom properties go through getComputedStyle, which is expensive enough that
199
+ // calling it for every colour of every node on every frame measurably slows rendering. Results are
200
+ // memoised per canvas element (a WeakMap so a destroyed canvas never leaks) and tagged with a
201
+ // global generation counter; bumping the generation invalidates every entry at once.
202
+ let themeGeneration = 0;
203
+ const themeColorCache = new WeakMap<HTMLCanvasElement, Map<string, CachedThemeColor>>();
204
+
205
+ /**
206
+ * Drops every cached theme colour so the next read recomputes it. Call this whenever the theme can
207
+ * change (for example from a MutationObserver watching the canvas / ancestor `class` or `style`).
208
+ */
209
+ export function invalidateThemeColors(): void {
210
+ themeGeneration += 1;
211
+ }
212
+
213
+ export function themeColor(
214
+ context: CanvasRenderingContext2D,
215
+ token: string,
216
+ fallback: string,
217
+ ): string {
218
+ if (typeof getComputedStyle !== 'function') return fallback;
219
+ const canvas = context.canvas;
220
+ let byToken = themeColorCache.get(canvas);
221
+ if (!byToken) {
222
+ byToken = new Map();
223
+ themeColorCache.set(canvas, byToken);
224
+ }
225
+ const cached = byToken.get(token);
226
+ if (cached && cached.generation === themeGeneration) return cached.value;
227
+ const value = getComputedStyle(canvas).getPropertyValue(token).trim() || fallback;
228
+ byToken.set(token, { generation: themeGeneration, value });
229
+ return value;
230
+ }
@@ -0,0 +1,15 @@
1
+ import type { ComponentKind } from '../types';
2
+
3
+ export const kindSymbol = Symbol.for('rnative55.web-canvas-ui.kind');
4
+
5
+ export type MarkedComponent<Props = Record<string, unknown>> = ((props: Props) => null) & {
6
+ displayName?: string;
7
+ [kindSymbol]?: ComponentKind;
8
+ };
9
+
10
+ export function createComponent<Props>(kind: ComponentKind): MarkedComponent<Props> {
11
+ const component = (() => null) as MarkedComponent<Props>;
12
+ component.displayName = kind[0].toUpperCase() + kind.slice(1);
13
+ component[kindSymbol] = kind;
14
+ return component;
15
+ }
@@ -0,0 +1,20 @@
1
+ import { number } from './draw-utils';
2
+
3
+ export interface ComponentSize {
4
+ width?: number;
5
+ height?: number;
6
+ }
7
+
8
+ export function componentWidth(props: Record<string, unknown>, fallback: number): number {
9
+ return number(sizeObject(props)?.width, number(props.width, fallback));
10
+ }
11
+
12
+ export function componentHeight(props: Record<string, unknown>, fallback: number): number {
13
+ return number(sizeObject(props)?.height, number(props.height, fallback));
14
+ }
15
+
16
+ function sizeObject(props: Record<string, unknown>): ComponentSize | undefined {
17
+ const size = props.size;
18
+ if (typeof size !== 'object' || size === null || Array.isArray(size)) return undefined;
19
+ return size as ComponentSize;
20
+ }
@@ -0,0 +1,37 @@
1
+ export type { ButtonProps } from './Button';
2
+ export type {
3
+ CanvasViewFit,
4
+ CanvasViewPointerEvent,
5
+ CanvasViewPointerEventHandler,
6
+ CanvasViewProps,
7
+ } from './CanvasView';
8
+ export type { CarouselItemProps, CarouselProps } from './Carousel';
9
+ export type { CheckboxProps } from './Checkbox';
10
+ export type { ChoiceGroupProps, ChoiceOption } from './ChoiceGroup';
11
+ export type { CircleProps } from './Circle';
12
+ export type { CollapseItemProps, CollapseProps } from './Collapse';
13
+ export type {
14
+ CustomDrawClickEvent,
15
+ CustomDrawHitTest,
16
+ CustomDrawProps,
17
+ CustomDrawSize,
18
+ } from './CustomDraw';
19
+ export type { DatePickerProps } from './DatePicker';
20
+ export type { DialogProps } from './Dialog';
21
+ export type { DropdownOption, DropdownProps } from './Dropdown';
22
+ export type { GridItemProps } from './GridItem';
23
+ export type { GridProps } from './Grid';
24
+ export type { GroupProps } from './Group';
25
+ export type { ImageProps } from './Image';
26
+ export type { IconName, IconProps, IconVariant } from './Icon';
27
+ export type { InputProps } from './Input';
28
+ export type { ProgressProps } from './Progress';
29
+ export type { RadioProps } from './Radio';
30
+ export type { RectProps } from './Rect';
31
+ export type { SliderProps } from './Slider';
32
+ export type { SwitchProps } from './Switch';
33
+ export type { TabItemProps, TabsProps } from './Tabs';
34
+ export type { TextProps } from './Text';
35
+ export type { TextareaProps } from './Textarea';
36
+ export type { UploadProps } from './Upload';
37
+ export type { ComponentSize } from './size';
package/components.ts ADDED
@@ -0,0 +1,98 @@
1
+ import React from 'react';
2
+ import { kindSymbol, type MarkedComponent } from './components/shared.ts';
3
+ import type { VNode } from './types.ts';
4
+
5
+ export type * from './components/types.ts';
6
+
7
+ export { Group } from './components/Group';
8
+ export { Grid } from './components/Grid';
9
+ export { GridItem } from './components/GridItem';
10
+ export { Rect } from './components/Rect';
11
+ export { Circle } from './components/Circle';
12
+ export { Text } from './components/Text';
13
+ export { Input } from './components/Input';
14
+ export { Textarea } from './components/Textarea';
15
+ export { Dropdown } from './components/Dropdown';
16
+ export { Slider } from './components/Slider';
17
+ export { Switch } from './components/Switch';
18
+ export { DatePicker } from './components/DatePicker';
19
+ export { Radio } from './components/Radio';
20
+ export { Checkbox } from './components/Checkbox';
21
+ export { Upload } from './components/Upload';
22
+ export { Carousel, CarouselItem } from './components/Carousel';
23
+ export type { ChoiceOption } from './components/ChoiceGroup';
24
+ export { Button } from './components/Button';
25
+ export { Image } from './components/Image';
26
+ export { Icon } from './components/Icon';
27
+ export { Dialog } from './components/Dialog';
28
+ export { Progress } from './components/Progress';
29
+ export { Tabs, TabItem } from './components/Tabs';
30
+ export { Collapse, CollapseItem } from './components/Collapse';
31
+ export { CustomDraw } from './components/CustomDraw';
32
+ export { CanvasView } from './components/CanvasView';
33
+
34
+ function compileChild(child: unknown, path: string): VNode[] {
35
+ if (child === null || child === undefined || typeof child === 'boolean') return [];
36
+ if (Array.isArray(child)) {
37
+ return child.flatMap((item, index) => compileChild(item, `${path}.${index}`));
38
+ }
39
+ if (typeof child === 'string' || typeof child === 'number') return [];
40
+ if (!React.isValidElement(child)) {
41
+ throw new Error(`Unsupported child inside <Canvas> at ${path}. Use Canvas UI components.`);
42
+ }
43
+ if (child.type === React.Fragment) {
44
+ return compileChild((child.props as { children?: React.ReactNode }).children, path);
45
+ }
46
+
47
+ const kind = (child.type as MarkedComponent)[kindSymbol];
48
+ if (!kind) {
49
+ throw new Error(
50
+ `Unsupported component inside <Canvas> at ${path}. Use Canvas UI components.`,
51
+ );
52
+ }
53
+
54
+ const props = { ...(child.props as Record<string, unknown>) };
55
+ const children = props.children as React.ReactNode;
56
+ delete props.children;
57
+ const key = child.key ?? (typeof props.key === 'string' ? props.key : null);
58
+ delete props.key;
59
+
60
+ if (kind === 'text') {
61
+ if (props.text === undefined) {
62
+ props.text =
63
+ typeof children === 'string' || typeof children === 'number'
64
+ ? String(children)
65
+ : '';
66
+ }
67
+ return [{ type: kind, key, props, children: [] }];
68
+ }
69
+ if (kind === 'button' && props.text === undefined) {
70
+ props.text =
71
+ typeof children === 'string' || typeof children === 'number' ? String(children) : '';
72
+ }
73
+
74
+ return [
75
+ {
76
+ type: kind,
77
+ key,
78
+ props,
79
+ children:
80
+ kind === 'group' ||
81
+ kind === 'grid' ||
82
+ kind === 'gridItem' ||
83
+ kind === 'dialog' ||
84
+ kind === 'tabs' ||
85
+ kind === 'tabItem' ||
86
+ kind === 'collapse' ||
87
+ kind === 'collapseItem' ||
88
+ kind === 'carousel' ||
89
+ kind === 'carouselItem'
90
+ ? compileChild(children, path)
91
+ : [],
92
+ },
93
+ ];
94
+ }
95
+
96
+ export function compileChildren(children: unknown): VNode[] {
97
+ return compileChild(children, 'canvas');
98
+ }