@web-canvas-ui/react 0.1.1 → 0.2.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.
Files changed (157) hide show
  1. package/README.md +172 -76
  2. package/index.js +2682 -0
  3. package/package.json +36 -27
  4. package/types/canvas-handle.d.ts +22 -0
  5. package/types/components/Button/button.d.ts +20 -0
  6. package/types/components/Button/logic.d.ts +3 -0
  7. package/types/components/CanvasView/canvasView.d.ts +28 -0
  8. package/types/components/CanvasView/index.d.ts +2 -0
  9. package/types/components/Carousel/carousel.d.ts +31 -0
  10. package/types/components/Carousel/logic.d.ts +2 -0
  11. package/types/components/Checkbox/checkbox.d.ts +8 -0
  12. package/types/components/ChoiceGroup/choiceGroup.d.ts +33 -0
  13. package/types/components/ChoiceGroup/logic.d.ts +3 -0
  14. package/types/components/Circle/circle.d.ts +9 -0
  15. package/types/components/Collapse/collapse.d.ts +32 -0
  16. package/types/components/Collapse/logic.d.ts +1 -0
  17. package/types/components/CustomDraw/customDraw.d.ts +22 -0
  18. package/types/components/CustomDraw/index.d.ts +2 -0
  19. package/types/components/DatePicker/datePicker.d.ts +24 -0
  20. package/types/components/Dialog/dialog.d.ts +20 -0
  21. package/types/components/Dropdown/dropdown.d.ts +30 -0
  22. package/types/components/Dropdown/logic.d.ts +5 -0
  23. package/{components/Grid/grid.ts → types/components/Grid/grid.d.ts} +3 -4
  24. package/{components/GridItem/gridItem.ts → types/components/GridItem/gridItem.d.ts} +1 -4
  25. package/{components/Group/group.ts → types/components/Group/group.d.ts} +1 -4
  26. package/types/components/Icon/icon.d.ts +14 -0
  27. package/types/components/Image/image.d.ts +12 -0
  28. package/types/components/Input/input.d.ts +32 -0
  29. package/types/components/Progress/progress.d.ts +15 -0
  30. package/types/components/Radio/radio.d.ts +8 -0
  31. package/types/components/Rect/rect.d.ts +11 -0
  32. package/types/components/Slider/logic.d.ts +1 -0
  33. package/types/components/Slider/slider.d.ts +17 -0
  34. package/types/components/Switch/logic.d.ts +2 -0
  35. package/types/components/Switch/switch.d.ts +14 -0
  36. package/types/components/Tabs/logic.d.ts +2 -0
  37. package/types/components/Tabs/tabs.d.ts +35 -0
  38. package/types/components/Text/text.d.ts +39 -0
  39. package/types/components/Textarea/textarea.d.ts +10 -0
  40. package/types/components/Upload/upload.d.ts +20 -0
  41. package/types/components/Video/index.d.ts +2 -0
  42. package/types/components/Video/video.d.ts +20 -0
  43. package/types/components/draw-utils.d.ts +48 -0
  44. package/types/components/shared.d.ts +7 -0
  45. package/types/components/size.d.ts +6 -0
  46. package/{components/types.ts → types/components/types.d.ts} +3 -12
  47. package/types/engine/carousel.d.ts +10 -0
  48. package/types/engine/events.d.ts +14 -0
  49. package/types/engine/hit-test.d.ts +31 -0
  50. package/types/engine/images.d.ts +8 -0
  51. package/types/engine/interaction.d.ts +11 -0
  52. package/types/engine/layout.d.ts +6 -0
  53. package/types/engine/nodes.d.ts +18 -0
  54. package/{engine/state.ts → types/engine/state.d.ts} +27 -22
  55. package/types/engine/text-controls.d.ts +10 -0
  56. package/types/engine/transform.d.ts +13 -0
  57. package/types/engine/videos.d.ts +14 -0
  58. package/types/engine/vnodes.d.ts +36 -0
  59. package/types/engine.d.ts +36 -0
  60. package/types/image-assets.d.ts +2 -0
  61. package/types/input-bridge.d.ts +40 -0
  62. package/types/react/Canvas.d.ts +4 -0
  63. package/types/react/components.d.ts +30 -0
  64. package/types/react/index.d.ts +4 -0
  65. package/types/renderer.d.ts +20 -0
  66. package/types/scene.d.ts +15 -0
  67. package/{types.ts → types/types.d.ts} +10 -142
  68. package/types/video-assets.d.ts +11 -0
  69. package/Canvas.tsx +0 -142
  70. package/__tests__/pure-logic.test.ts +0 -153
  71. package/__tests__/scene-and-node.test.ts +0 -131
  72. package/__tests__/scratch-space.test.ts +0 -30
  73. package/__tests__/textarea-caret.test.ts +0 -96
  74. package/__tests__/utils.test.ts +0 -114
  75. package/canvas-handle.ts +0 -114
  76. package/components/Button/button.ts +0 -57
  77. package/components/Button/logic.ts +0 -11
  78. package/components/CanvasView/canvasView.ts +0 -103
  79. package/components/CanvasView/index.ts +0 -7
  80. package/components/Carousel/carousel.ts +0 -107
  81. package/components/Carousel/logic.ts +0 -36
  82. package/components/Checkbox/checkbox.ts +0 -14
  83. package/components/ChoiceGroup/choiceGroup.ts +0 -172
  84. package/components/ChoiceGroup/logic.ts +0 -14
  85. package/components/Circle/circle.ts +0 -31
  86. package/components/Collapse/collapse.ts +0 -69
  87. package/components/Collapse/logic.ts +0 -14
  88. package/components/CustomDraw/customDraw.ts +0 -50
  89. package/components/CustomDraw/index.ts +0 -7
  90. package/components/DatePicker/datePicker.ts +0 -70
  91. package/components/Dialog/dialog.ts +0 -102
  92. package/components/Dropdown/dropdown.ts +0 -125
  93. package/components/Dropdown/logic.ts +0 -29
  94. package/components/Icon/icon.ts +0 -669
  95. package/components/Image/image.ts +0 -69
  96. package/components/Input/input.ts +0 -139
  97. package/components/Progress/progress.ts +0 -59
  98. package/components/Radio/radio.ts +0 -14
  99. package/components/Rect/rect.ts +0 -35
  100. package/components/Slider/logic.ts +0 -19
  101. package/components/Slider/slider.ts +0 -58
  102. package/components/Switch/logic.ts +0 -8
  103. package/components/Switch/switch.ts +0 -37
  104. package/components/Tabs/logic.ts +0 -14
  105. package/components/Tabs/tabs.ts +0 -78
  106. package/components/Text/text.ts +0 -95
  107. package/components/Textarea/textarea.ts +0 -16
  108. package/components/Upload/upload.ts +0 -66
  109. package/components/draw-utils.ts +0 -230
  110. package/components/shared.ts +0 -15
  111. package/components/size.ts +0 -20
  112. package/components.ts +0 -98
  113. package/engine/__tests__/carousel.test.ts +0 -218
  114. package/engine/__tests__/hit-test.test.ts +0 -282
  115. package/engine/__tests__/layout.test.ts +0 -474
  116. package/engine/carousel.ts +0 -150
  117. package/engine/events.ts +0 -73
  118. package/engine/hit-test.ts +0 -321
  119. package/engine/images.ts +0 -41
  120. package/engine/interaction.ts +0 -442
  121. package/engine/layout.ts +0 -355
  122. package/engine/nodes.ts +0 -166
  123. package/engine/text-controls.ts +0 -122
  124. package/engine/transform.ts +0 -61
  125. package/engine/vnodes.ts +0 -204
  126. package/engine.ts +0 -162
  127. package/image-assets.ts +0 -37
  128. package/index.ts +0 -78
  129. package/input-bridge.ts +0 -285
  130. package/renderer.ts +0 -303
  131. package/scene.ts +0 -122
  132. package/theme.scss +0 -234
  133. package/web-canvas-ui-react-0.1.1.tgz +0 -0
  134. /package/{components/Button/index.ts → types/components/Button/index.d.ts} +0 -0
  135. /package/{components/Carousel/index.ts → types/components/Carousel/index.d.ts} +0 -0
  136. /package/{components/Checkbox/index.ts → types/components/Checkbox/index.d.ts} +0 -0
  137. /package/{components/ChoiceGroup/index.ts → types/components/ChoiceGroup/index.d.ts} +0 -0
  138. /package/{components/Circle/index.ts → types/components/Circle/index.d.ts} +0 -0
  139. /package/{components/Collapse/index.ts → types/components/Collapse/index.d.ts} +0 -0
  140. /package/{components/DatePicker/index.ts → types/components/DatePicker/index.d.ts} +0 -0
  141. /package/{components/Dialog/index.ts → types/components/Dialog/index.d.ts} +0 -0
  142. /package/{components/Dropdown/index.ts → types/components/Dropdown/index.d.ts} +0 -0
  143. /package/{components/Grid/index.ts → types/components/Grid/index.d.ts} +0 -0
  144. /package/{components/GridItem/index.ts → types/components/GridItem/index.d.ts} +0 -0
  145. /package/{components/Group/index.ts → types/components/Group/index.d.ts} +0 -0
  146. /package/{components/Icon/index.ts → types/components/Icon/index.d.ts} +0 -0
  147. /package/{components/Image/index.ts → types/components/Image/index.d.ts} +0 -0
  148. /package/{components/Input/index.ts → types/components/Input/index.d.ts} +0 -0
  149. /package/{components/Progress/index.ts → types/components/Progress/index.d.ts} +0 -0
  150. /package/{components/Radio/index.ts → types/components/Radio/index.d.ts} +0 -0
  151. /package/{components/Rect/index.ts → types/components/Rect/index.d.ts} +0 -0
  152. /package/{components/Slider/index.ts → types/components/Slider/index.d.ts} +0 -0
  153. /package/{components/Switch/index.ts → types/components/Switch/index.d.ts} +0 -0
  154. /package/{components/Tabs/index.ts → types/components/Tabs/index.d.ts} +0 -0
  155. /package/{components/Text/index.ts → types/components/Text/index.d.ts} +0 -0
  156. /package/{components/Textarea/index.ts → types/components/Textarea/index.d.ts} +0 -0
  157. /package/{components/Upload/index.ts → types/components/Upload/index.d.ts} +0 -0
@@ -1,282 +0,0 @@
1
- import { describe, expect, it } from 'vitest';
2
- import { SceneNode } from '../../scene';
3
- import {
4
- canvasPoint,
5
- findCarouselControlHit,
6
- findGridScrollbarHit,
7
- findHit,
8
- isWithinClip,
9
- localPoint,
10
- } from '../hit-test';
11
- import type { EngineCore } from '../state';
12
-
13
- // --- fixtures ---------------------------------------------------------------
14
-
15
- function makeCore(scene: SceneNode, pixelRatio = 1): EngineCore {
16
- // hit-testing only reads `scene` and `pixelRatio` off the core.
17
- return { scene, pixelRatio } as unknown as EngineCore;
18
- }
19
-
20
- function makeCanvas(width = 200, height = 200, left = 0, top = 0): HTMLCanvasElement {
21
- return {
22
- width,
23
- height,
24
- getBoundingClientRect: () => ({
25
- width,
26
- height,
27
- left,
28
- top,
29
- right: left + width,
30
- bottom: top + height,
31
- }),
32
- } as unknown as HTMLCanvasElement;
33
- }
34
-
35
- function link(parent: SceneNode, ...children: SceneNode[]): SceneNode {
36
- for (const child of children) {
37
- child.parent = parent;
38
- parent.children.push(child);
39
- }
40
- return parent;
41
- }
42
-
43
- function root(): SceneNode {
44
- return new SceneNode('root');
45
- }
46
-
47
- // --- canvasPoint ------------------------------------------------------------
48
-
49
- describe('canvasPoint', () => {
50
- it('maps client coordinates 1:1 when the element is unscaled', () => {
51
- const core = makeCore(root());
52
- expect(canvasPoint(core, makeCanvas(200, 200, 0, 0), 50, 60)).toEqual({ x: 50, y: 60 });
53
- });
54
-
55
- it('accounts for CSS scaling (canvas.width / rect.width)', () => {
56
- const core = makeCore(root());
57
- // backing store 400px shown in a 200px CSS box → each CSS px maps to 2.
58
- const canvas = {
59
- width: 400,
60
- height: 400,
61
- getBoundingClientRect: () => ({
62
- width: 200,
63
- height: 200,
64
- left: 0,
65
- top: 0,
66
- right: 200,
67
- bottom: 200,
68
- }),
69
- } as unknown as HTMLCanvasElement;
70
- expect(canvasPoint(core, canvas, 50, 0)).toEqual({ x: 100, y: 0 });
71
- });
72
-
73
- it('divides by the device pixel ratio', () => {
74
- const core = makeCore(root(), 2);
75
- expect(canvasPoint(core, makeCanvas(200, 200, 0, 0), 80, 40)).toEqual({ x: 40, y: 20 });
76
- });
77
-
78
- it('offsets by the bounding-rect origin', () => {
79
- const core = makeCore(root());
80
- expect(canvasPoint(core, makeCanvas(200, 200, 30, 40), 80, 90)).toEqual({ x: 50, y: 50 });
81
- });
82
-
83
- it('returns null for a zero-sized element', () => {
84
- const core = makeCore(root());
85
- expect(canvasPoint(core, makeCanvas(200, 200, 0, 0), 10, 10)).not.toBeNull();
86
- const zero = {
87
- width: 200,
88
- height: 200,
89
- getBoundingClientRect: () => ({
90
- width: 0,
91
- height: 0,
92
- left: 0,
93
- top: 0,
94
- right: 0,
95
- bottom: 0,
96
- }),
97
- } as unknown as HTMLCanvasElement;
98
- expect(canvasPoint(core, zero, 10, 10)).toBeNull();
99
- });
100
- });
101
-
102
- // --- localPoint -------------------------------------------------------------
103
-
104
- describe('localPoint', () => {
105
- it('inverts the accumulated node transform into local space', () => {
106
- const scene = root();
107
- const button = new SceneNode('button', { x: 20, y: 10, width: 100, height: 40 });
108
- link(scene, button);
109
- const core = makeCore(scene);
110
- expect(localPoint(core, makeCanvas(), 30, 15, button)).toEqual({ x: 10, y: 5 });
111
- });
112
-
113
- it('returns null when the target is not in the tree', () => {
114
- const scene = root();
115
- link(scene, new SceneNode('button', { x: 0, y: 0 }));
116
- const orphan = new SceneNode('button');
117
- const core = makeCore(scene);
118
- expect(localPoint(core, makeCanvas(), 10, 10, orphan)).toBeNull();
119
- });
120
- });
121
-
122
- // --- isWithinClip -----------------------------------------------------------
123
-
124
- describe('isWithinClip', () => {
125
- it('clips children to an enclosing scrollable grid', () => {
126
- const scene = root();
127
- const grid = new SceneNode('grid', { x: 0, y: 0, width: 100, height: 100 });
128
- const child = new SceneNode('rect');
129
- link(scene, grid);
130
- link(grid, child);
131
- const core = makeCore(scene);
132
- expect(isWithinClip(core, makeCanvas(), 50, 50, child)).toBe(true);
133
- expect(isWithinClip(core, makeCanvas(), 150, 150, child)).toBe(false);
134
- });
135
-
136
- it('is always true without grid/carousel ancestors', () => {
137
- const scene = root();
138
- const node = new SceneNode('rect');
139
- link(scene, node);
140
- const core = makeCore(scene);
141
- expect(isWithinClip(core, makeCanvas(), 1000, 1000, node)).toBe(true);
142
- });
143
- });
144
-
145
- // --- findHit ----------------------------------------------------------------
146
-
147
- describe('findHit', () => {
148
- it('returns the node and local coordinates for a point inside a control', () => {
149
- const scene = root();
150
- const button = new SceneNode('button', { x: 10, y: 10, width: 100, height: 40 });
151
- link(scene, button);
152
- const core = makeCore(scene);
153
- expect(findHit(core, makeCanvas(), 20, 20)).toEqual({ node: button, x: 10, y: 10 });
154
- });
155
-
156
- it('misses when the point falls outside the control bounds', () => {
157
- const scene = root();
158
- link(scene, new SceneNode('button', { x: 10, y: 10, width: 100, height: 40 }));
159
- const core = makeCore(scene);
160
- expect(findHit(core, makeCanvas(), 20, 60)).toBeNull();
161
- });
162
-
163
- it('ignores invisible nodes', () => {
164
- const scene = root();
165
- link(
166
- scene,
167
- new SceneNode('button', { x: 0, y: 0, width: 100, height: 40, visible: false }),
168
- );
169
- const core = makeCore(scene);
170
- expect(findHit(core, makeCanvas(), 10, 10)).toBeNull();
171
- });
172
-
173
- it('ignores non-interactive nodes such as rect', () => {
174
- const scene = root();
175
- link(scene, new SceneNode('rect', { x: 0, y: 0, width: 100, height: 40 }));
176
- const core = makeCore(scene);
177
- expect(findHit(core, makeCanvas(), 10, 10)).toBeNull();
178
- });
179
-
180
- it('lets an open modal dialog occlude controls outside it', () => {
181
- const scene = root();
182
- const button = new SceneNode('button', { x: 0, y: 0, width: 100, height: 40 });
183
- const dialog = new SceneNode('dialog', { x: 0, y: 0, width: 200, height: 200, open: true });
184
- link(scene, button, dialog);
185
- const core = makeCore(scene);
186
- // Click sits inside the (earlier) button, but the dialog is modal.
187
- expect(findHit(core, makeCanvas(), 10, 10)?.node.type).toBe('dialog');
188
- });
189
- });
190
-
191
- // --- findGridScrollbarHit ---------------------------------------------------
192
-
193
- describe('findGridScrollbarHit', () => {
194
- it('hits the thumb when the grid overflows', () => {
195
- const scene = root();
196
- const grid = new SceneNode('grid', {
197
- x: 0,
198
- y: 0,
199
- width: 100,
200
- height: 100,
201
- _gridWidth: 100,
202
- _gridContentHeight: 200,
203
- _scrollTop: 0,
204
- });
205
- link(scene, grid);
206
- const core = makeCore(scene);
207
- // track occupies x∈[94,100]; thumb height 50 at scrollTop 0 → y∈[0,50].
208
- const hit = findGridScrollbarHit(core, makeCanvas(), 97, 10);
209
- expect(hit?.node).toBe(grid);
210
- expect(hit?.scrollTop).toBe(0);
211
- expect(hit?.pointerY).toBe(10);
212
- });
213
-
214
- it('returns null when there is nothing to scroll', () => {
215
- const scene = root();
216
- const grid = new SceneNode('grid', {
217
- x: 0,
218
- y: 0,
219
- width: 100,
220
- height: 100,
221
- _gridWidth: 100,
222
- _gridContentHeight: 100,
223
- });
224
- link(scene, grid);
225
- const core = makeCore(scene);
226
- expect(findGridScrollbarHit(core, makeCanvas(), 97, 10)).toBeNull();
227
- });
228
- });
229
-
230
- // --- findCarouselControlHit -------------------------------------------------
231
-
232
- describe('findCarouselControlHit', () => {
233
- function carouselScene() {
234
- const scene = root();
235
- const carousel = new SceneNode('carousel', {
236
- x: 0,
237
- y: 0,
238
- width: 300,
239
- height: 180,
240
- _activeIndex: 0,
241
- });
242
- link(scene, carousel);
243
- link(
244
- carousel,
245
- new SceneNode('carouselItem'),
246
- new SceneNode('carouselItem'),
247
- new SceneNode('carouselItem'),
248
- );
249
- return { scene, carousel };
250
- }
251
-
252
- it('maps a left-arrow press to the previous index', () => {
253
- const { scene, carousel } = carouselScene();
254
- const core = makeCore(scene);
255
- // |y - height/2| <= 20 and x <= 42 → previous (0 - 1 = -1).
256
- expect(findCarouselControlHit(core, makeCanvas(), 10, 90)).toEqual({
257
- node: carousel,
258
- index: -1,
259
- });
260
- });
261
-
262
- it('maps a right-arrow press to the next index', () => {
263
- const { scene, carousel } = carouselScene();
264
- const core = makeCore(scene);
265
- // x >= width - 42 → next (0 + 1 = 1).
266
- expect(findCarouselControlHit(core, makeCanvas(), 295, 90)).toEqual({
267
- node: carousel,
268
- index: 1,
269
- });
270
- });
271
-
272
- it('returns null for a press in the carousel body', () => {
273
- const { scene } = carouselScene();
274
- const core = makeCore(scene);
275
- expect(findCarouselControlHit(core, makeCanvas(), 150, 90)).toBeNull();
276
- });
277
-
278
- it('returns null when the scene has no carousel', () => {
279
- const core = makeCore(root());
280
- expect(findCarouselControlHit(core, makeCanvas(), 150, 90)).toBeNull();
281
- });
282
- });