@dxos/react-ui-canvas 0.8.4-main.c4373fc → 0.8.4-main.c85a9c8dae

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/package.json CHANGED
@@ -1,9 +1,13 @@
1
1
  {
2
2
  "name": "@dxos/react-ui-canvas",
3
- "version": "0.8.4-main.c4373fc",
3
+ "version": "0.8.4-main.c85a9c8dae",
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.c4373fc",
35
- "@dxos/invariant": "0.8.4-main.c4373fc",
36
- "@dxos/log": "0.8.4-main.c4373fc",
37
- "@dxos/util": "0.8.4-main.c4373fc"
36
+ "@dxos/invariant": "0.8.4-main.c85a9c8dae",
37
+ "@dxos/log": "0.8.4-main.c85a9c8dae",
38
+ "@dxos/util": "0.8.4-main.c85a9c8dae",
39
+ "@dxos/debug": "0.8.4-main.c85a9c8dae"
38
40
  },
39
41
  "devDependencies": {
40
42
  "@types/d3": "^7.4.3",
41
- "@types/react": "~19.2.2",
42
- "@types/react-dom": "~19.2.1",
43
- "effect": "3.18.3",
44
- "react": "~19.2.0",
45
- "react-dom": "~19.2.0",
46
- "vite": "7.1.9",
47
- "@dxos/random": "0.8.4-main.c4373fc",
48
- "@dxos/react-ui": "0.8.4-main.c4373fc",
49
- "@dxos/storybook-utils": "0.8.4-main.c4373fc",
50
- "@dxos/react-ui-theme": "0.8.4-main.c4373fc"
43
+ "@types/react": "~19.2.7",
44
+ "@types/react-dom": "~19.2.3",
45
+ "effect": "3.19.16",
46
+ "react": "~19.2.3",
47
+ "react-dom": "~19.2.3",
48
+ "vite": "^7.1.11",
49
+ "@dxos/random": "0.8.4-main.c85a9c8dae",
50
+ "@dxos/storybook-utils": "0.8.4-main.c85a9c8dae",
51
+ "@dxos/react-ui": "0.8.4-main.c85a9c8dae",
52
+ "@dxos/ui-theme": "0.8.4-main.c85a9c8dae"
51
53
  },
52
54
  "peerDependencies": {
53
- "effect": "3.13.3",
54
- "react": "^19.0.0",
55
- "react-dom": "^19.0.0",
56
- "@dxos/react-ui": "0.8.4-main.c4373fc",
57
- "@dxos/react-ui-theme": "0.8.4-main.c4373fc"
55
+ "effect": "3.19.16",
56
+ "react": "~19.2.3",
57
+ "react-dom": "~19.2.3",
58
+ "@dxos/ui-theme": "0.8.4-main.c85a9c8dae",
59
+ "@dxos/react-ui": "0.8.4-main.c85a9c8dae"
58
60
  },
59
61
  "publishConfig": {
60
62
  "access": "public"
@@ -5,9 +5,9 @@
5
5
  import type { Meta, StoryObj } from '@storybook/react-vite';
6
6
  import React from 'react';
7
7
 
8
- import { withTheme } from '@dxos/react-ui/testing';
8
+ import { withLayout, 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';
@@ -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) => (
@@ -91,7 +92,7 @@ const meta = {
91
92
  title: 'ui/react-ui-canvas/Canvas',
92
93
  component: Grid,
93
94
  render: DefaultStory,
94
- decorators: [withTheme],
95
+ decorators: [withTheme(), withLayout({ layout: 'fullscreen' })],
95
96
  parameters: {
96
97
  layout: 'fullscreen',
97
98
  },
@@ -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/react-ui-theme';
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: _offset = defaultOrigin, ...props }, forwardedRef) => {
33
+ ({ children, classNames, scale: scaleProp = 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: _offset });
41
+ const [{ scale, offset }, setProjection] = useState<ProjectionState>({ scale: scaleProp, offset: offsetProp });
42
42
  useEffect(() => {
43
43
  if (width && height && offset === defaultOrigin) {
44
44
  setProjection({ scale, offset: { x: width / 2, y: height / 2 } });
@@ -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/react-ui-theme';
9
+ import { mx } from '@dxos/ui-theme';
10
10
 
11
11
  export type FPSProps = ThemedClassName<{
12
12
  width?: number;
@@ -73,7 +73,7 @@ export const FPS = ({ classNames, width = 60, height = 30, bar = 'bg-cyan-500' }
73
73
  style={{ width: width + 6 }}
74
74
  className={mx(
75
75
  'relative flex flex-col p-0.5',
76
- 'bg-baseSurface text-xs text-subdued font-thin pointer-events-none border border-separator',
76
+ 'bg-base-surface text-xs text-subdued font-thin pointer-events-none border border-separator',
77
77
  classNames,
78
78
  )}
79
79
  >
@@ -5,7 +5,7 @@
5
5
  import { type Meta, type StoryObj } from '@storybook/react-vite';
6
6
  import React, { useRef, useState } from 'react';
7
7
 
8
- import { withTheme } from '@dxos/react-ui/testing';
8
+ import { withLayout, withTheme } from '@dxos/react-ui/testing';
9
9
 
10
10
  import { type ProjectionState } from '../../hooks';
11
11
 
@@ -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
  );
@@ -26,7 +26,7 @@ const meta = {
26
26
  title: 'ui/react-ui-canvas/Grid',
27
27
  component: GridComponent,
28
28
  render: DefaultStory,
29
- decorators: [withTheme],
29
+ decorators: [withTheme(), withLayout({ layout: 'fullscreen' })],
30
30
  parameters: {
31
31
  layout: 'fullscreen',
32
32
  },
@@ -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/react-ui-theme';
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,23 +27,29 @@ 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 <= 256),
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')}
@@ -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
- };
@@ -4,4 +4,5 @@
4
4
 
5
5
  export * from './projection';
6
6
  export * from './useCanvasContext';
7
+ export * from './useDrag';
7
8
  export * from './useWheel';
@@ -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
+ };
@@ -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
- };
@@ -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-neutral-500 w-[30rem] h-[400px]'>
14
+ <svg className='border border-separator w-[30rem] h-[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' />
@@ -32,7 +32,7 @@ const DefaultStory = () => (
32
32
  const meta = {
33
33
  title: 'ui/react-ui-canvas/svg',
34
34
  render: DefaultStory,
35
- decorators: [withTheme],
35
+ decorators: [withTheme()],
36
36
  parameters: {
37
37
  layout: 'centered',
38
38
  },
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/react-ui-theme';
8
+ import { mx } from '@dxos/ui-theme';
9
9
 
10
10
  import { type Dimension, type Point } from '../types';
11
11