@dxos/react-ui-attention 0.8.4-main.9735255 → 0.8.4-main.9be5663bfe

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 (48) hide show
  1. package/dist/lib/browser/{chunk-HJRZWOEX.mjs → chunk-4SEURHOL.mjs} +122 -106
  2. package/dist/lib/browser/chunk-4SEURHOL.mjs.map +7 -0
  3. package/dist/lib/browser/chunk-GMKHOVJ5.mjs +1 -0
  4. package/dist/lib/browser/chunk-GMKHOVJ5.mjs.map +7 -0
  5. package/dist/lib/browser/index.mjs +17 -12
  6. package/dist/lib/browser/index.mjs.map +3 -3
  7. package/dist/lib/browser/meta.json +1 -1
  8. package/dist/lib/browser/testing/index.mjs +16 -7
  9. package/dist/lib/browser/testing/index.mjs.map +3 -3
  10. package/dist/lib/browser/types.mjs +1 -6
  11. package/dist/lib/node-esm/{chunk-7TQWDE4M.mjs → chunk-6PQ4P7AL.mjs} +122 -105
  12. package/dist/lib/node-esm/chunk-6PQ4P7AL.mjs.map +7 -0
  13. package/dist/lib/node-esm/chunk-UHVMYYIB.mjs +2 -0
  14. package/dist/lib/node-esm/chunk-UHVMYYIB.mjs.map +7 -0
  15. package/dist/lib/node-esm/index.mjs +17 -12
  16. package/dist/lib/node-esm/index.mjs.map +3 -3
  17. package/dist/lib/node-esm/meta.json +1 -1
  18. package/dist/lib/node-esm/testing/index.mjs +16 -7
  19. package/dist/lib/node-esm/testing/index.mjs.map +3 -3
  20. package/dist/lib/node-esm/types.mjs +1 -6
  21. package/dist/types/src/attention.d.ts +41 -10
  22. package/dist/types/src/attention.d.ts.map +1 -1
  23. package/dist/types/src/components/AttentionGlyph.d.ts.map +1 -1
  24. package/dist/types/src/components/AttentionProvider.d.ts +9 -11
  25. package/dist/types/src/components/AttentionProvider.d.ts.map +1 -1
  26. package/dist/types/src/components/AttentionProvider.stories.d.ts.map +1 -1
  27. package/dist/types/src/components/SelectionProvider.d.ts.map +1 -1
  28. package/dist/types/src/testing/decorators/withAttention.d.ts +5 -1
  29. package/dist/types/src/testing/decorators/withAttention.d.ts.map +1 -1
  30. package/dist/types/src/types.d.ts +0 -1
  31. package/dist/types/src/types.d.ts.map +1 -1
  32. package/dist/types/tsconfig.tsbuildinfo +1 -1
  33. package/package.json +16 -16
  34. package/src/attention.test.ts +179 -23
  35. package/src/attention.ts +132 -54
  36. package/src/components/AttentionGlyph.stories.tsx +3 -3
  37. package/src/components/AttentionGlyph.tsx +12 -5
  38. package/src/components/AttentionProvider.stories.tsx +2 -3
  39. package/src/components/AttentionProvider.tsx +21 -48
  40. package/src/components/SelectionProvider.tsx +6 -4
  41. package/src/testing/decorators/withAttention.ts +18 -7
  42. package/src/types.ts +0 -3
  43. package/dist/lib/browser/chunk-HJRZWOEX.mjs.map +0 -7
  44. package/dist/lib/browser/chunk-PAZBZOO2.mjs +0 -7
  45. package/dist/lib/browser/chunk-PAZBZOO2.mjs.map +0 -7
  46. package/dist/lib/node-esm/chunk-7TQWDE4M.mjs.map +0 -7
  47. package/dist/lib/node-esm/chunk-PMGNPL4M.mjs +0 -9
  48. package/dist/lib/node-esm/chunk-PMGNPL4M.mjs.map +0 -7
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dxos/react-ui-attention",
3
- "version": "0.8.4-main.9735255",
3
+ "version": "0.8.4-main.9be5663bfe",
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",
@@ -48,7 +48,7 @@
48
48
  "src"
49
49
  ],
50
50
  "dependencies": {
51
- "@effect-atom/atom-react": "^0.4.6",
51
+ "@effect-atom/atom-react": "^0.5.0",
52
52
  "@radix-ui/primitive": "1.1.1",
53
53
  "@radix-ui/react-compose-refs": "1.1.1",
54
54
  "@radix-ui/react-context": "1.1.1",
@@ -56,30 +56,30 @@
56
56
  "@radix-ui/react-primitive": "2.0.2",
57
57
  "@radix-ui/react-slot": "1.1.2",
58
58
  "@radix-ui/react-use-controllable-state": "1.1.0",
59
- "effect": "3.19.11",
60
- "@dxos/echo": "0.8.4-main.9735255",
61
- "@dxos/invariant": "0.8.4-main.9735255",
62
- "@dxos/react-hooks": "0.8.4-main.9735255",
63
- "@dxos/log": "0.8.4-main.9735255",
64
- "@dxos/util": "0.8.4-main.9735255"
59
+ "effect": "3.20.0",
60
+ "@dxos/invariant": "0.8.4-main.9be5663bfe",
61
+ "@dxos/log": "0.8.4-main.9be5663bfe",
62
+ "@dxos/echo": "0.8.4-main.9be5663bfe",
63
+ "@dxos/react-hooks": "0.8.4-main.9be5663bfe",
64
+ "@dxos/util": "0.8.4-main.9be5663bfe"
65
65
  },
66
66
  "devDependencies": {
67
67
  "@types/react": "~19.2.7",
68
68
  "@types/react-dom": "~19.2.3",
69
69
  "react": "~19.2.3",
70
70
  "react-dom": "~19.2.3",
71
- "vite": "7.1.9",
72
- "@dxos/random": "0.8.4-main.9735255",
73
- "@dxos/storybook-utils": "0.8.4-main.9735255",
74
- "@dxos/react-ui": "0.8.4-main.9735255",
75
- "@dxos/ui-theme": "0.8.4-main.9735255"
71
+ "vite": "^7.1.11",
72
+ "@dxos/react-ui": "0.8.4-main.9be5663bfe",
73
+ "@dxos/random": "0.8.4-main.9be5663bfe",
74
+ "@dxos/storybook-utils": "0.8.4-main.9be5663bfe",
75
+ "@dxos/ui-theme": "0.8.4-main.9be5663bfe"
76
76
  },
77
77
  "peerDependencies": {
78
- "effect": "3.19.11",
78
+ "effect": "3.20.0",
79
79
  "react": "~19.2.3",
80
80
  "react-dom": "~19.2.3",
81
- "@dxos/react-ui": "0.8.4-main.9735255",
82
- "@dxos/ui-theme": "0.8.4-main.9735255"
81
+ "@dxos/react-ui": "0.8.4-main.9be5663bfe",
82
+ "@dxos/ui-theme": "0.8.4-main.9be5663bfe"
83
83
  },
84
84
  "publishConfig": {
85
85
  "access": "public"
@@ -3,50 +3,206 @@
3
3
  //
4
4
 
5
5
  import { Registry } from '@effect-atom/atom-react';
6
- import { describe, expect, test } from 'vitest';
6
+ import { describe, test } from 'vitest';
7
7
 
8
- import { AttentionManager } from './attention';
8
+ import {
9
+ AttentionManager,
10
+ expandAttendableId,
11
+ getLinkedVariant,
12
+ getParentId,
13
+ getSegmentId,
14
+ isLinkedSegment,
15
+ linkedSegment,
16
+ } from './attention';
9
17
 
10
18
  describe('AttentionManager', () => {
11
- test('takes an initial attendable', () => {
19
+ test('takes an initial attended id', ({ expect }) => {
12
20
  const registry = Registry.make();
13
- const attention = new AttentionManager(registry, ['a', 'b', 'c']);
14
- expect(attention.getCurrent()).to.deep.equal(['a', 'b', 'c']);
15
- expect(attention.keys()).to.deep.equal([['a'], ['b', 'c'], ['c'], ['a', 'b', 'c']]);
16
- expect(attention.get(['a'])).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: true });
17
- expect(attention.get(['b', 'c'])).to.deep.equal({ hasAttention: false, isAncestor: true, isRelated: false });
18
- expect(attention.get(['c'])).to.deep.equal({ hasAttention: false, isAncestor: true, isRelated: false });
19
- expect(attention.get(['a', 'b', 'c'])).to.deep.equal({ hasAttention: true, isAncestor: false, isRelated: false });
20
- expect(attention.get(['b'])).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: false });
21
+ const attention = new AttentionManager(registry, ['root/a/b']);
22
+
23
+ expect(attention.getCurrent()).to.deep.equal(['root/a/b']);
24
+ expect(attention.get('root')).to.deep.equal({ hasAttention: false, isAncestor: true, isRelated: false });
25
+ expect(attention.get('root/a')).to.deep.equal({ hasAttention: false, isAncestor: true, isRelated: false });
26
+ expect(attention.get('root/a/b')).to.deep.equal({ hasAttention: true, isAncestor: false, isRelated: false });
27
+ expect(attention.get('unrelated')).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: false });
21
28
  });
22
29
 
23
- test('current returns last updated key', () => {
30
+ test('current returns last updated ids', ({ expect }) => {
24
31
  const registry = Registry.make();
25
32
  const attention = new AttentionManager(registry);
26
33
  expect(attention.getCurrent()).to.deep.equal([]);
27
34
 
28
- attention.update(['a', 'b', 'c']);
29
- expect(attention.getCurrent()).to.deep.equal(['a', 'b', 'c']);
35
+ attention.update(['root/a/b/c']);
36
+ expect(attention.getCurrent()).to.deep.equal(['root/a/b/c']);
30
37
  });
31
38
 
32
- test('keys returns all stored attendables', () => {
39
+ test('keys returns all tracked qualified ids', ({ expect }) => {
33
40
  const registry = Registry.make();
34
41
  const attention = new AttentionManager(registry);
35
42
  expect(attention.keys()).to.deep.equal([]);
36
43
 
37
- attention.update(['a']);
38
- expect(attention.keys()).to.deep.equal([['a']]);
44
+ attention.update(['root/a']);
45
+ expect(attention.keys()).to.include('root');
46
+ expect(attention.keys()).to.include('root/a');
47
+ });
48
+
49
+ test('get returns attention state for a given qualified id', ({ expect }) => {
50
+ const registry = Registry.make();
51
+ const attention = new AttentionManager(registry);
52
+ expect(attention.get('root/a')).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: false });
53
+
54
+ attention.update(['root/a']);
55
+ expect(attention.get('root/a')).to.deep.equal({ hasAttention: true, isAncestor: false, isRelated: false });
56
+ expect(attention.get('root')).to.deep.equal({ hasAttention: false, isAncestor: true, isRelated: false });
57
+ });
58
+
59
+ test('clears previous attention on update', ({ expect }) => {
60
+ const registry = Registry.make();
61
+ const attention = new AttentionManager(registry);
62
+
63
+ attention.update(['root/a/b']);
64
+ expect(attention.get('root/a/b')).to.deep.equal({ hasAttention: true, isAncestor: false, isRelated: false });
65
+ expect(attention.get('root/a')).to.deep.equal({ hasAttention: false, isAncestor: true, isRelated: false });
66
+
67
+ attention.update(['root/x/y']);
68
+ expect(attention.get('root/a/b')).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: false });
69
+ expect(attention.get('root/a')).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: false });
70
+ expect(attention.get('root/x/y')).to.deep.equal({ hasAttention: true, isAncestor: false, isRelated: false });
71
+ expect(attention.get('root/x')).to.deep.equal({ hasAttention: false, isAncestor: true, isRelated: false });
72
+ expect(attention.get('root')).to.deep.equal({ hasAttention: false, isAncestor: true, isRelated: false });
73
+ });
74
+
75
+ test('linked segment marks parent with isRelated and isAncestor', ({ expect }) => {
76
+ const registry = Registry.make();
77
+ const attention = new AttentionManager(registry);
39
78
 
40
- attention.update(['a', 'b']);
41
- expect(attention.keys()).to.deep.equal([['a'], ['b'], ['a', 'b']]);
79
+ attention.update(['root/space/obj/~settings']);
80
+ expect(attention.get('root/space/obj/~settings')).to.deep.equal({
81
+ hasAttention: true,
82
+ isAncestor: false,
83
+ isRelated: false,
84
+ });
85
+ expect(attention.get('root/space/obj')).to.deep.equal({
86
+ hasAttention: false,
87
+ isAncestor: true,
88
+ isRelated: true,
89
+ });
90
+ expect(attention.get('root/space')).to.deep.equal({
91
+ hasAttention: false,
92
+ isAncestor: true,
93
+ isRelated: false,
94
+ });
95
+ expect(attention.get('root')).to.deep.equal({
96
+ hasAttention: false,
97
+ isAncestor: true,
98
+ isRelated: false,
99
+ });
42
100
  });
43
101
 
44
- test('get returns attention object for a given key', () => {
102
+ test('switching from linked segment to non-linked clears parent isRelated', ({ expect }) => {
45
103
  const registry = Registry.make();
46
104
  const attention = new AttentionManager(registry);
47
- expect(attention.get(['a'])).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: false });
48
105
 
49
- attention.update(['a']);
50
- expect(attention.get(['a'])).to.deep.equal({ hasAttention: true, isAncestor: false, isRelated: false });
106
+ attention.update(['root/space/obj/~settings']);
107
+ expect(attention.get('root/space/obj')).to.deep.equal({
108
+ hasAttention: false,
109
+ isAncestor: true,
110
+ isRelated: true,
111
+ });
112
+
113
+ attention.update(['root/space/obj']);
114
+ expect(attention.get('root/space/obj')).to.deep.equal({
115
+ hasAttention: true,
116
+ isAncestor: false,
117
+ isRelated: false,
118
+ });
119
+ expect(attention.get('root/space/obj/~settings')).to.deep.equal({
120
+ hasAttention: false,
121
+ isAncestor: false,
122
+ isRelated: false,
123
+ });
124
+ });
125
+
126
+ test('keys sharing the same segment id are marked isRelated', ({ expect }) => {
127
+ const registry = Registry.make();
128
+ const attention = new AttentionManager(registry);
129
+
130
+ // Pre-register the alternate path so the manager knows about it.
131
+ attention.get('root/z/y/c');
132
+
133
+ attention.update(['root/a/b/c']);
134
+ expect(attention.get('root/a/b/c')).to.deep.equal({ hasAttention: true, isAncestor: false, isRelated: false });
135
+ expect(attention.get('root/z/y/c')).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: true });
136
+ });
137
+
138
+ test('related keys are cleared when attention moves to a different segment id', ({ expect }) => {
139
+ const registry = Registry.make();
140
+ const attention = new AttentionManager(registry);
141
+
142
+ attention.get('root/z/y/c');
143
+ attention.update(['root/a/b/c']);
144
+ expect(attention.get('root/z/y/c')).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: true });
145
+
146
+ attention.update(['root/x/y/d']);
147
+ expect(attention.get('root/z/y/c')).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: false });
148
+ expect(attention.get('root/a/b/c')).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: false });
149
+ });
150
+ });
151
+
152
+ describe('expandAttendableId', () => {
153
+ test('produces progressive prefixes', ({ expect }) => {
154
+ expect(expandAttendableId('root/space/types/doc')).to.deep.equal([
155
+ 'root',
156
+ 'root/space',
157
+ 'root/space/types',
158
+ 'root/space/types/doc',
159
+ ]);
160
+ });
161
+
162
+ test('single segment returns itself', ({ expect }) => {
163
+ expect(expandAttendableId('root')).to.deep.equal(['root']);
164
+ });
165
+ });
166
+
167
+ describe('isLinkedSegment', () => {
168
+ test('detects linked prefix on last segment', ({ expect }) => {
169
+ expect(isLinkedSegment('root/space/obj/~settings')).toBe(true);
170
+ expect(isLinkedSegment('root/space/obj')).toBe(false);
171
+ expect(isLinkedSegment('~standalone')).toBe(true);
172
+ });
173
+ });
174
+
175
+ describe('linkedSegment', () => {
176
+ test('builds a linked segment', ({ expect }) => {
177
+ expect(linkedSegment('settings')).toBe('~settings');
178
+ expect(linkedSegment('comments')).toBe('~comments');
179
+ });
180
+ });
181
+
182
+ describe('getLinkedVariant', () => {
183
+ test('extracts variant from linked last segment', ({ expect }) => {
184
+ expect(getLinkedVariant('root/space/obj/~settings')).toBe('settings');
185
+ expect(getLinkedVariant('root/a/~comments')).toBe('comments');
186
+ });
187
+
188
+ test('returns segment as-is when not linked', ({ expect }) => {
189
+ expect(getLinkedVariant('root/space/obj')).toBe('obj');
190
+ expect(getLinkedVariant('root')).toBe('root');
191
+ });
192
+ });
193
+
194
+ describe('getParentId', () => {
195
+ test('returns parent qualified id', ({ expect }) => {
196
+ expect(getParentId('root/space/obj/~settings')).toBe('root/space/obj');
197
+ expect(getParentId('root/space')).toBe('root');
198
+ expect(getParentId('root')).toBeUndefined();
199
+ });
200
+ });
201
+
202
+ describe('getSegmentId', () => {
203
+ test('returns the last path segment', ({ expect }) => {
204
+ expect(getSegmentId('root/space/obj')).toBe('obj');
205
+ expect(getSegmentId('root')).toBe('root');
206
+ expect(getSegmentId('root/a/~settings')).toBe('~settings');
51
207
  });
52
208
  });
package/src/attention.ts CHANGED
@@ -4,48 +4,42 @@
4
4
 
5
5
  import { Atom, type Registry } from '@effect-atom/atom-react';
6
6
 
7
- import { ComplexMap } from '@dxos/util';
8
-
9
- import { ATTENDABLE_PATH_SEPARATOR, type Attention } from './types';
10
-
11
- // TODO(wittjosiah): Use mosaic path utility?
12
- const stringKey = (key: string[]) => key.join(',');
7
+ import { type Attention } from './types';
13
8
 
14
9
  /**
15
10
  * Manages attention state for an application.
11
+ * Attention keys are slash-qualified graph IDs; ancestry is derived from progressive prefixes.
16
12
  */
17
13
  export class AttentionManager {
18
- // Each attention path is associated with an attention atom.
19
- // The lookup is not a reactive object to ensure that attention for each path is subscribable independently.
20
- private readonly _map = new ComplexMap<string[], Atom.Writable<Attention>>(stringKey);
14
+ private readonly _map = new Map<string, Atom.Writable<Attention>>();
21
15
  private readonly _currentAtom: Atom.Writable<string[]>;
22
16
 
23
17
  constructor(
24
18
  private readonly _registry: Registry.Registry,
25
19
  initial: string[] = [],
26
20
  ) {
27
- this._currentAtom = Atom.make<string[]>([]);
21
+ this._currentAtom = Atom.make<string[]>([]).pipe(Atom.keepAlive);
28
22
  if (initial.length > 0) {
29
23
  this.update(initial);
30
24
  }
31
25
  }
32
26
 
33
27
  /**
34
- * Atom for the currently attended element path.
28
+ * Atom for the currently attended element IDs.
35
29
  */
36
30
  get current(): Atom.Atom<string[]> {
37
31
  return this._currentAtom;
38
32
  }
39
33
 
40
34
  /**
41
- * Gets the currently attended element path.
35
+ * Gets the currently attended element IDs.
42
36
  */
43
37
  getCurrent(): readonly string[] {
44
38
  return this._registry.get(this._currentAtom);
45
39
  }
46
40
 
47
41
  /**
48
- * Subscribe to changes in the current attention path.
42
+ * Subscribe to changes in the current attention IDs.
49
43
  */
50
44
  subscribeCurrent(cb: (current: readonly string[]) => void): () => void {
51
45
  this._registry.get(this._currentAtom);
@@ -55,79 +49,104 @@ export class AttentionManager {
55
49
  }
56
50
 
57
51
  /**
58
- * All attention paths.
52
+ * All tracked qualified IDs.
59
53
  */
60
- keys(): string[][] {
54
+ keys(): string[] {
61
55
  return Array.from(this._map.keys());
62
56
  }
63
57
 
64
58
  /**
65
- * Get the attention state for a given path.
59
+ * Get the attention state for a given qualified ID.
66
60
  */
67
- get(key: string[]): Attention {
68
- const atom = this._getAtom(key);
61
+ get(id: string): Attention {
62
+ const atom = this._getAtom(id);
69
63
  return this._registry.get(atom);
70
64
  }
71
65
 
72
66
  /**
73
- * Subscribe to changes in the attention state for a given path.
67
+ * Subscribe to changes in the attention state for a given qualified ID.
74
68
  */
75
- subscribe(key: string[], cb: (attention: Attention) => void): () => void {
76
- const atom = this._getAtom(key);
69
+ subscribe(id: string, cb: (attention: Attention) => void): () => void {
70
+ const atom = this._getAtom(id);
77
71
  this._registry.get(atom);
78
72
  return this._registry.subscribe(atom, () => {
79
73
  cb(this._registry.get(atom));
80
74
  });
81
75
  }
82
76
 
83
- private _getAtom(key: string[]): Atom.Writable<Attention> {
84
- const existing = this._map.get(key);
77
+ private _getAtom(id: string): Atom.Writable<Attention> {
78
+ const existing = this._map.get(id);
85
79
  if (existing) {
86
80
  return existing;
87
81
  }
88
82
 
89
- const newAtom = Atom.make<Attention>({ hasAttention: false, isAncestor: false, isRelated: false });
90
- this._map.set(key, newAtom);
83
+ const newAtom = Atom.make<Attention>({ hasAttention: false, isAncestor: false, isRelated: false }).pipe(
84
+ Atom.keepAlive,
85
+ );
86
+ this._map.set(id, newAtom);
91
87
  return newAtom;
92
88
  }
93
89
 
94
90
  /**
95
91
  * Update the currently attended element.
92
+ * Takes the array of qualified IDs collected from the DOM; the first element is the primary attended item.
93
+ * Ancestry is derived from the progressive prefixes of the primary ID.
94
+ * Relatedness is derived from the segment ID: any tracked key whose last `/` segment matches the
95
+ * attended ID's segment ID is marked `isRelated`. Additionally, if the primary ID is a linked segment
96
+ * (starts with `~`), its immediate parent gets `isRelated` alongside `isAncestor`.
96
97
  *
97
98
  * @internal
98
99
  */
99
- update(nextKey: string[]): void {
100
- const currentKey = this.getCurrent();
101
- const currentRelatedTo = currentKey[0];
102
- const nextRelatedTo = nextKey[0];
100
+ update(nextIds: string[]): void {
101
+ const primaryId = nextIds[0];
102
+ if (!primaryId) {
103
+ return;
104
+ }
103
105
 
104
- // Ensure related attention object is initialized.
105
- this._getAtom([nextRelatedTo]);
106
+ const currentIds = this.getCurrent();
107
+ const prevPrimaryId = currentIds[0];
108
+ // Clear previous attention state: ancestors, primary, and related keys.
109
+ if (prevPrimaryId) {
110
+ const prevPrefixes = expandAttendableId(prevPrimaryId);
111
+ for (const prefix of prevPrefixes) {
112
+ this._set(prefix, {});
113
+ }
114
+ const prevSegmentId = getSegmentId(prevPrimaryId);
115
+ for (const key of this.keys()) {
116
+ if (getSegmentId(key) === prevSegmentId) {
117
+ this._set(key, {});
118
+ }
119
+ }
120
+ }
106
121
 
107
- const currentRelatedKeys = this.keys().filter((key) => key[0] === currentRelatedTo);
108
- const nextRelatedKeys = this.keys().filter(
109
- (key) => key[0] === nextRelatedTo && stringKey(key) !== stringKey(nextKey),
110
- );
111
- const prevRelatedKeys = currentRelatedKeys.filter(
112
- (curr) => !nextRelatedKeys.find((next) => stringKey(curr) === stringKey(next)),
113
- );
122
+ this._registry.set(this._currentAtom, nextIds);
114
123
 
115
- const currentAncestorKeys = currentKey.slice(1).map((_, i) => currentKey.slice(i + 1, currentKey.length));
116
- const nextAncestorKeys = nextKey.slice(1).map((_, i) => nextKey.slice(i + 1, nextKey.length));
117
- const prevAncestorKeys = currentAncestorKeys.filter(
118
- (curr) => !nextAncestorKeys.find((next) => stringKey(curr) === stringKey(next)),
119
- );
124
+ // Set ancestors and primary.
125
+ const prefixes = expandAttendableId(primaryId);
126
+ const prefixSet = new Set(prefixes);
127
+ const linkedParent = isLinkedSegment(primaryId) ? getParentId(primaryId) : undefined;
128
+
129
+ for (const prefix of prefixes) {
130
+ if (prefix === primaryId) {
131
+ this._set(prefix, { hasAttention: true });
132
+ } else if (prefix === linkedParent) {
133
+ this._set(prefix, { isAncestor: true, isRelated: true });
134
+ } else {
135
+ this._set(prefix, { isAncestor: true });
136
+ }
137
+ }
120
138
 
121
- this._registry.set(this._currentAtom, nextKey);
122
- prevRelatedKeys.forEach((key) => this._set(key, {}));
123
- prevAncestorKeys.forEach((key) => this._set(key, {}));
124
- nextRelatedKeys.forEach((key) => this._set(key, { isRelated: true }));
125
- nextAncestorKeys.forEach((key) => this._set(key, { isAncestor: true }));
126
- this._set(nextKey, { hasAttention: true });
139
+ // Set related keys: any tracked key sharing the same segment ID.
140
+ const segmentId = getSegmentId(primaryId);
141
+ for (const key of this.keys()) {
142
+ if (!prefixSet.has(key) && getSegmentId(key) === segmentId) {
143
+ this._set(key, { isRelated: true });
144
+ }
145
+ }
127
146
  }
128
147
 
129
- private _set(key: string[], attention: Partial<Attention>): void {
130
- const atom = this._getAtom(key);
148
+ private _set(id: string, attention: Partial<Attention>): void {
149
+ const atom = this._getAtom(id);
131
150
  this._registry.set(atom, {
132
151
  hasAttention: attention.hasAttention ?? false,
133
152
  isAncestor: attention.isAncestor ?? false,
@@ -137,7 +156,8 @@ export class AttentionManager {
137
156
  }
138
157
 
139
158
  /**
140
- * Accumulates all attendable id's between the element provided and the root, inclusive.
159
+ * Accumulates all attendable IDs between the element provided and the root, inclusive.
160
+ * Each `data-attendable-id` value is treated as a single qualified ID (no splitting).
141
161
  */
142
162
  export const getAttendables = (selector: string, cursor: Element, acc: string[] = []): string[] => {
143
163
  // Find the closest element with `data-attendable-id`, if any; start from cursor and move up the DOM tree.
@@ -153,8 +173,7 @@ export const getAttendables = (selector: string, cursor: Element, acc: string[]
153
173
  return getAttendables(selector, trigger, acc);
154
174
  }
155
175
  } else {
156
- acc.push(...attendableId.split(ATTENDABLE_PATH_SEPARATOR));
157
- // TODO: Attempt tail recursion.
176
+ acc.push(attendableId);
158
177
  return !closestAttendable.parentElement ? acc : getAttendables(selector, closestAttendable.parentElement, acc);
159
178
  }
160
179
  }
@@ -165,3 +184,62 @@ export const getAttendables = (selector: string, cursor: Element, acc: string[]
165
184
  export type AttendableId = { attendableId?: string };
166
185
 
167
186
  export type Related = { related?: boolean };
187
+
188
+ /**
189
+ * Prefix for linked segments. A linked segment's attention state is shared with its parent:
190
+ * when a linked node has attention, the parent is marked as both ancestor and related.
191
+ */
192
+ const LINKED_PREFIX = '~';
193
+
194
+ /**
195
+ * Decompose a qualified graph ID into progressive prefixes for ancestry tracking.
196
+ * e.g. `root/a/b/c` yields `['root', 'root/a', 'root/a/b', 'root/a/b/c']`.
197
+ */
198
+ export const expandAttendableId = (qualifiedId: string): string[] => {
199
+ const segments = qualifiedId.split('/');
200
+ return segments.map((_, index) => segments.slice(0, index + 1).join('/'));
201
+ };
202
+
203
+ /**
204
+ * Whether the last segment of a qualified ID is a linked segment (starts with `~`).
205
+ * Linked segments share their attention state with the parent node.
206
+ */
207
+ export const isLinkedSegment = (qualifiedId: string): boolean => {
208
+ const lastSegment = qualifiedId.split('/').pop() ?? '';
209
+ return lastSegment.startsWith(LINKED_PREFIX);
210
+ };
211
+
212
+ /**
213
+ * Build a linked segment ID from a variant name (e.g., `'settings'` -> `'~settings'`).
214
+ * Nodes identified by linked segments share their attention state with the parent node.
215
+ */
216
+ export const linkedSegment = (variant: string): string => `${LINKED_PREFIX}${variant}`;
217
+
218
+ /**
219
+ * Extract the variant name from the last segment of a qualified ID, stripping the linked prefix.
220
+ * e.g. `'root/space/obj/~settings'` -> `'settings'`.
221
+ * If the segment is not linked (no `~` prefix), returns the segment as-is.
222
+ */
223
+ export const getLinkedVariant = (qualifiedId: string): string => {
224
+ const lastSegment = qualifiedId.split('/').pop() ?? '';
225
+ return lastSegment.startsWith(LINKED_PREFIX) ? lastSegment.slice(LINKED_PREFIX.length) : lastSegment;
226
+ };
227
+
228
+ /**
229
+ * Get the parent qualified ID (everything before the last `/` segment).
230
+ */
231
+ export const getParentId = (qualifiedId: string | undefined): string | undefined => {
232
+ if (!qualifiedId) {
233
+ return undefined;
234
+ }
235
+
236
+ const lastSlash = qualifiedId.lastIndexOf('/');
237
+ return lastSlash > 0 ? qualifiedId.slice(0, lastSlash) : undefined;
238
+ };
239
+
240
+ /**
241
+ * Extract the segment ID (last segment) of a qualified ID.
242
+ */
243
+ export const getSegmentId = (qualifiedId: string): string => {
244
+ return qualifiedId.split('/').pop() ?? qualifiedId;
245
+ };
@@ -13,7 +13,7 @@ import { AttentionGlyph, type AttentionGlyphProps } from './AttentionGlyph';
13
13
 
14
14
  const DefaultStory = (props: AttentionGlyphProps) => {
15
15
  return (
16
- <ul className='flex gap-2 mbe-2'>
16
+ <ul className='flex gap-2 mb-2'>
17
17
  <li>
18
18
  <AttentionGlyph presence='none' {...props} />
19
19
  </li>
@@ -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;
@@ -76,7 +76,7 @@ export const Syncing: Story = {
76
76
  });
77
77
 
78
78
  return (
79
- <div className='flex flex-col p-2 is-[200px]'>
79
+ <div className='flex flex-col p-2 w-[200px]'>
80
80
  <Button onClick={handleChangeAttended}>Change attended</Button>
81
81
  {Array.from(spaces.entries()).map(([space, sync]) => (
82
82
  <div key={space} className='flex items-center'>
@@ -8,11 +8,18 @@ import React, { type CSSProperties, type ComponentPropsWithRef, forwardRef, useM
8
8
  import { Icon, type ThemedClassName } from '@dxos/react-ui';
9
9
  import { mx } from '@dxos/ui-theme';
10
10
 
11
- const attentionGlyphStyles =
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';
11
+ const attentionGlyphStyles = mx(
12
+ 'inline-block rounded-xs w-3 h-3 bg-transparent text-accent-text transition-colors',
13
+ '[[data-contains-attended=true]_&]:bg-attention-contains',
14
+ '[[data-attention=true]_&]:bg-accent-surface',
15
+ '[[data-attention=true]_&]:text-accent-surface-text',
16
+ '[[aria-current][data-attention=true]_&]:bg-accent-surface',
17
+ '[[aria-current][data-attention=true]_&]:text-accent-surface-text',
18
+ '[[aria-selected="true"][data-attention=true]_&]:bg-accent-surface',
19
+ '[[aria-selected="true"][data-attention=true]_&]:text-accent-surface-text',
20
+ );
13
21
 
14
- const presenceIconStyles =
15
- 'is-3 bs-3 group-[[aria-current]_&:hover]/attentionGlyphButton:hidden group-[[aria-current]_&:focus]/attentionGlyphButton:hidden group-[[data-attention=true]_&:hover]/attentionGlyphButton:hidden group-[[data-attention=true]_&:focus]/attentionGlyphButton:hidden group-[[aria-current][data-attention=true]_&:hover]/attentionGlyphButton:hidden group-[[aria-current][data-attention=true]_&:focus]/attentionGlyphButton:hidden';
22
+ const presenceIconStyles = mx('w-3 h-3');
16
23
 
17
24
  const PresenceOne = () => {
18
25
  return (
@@ -87,7 +94,7 @@ export const AttentionGlyph = forwardRef<HTMLSpanElement, AttentionGlyphProps>(
87
94
  ) : null;
88
95
 
89
96
  return (
90
- <div role='none' className='flex' data-attention={attended} data-contains-attended={containsAttended}>
97
+ <div role='none' className='flex group' data-attention={attended} data-contains-attended={containsAttended}>
91
98
  <span role='none' {...props} className={mx(attentionGlyphStyles, classNames)} ref={forwardedRef}>
92
99
  {icon}
93
100
  </span>
@@ -8,7 +8,6 @@ import React from 'react';
8
8
  import { withTheme } from '@dxos/react-ui/testing';
9
9
 
10
10
  import { withAttention } from '../testing';
11
-
12
11
  import { useAttentionAttributes } from './AttentionProvider';
13
12
 
14
13
  const Attendable = ({ id }: { id: string }) => {
@@ -16,7 +15,7 @@ const Attendable = ({ id }: { id: string }) => {
16
15
 
17
16
  return (
18
17
  <div {...attentionAttrs}>
19
- <textarea className='attention-surface resize-none' placeholder={id} />
18
+ <textarea className='dx-attention-surface resize-none' placeholder={id} />
20
19
  </div>
21
20
  );
22
21
  };
@@ -35,7 +34,7 @@ const Story = () => {
35
34
  const meta = {
36
35
  title: 'ui/react-ui-attention/AttentionProvider',
37
36
  render: Story,
38
- decorators: [withTheme, withAttention],
37
+ decorators: [withTheme(), withAttention()],
39
38
  } satisfies Meta;
40
39
 
41
40
  export default meta;