@dxos/react-ui-attention 0.8.4-main.e8ec1fe → 0.8.4-main.ef1bc66f44

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 (53) hide show
  1. package/dist/lib/browser/chunk-PAZBZOO2.mjs +7 -0
  2. package/dist/lib/browser/chunk-PAZBZOO2.mjs.map +7 -0
  3. package/dist/lib/browser/chunk-W5HPVX5Q.mjs +705 -0
  4. package/dist/lib/browser/chunk-W5HPVX5Q.mjs.map +7 -0
  5. package/dist/lib/browser/index.mjs +73 -6
  6. package/dist/lib/browser/index.mjs.map +4 -4
  7. package/dist/lib/browser/meta.json +1 -1
  8. package/dist/lib/browser/testing/index.mjs +19 -4
  9. package/dist/lib/browser/testing/index.mjs.map +3 -3
  10. package/dist/lib/browser/types.mjs +7 -0
  11. package/dist/lib/browser/types.mjs.map +7 -0
  12. package/dist/lib/node-esm/chunk-NFJ45DDP.mjs +706 -0
  13. package/dist/lib/node-esm/chunk-NFJ45DDP.mjs.map +7 -0
  14. package/dist/lib/node-esm/chunk-PMGNPL4M.mjs +9 -0
  15. package/dist/lib/node-esm/chunk-PMGNPL4M.mjs.map +7 -0
  16. package/dist/lib/node-esm/index.mjs +73 -6
  17. package/dist/lib/node-esm/index.mjs.map +4 -4
  18. package/dist/lib/node-esm/meta.json +1 -1
  19. package/dist/lib/node-esm/testing/index.mjs +19 -4
  20. package/dist/lib/node-esm/testing/index.mjs.map +3 -3
  21. package/dist/lib/node-esm/types.mjs +8 -0
  22. package/dist/lib/node-esm/types.mjs.map +7 -0
  23. package/dist/types/src/attention.d.ts +22 -17
  24. package/dist/types/src/attention.d.ts.map +1 -1
  25. package/dist/types/src/components/AttentionProvider.d.ts +3 -2
  26. package/dist/types/src/components/AttentionProvider.d.ts.map +1 -1
  27. package/dist/types/src/components/SelectionProvider.d.ts +5 -4
  28. package/dist/types/src/components/SelectionProvider.d.ts.map +1 -1
  29. package/dist/types/src/index.d.ts +1 -0
  30. package/dist/types/src/index.d.ts.map +1 -1
  31. package/dist/types/src/selection.d.ts +23 -3
  32. package/dist/types/src/selection.d.ts.map +1 -1
  33. package/dist/types/src/testing/decorators/withAttention.d.ts +5 -1
  34. package/dist/types/src/testing/decorators/withAttention.d.ts.map +1 -1
  35. package/dist/types/src/types.d.ts +10 -0
  36. package/dist/types/src/types.d.ts.map +1 -0
  37. package/dist/types/tsconfig.tsbuildinfo +1 -1
  38. package/package.json +35 -23
  39. package/src/attention.test.ts +12 -7
  40. package/src/attention.ts +64 -42
  41. package/src/components/AttentionGlyph.stories.tsx +1 -1
  42. package/src/components/AttentionGlyph.tsx +1 -1
  43. package/src/components/AttentionProvider.stories.tsx +1 -1
  44. package/src/components/AttentionProvider.tsx +64 -55
  45. package/src/components/SelectionProvider.tsx +62 -15
  46. package/src/index.ts +1 -0
  47. package/src/selection.ts +111 -71
  48. package/src/testing/decorators/withAttention.ts +20 -3
  49. package/src/types.ts +16 -0
  50. package/dist/lib/browser/chunk-MVIMDGR2.mjs +0 -662
  51. package/dist/lib/browser/chunk-MVIMDGR2.mjs.map +0 -7
  52. package/dist/lib/node-esm/chunk-NPW2V7JQ.mjs +0 -664
  53. package/dist/lib/node-esm/chunk-NPW2V7JQ.mjs.map +0 -7
package/package.json CHANGED
@@ -1,12 +1,16 @@
1
1
  {
2
2
  "name": "@dxos/react-ui-attention",
3
- "version": "0.8.4-main.e8ec1fe",
3
+ "version": "0.8.4-main.ef1bc66f44",
4
4
  "description": "Signifier components and providers for attention system.",
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
- "sideEffects": true,
13
+ "sideEffects": false,
10
14
  "type": "module",
11
15
  "exports": {
12
16
  ".": {
@@ -15,6 +19,12 @@
15
19
  "browser": "./dist/lib/browser/index.mjs",
16
20
  "node": "./dist/lib/node-esm/index.mjs"
17
21
  },
22
+ "./types": {
23
+ "source": "./src/types.ts",
24
+ "types": "./dist/types/src/types.d.ts",
25
+ "browser": "./dist/lib/browser/types.mjs",
26
+ "node": "./dist/lib/node-esm/types.mjs"
27
+ },
18
28
  "./testing": {
19
29
  "source": "./src/testing/index.ts",
20
30
  "types": "./dist/types/src/testing/index.d.ts",
@@ -25,6 +35,9 @@
25
35
  "types": "dist/types/src/index.d.ts",
26
36
  "typesVersions": {
27
37
  "*": {
38
+ "types": [
39
+ "dist/types/src/types.d.ts"
40
+ ],
28
41
  "testing": [
29
42
  "dist/types/src/testing/index.d.ts"
30
43
  ]
@@ -35,8 +48,7 @@
35
48
  "src"
36
49
  ],
37
50
  "dependencies": {
38
- "@preact-signals/safe-react": "^0.9.0",
39
- "@preact/signals-core": "^1.12.1",
51
+ "@effect-atom/atom-react": "^0.5.0",
40
52
  "@radix-ui/primitive": "1.1.1",
41
53
  "@radix-ui/react-compose-refs": "1.1.1",
42
54
  "@radix-ui/react-context": "1.1.1",
@@ -44,30 +56,30 @@
44
56
  "@radix-ui/react-primitive": "2.0.2",
45
57
  "@radix-ui/react-slot": "1.1.2",
46
58
  "@radix-ui/react-use-controllable-state": "1.1.0",
47
- "effect": "3.18.3",
48
- "@dxos/echo": "0.8.4-main.e8ec1fe",
49
- "@dxos/invariant": "0.8.4-main.e8ec1fe",
50
- "@dxos/live-object": "0.8.4-main.e8ec1fe",
51
- "@dxos/log": "0.8.4-main.e8ec1fe",
52
- "@dxos/util": "0.8.4-main.e8ec1fe"
59
+ "effect": "3.19.16",
60
+ "@dxos/echo": "0.8.4-main.ef1bc66f44",
61
+ "@dxos/invariant": "0.8.4-main.ef1bc66f44",
62
+ "@dxos/log": "0.8.4-main.ef1bc66f44",
63
+ "@dxos/react-hooks": "0.8.4-main.ef1bc66f44",
64
+ "@dxos/util": "0.8.4-main.ef1bc66f44"
53
65
  },
54
66
  "devDependencies": {
55
- "@types/react": "~19.2.2",
56
- "@types/react-dom": "~19.2.2",
57
- "react": "~19.2.0",
58
- "react-dom": "~19.2.0",
67
+ "@types/react": "~19.2.7",
68
+ "@types/react-dom": "~19.2.3",
69
+ "react": "~19.2.3",
70
+ "react-dom": "~19.2.3",
59
71
  "vite": "7.1.9",
60
- "@dxos/random": "0.8.4-main.e8ec1fe",
61
- "@dxos/react-ui": "0.8.4-main.e8ec1fe",
62
- "@dxos/react-ui-theme": "0.8.4-main.e8ec1fe",
63
- "@dxos/storybook-utils": "0.8.4-main.e8ec1fe"
72
+ "@dxos/random": "0.8.4-main.ef1bc66f44",
73
+ "@dxos/ui-theme": "0.8.4-main.ef1bc66f44",
74
+ "@dxos/react-ui": "0.8.4-main.ef1bc66f44",
75
+ "@dxos/storybook-utils": "0.8.4-main.ef1bc66f44"
64
76
  },
65
77
  "peerDependencies": {
66
- "effect": "^3.13.3",
67
- "react": "^19.0.0",
68
- "react-dom": "^19.0.0",
69
- "@dxos/react-ui-theme": "0.8.4-main.e8ec1fe",
70
- "@dxos/react-ui": "0.8.4-main.e8ec1fe"
78
+ "effect": "3.19.16",
79
+ "react": "~19.2.3",
80
+ "react-dom": "~19.2.3",
81
+ "@dxos/react-ui": "0.8.4-main.ef1bc66f44",
82
+ "@dxos/ui-theme": "0.8.4-main.ef1bc66f44"
71
83
  },
72
84
  "publishConfig": {
73
85
  "access": "public"
@@ -2,14 +2,16 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
+ import { Registry } from '@effect-atom/atom-react';
5
6
  import { describe, expect, test } from 'vitest';
6
7
 
7
8
  import { AttentionManager } from './attention';
8
9
 
9
10
  describe('AttentionManager', () => {
10
11
  test('takes an initial attendable', () => {
11
- const attention = new AttentionManager(['a', 'b', 'c']);
12
- expect(attention.current).to.deep.equal(['a', 'b', 'c']);
12
+ const registry = Registry.make();
13
+ const attention = new AttentionManager(registry, ['a', 'b', 'c']);
14
+ expect(attention.getCurrent()).to.deep.equal(['a', 'b', 'c']);
13
15
  expect(attention.keys()).to.deep.equal([['a'], ['b', 'c'], ['c'], ['a', 'b', 'c']]);
14
16
  expect(attention.get(['a'])).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: true });
15
17
  expect(attention.get(['b', 'c'])).to.deep.equal({ hasAttention: false, isAncestor: true, isRelated: false });
@@ -19,15 +21,17 @@ describe('AttentionManager', () => {
19
21
  });
20
22
 
21
23
  test('current returns last updated key', () => {
22
- const attention = new AttentionManager();
23
- expect(attention.current).to.deep.equal([]);
24
+ const registry = Registry.make();
25
+ const attention = new AttentionManager(registry);
26
+ expect(attention.getCurrent()).to.deep.equal([]);
24
27
 
25
28
  attention.update(['a', 'b', 'c']);
26
- expect(attention.current).to.deep.equal(['a', 'b', 'c']);
29
+ expect(attention.getCurrent()).to.deep.equal(['a', 'b', 'c']);
27
30
  });
28
31
 
29
32
  test('keys returns all stored attendables', () => {
30
- const attention = new AttentionManager();
33
+ const registry = Registry.make();
34
+ const attention = new AttentionManager(registry);
31
35
  expect(attention.keys()).to.deep.equal([]);
32
36
 
33
37
  attention.update(['a']);
@@ -38,7 +42,8 @@ describe('AttentionManager', () => {
38
42
  });
39
43
 
40
44
  test('get returns attention object for a given key', () => {
41
- const attention = new AttentionManager();
45
+ const registry = Registry.make();
46
+ const attention = new AttentionManager(registry);
42
47
  expect(attention.get(['a'])).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: false });
43
48
 
44
49
  attention.update(['a']);
package/src/attention.ts CHANGED
@@ -2,25 +2,11 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import { untracked } from '@preact/signals-core';
6
- import * as Schema from 'effect/Schema';
5
+ import { Atom, type Registry } from '@effect-atom/atom-react';
7
6
 
8
- import { live } from '@dxos/echo/internal';
9
- import { type Live } from '@dxos/live-object';
10
7
  import { ComplexMap } from '@dxos/util';
11
8
 
12
- // NOTE: Chosen from RFC 1738’s `safe` characters: http://www.faqs.org/rfcs/rfc1738.html
13
- export const ATTENDABLE_PATH_SEPARATOR = '~';
14
-
15
- export const AttentionSchema = Schema.mutable(
16
- Schema.Struct({
17
- hasAttention: Schema.Boolean,
18
- isAncestor: Schema.Boolean,
19
- isRelated: Schema.Boolean,
20
- }),
21
- );
22
-
23
- export type Attention = Schema.Schema.Type<typeof AttentionSchema>;
9
+ import { ATTENDABLE_PATH_SEPARATOR, type Attention } from './types';
24
10
 
25
11
  // TODO(wittjosiah): Use mosaic path utility?
26
12
  const stringKey = (key: string[]) => key.join(',');
@@ -28,26 +14,44 @@ const stringKey = (key: string[]) => key.join(',');
28
14
  /**
29
15
  * Manages attention state for an application.
30
16
  */
31
- // TODO(wittjosiah): Write unit tests.
32
17
  export class AttentionManager {
33
- // Each attention path is associated with an attention object.
18
+ // Each attention path is associated with an attention atom.
34
19
  // The lookup is not a reactive object to ensure that attention for each path is subscribable independently.
35
- private readonly _map = new ComplexMap<string[], Live<Attention>>(stringKey);
36
- private readonly _state = live<{ current: string[] }>({ current: [] });
37
-
38
- constructor(initial: string[] = []) {
20
+ private readonly _map = new ComplexMap<string[], Atom.Writable<Attention>>(stringKey);
21
+ private readonly _currentAtom: Atom.Writable<string[]>;
22
+
23
+ constructor(
24
+ private readonly _registry: Registry.Registry,
25
+ initial: string[] = [],
26
+ ) {
27
+ this._currentAtom = Atom.make<string[]>([]);
39
28
  if (initial.length > 0) {
40
29
  this.update(initial);
41
30
  }
42
31
  }
43
32
 
44
33
  /**
45
- * Currently attended element.
46
- *
47
- * @reactive
34
+ * Atom for the currently attended element path.
35
+ */
36
+ get current(): Atom.Atom<string[]> {
37
+ return this._currentAtom;
38
+ }
39
+
40
+ /**
41
+ * Gets the currently attended element path.
42
+ */
43
+ getCurrent(): readonly string[] {
44
+ return this._registry.get(this._currentAtom);
45
+ }
46
+
47
+ /**
48
+ * Subscribe to changes in the current attention path.
48
49
  */
49
- get current(): Live<readonly string[]> {
50
- return this._state.current;
50
+ subscribeCurrent(cb: (current: readonly string[]) => void): () => void {
51
+ this._registry.get(this._currentAtom);
52
+ return this._registry.subscribe(this._currentAtom, () => {
53
+ cb(this._registry.get(this._currentAtom));
54
+ });
51
55
  }
52
56
 
53
57
  /**
@@ -60,15 +64,31 @@ export class AttentionManager {
60
64
  /**
61
65
  * Get the attention state for a given path.
62
66
  */
63
- get(key: string[]): Live<Attention> {
64
- const object = this._map.get(key);
65
- if (object) {
66
- return object;
67
+ get(key: string[]): Attention {
68
+ const atom = this._getAtom(key);
69
+ return this._registry.get(atom);
70
+ }
71
+
72
+ /**
73
+ * Subscribe to changes in the attention state for a given path.
74
+ */
75
+ subscribe(key: string[], cb: (attention: Attention) => void): () => void {
76
+ const atom = this._getAtom(key);
77
+ this._registry.get(atom);
78
+ return this._registry.subscribe(atom, () => {
79
+ cb(this._registry.get(atom));
80
+ });
81
+ }
82
+
83
+ private _getAtom(key: string[]): Atom.Writable<Attention> {
84
+ const existing = this._map.get(key);
85
+ if (existing) {
86
+ return existing;
67
87
  }
68
88
 
69
- const newObject = live(AttentionSchema, { hasAttention: false, isAncestor: false, isRelated: false });
70
- this._map.set(key, newObject);
71
- return newObject;
89
+ const newAtom = Atom.make<Attention>({ hasAttention: false, isAncestor: false, isRelated: false });
90
+ this._map.set(key, newAtom);
91
+ return newAtom;
72
92
  }
73
93
 
74
94
  /**
@@ -77,12 +97,12 @@ export class AttentionManager {
77
97
  * @internal
78
98
  */
79
99
  update(nextKey: string[]): void {
80
- const currentKey = untracked(() => this.current);
100
+ const currentKey = this.getCurrent();
81
101
  const currentRelatedTo = currentKey[0];
82
102
  const nextRelatedTo = nextKey[0];
83
103
 
84
104
  // Ensure related attention object is initialized.
85
- this.get([nextRelatedTo]);
105
+ this._getAtom([nextRelatedTo]);
86
106
 
87
107
  const currentRelatedKeys = this.keys().filter((key) => key[0] === currentRelatedTo);
88
108
  const nextRelatedKeys = this.keys().filter(
@@ -98,7 +118,7 @@ export class AttentionManager {
98
118
  (curr) => !nextAncestorKeys.find((next) => stringKey(curr) === stringKey(next)),
99
119
  );
100
120
 
101
- this._state.current = nextKey;
121
+ this._registry.set(this._currentAtom, nextKey);
102
122
  prevRelatedKeys.forEach((key) => this._set(key, {}));
103
123
  prevAncestorKeys.forEach((key) => this._set(key, {}));
104
124
  nextRelatedKeys.forEach((key) => this._set(key, { isRelated: true }));
@@ -107,15 +127,17 @@ export class AttentionManager {
107
127
  }
108
128
 
109
129
  private _set(key: string[], attention: Partial<Attention>): void {
110
- const object = this.get(key);
111
- object.hasAttention = attention.hasAttention ?? false;
112
- object.isAncestor = attention.isAncestor ?? false;
113
- object.isRelated = attention.isRelated ?? false;
130
+ const atom = this._getAtom(key);
131
+ this._registry.set(atom, {
132
+ hasAttention: attention.hasAttention ?? false,
133
+ isAncestor: attention.isAncestor ?? false,
134
+ isRelated: attention.isRelated ?? false,
135
+ });
114
136
  }
115
137
  }
116
138
 
117
139
  /**
118
- * Accumulates all attendable id’s between the element provided and the root, inclusive.
140
+ * Accumulates all attendable id's between the element provided and the root, inclusive.
119
141
  */
120
142
  export const getAttendables = (selector: string, cursor: Element, acc: string[] = []): string[] => {
121
143
  // Find the closest element with `data-attendable-id`, if any; start from cursor and move up the DOM tree.
@@ -31,7 +31,7 @@ const meta = {
31
31
  title: 'ui/react-ui-attention/AttentionGlyph',
32
32
  component: AttentionGlyph as any,
33
33
  render: DefaultStory,
34
- decorators: [withTheme],
34
+ decorators: [withTheme()],
35
35
  } satisfies Meta<typeof DefaultStory>;
36
36
 
37
37
  export default meta;
@@ -6,7 +6,7 @@ import { type Primitive } from '@radix-ui/react-primitive';
6
6
  import React, { type CSSProperties, type ComponentPropsWithRef, forwardRef, useMemo } from 'react';
7
7
 
8
8
  import { Icon, 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
  const attentionGlyphStyles =
12
12
  'inline-block rounded-sm is-3 bs-3 bg-transparent text-accentText transition-colors [[data-contains-attended=true]_&]:bg-attentionRelated [[data-attention=true]_&]:bg-accentSurface [[data-attention=true]_&]:text-accentSurfaceText [[aria-current][data-attention=true]_&]:bg-accentSurface [[aria-current][data-attention=true]_&]:text-accentSurfaceText';
@@ -35,7 +35,7 @@ const Story = () => {
35
35
  const meta = {
36
36
  title: 'ui/react-ui-attention/AttentionProvider',
37
37
  render: Story,
38
- decorators: [withTheme, withAttention],
38
+ decorators: [withTheme(), withAttention()],
39
39
  } satisfies Meta;
40
40
 
41
41
  export default meta;
@@ -2,16 +2,28 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
+ import { RegistryContext } from '@effect-atom/atom-react';
5
6
  import { createContext } from '@radix-ui/react-context';
6
7
  import { Primitive } from '@radix-ui/react-primitive';
7
8
  import { Slot } from '@radix-ui/react-slot';
8
- import React, { type ComponentPropsWithRef, type FocusEvent, type PropsWithChildren, forwardRef, useMemo } from 'react';
9
+ import React, {
10
+ type ComponentPropsWithRef,
11
+ type FocusEvent,
12
+ type PropsWithChildren,
13
+ forwardRef,
14
+ useContext,
15
+ useEffect,
16
+ useMemo,
17
+ useState,
18
+ } from 'react';
9
19
 
10
20
  import { log } from '@dxos/log';
11
- import { type ThemedClassName, useDefaultValue } from '@dxos/react-ui';
12
- import { mx } from '@dxos/react-ui-theme';
21
+ import { useDefaultValue } from '@dxos/react-hooks';
22
+ import { type ThemedClassName } from '@dxos/react-ui';
23
+ import { mx } from '@dxos/ui-theme';
13
24
 
14
- import { ATTENDABLE_PATH_SEPARATOR, type Attention, AttentionManager, getAttendables } from '../attention';
25
+ import { AttentionManager, getAttendables } from '../attention';
26
+ import { ATTENDABLE_PATH_SEPARATOR, type Attention } from '../types';
15
27
 
16
28
  const ATTENTION_NAME = 'Attention';
17
29
  const ATTENDABLE_ATTRIBUTE = 'data-attendable-id';
@@ -23,25 +35,60 @@ type AttentionContextValue = {
23
35
  };
24
36
 
25
37
  const [AttentionContextProvider, useAttentionContext] = createContext<AttentionContextValue>(ATTENTION_NAME, {
26
- attention: new AttentionManager(),
38
+ attention: undefined as unknown as AttentionManager,
27
39
  path: [],
28
40
  });
29
41
 
30
- const UNKNOWN_ATTENDABLE: Attention = { hasAttention: false, isAncestor: false, isRelated: false };
42
+ const UNKNOWN_ATTENDABLE = { hasAttention: false, isAncestor: false, isRelated: false } as Attention;
31
43
 
32
44
  const useAttention = (attendableId?: string): Attention => {
33
45
  const { attention, path } = useAttentionContext(ATTENTION_NAME);
34
- if (!attendableId) {
35
- return UNKNOWN_ATTENDABLE;
36
- }
46
+ const [state, setState] = useState<Attention>(UNKNOWN_ATTENDABLE);
37
47
 
38
- const current = [...attendableId.split(ATTENDABLE_PATH_SEPARATOR), ...path];
39
- return attention.get(current);
48
+ const key = useMemo(() => {
49
+ if (!attendableId) {
50
+ return undefined;
51
+ }
52
+ return [...attendableId.split(ATTENDABLE_PATH_SEPARATOR), ...path];
53
+ }, [attendableId, path]);
54
+
55
+ useEffect(() => {
56
+ if (!key || !attention) {
57
+ setState(UNKNOWN_ATTENDABLE);
58
+ return;
59
+ }
60
+
61
+ // Set initial state.
62
+ setState(attention.get(key));
63
+
64
+ // Subscribe to changes.
65
+ return attention.subscribe(key, (newState) => {
66
+ setState(newState);
67
+ });
68
+ }, [attention, key]);
69
+
70
+ return state;
40
71
  };
41
72
 
42
73
  const useAttended = () => {
43
74
  const { attention } = useAttentionContext(ATTENTION_NAME);
44
- return attention.current;
75
+ const [current, setCurrent] = useState<readonly string[]>([]);
76
+
77
+ useEffect(() => {
78
+ if (!attention) {
79
+ return;
80
+ }
81
+
82
+ // Set initial state.
83
+ setCurrent(attention.getCurrent());
84
+
85
+ // Subscribe to changes.
86
+ return attention.subscribeCurrent((newCurrent) => {
87
+ setCurrent(newCurrent);
88
+ });
89
+ }, [attention]);
90
+
91
+ return current;
45
92
  };
46
93
 
47
94
  /**
@@ -75,7 +122,9 @@ const RootAttentionProvider = ({
75
122
  onChange: (nextAttended: string[]) => void;
76
123
  }>
77
124
  >) => {
78
- const attention = useDefaultValue(propsAttention, () => new AttentionManager());
125
+ const registry = useContext(RegistryContext);
126
+ const attention = useDefaultValue(propsAttention, () => new AttentionManager(registry));
127
+
79
128
  const handleFocus = (event: FocusEvent) => {
80
129
  // NOTE(thure): Use the following to debug focus movement across the app:
81
130
  log('focus', { related: event.relatedTarget, target: event.target });
@@ -86,7 +135,7 @@ const RootAttentionProvider = ({
86
135
  (el) => `[id="${el.getAttribute('aria-controls')}"]`,
87
136
  ),
88
137
  ].join(',');
89
- const prev = attention.current;
138
+ const prev = attention.getCurrent();
90
139
  const next = getAttendables(selector, event.target);
91
140
  // TODO(wittjosiah): Not allowing empty state means that the attended item is not strictly guaranteed to be in the DOM.
92
141
  // Currently this depends on the deck in order to ensure that when the attended item is removed something else is attended.
@@ -97,49 +146,9 @@ const RootAttentionProvider = ({
97
146
  }
98
147
  };
99
148
 
100
- // NOTE(thure): Use the following to debug the macOS package issue #8540:
101
-
102
- // const [startEl, setStartEl] = useState<HTMLElement | null>(null);
103
- // const [endEl, setEndEl] = useState<HTMLElement | null>(null);
104
- //
105
- // const handleEventDebug = useCallback((event: any) => {
106
- // console.log(`[${event.type}]`, event.target, event.currentTarget, [event.clientX, event.clientY]);
107
- // }, []);
108
- //
109
- // const handleStartDebug = useCallback(
110
- // (event: any) => {
111
- // setStartEl(event.target);
112
- // handleEventDebug(event);
113
- // },
114
- // [handleEventDebug],
115
- // );
116
- //
117
- // const handleEndDebug = useCallback(
118
- // (event: any) => {
119
- // setEndEl(event.target);
120
- // handleEventDebug(event);
121
- // },
122
- // [handleEventDebug],
123
- // );
124
- //
125
- // const handleClickDebug = useCallback(
126
- // (event: any) => {
127
- // console.log('[click compare]', startEl, endEl, startEl === endEl);
128
- // handleEventDebug(event);
129
- // },
130
- // [startEl, endEl, handleEventDebug],
131
- // );
132
-
133
149
  return (
134
150
  <AttentionContextProvider attention={attention} path={[]}>
135
- <div
136
- role='none'
137
- className='contents'
138
- onFocusCapture={handleFocus}
139
- // onClick={handleClickDebug}
140
- // onMouseDown={handleStartDebug}
141
- // onMouseUp={handleEndDebug}
142
- >
151
+ <div role='none' className='contents' onFocusCapture={handleFocus}>
143
152
  {children}
144
153
  </div>
145
154
  </AttentionContextProvider>
@@ -2,11 +2,12 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
+ import { RegistryContext } from '@effect-atom/atom-react';
5
6
  import { createContext } from '@radix-ui/react-context';
6
7
  import * as Match from 'effect/Match';
7
- import React, { type PropsWithChildren, useCallback, useMemo } from 'react';
8
+ import React, { type PropsWithChildren, useCallback, useContext, useEffect, useMemo, useState } from 'react';
8
9
 
9
- import { useDefaultValue } from '@dxos/react-ui';
10
+ import { useDefaultValue } from '@dxos/react-hooks';
10
11
 
11
12
  import {
12
13
  type Selection,
@@ -23,7 +24,7 @@ type SelectionContextValue = {
23
24
  };
24
25
 
25
26
  const [SelectionContextProvider, useSelectionContext] = createContext<SelectionContextValue>(SELECTION_NAME, {
26
- selection: new SelectionManager(),
27
+ selection: undefined as unknown as SelectionManager,
27
28
  });
28
29
 
29
30
  /**
@@ -34,7 +35,8 @@ export const SelectionProvider = ({
34
35
  children,
35
36
  selection: propsSelection,
36
37
  }: PropsWithChildren<{ selection?: SelectionManager }>) => {
37
- const selection = useDefaultValue(propsSelection, () => new SelectionManager());
38
+ const registry = useContext(RegistryContext);
39
+ const selection = useDefaultValue(propsSelection, () => new SelectionManager(registry));
38
40
  return <SelectionContextProvider selection={selection}>{children}</SelectionContextProvider>;
39
41
  };
40
42
 
@@ -46,6 +48,18 @@ export const useSelectionManager = () => {
46
48
  return selection;
47
49
  };
48
50
 
51
+ const getDefaultResult = <T extends SelectionMode>(mode: T): SelectionResult<T> => {
52
+ return Match.type<Selection>().pipe(
53
+ Match.when({ mode: 'single' }, (selection) => selection.id),
54
+ Match.when({ mode: 'multi' }, (selection) => selection.ids),
55
+ Match.when({ mode: 'range' }, (selection) =>
56
+ selection.from && selection.to ? { from: selection.from, to: selection.to } : undefined,
57
+ ),
58
+ Match.when({ mode: 'multi-range' }, (selection) => selection.ranges),
59
+ Match.exhaustive,
60
+ )(defaultSelection(mode)) as SelectionResult<T>;
61
+ };
62
+
49
63
  /**
50
64
  * Get the selected objects for a given context.
51
65
  */
@@ -54,28 +68,49 @@ export const useSelected = <T extends SelectionMode>(
54
68
  mode: T = 'multi' as T,
55
69
  ): SelectionResult<T> => {
56
70
  const { selection } = useSelectionContext(SELECTION_NAME);
57
- if (contextId) {
58
- return selection.getSelected(contextId, mode);
59
- }
71
+ const [state, setState] = useState<SelectionResult<T>>(() =>
72
+ contextId && selection ? selection.getSelected(contextId, mode) : getDefaultResult(mode),
73
+ );
60
74
 
61
- return Match.type<Selection>().pipe(
62
- Match.when({ mode: 'single' }, (s) => s.id),
63
- Match.when({ mode: 'multi' }, (s) => s.ids),
64
- Match.when({ mode: 'range' }, (s) => (s.from && s.to ? { from: s.from, to: s.to } : undefined)),
65
- Match.when({ mode: 'multi-range' }, (s) => s.ranges),
66
- Match.exhaustive,
67
- )(defaultSelection(mode)) as any;
75
+ useEffect(() => {
76
+ if (!contextId || !selection) {
77
+ setState(getDefaultResult(mode));
78
+ return;
79
+ }
80
+
81
+ // Set initial state.
82
+ setState(selection.getSelected(contextId, mode));
83
+
84
+ // Subscribe to changes.
85
+ return selection.subscribe(() => {
86
+ setState(selection.getSelected(contextId, mode));
87
+ });
88
+ }, [selection, contextId, mode]);
89
+
90
+ return state;
91
+ };
92
+
93
+ export type UseSelectionActions = {
94
+ singleSelect: (id: string) => void;
95
+ multiSelect: (ids: string[]) => void;
96
+ rangeSelect: (from: string, to: string) => void;
97
+ toggle: (id: string) => void;
98
+ clear: () => void;
68
99
  };
69
100
 
70
101
  /**
71
102
  * Provides functions to manage the selection state for multiple contexts.
72
103
  */
73
- export const useSelectionActions = (contextIds: string[], mode: SelectionMode = 'multi') => {
104
+ // TODO(burdon): Mode not used.
105
+ export const useSelectionActions = (contextIds: string[], mode: SelectionMode = 'multi'): UseSelectionActions => {
74
106
  const stableContextIds = useMemo(() => contextIds, [JSON.stringify(contextIds)]); // TODO(burdon): Avoid stringify.
75
107
  const { selection } = useSelectionContext(SELECTION_NAME);
76
108
 
77
109
  const singleSelect = useCallback(
78
110
  (id: string) => {
111
+ if (!selection) {
112
+ return;
113
+ }
79
114
  for (const contextId of stableContextIds) {
80
115
  selection.updateSingle(contextId, id);
81
116
  }
@@ -85,6 +120,9 @@ export const useSelectionActions = (contextIds: string[], mode: SelectionMode =
85
120
 
86
121
  const multiSelect = useCallback(
87
122
  (ids: string[]) => {
123
+ if (!selection) {
124
+ return;
125
+ }
88
126
  for (const contextId of stableContextIds) {
89
127
  selection.updateMulti(contextId, ids);
90
128
  }
@@ -94,6 +132,9 @@ export const useSelectionActions = (contextIds: string[], mode: SelectionMode =
94
132
 
95
133
  const rangeSelect = useCallback(
96
134
  (from: string, to: string) => {
135
+ if (!selection) {
136
+ return;
137
+ }
97
138
  for (const contextId of stableContextIds) {
98
139
  selection.updateRange(contextId, from, to);
99
140
  }
@@ -103,6 +144,9 @@ export const useSelectionActions = (contextIds: string[], mode: SelectionMode =
103
144
 
104
145
  const toggle = useCallback(
105
146
  (id: string) => {
147
+ if (!selection) {
148
+ return;
149
+ }
106
150
  for (const contextId of stableContextIds) {
107
151
  selection.toggleSelection(contextId, id);
108
152
  }
@@ -111,6 +155,9 @@ export const useSelectionActions = (contextIds: string[], mode: SelectionMode =
111
155
  );
112
156
 
113
157
  const clear = useCallback(() => {
158
+ if (!selection) {
159
+ return;
160
+ }
114
161
  for (const contextId of stableContextIds) {
115
162
  selection.clearSelection(contextId);
116
163
  }