@dxos/react-ui-canvas 0.8.4-main.ae835ea → 0.8.4-main.bc674ce
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/lib/browser/index.mjs +366 -368
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +366 -368
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/components/Canvas/Canvas.d.ts +1 -1
- package/dist/types/src/components/Canvas/Canvas.stories.d.ts.map +1 -1
- package/dist/types/src/components/Grid/Grid.d.ts +2 -2
- package/dist/types/src/components/Grid/Grid.d.ts.map +1 -1
- package/dist/types/src/components/Grid/Grid.stories.d.ts +1 -1
- package/dist/types/src/hooks/index.d.ts +1 -0
- package/dist/types/src/hooks/index.d.ts.map +1 -1
- package/dist/types/src/hooks/useDrag.d.ts +6 -0
- package/dist/types/src/hooks/useDrag.d.ts.map +1 -0
- package/dist/types/src/hooks/useWheel.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +23 -21
- package/src/components/Canvas/Canvas.stories.tsx +6 -5
- package/src/components/Canvas/Canvas.tsx +3 -3
- package/src/components/FPS.tsx +2 -2
- package/src/components/Grid/Grid.stories.tsx +1 -1
- package/src/components/Grid/Grid.tsx +13 -11
- package/src/hooks/index.ts +1 -0
- package/src/hooks/useDrag.tsx +96 -0
- package/src/hooks/useWheel.tsx +0 -27
- package/src/util/svg.stories.tsx +1 -1
- package/src/util/svg.tsx +1 -1
package/package.json
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/react-ui-canvas",
|
|
3
|
-
"version": "0.8.4-main.
|
|
3
|
+
"version": "0.8.4-main.bc674ce",
|
|
4
4
|
"description": "A canvas component.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/dxos/dxos"
|
|
10
|
+
},
|
|
7
11
|
"license": "MIT",
|
|
8
12
|
"author": "DXOS.org",
|
|
9
13
|
"type": "module",
|
|
@@ -24,37 +28,35 @@
|
|
|
24
28
|
"src"
|
|
25
29
|
],
|
|
26
30
|
"dependencies": {
|
|
27
|
-
"@preact-signals/safe-react": "^0.9.0",
|
|
28
|
-
"@preact/signals-core": "^1.12.1",
|
|
29
31
|
"@radix-ui/react-context": "1.1.1",
|
|
30
32
|
"bind-event-listener": "^3.0.0",
|
|
31
33
|
"d3": "^7.9.0",
|
|
32
34
|
"react-resize-detector": "^11.0.1",
|
|
33
35
|
"transformation-matrix": "^2.16.1",
|
|
34
|
-
"@dxos/debug": "0.8.4-main.
|
|
35
|
-
"@dxos/invariant": "0.8.4-main.
|
|
36
|
-
"@dxos/log": "0.8.4-main.
|
|
37
|
-
"@dxos/util": "0.8.4-main.
|
|
36
|
+
"@dxos/debug": "0.8.4-main.bc674ce",
|
|
37
|
+
"@dxos/invariant": "0.8.4-main.bc674ce",
|
|
38
|
+
"@dxos/log": "0.8.4-main.bc674ce",
|
|
39
|
+
"@dxos/util": "0.8.4-main.bc674ce"
|
|
38
40
|
},
|
|
39
41
|
"devDependencies": {
|
|
40
42
|
"@types/d3": "^7.4.3",
|
|
41
|
-
"@types/react": "~19.2.
|
|
42
|
-
"@types/react-dom": "~19.2.
|
|
43
|
-
"effect": "3.
|
|
44
|
-
"react": "~19.2.
|
|
45
|
-
"react-dom": "~19.2.
|
|
43
|
+
"@types/react": "~19.2.7",
|
|
44
|
+
"@types/react-dom": "~19.2.3",
|
|
45
|
+
"effect": "3.19.11",
|
|
46
|
+
"react": "~19.2.3",
|
|
47
|
+
"react-dom": "~19.2.3",
|
|
46
48
|
"vite": "7.1.9",
|
|
47
|
-
"@dxos/random": "0.8.4-main.
|
|
48
|
-
"@dxos/react-ui": "0.8.4-main.
|
|
49
|
-
"@dxos/
|
|
50
|
-
"@dxos/
|
|
49
|
+
"@dxos/random": "0.8.4-main.bc674ce",
|
|
50
|
+
"@dxos/react-ui": "0.8.4-main.bc674ce",
|
|
51
|
+
"@dxos/storybook-utils": "0.8.4-main.bc674ce",
|
|
52
|
+
"@dxos/ui-theme": "0.8.4-main.bc674ce"
|
|
51
53
|
},
|
|
52
54
|
"peerDependencies": {
|
|
53
|
-
"effect": "3.
|
|
54
|
-
"react": "
|
|
55
|
-
"react-dom": "
|
|
56
|
-
"@dxos/react-ui": "0.8.4-main.
|
|
57
|
-
"@dxos/
|
|
55
|
+
"effect": "3.19.11",
|
|
56
|
+
"react": "~19.2.3",
|
|
57
|
+
"react-dom": "~19.2.3",
|
|
58
|
+
"@dxos/react-ui": "0.8.4-main.bc674ce",
|
|
59
|
+
"@dxos/ui-theme": "0.8.4-main.bc674ce"
|
|
58
60
|
},
|
|
59
61
|
"publishConfig": {
|
|
60
62
|
"access": "public"
|
|
@@ -7,7 +7,7 @@ import React from 'react';
|
|
|
7
7
|
|
|
8
8
|
import { withTheme } from '@dxos/react-ui/testing';
|
|
9
9
|
|
|
10
|
-
import { useCanvasContext, useWheel } from '../../hooks';
|
|
10
|
+
import { useCanvasContext, useDrag, useWheel } from '../../hooks';
|
|
11
11
|
import { type Point } from '../../types';
|
|
12
12
|
import { testId } from '../../util';
|
|
13
13
|
import { Grid, type GridProps } from '../Grid';
|
|
@@ -32,14 +32,14 @@ const DefaultStory = (props: GridProps) => {
|
|
|
32
32
|
|
|
33
33
|
const TwoCanvases = (props: GridProps) => {
|
|
34
34
|
return (
|
|
35
|
-
<div className='grid grid-cols-2 gap-2
|
|
36
|
-
<div className='
|
|
35
|
+
<div className='grid grid-cols-2 gap-2 is-full bs-full'>
|
|
36
|
+
<div className='bs-full relative'>
|
|
37
37
|
<Canvas>
|
|
38
38
|
<Grid {...props} />
|
|
39
39
|
<Content />
|
|
40
40
|
</Canvas>
|
|
41
41
|
</div>
|
|
42
|
-
<div className='
|
|
42
|
+
<div className='bs-full relative'>
|
|
43
43
|
<Canvas>
|
|
44
44
|
<Grid {...props} />
|
|
45
45
|
<Content />
|
|
@@ -51,6 +51,7 @@ const TwoCanvases = (props: GridProps) => {
|
|
|
51
51
|
|
|
52
52
|
const Content = () => {
|
|
53
53
|
useWheel();
|
|
54
|
+
useDrag();
|
|
54
55
|
return (
|
|
55
56
|
<div>
|
|
56
57
|
{points.map(({ x, y }, i) => (
|
|
@@ -106,6 +107,6 @@ export const Default: Story = {
|
|
|
106
107
|
};
|
|
107
108
|
|
|
108
109
|
export const SideBySide: Story = {
|
|
109
|
-
args: { size: 16 },
|
|
110
110
|
render: TwoCanvases,
|
|
111
|
+
args: { size: 16 },
|
|
111
112
|
};
|
|
@@ -15,7 +15,7 @@ import React, {
|
|
|
15
15
|
import { useResizeDetector } from 'react-resize-detector';
|
|
16
16
|
|
|
17
17
|
import { type ThemedClassName } from '@dxos/react-ui';
|
|
18
|
-
import { mx } from '@dxos/
|
|
18
|
+
import { mx } from '@dxos/ui-theme';
|
|
19
19
|
|
|
20
20
|
import { CanvasContext, ProjectionMapper, type ProjectionState, defaultOrigin } from '../../hooks';
|
|
21
21
|
|
|
@@ -30,7 +30,7 @@ export type CanvasProps = ThemedClassName<PropsWithChildren<Partial<ProjectionSt
|
|
|
30
30
|
* Manages CSS projection.
|
|
31
31
|
*/
|
|
32
32
|
export const Canvas = forwardRef<CanvasController, CanvasProps>(
|
|
33
|
-
({ children, classNames, scale: _scale = 1, offset:
|
|
33
|
+
({ children, classNames, scale: _scale = 1, offset: offsetProp = defaultOrigin, ...props }, forwardedRef) => {
|
|
34
34
|
// Size.
|
|
35
35
|
const { ref, width = 0, height = 0 } = useResizeDetector();
|
|
36
36
|
|
|
@@ -38,7 +38,7 @@ export const Canvas = forwardRef<CanvasController, CanvasProps>(
|
|
|
38
38
|
const [ready, setReady] = useState(false);
|
|
39
39
|
|
|
40
40
|
// Projection.
|
|
41
|
-
const [{ scale, offset }, setProjection] = useState<ProjectionState>({ scale: _scale, offset:
|
|
41
|
+
const [{ scale, offset }, setProjection] = useState<ProjectionState>({ scale: _scale, offset: offsetProp });
|
|
42
42
|
useEffect(() => {
|
|
43
43
|
if (width && height && offset === defaultOrigin) {
|
|
44
44
|
setProjection({ scale, offset: { x: width / 2, y: height / 2 } });
|
package/src/components/FPS.tsx
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
import React, { useEffect, useReducer, useRef } from 'react';
|
|
7
7
|
|
|
8
8
|
import { type ThemedClassName } from '@dxos/react-ui';
|
|
9
|
-
import { mx } from '@dxos/
|
|
9
|
+
import { mx } from '@dxos/ui-theme';
|
|
10
10
|
|
|
11
11
|
export type FPSProps = ThemedClassName<{
|
|
12
12
|
width?: number;
|
|
@@ -78,7 +78,7 @@ export const FPS = ({ classNames, width = 60, height = 30, bar = 'bg-cyan-500' }
|
|
|
78
78
|
)}
|
|
79
79
|
>
|
|
80
80
|
<div>{fps[len - 1]} FPS</div>
|
|
81
|
-
<div className='
|
|
81
|
+
<div className='is-full relative' style={{ height }}>
|
|
82
82
|
{fps.map((frame, i) => (
|
|
83
83
|
<div
|
|
84
84
|
key={`fps-${i}`}
|
|
@@ -16,7 +16,7 @@ const DefaultStory = (props: GridProps) => {
|
|
|
16
16
|
const [{ scale, offset }] = useState<ProjectionState>({ scale: 1, offset: { x: 0, y: 0 } });
|
|
17
17
|
|
|
18
18
|
return (
|
|
19
|
-
<div ref={ref} className='grow'>
|
|
19
|
+
<div role='none' ref={ref} className='grow'>
|
|
20
20
|
<GridComponent scale={scale} offset={offset} {...props} />
|
|
21
21
|
</div>
|
|
22
22
|
);
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
import React, { forwardRef, useId, useMemo } from 'react';
|
|
6
6
|
|
|
7
7
|
import { type ThemedClassName, useForwardedRef } from '@dxos/react-ui';
|
|
8
|
-
import { mx } from '@dxos/
|
|
8
|
+
import { mx } from '@dxos/ui-theme';
|
|
9
9
|
|
|
10
10
|
import { useCanvasContext } from '../../hooks';
|
|
11
11
|
import { type Point } from '../../types';
|
|
@@ -18,6 +18,8 @@ const defaultOffset: Point = { x: 0, y: 0 };
|
|
|
18
18
|
|
|
19
19
|
const createId = (parent: string, grid: number) => `dx-canvas-grid-${parent}-${grid}`;
|
|
20
20
|
|
|
21
|
+
// TODO(burdon): Click to drag.
|
|
22
|
+
|
|
21
23
|
export type GridProps = ThemedClassName<{
|
|
22
24
|
size?: number;
|
|
23
25
|
scale?: number;
|
|
@@ -25,29 +27,35 @@ export type GridProps = ThemedClassName<{
|
|
|
25
27
|
showAxes?: boolean;
|
|
26
28
|
}>;
|
|
27
29
|
|
|
30
|
+
// TODO(burdon): Use id of parent canvas.
|
|
31
|
+
export const Grid = (props: GridProps) => {
|
|
32
|
+
const { scale, offset } = useCanvasContext();
|
|
33
|
+
return <GridComponent {...props} scale={scale} offset={offset} />;
|
|
34
|
+
};
|
|
35
|
+
|
|
28
36
|
export const GridComponent = forwardRef<SVGSVGElement, GridProps>(
|
|
29
37
|
(
|
|
30
38
|
{ size: gridSize = defaultGridSize, scale = 1, offset = defaultOffset, showAxes = true, classNames },
|
|
31
39
|
forwardedRef,
|
|
32
40
|
) => {
|
|
33
41
|
const svgRef = useForwardedRef(forwardedRef);
|
|
42
|
+
const { width = 0, height = 0 } = svgRef.current?.getBoundingClientRect() ?? {};
|
|
43
|
+
|
|
34
44
|
const instanceId = useId();
|
|
35
45
|
const grids = useMemo(
|
|
36
46
|
() =>
|
|
37
47
|
gridRatios
|
|
38
48
|
.map((ratio) => ({ id: ratio, size: ratio * gridSize * scale }))
|
|
39
|
-
.filter(({ size }) => size >= gridSize && size <=
|
|
49
|
+
.filter(({ size }) => size >= gridSize && size <= 128),
|
|
40
50
|
[gridSize, scale],
|
|
41
51
|
);
|
|
42
52
|
|
|
43
|
-
const { width = 0, height = 0 } = svgRef.current?.getBoundingClientRect() ?? {};
|
|
44
|
-
|
|
45
53
|
return (
|
|
46
54
|
<svg
|
|
47
55
|
{...testId('dx-canvas-grid')}
|
|
48
56
|
ref={svgRef}
|
|
49
57
|
className={mx(
|
|
50
|
-
'absolute inset-0
|
|
58
|
+
'absolute inset-0 is-full bs-full pointer-events-none touch-none select-none',
|
|
51
59
|
'stroke-neutral-500',
|
|
52
60
|
classNames,
|
|
53
61
|
)}
|
|
@@ -79,9 +87,3 @@ export const GridComponent = forwardRef<SVGSVGElement, GridProps>(
|
|
|
79
87
|
);
|
|
80
88
|
},
|
|
81
89
|
);
|
|
82
|
-
|
|
83
|
-
// TODO(burdon): Use id of parent canvas.
|
|
84
|
-
export const Grid = (props: GridProps) => {
|
|
85
|
-
const { scale, offset } = useCanvasContext();
|
|
86
|
-
return <GridComponent {...props} scale={scale} offset={offset} />;
|
|
87
|
-
};
|
package/src/hooks/index.ts
CHANGED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { bind } from 'bind-event-listener';
|
|
6
|
+
import { useEffect, useRef } from 'react';
|
|
7
|
+
|
|
8
|
+
import { useCanvasContext } from './useCanvasContext';
|
|
9
|
+
|
|
10
|
+
export type DragOptions = {
|
|
11
|
+
// TODO(burdon): Add constraints?
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Handle drag events to update the transform state (offset).
|
|
16
|
+
*/
|
|
17
|
+
export const useDrag = (_options: DragOptions = {}) => {
|
|
18
|
+
const { root, setProjection } = useCanvasContext();
|
|
19
|
+
|
|
20
|
+
// Track drag state.
|
|
21
|
+
const state = useRef<{
|
|
22
|
+
panning: boolean;
|
|
23
|
+
x: number;
|
|
24
|
+
y: number;
|
|
25
|
+
}>({ panning: false, x: 0, y: 0 });
|
|
26
|
+
|
|
27
|
+
useEffect(() => {
|
|
28
|
+
if (!root) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// TODO(burdon): Use d3-drag?
|
|
33
|
+
return bind(root, {
|
|
34
|
+
type: 'pointerdown',
|
|
35
|
+
listener: (ev: PointerEvent) => {
|
|
36
|
+
// Only left click.
|
|
37
|
+
if (ev.button !== 0) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (ev.defaultPrevented) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (ev.target !== root || ev.shiftKey) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Check if clicking on an interactive element?
|
|
50
|
+
// For now, assume if it bubbled to root, it's fair game unless prevented.
|
|
51
|
+
|
|
52
|
+
ev.preventDefault(); // Prevent text selection.
|
|
53
|
+
root.setPointerCapture(ev.pointerId);
|
|
54
|
+
state.current = { panning: true, x: ev.clientX, y: ev.clientY };
|
|
55
|
+
|
|
56
|
+
const moveUnbind = bind(root, {
|
|
57
|
+
type: 'pointermove',
|
|
58
|
+
listener: (ev: PointerEvent) => {
|
|
59
|
+
if (!state.current.panning) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Calculate delta.
|
|
64
|
+
const dx = ev.clientX - state.current.x;
|
|
65
|
+
const dy = ev.clientY - state.current.y;
|
|
66
|
+
|
|
67
|
+
state.current.x = ev.clientX;
|
|
68
|
+
state.current.y = ev.clientY;
|
|
69
|
+
|
|
70
|
+
setProjection((prev) => ({
|
|
71
|
+
...prev,
|
|
72
|
+
offset: {
|
|
73
|
+
x: prev.offset.x + dx,
|
|
74
|
+
y: prev.offset.y + dy,
|
|
75
|
+
},
|
|
76
|
+
}));
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
const upUnbind = bind(root, {
|
|
81
|
+
type: 'pointerup',
|
|
82
|
+
listener: (ev: PointerEvent) => {
|
|
83
|
+
state.current.panning = false;
|
|
84
|
+
root.releasePointerCapture(ev.pointerId);
|
|
85
|
+
moveUnbind();
|
|
86
|
+
upUnbind();
|
|
87
|
+
// Clean up lostpointercapture as well?
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
// Handle cancellation/lost capture just in case?
|
|
92
|
+
// Using setPointerCapture usually handles this well on the element.
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
}, [root]);
|
|
96
|
+
};
|
package/src/hooks/useWheel.tsx
CHANGED
|
@@ -34,9 +34,6 @@ export const useWheel = (options: WheelOptions = defaultOptions) => {
|
|
|
34
34
|
options: { capture: true, passive: false },
|
|
35
35
|
listener: (ev: WheelEvent) => {
|
|
36
36
|
const zooming = isWheelZooming(ev);
|
|
37
|
-
if (!hasFocus(root) && !zooming) {
|
|
38
|
-
return;
|
|
39
|
-
}
|
|
40
37
|
|
|
41
38
|
ev.preventDefault();
|
|
42
39
|
if (zooming && !options.zoom) {
|
|
@@ -82,27 +79,3 @@ const isWheelZooming = (ev: WheelEvent): boolean => {
|
|
|
82
79
|
|
|
83
80
|
return false;
|
|
84
81
|
};
|
|
85
|
-
|
|
86
|
-
const hasFocus = (element: HTMLElement): boolean => {
|
|
87
|
-
const activeElement = document.activeElement;
|
|
88
|
-
if (!activeElement) {
|
|
89
|
-
return false;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
// Handle shadow DOM.
|
|
93
|
-
let shadowActive = activeElement;
|
|
94
|
-
while (shadowActive?.shadowRoot?.activeElement) {
|
|
95
|
-
shadowActive = shadowActive.shadowRoot.activeElement;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
// Check if element or any parent has focus.
|
|
99
|
-
let current: HTMLElement | null = element;
|
|
100
|
-
while (current) {
|
|
101
|
-
if (current === activeElement || current === shadowActive) {
|
|
102
|
-
return true;
|
|
103
|
-
}
|
|
104
|
-
current = current.parentElement;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
return false;
|
|
108
|
-
};
|
package/src/util/svg.stories.tsx
CHANGED
|
@@ -11,7 +11,7 @@ import { Arrow, createPath } from './svg';
|
|
|
11
11
|
import { testId } from './util';
|
|
12
12
|
|
|
13
13
|
const DefaultStory = () => (
|
|
14
|
-
<svg className='border border-
|
|
14
|
+
<svg className='border border-separator is-[30rem] bs-[400px]'>
|
|
15
15
|
<defs>
|
|
16
16
|
<Arrow id='arrow-start' classNames='fill-none stroke-red-500' dir='start' />
|
|
17
17
|
<Arrow id='arrow-end' classNames='fill-none stroke-red-500' dir='end' />
|
package/src/util/svg.tsx
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
import React, { type PropsWithChildren, type SVGProps } from 'react';
|
|
6
6
|
|
|
7
7
|
import { type ThemedClassName } from '@dxos/react-ui';
|
|
8
|
-
import { mx } from '@dxos/
|
|
8
|
+
import { mx } from '@dxos/ui-theme';
|
|
9
9
|
|
|
10
10
|
import { type Dimension, type Point } from '../types';
|
|
11
11
|
|