@dxos/react-ui-attention 0.16.0 → 0.17.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/lib/chunk-ViewStateProvider.mjs +1 -1
- package/dist/lib/chunk-types.mjs +240 -9
- package/dist/lib/chunk-types.mjs.map +1 -1
- package/dist/lib/index.mjs +242 -56
- package/dist/lib/index.mjs.map +1 -1
- package/dist/lib/testing.mjs +1 -1
- package/dist/lib/types.mjs +1 -1
- package/dist/types/src/components/AttentionGlyph/AttentionGlyph.d.ts +6 -11
- package/dist/types/src/components/AttentionGlyph/AttentionGlyph.d.ts.map +1 -1
- package/dist/types/src/components/AttentionGlyph/AttentionGlyph.stories.d.ts +13 -6
- package/dist/types/src/components/AttentionGlyph/AttentionGlyph.stories.d.ts.map +1 -1
- package/dist/types/src/components/RemoteCursor/RemoteCursor.d.ts +18 -0
- package/dist/types/src/components/RemoteCursor/RemoteCursor.d.ts.map +1 -0
- package/dist/types/src/components/RemoteCursor/RemoteCursorLayer.d.ts +32 -0
- package/dist/types/src/components/RemoteCursor/RemoteCursorLayer.d.ts.map +1 -0
- package/dist/types/src/components/RemoteCursor/RemoteCursorLayer.test.d.ts +2 -0
- package/dist/types/src/components/RemoteCursor/RemoteCursorLayer.test.d.ts.map +1 -0
- package/dist/types/src/components/RemoteCursor/index.d.ts +3 -0
- package/dist/types/src/components/RemoteCursor/index.d.ts.map +1 -0
- package/dist/types/src/components/ScrollScope/ScrollScope.d.ts +11 -0
- package/dist/types/src/components/ScrollScope/ScrollScope.d.ts.map +1 -0
- package/dist/types/src/components/ScrollScope/ScrollScope.stories.d.ts +15 -0
- package/dist/types/src/components/ScrollScope/ScrollScope.stories.d.ts.map +1 -0
- package/dist/types/src/components/ScrollScope/index.d.ts +2 -0
- package/dist/types/src/components/ScrollScope/index.d.ts.map +1 -0
- package/dist/types/src/components/index.d.ts +2 -0
- package/dist/types/src/components/index.d.ts.map +1 -1
- package/dist/types/src/types/Activity.d.ts +71 -0
- package/dist/types/src/types/Activity.d.ts.map +1 -0
- package/dist/types/src/types/Activity.test.d.ts +2 -0
- package/dist/types/src/types/Activity.test.d.ts.map +1 -0
- package/dist/types/src/types/ScrollPosition.d.ts +24 -0
- package/dist/types/src/types/ScrollPosition.d.ts.map +1 -0
- package/dist/types/src/types/ViewLocation.d.ts +16 -0
- package/dist/types/src/types/ViewLocation.d.ts.map +1 -0
- package/dist/types/src/types/ViewState.d.ts +44 -2
- package/dist/types/src/types/ViewState.d.ts.map +1 -1
- package/dist/types/src/types/index.d.ts +3 -0
- package/dist/types/src/types/index.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +17 -16
- package/src/components/AttentionGlyph/AttentionGlyph.stories.tsx +424 -31
- package/src/components/AttentionGlyph/AttentionGlyph.tsx +80 -56
- package/src/components/RemoteCursor/RemoteCursor.tsx +55 -0
- package/src/components/RemoteCursor/RemoteCursorLayer.test.tsx +98 -0
- package/src/components/RemoteCursor/RemoteCursorLayer.tsx +97 -0
- package/src/components/RemoteCursor/index.ts +6 -0
- package/src/components/ScrollScope/ScrollScope.stories.tsx +97 -0
- package/src/components/ScrollScope/ScrollScope.tsx +59 -0
- package/src/components/ScrollScope/index.ts +5 -0
- package/src/components/index.ts +2 -0
- package/src/types/Activity.test.ts +53 -0
- package/src/types/Activity.ts +110 -0
- package/src/types/ScrollPosition.ts +34 -0
- package/src/types/ViewLocation.ts +28 -0
- package/src/types/ViewState.test.ts +74 -1
- package/src/types/ViewState.ts +217 -4
- package/src/types/index.ts +3 -0
|
@@ -4,79 +4,103 @@
|
|
|
4
4
|
|
|
5
5
|
import React, { type ComponentPropsWithoutRef, type CSSProperties, forwardRef, useMemo } from 'react';
|
|
6
6
|
|
|
7
|
-
import
|
|
7
|
+
import { useIconHref } from '@dxos/react-ui/Icon';
|
|
8
8
|
import { mx } from '@dxos/ui-theme';
|
|
9
|
-
import { type ThemedClassName } from '@dxos/ui-types';
|
|
9
|
+
import { type Hue, type ThemedClassName } from '@dxos/ui-types';
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
import * as Activity from '../../types/Activity.ts';
|
|
12
12
|
|
|
13
13
|
export type AttentionGlyphProps = ThemedClassName<Omit<ComponentPropsWithoutRef<'span'>, 'children'>> & {
|
|
14
14
|
/** The item is the attended one: the glyph fills with the accent. */
|
|
15
15
|
attended?: boolean;
|
|
16
16
|
/** The attended item is inside this one: the glyph takes the softer contains tint. */
|
|
17
17
|
containsAttended?: boolean;
|
|
18
|
-
/**
|
|
19
|
-
|
|
20
|
-
/** How many others are viewing the item. */
|
|
21
|
-
presence?: AttentionGlyphPresence;
|
|
18
|
+
/** Activity on the item; the glyph draws one treatment per slot and animates at most one (see `Activity.resolve`). */
|
|
19
|
+
treatments?: readonly Activity.Treatment[];
|
|
22
20
|
};
|
|
23
21
|
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
fill='none'
|
|
28
|
-
aria-hidden
|
|
29
|
-
data-scope='attention-glyph'
|
|
30
|
-
data-part='mark'
|
|
31
|
-
className={'dx-attention-glyph-mark'}
|
|
32
|
-
>
|
|
33
|
-
<circle cx='6' cy='6' r='2.5' fill='currentColor' />
|
|
34
|
-
</svg>
|
|
35
|
-
);
|
|
22
|
+
const VIEWERS_PATH =
|
|
23
|
+
'M6.75 8.27311C7.38815 7.72296 7.79212 6.90866 7.79212 6C7.79212 5.09134 7.38815 4.27704 6.75 3.72689C7.06722 3.58122 7.42019 3.5 7.79212 3.5C9.17283 3.5 10.2921 4.61929 10.2921 6C10.2921 7.38071 9.17283 8.5 7.79212 8.5C7.42019 8.5 7.06723 8.41878 6.75 8.27311Z';
|
|
24
|
+
const STAR_PATH = 'M6 2.2l1 2.8 2.8 1-2.8 1-1 2.8-1-2.8-2.8-1 2.8-1z';
|
|
36
25
|
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
aria-hidden
|
|
42
|
-
data-scope='attention-glyph'
|
|
43
|
-
data-part='mark'
|
|
44
|
-
className={'dx-attention-glyph-mark'}
|
|
45
|
-
>
|
|
46
|
-
<path
|
|
47
|
-
d='M6.75 8.27311C7.38815 7.72296 7.79212 6.90866 7.79212 6C7.79212 5.09134 7.38815 4.27704 6.75 3.72689C7.06722 3.58122 7.42019 3.5 7.79212 3.5C9.17283 3.5 10.2921 4.61929 10.2921 6C10.2921 7.38071 9.17283 8.5 7.79212 8.5C7.42019 8.5 7.06723 8.41878 6.75 8.27311Z'
|
|
48
|
-
fill='currentColor'
|
|
49
|
-
/>
|
|
50
|
-
<circle cx='4.25' cy='6' r='2.5' fill='currentColor' />
|
|
51
|
-
</svg>
|
|
52
|
-
);
|
|
26
|
+
const DEFAULT_FILL_OPACITY = 0.25;
|
|
27
|
+
|
|
28
|
+
/** A hue's saturated fill shade, which reads on both themes at the glyph's size. */
|
|
29
|
+
const solid = (hue: Hue) => `var(--color-${hue}-bg)`;
|
|
53
30
|
|
|
54
|
-
|
|
55
|
-
const
|
|
56
|
-
|
|
57
|
-
|
|
31
|
+
const IconMark = ({ icon }: { icon: string }) => {
|
|
32
|
+
const href = useIconHref(icon);
|
|
33
|
+
return <use data-part='mark' href={href} x='2' y='2' width='8' height='8' />;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const Mark = ({ icon }: { icon: string }) => {
|
|
37
|
+
switch (icon) {
|
|
38
|
+
case 'dot':
|
|
39
|
+
return <circle data-part='mark' cx='6' cy='6' r='2.5' />;
|
|
40
|
+
case 'dots':
|
|
41
|
+
return (
|
|
42
|
+
<g data-part='mark'>
|
|
43
|
+
<path d={VIEWERS_PATH} />
|
|
44
|
+
<circle cx='4.25' cy='6' r='2.5' />
|
|
45
|
+
</g>
|
|
46
|
+
);
|
|
47
|
+
case 'star':
|
|
48
|
+
return <path data-part='mark' d={STAR_PATH} />;
|
|
49
|
+
default:
|
|
50
|
+
return <IconMark icon={icon} />;
|
|
51
|
+
}
|
|
58
52
|
};
|
|
59
53
|
|
|
60
54
|
/**
|
|
61
|
-
* A small square marking an item's attention state
|
|
62
|
-
* else is viewing it or whether it is syncing. The state is the glyph's own attributes, so it needs no host markup.
|
|
55
|
+
* A small square marking an item's attention state and the activity on it, one treatment per slot (see `Activity.Slot`).
|
|
63
56
|
*/
|
|
64
57
|
export const AttentionGlyph = forwardRef<HTMLSpanElement, AttentionGlyphProps>(
|
|
65
|
-
({ classNames, attended, containsAttended,
|
|
66
|
-
|
|
67
|
-
{
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
58
|
+
({ classNames, style, attended, containsAttended, treatments, ...props }, forwardedRef) => {
|
|
59
|
+
const { fill, mark, edge, corner, moving } = useMemo(
|
|
60
|
+
() => Activity.resolve(treatments ?? [], { attention: attended || containsAttended }),
|
|
61
|
+
[attended, containsAttended, treatments],
|
|
62
|
+
);
|
|
63
|
+
const colors = useMemo(
|
|
64
|
+
(): CSSProperties & Record<`--${string}`, string> => ({
|
|
65
|
+
...(fill && {
|
|
66
|
+
'--dx-attention-glyph-fill-hue': solid(fill.hue),
|
|
67
|
+
'--dx-attention-glyph-fill-opacity': `${(fill.opacity ?? DEFAULT_FILL_OPACITY) * 100}%`,
|
|
68
|
+
}),
|
|
69
|
+
...(mark && { '--dx-attention-glyph-mark': mark.hue ? solid(mark.hue) : 'var(--color-fg)' }),
|
|
70
|
+
...(edge && { '--dx-attention-glyph-edge': solid(edge.hue) }),
|
|
71
|
+
...(corner && { '--dx-attention-glyph-corner': solid(corner.hue) }),
|
|
72
|
+
}),
|
|
73
|
+
[fill, mark, edge, corner],
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
return (
|
|
77
|
+
<span
|
|
78
|
+
{...props}
|
|
79
|
+
style={{ ...colors, ...style }}
|
|
80
|
+
data-scope='attention-glyph'
|
|
81
|
+
data-part='root'
|
|
82
|
+
data-fill={attended ? 'attended' : containsAttended ? 'contains-attended' : fill && 'hue'}
|
|
83
|
+
data-fill-hue={fill?.hue}
|
|
84
|
+
data-mark={mark?.icon}
|
|
85
|
+
data-mark-hue={mark?.hue}
|
|
86
|
+
data-edge={edge && (edge.dashed ? 'dashed' : 'solid')}
|
|
87
|
+
data-edge-hue={edge?.hue}
|
|
88
|
+
data-corner={corner && (corner.shape ?? 'dot')}
|
|
89
|
+
data-corner-hue={corner?.hue}
|
|
90
|
+
data-moving={moving}
|
|
91
|
+
className={mx('dx-attention-glyph', classNames)}
|
|
92
|
+
ref={forwardedRef}
|
|
93
|
+
>
|
|
94
|
+
{(mark || edge || corner) && (
|
|
95
|
+
<svg viewBox='0 0 12 12' fill='none' aria-hidden data-scope='attention-glyph' data-part='overlay'>
|
|
96
|
+
{edge && <rect data-part='edge' x='0.5' y='0.5' width='11' height='11' rx='1.75' pathLength='48' />}
|
|
97
|
+
{mark && <Mark icon={mark.icon} />}
|
|
98
|
+
{corner && <circle data-part='corner' cx='12.25' cy='-0.25' r={corner.shape === 'ring' ? 2 : 2.5} />}
|
|
99
|
+
</svg>
|
|
100
|
+
)}
|
|
101
|
+
</span>
|
|
102
|
+
);
|
|
103
|
+
},
|
|
80
104
|
);
|
|
81
105
|
|
|
82
106
|
AttentionGlyph.displayName = 'AttentionGlyph';
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import React from 'react';
|
|
6
|
+
|
|
7
|
+
import { mx } from '@dxos/ui-theme';
|
|
8
|
+
import { type ThemedClassName } from '@dxos/ui-types';
|
|
9
|
+
|
|
10
|
+
export type RemoteCursorProps = ThemedClassName<{
|
|
11
|
+
/** Collaborator's display name, shown in the tag. */
|
|
12
|
+
name: string;
|
|
13
|
+
/** Member hue (a palette name such as `red`), matching their presence avatar. */
|
|
14
|
+
hue: string;
|
|
15
|
+
/** Tip position in the positioned parent's pixels. */
|
|
16
|
+
x: number;
|
|
17
|
+
y: number;
|
|
18
|
+
/** Divides the cursor's size, for a parent drawn under a zoom that the cursor should not follow. */
|
|
19
|
+
zoom?: number;
|
|
20
|
+
}>;
|
|
21
|
+
|
|
22
|
+
// The hue lands in a CSS variable name, and it arrives from other peers.
|
|
23
|
+
const safeHue = (hue: string) => (/^[a-z]+$/.test(hue) ? hue : 'neutral');
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Another collaborator's pointer: an arrow and a name tag in their member hue.
|
|
27
|
+
*/
|
|
28
|
+
export const RemoteCursor = ({ classNames, name, hue, x, y, zoom = 1 }: RemoteCursorProps) => {
|
|
29
|
+
const color = safeHue(hue);
|
|
30
|
+
return (
|
|
31
|
+
<div
|
|
32
|
+
role='none'
|
|
33
|
+
className={mx('absolute top-0 left-0 pointer-events-none origin-top-left', classNames)}
|
|
34
|
+
style={{ transform: `translate(${x}px, ${y}px) scale(${1 / zoom})` }}
|
|
35
|
+
data-testid='remote-cursor'
|
|
36
|
+
data-hue={color}
|
|
37
|
+
>
|
|
38
|
+
<svg width='16' height='16' viewBox='0 0 16 16' aria-hidden className='block drop-shadow-sm'>
|
|
39
|
+
<path
|
|
40
|
+
d='M1 1 L1 13 L4.5 9.75 L7 15 L9 14 L6.75 8.75 L11.5 8.5 Z'
|
|
41
|
+
style={{ fill: `var(--color-${color}-bg)` }}
|
|
42
|
+
stroke='white'
|
|
43
|
+
strokeWidth='1'
|
|
44
|
+
strokeLinejoin='round'
|
|
45
|
+
/>
|
|
46
|
+
</svg>
|
|
47
|
+
<span
|
|
48
|
+
className='absolute top-[14px] left-[10px] px-1.5 py-0.5 rounded-sm text-xs leading-none whitespace-nowrap shadow-sm'
|
|
49
|
+
style={{ backgroundColor: `var(--color-${color}-surface)`, color: `var(--color-${color}-fg)` }}
|
|
50
|
+
>
|
|
51
|
+
{name}
|
|
52
|
+
</span>
|
|
53
|
+
</div>
|
|
54
|
+
);
|
|
55
|
+
};
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { cleanup, fireEvent, render } from '@testing-library/react';
|
|
6
|
+
import React, { useMemo } from 'react';
|
|
7
|
+
import { afterEach, describe, test, vi } from 'vitest';
|
|
8
|
+
|
|
9
|
+
import { type CursorProjection, RemoteCursorLayer } from './RemoteCursorLayer.tsx';
|
|
10
|
+
|
|
11
|
+
// The surface is drawn at twice its own units.
|
|
12
|
+
const projection: CursorProjection = {
|
|
13
|
+
fromLayer: ({ x, y }) => ({ x: x / 2, y: y / 2 }),
|
|
14
|
+
toLayer: ({ x, y }) => ({ x: x * 2, y: y * 2 }),
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
describe('RemoteCursorLayer', () => {
|
|
18
|
+
afterEach(cleanup);
|
|
19
|
+
|
|
20
|
+
test('reports the local pointer in surface coordinates and clears it on leave', ({ expect }) => {
|
|
21
|
+
const onPointerChange = vi.fn();
|
|
22
|
+
const { getByTestId } = render(
|
|
23
|
+
<div data-testid='surface'>
|
|
24
|
+
<RemoteCursorLayer cursors={[]} projection={projection} onPointerChange={onPointerChange} />
|
|
25
|
+
</div>,
|
|
26
|
+
);
|
|
27
|
+
const root = getByTestId('surface');
|
|
28
|
+
fireEvent.pointerMove(root, { clientX: 40, clientY: 20 });
|
|
29
|
+
expect(onPointerChange).toHaveBeenLastCalledWith({ x: 20, y: 10 });
|
|
30
|
+
fireEvent.pointerLeave(root);
|
|
31
|
+
expect(onPointerChange).toHaveBeenLastCalledWith(undefined);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test('reports a still pointer again when the projection moves the surface under it', ({ expect }) => {
|
|
35
|
+
const onPointerChange = vi.fn();
|
|
36
|
+
const shifted = (by: number): CursorProjection => ({
|
|
37
|
+
fromLayer: ({ x, y }) => ({ x: x + by, y }),
|
|
38
|
+
toLayer: ({ x, y }) => ({ x: x - by, y }),
|
|
39
|
+
});
|
|
40
|
+
const { getByTestId, rerender } = render(
|
|
41
|
+
<div data-testid='surface'>
|
|
42
|
+
<RemoteCursorLayer cursors={[]} projection={shifted(0)} onPointerChange={onPointerChange} />
|
|
43
|
+
</div>,
|
|
44
|
+
);
|
|
45
|
+
fireEvent.pointerMove(getByTestId('surface'), { clientX: 40, clientY: 20 });
|
|
46
|
+
rerender(
|
|
47
|
+
<div data-testid='surface'>
|
|
48
|
+
<RemoteCursorLayer cursors={[]} projection={shifted(100)} onPointerChange={onPointerChange} />
|
|
49
|
+
</div>,
|
|
50
|
+
);
|
|
51
|
+
expect(onPointerChange).toHaveBeenLastCalledWith({ x: 140, y: 20 });
|
|
52
|
+
|
|
53
|
+
fireEvent.pointerLeave(getByTestId('surface'));
|
|
54
|
+
onPointerChange.mockClear();
|
|
55
|
+
rerender(
|
|
56
|
+
<div data-testid='surface'>
|
|
57
|
+
<RemoteCursorLayer cursors={[]} projection={shifted(200)} onPointerChange={onPointerChange} />
|
|
58
|
+
</div>,
|
|
59
|
+
);
|
|
60
|
+
expect(onPointerChange).not.toHaveBeenCalled();
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test('places remote cursors through the projection', ({ expect }) => {
|
|
64
|
+
const { getByTestId } = render(
|
|
65
|
+
<div>
|
|
66
|
+
<RemoteCursorLayer cursors={[{ id: 'a', name: 'Alice', hue: 'red', x: 5, y: 7 }]} projection={projection} />
|
|
67
|
+
</div>,
|
|
68
|
+
);
|
|
69
|
+
const cursor = getByTestId('remote-cursor');
|
|
70
|
+
expect(cursor.style.transform).toContain('translate(10px, 14px)');
|
|
71
|
+
expect(cursor.textContent).toBe('Alice');
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test('places cursors again from the committed surface when the projection changes', ({ expect }) => {
|
|
75
|
+
// Like an svg's viewBox, the scale reaches the DOM only when React commits the render that changes it.
|
|
76
|
+
const Surface = ({ scale }: { scale: number }) => {
|
|
77
|
+
const scaled = useMemo<CursorProjection>(
|
|
78
|
+
() => ({
|
|
79
|
+
fromLayer: (point) => point,
|
|
80
|
+
toLayer: ({ x, y }, layer) => {
|
|
81
|
+
const committed = Number(layer.parentElement?.dataset.scale);
|
|
82
|
+
return { x: x * committed, y: y * committed };
|
|
83
|
+
},
|
|
84
|
+
}),
|
|
85
|
+
[scale],
|
|
86
|
+
);
|
|
87
|
+
return (
|
|
88
|
+
<div data-scale={scale}>
|
|
89
|
+
<RemoteCursorLayer cursors={[{ id: 'a', name: 'Alice', hue: 'red', x: 5, y: 7 }]} projection={scaled} />
|
|
90
|
+
</div>
|
|
91
|
+
);
|
|
92
|
+
};
|
|
93
|
+
const { getByTestId, rerender } = render(<Surface scale={1} />);
|
|
94
|
+
expect(getByTestId('remote-cursor').style.transform).toContain('translate(5px, 7px)');
|
|
95
|
+
rerender(<Surface scale={3} />);
|
|
96
|
+
expect(getByTestId('remote-cursor').style.transform).toContain('translate(15px, 21px)');
|
|
97
|
+
});
|
|
98
|
+
});
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import React, { useEffect, useLayoutEffect, useRef, useState } from 'react';
|
|
6
|
+
|
|
7
|
+
import { RemoteCursor } from './RemoteCursor.tsx';
|
|
8
|
+
|
|
9
|
+
export type CursorPoint = { x: number; y: number };
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Maps between the layer's pixels and the surface's own coordinates (a scene's world, an svg's viewBox, a map's
|
|
13
|
+
* lat/lng), which peers share. Either returns `undefined` while the surface is not laid out.
|
|
14
|
+
*/
|
|
15
|
+
export type CursorProjection = {
|
|
16
|
+
fromLayer: (point: CursorPoint, layer: HTMLElement) => CursorPoint | undefined;
|
|
17
|
+
toLayer: (point: CursorPoint, layer: HTMLElement) => CursorPoint | undefined;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export type LayerCursor = CursorPoint & { id: string; name: string; hue: string };
|
|
21
|
+
|
|
22
|
+
export type RemoteCursorLayerProps = {
|
|
23
|
+
/** Other collaborators' pointers, in surface coordinates. */
|
|
24
|
+
cursors: readonly LayerCursor[];
|
|
25
|
+
/** Pass a new projection when the surface's transform changes, so the cursors are placed again. */
|
|
26
|
+
projection: CursorProjection;
|
|
27
|
+
/** The local pointer moved over the surface (in surface coordinates), or left it (`undefined`). */
|
|
28
|
+
onPointerChange?: (point: CursorPoint | undefined) => void;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Collaborators' pointers over any surface, and the local one reported back. Renders as a cover over its parent,
|
|
33
|
+
* which must be positioned and contain the surface; the parent's pointer events are what it tracks.
|
|
34
|
+
*/
|
|
35
|
+
export const RemoteCursorLayer = ({ cursors, projection, onPointerChange }: RemoteCursorLayerProps) => {
|
|
36
|
+
const [layer, setLayer] = useState<HTMLDivElement | null>(null);
|
|
37
|
+
const latest = useRef({ projection, onPointerChange });
|
|
38
|
+
latest.current = { projection, onPointerChange };
|
|
39
|
+
// Where the local pointer rests in the layer, while it is over the surface.
|
|
40
|
+
const pointer = useRef<CursorPoint | undefined>(undefined);
|
|
41
|
+
// A resize moves the surface under the layer without a new projection.
|
|
42
|
+
const [, setLayout] = useState(0);
|
|
43
|
+
|
|
44
|
+
// A new projection renders before its surface reaches the DOM, so place the cursors again once it has.
|
|
45
|
+
useLayoutEffect(() => setLayout((layout) => layout + 1), [projection]);
|
|
46
|
+
|
|
47
|
+
// A pan or zoom moves the surface under a pointer that stays still, so report the point it now rests on.
|
|
48
|
+
useEffect(() => {
|
|
49
|
+
const point = layer && pointer.current && projection.fromLayer(pointer.current, layer);
|
|
50
|
+
if (point) {
|
|
51
|
+
latest.current.onPointerChange?.(point);
|
|
52
|
+
}
|
|
53
|
+
}, [layer, projection]);
|
|
54
|
+
|
|
55
|
+
useEffect(() => {
|
|
56
|
+
const root = layer?.parentElement;
|
|
57
|
+
if (!layer || !root) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const handleMove = (event: PointerEvent) => {
|
|
62
|
+
const rect = layer.getBoundingClientRect();
|
|
63
|
+
pointer.current = { x: event.clientX - rect.left, y: event.clientY - rect.top };
|
|
64
|
+
const point = latest.current.projection.fromLayer(pointer.current, layer);
|
|
65
|
+
if (point) {
|
|
66
|
+
latest.current.onPointerChange?.(point);
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
const handleLeave = () => {
|
|
70
|
+
pointer.current = undefined;
|
|
71
|
+
latest.current.onPointerChange?.(undefined);
|
|
72
|
+
};
|
|
73
|
+
const observer = new ResizeObserver(() => setLayout((layout) => layout + 1));
|
|
74
|
+
|
|
75
|
+
root.addEventListener('pointermove', handleMove, { passive: true });
|
|
76
|
+
root.addEventListener('pointerleave', handleLeave);
|
|
77
|
+
observer.observe(root);
|
|
78
|
+
return () => {
|
|
79
|
+
root.removeEventListener('pointermove', handleMove);
|
|
80
|
+
root.removeEventListener('pointerleave', handleLeave);
|
|
81
|
+
observer.disconnect();
|
|
82
|
+
handleLeave();
|
|
83
|
+
};
|
|
84
|
+
}, [layer]);
|
|
85
|
+
|
|
86
|
+
return (
|
|
87
|
+
<div ref={setLayer} role='none' className='dx-cover overflow-hidden pointer-events-none'>
|
|
88
|
+
{layer &&
|
|
89
|
+
cursors.map((cursor) => {
|
|
90
|
+
const point = projection.toLayer(cursor, layer);
|
|
91
|
+
return point ? (
|
|
92
|
+
<RemoteCursor key={cursor.id} name={cursor.name} hue={cursor.hue} x={point.x} y={point.y} />
|
|
93
|
+
) : null;
|
|
94
|
+
})}
|
|
95
|
+
</div>
|
|
96
|
+
);
|
|
97
|
+
};
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type Meta, type StoryObj } from '@storybook/react-vite';
|
|
6
|
+
import * as Registry from 'effect/reactivity/AtomRegistry';
|
|
7
|
+
import React, { useEffect, useMemo } from 'react';
|
|
8
|
+
import { expect, waitFor } from 'storybook/test';
|
|
9
|
+
|
|
10
|
+
import '@dxos/react-ui/theme.css';
|
|
11
|
+
import * as ScrollArea from '@dxos/react-ui/ScrollArea';
|
|
12
|
+
import { withTheme } from '@dxos/react-ui/testing';
|
|
13
|
+
|
|
14
|
+
import { createDefaultBackends } from '../../core/index.ts';
|
|
15
|
+
import { ViewState } from '../../types/index.ts';
|
|
16
|
+
import { ViewStateProvider } from '../ViewStateProvider/index.ts';
|
|
17
|
+
import { ScrollScope } from './ScrollScope.tsx';
|
|
18
|
+
|
|
19
|
+
const OBJECT = 'echo:@ABC/01J00000000000000000000000';
|
|
20
|
+
|
|
21
|
+
const ROWS = Array.from({ length: 60 }, (_, index) => ({
|
|
22
|
+
id: `row-${index}`,
|
|
23
|
+
text: `Row ${index}. ${'A row of text that wraps to more lines in a narrow pane than in a wide one. '.repeat(2)}`,
|
|
24
|
+
}));
|
|
25
|
+
|
|
26
|
+
const makeManager = () => {
|
|
27
|
+
const registry = Registry.make();
|
|
28
|
+
return new ViewState.Manager({ registry, backends: createDefaultBackends(registry) });
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const Article = ({ prefix, width }: { prefix: string; width: string }) => (
|
|
32
|
+
<ScrollArea.Root classNames={['h-[14rem] border border-separator', width]}>
|
|
33
|
+
<ScrollArea.Viewport data-testid={`${prefix}-viewport`}>
|
|
34
|
+
{ROWS.map(({ id, text }) => (
|
|
35
|
+
<div key={id} data-scroll-anchor={id} className='p-2 border-b border-separator'>
|
|
36
|
+
{text}
|
|
37
|
+
</div>
|
|
38
|
+
))}
|
|
39
|
+
</ScrollArea.Viewport>
|
|
40
|
+
</ScrollArea.Root>
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
/** Two sessions' views of one object; the follower takes each value the leader sends, as the follow does. */
|
|
44
|
+
const FollowStory = () => {
|
|
45
|
+
const leader = useMemo(makeManager, []);
|
|
46
|
+
const follower = useMemo(makeManager, []);
|
|
47
|
+
useEffect(
|
|
48
|
+
() =>
|
|
49
|
+
leader.subscribeFollowed((key, contextId) => {
|
|
50
|
+
const value = leader.followed([contextId])[key]?.[contextId];
|
|
51
|
+
if (value !== undefined) {
|
|
52
|
+
follower.setEncoded(key, contextId, value);
|
|
53
|
+
}
|
|
54
|
+
}),
|
|
55
|
+
[leader, follower],
|
|
56
|
+
);
|
|
57
|
+
return (
|
|
58
|
+
<div className='flex gap-4 p-4'>
|
|
59
|
+
<ViewStateProvider manager={leader}>
|
|
60
|
+
<ScrollScope contextId={OBJECT}>
|
|
61
|
+
<Article prefix='leader' width='w-[26rem]' />
|
|
62
|
+
</ScrollScope>
|
|
63
|
+
</ViewStateProvider>
|
|
64
|
+
<ViewStateProvider manager={follower}>
|
|
65
|
+
<ScrollScope contextId={OBJECT}>
|
|
66
|
+
<Article prefix='follower' width='w-[14rem]' />
|
|
67
|
+
</ScrollScope>
|
|
68
|
+
</ViewStateProvider>
|
|
69
|
+
</div>
|
|
70
|
+
);
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const meta = {
|
|
74
|
+
title: 'ui/react-ui-attention/ScrollScope',
|
|
75
|
+
render: FollowStory,
|
|
76
|
+
decorators: [withTheme()],
|
|
77
|
+
} satisfies Meta;
|
|
78
|
+
|
|
79
|
+
export default meta;
|
|
80
|
+
|
|
81
|
+
type Story = StoryObj<typeof meta>;
|
|
82
|
+
|
|
83
|
+
/** The follower's view lands on the row at the top of the leader's. */
|
|
84
|
+
export const Follow: Story = {
|
|
85
|
+
play: async ({ canvasElement }) => {
|
|
86
|
+
const leader = canvasElement.querySelector<HTMLElement>('[data-testid="leader-viewport"]');
|
|
87
|
+
const follower = canvasElement.querySelector<HTMLElement>('[data-testid="follower-viewport"]');
|
|
88
|
+
const row = leader?.querySelector('[data-scroll-anchor="row-20"]');
|
|
89
|
+
if (!leader || !follower || !row) {
|
|
90
|
+
throw new Error('missing views');
|
|
91
|
+
}
|
|
92
|
+
leader.scrollTop += row.getBoundingClientRect().top - leader.getBoundingClientRect().top;
|
|
93
|
+
await waitFor(() =>
|
|
94
|
+
expect(ScrollArea.captureScrollPosition(follower)).toMatchObject({ anchor: 'row-20', offset: 0 }),
|
|
95
|
+
);
|
|
96
|
+
},
|
|
97
|
+
};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import React, { type PropsWithChildren, useEffect, useMemo, useRef } from 'react';
|
|
6
|
+
|
|
7
|
+
import * as ScrollArea from '@dxos/react-ui/ScrollArea';
|
|
8
|
+
|
|
9
|
+
import { ScrollPosition } from '../../types/index.ts';
|
|
10
|
+
import { useManagerOptional } from '../ViewStateProvider/view-state-hooks.ts';
|
|
11
|
+
|
|
12
|
+
export type ScrollScopeProps = PropsWithChildren<{
|
|
13
|
+
/** The EID of the object whose view this is; no scroll is kept without one. */
|
|
14
|
+
contextId?: string;
|
|
15
|
+
}>;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Keeps the scroll of an object's view as its `ScrollPosition` view state: the outermost `ScrollArea` inside reports
|
|
19
|
+
* where it is, and it is restored when it mounts and when a follow writes the leader's position.
|
|
20
|
+
*/
|
|
21
|
+
export const ScrollScope = ({ contextId, children }: ScrollScopeProps) => {
|
|
22
|
+
const manager = useManagerOptional();
|
|
23
|
+
const tracking = useRef<ScrollArea.TrackingHandle>(null);
|
|
24
|
+
|
|
25
|
+
// Only the areas whose position the follow changed move; the session's own writes are where it already is.
|
|
26
|
+
useEffect(() => {
|
|
27
|
+
if (!manager || !contextId) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
let previous = manager.get(ScrollPosition.aspect, contextId);
|
|
31
|
+
return manager.subscribe(ScrollPosition.aspect, contextId, (state, origin) => {
|
|
32
|
+
const changed = Object.entries(state).filter(
|
|
33
|
+
([name, position]) => JSON.stringify(position) !== JSON.stringify(previous[name]),
|
|
34
|
+
);
|
|
35
|
+
previous = state;
|
|
36
|
+
if (origin === 'follow') {
|
|
37
|
+
changed.forEach(([name, position]) => tracking.current?.restore(name, position));
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
}, [manager, contextId]);
|
|
41
|
+
|
|
42
|
+
const options = useMemo<ScrollArea.TrackingProps>(
|
|
43
|
+
() =>
|
|
44
|
+
manager && contextId
|
|
45
|
+
? {
|
|
46
|
+
initial: (name) => manager.get(ScrollPosition.aspect, contextId)[name],
|
|
47
|
+
onScroll: (name, position) =>
|
|
48
|
+
manager.update(ScrollPosition.aspect, contextId, (state) => ({ ...state, [name]: position })),
|
|
49
|
+
}
|
|
50
|
+
: {},
|
|
51
|
+
[manager, contextId],
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
return (
|
|
55
|
+
<ScrollArea.Tracking ref={tracking} {...options}>
|
|
56
|
+
{children}
|
|
57
|
+
</ScrollArea.Tracking>
|
|
58
|
+
);
|
|
59
|
+
};
|
package/src/components/index.ts
CHANGED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { describe, test } from 'vitest';
|
|
6
|
+
|
|
7
|
+
import * as Activity from './Activity.ts';
|
|
8
|
+
|
|
9
|
+
const viewer: Activity.Treatment = { slot: 'mark', icon: 'dot', hue: 'primary', priority: 10 };
|
|
10
|
+
const agent: Activity.Treatment = { slot: 'mark', icon: 'star', priority: 30, animate: true };
|
|
11
|
+
const unread: Activity.Treatment = { slot: 'mark', icon: 'star', hue: 'amber', priority: 20 };
|
|
12
|
+
const busy: Activity.Treatment = { slot: 'fill', hue: 'amber', priority: 10, animate: true };
|
|
13
|
+
const working: Activity.Treatment = { slot: 'edge', hue: 'amber', priority: 10 };
|
|
14
|
+
const failed: Activity.Treatment = { slot: 'edge', hue: 'error', priority: 40 };
|
|
15
|
+
const asked: Activity.Treatment = { slot: 'corner', hue: 'amber', priority: 40, animate: true };
|
|
16
|
+
const mention: Activity.Treatment = { slot: 'corner', hue: 'rose', priority: 30 };
|
|
17
|
+
|
|
18
|
+
describe('Activity.resolve', () => {
|
|
19
|
+
test('nothing requested draws nothing', ({ expect }) => {
|
|
20
|
+
expect(Activity.resolve([])).toEqual({});
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test('each slot keeps only its highest-priority treatment', ({ expect }) => {
|
|
24
|
+
expect(Activity.resolve([busy, viewer, agent, working, failed, mention, asked])).toEqual({
|
|
25
|
+
fill: busy,
|
|
26
|
+
mark: agent,
|
|
27
|
+
edge: failed,
|
|
28
|
+
corner: asked,
|
|
29
|
+
moving: 'corner',
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test('a tie goes to the first requested', ({ expect }) => {
|
|
34
|
+
const other: Activity.Treatment = { ...viewer, hue: 'violet' };
|
|
35
|
+
expect(Activity.resolve([viewer, other]).mark).toBe(viewer);
|
|
36
|
+
expect(Activity.resolve([other, viewer]).mark).toBe(other);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test('attention holds the fill, so a fill treatment neither draws nor moves', ({ expect }) => {
|
|
40
|
+
expect(Activity.resolve([busy], { attention: true })).toEqual({ moving: undefined });
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test('only the first animated slot in motion order moves', ({ expect }) => {
|
|
44
|
+
expect(Activity.resolve([busy, agent]).moving).toBe('mark');
|
|
45
|
+
expect(Activity.resolve([busy, agent, asked]).moving).toBe('corner');
|
|
46
|
+
expect(Activity.resolve([busy]).moving).toBe('fill');
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test('a still winner passes motion to the next slot', ({ expect }) => {
|
|
50
|
+
expect(Activity.resolve([mention, agent])).toMatchObject({ corner: mention, moving: 'mark' });
|
|
51
|
+
expect(Activity.resolve([unread, busy])).toMatchObject({ mark: unread, moving: 'fill' });
|
|
52
|
+
});
|
|
53
|
+
});
|