@dxos/react-ui-attention 0.8.4-main.ead640a → 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 +75 -8
  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 +21 -5
  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 +75 -8
  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 +21 -5
  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 +5 -4
  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 +2 -2
  42. package/src/components/AttentionGlyph.tsx +1 -1
  43. package/src/components/AttentionProvider.stories.tsx +1 -1
  44. package/src/components/AttentionProvider.tsx +72 -65
  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 +21 -4
  49. package/src/types.ts +16 -0
  50. package/dist/lib/browser/chunk-HELLA3BH.mjs +0 -675
  51. package/dist/lib/browser/chunk-HELLA3BH.mjs.map +0 -7
  52. package/dist/lib/node-esm/chunk-FBZV7QG7.mjs +0 -677
  53. package/dist/lib/node-esm/chunk-FBZV7QG7.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.ead640a",
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.ead640a",
49
- "@dxos/invariant": "0.8.4-main.ead640a",
50
- "@dxos/live-object": "0.8.4-main.ead640a",
51
- "@dxos/log": "0.8.4-main.ead640a",
52
- "@dxos/util": "0.8.4-main.ead640a"
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.1",
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/react-ui-theme": "0.8.4-main.ead640a",
61
- "@dxos/random": "0.8.4-main.ead640a",
62
- "@dxos/react-ui": "0.8.4-main.ead640a",
63
- "@dxos/storybook-utils": "0.8.4-main.ead640a"
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": "0.8.4-main.ead640a",
70
- "@dxos/react-ui-theme": "0.8.4-main.ead640a"
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;
@@ -76,7 +76,7 @@ export const Syncing: Story = {
76
76
  });
77
77
 
78
78
  return (
79
- <div className='flex flex-col p-2 w-[200px]'>
79
+ <div className='flex flex-col p-2 is-[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'>
@@ -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,21 +2,31 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- // NOTE(thure): The following unused imports quell TS2742 (“likely not portable”).
6
-
7
- // eslint-disable-next-line unused-imports/no-unused-imports
8
- import { type CreateScope, type Scope, createContext } from '@radix-ui/react-context';
5
+ import { RegistryContext } from '@effect-atom/atom-react';
6
+ import { createContext } from '@radix-ui/react-context';
9
7
  import { Primitive } from '@radix-ui/react-primitive';
10
8
  import { Slot } from '@radix-ui/react-slot';
11
- import React, { type ComponentPropsWithRef, type FocusEvent, type PropsWithChildren, forwardRef, useMemo } from 'react';
12
-
13
- import { type ThemedClassName, useDefaultValue } from '@dxos/react-ui';
14
- import { mx } from '@dxos/react-ui-theme';
15
-
16
- import { ATTENDABLE_PATH_SEPARATOR, type Attention, AttentionManager, getAttendables } from '../attention';
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';
19
+
20
+ import { log } from '@dxos/log';
21
+ import { useDefaultValue } from '@dxos/react-hooks';
22
+ import { type ThemedClassName } from '@dxos/react-ui';
23
+ import { mx } from '@dxos/ui-theme';
24
+
25
+ import { AttentionManager, getAttendables } from '../attention';
26
+ import { ATTENDABLE_PATH_SEPARATOR, type Attention } from '../types';
17
27
 
18
28
  const ATTENTION_NAME = 'Attention';
19
- const ATTENABLE_ATTRIBUTE = 'data-attendable-id';
29
+ const ATTENDABLE_ATTRIBUTE = 'data-attendable-id';
20
30
  const ATTENTION_SOURCE_ATTRIBUTE = 'data-is-attention-source';
21
31
 
22
32
  type AttentionContextValue = {
@@ -25,25 +35,60 @@ type AttentionContextValue = {
25
35
  };
26
36
 
27
37
  const [AttentionContextProvider, useAttentionContext] = createContext<AttentionContextValue>(ATTENTION_NAME, {
28
- attention: new AttentionManager(),
38
+ attention: undefined as unknown as AttentionManager,
29
39
  path: [],
30
40
  });
31
41
 
32
- const UNKNOWN_ATTENDABLE = { hasAttention: false, isAncestor: false, isRelated: false };
42
+ const UNKNOWN_ATTENDABLE = { hasAttention: false, isAncestor: false, isRelated: false } as Attention;
33
43
 
34
44
  const useAttention = (attendableId?: string): Attention => {
35
45
  const { attention, path } = useAttentionContext(ATTENTION_NAME);
36
- if (!attendableId) {
37
- return UNKNOWN_ATTENDABLE;
38
- }
46
+ const [state, setState] = useState<Attention>(UNKNOWN_ATTENDABLE);
47
+
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]);
39
69
 
40
- const current = [...attendableId.split(ATTENDABLE_PATH_SEPARATOR), ...path];
41
- return attention.get(current);
70
+ return state;
42
71
  };
43
72
 
44
73
  const useAttended = () => {
45
74
  const { attention } = useAttentionContext(ATTENTION_NAME);
46
- 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;
47
92
  };
48
93
 
49
94
  /**
@@ -53,7 +98,7 @@ const useAttended = () => {
53
98
  const useAttentionAttributes = (attendableId?: string) => {
54
99
  const { hasAttention } = useAttention(attendableId);
55
100
  return useMemo(() => {
56
- const attributes: Record<string, string | undefined> = { [ATTENABLE_ATTRIBUTE]: attendableId };
101
+ const attributes: Record<string, string | undefined> = { [ATTENDABLE_ATTRIBUTE]: attendableId };
57
102
  if (hasAttention) {
58
103
  attributes[ATTENTION_SOURCE_ATTRIBUTE] = 'true';
59
104
  }
@@ -77,10 +122,12 @@ const RootAttentionProvider = ({
77
122
  onChange: (nextAttended: string[]) => void;
78
123
  }>
79
124
  >) => {
80
- const attention = useDefaultValue(propsAttention, () => new AttentionManager());
125
+ const registry = useContext(RegistryContext);
126
+ const attention = useDefaultValue(propsAttention, () => new AttentionManager(registry));
127
+
81
128
  const handleFocus = (event: FocusEvent) => {
82
129
  // NOTE(thure): Use the following to debug focus movement across the app:
83
- // console.log('[focus]', event.relatedTarget, event.target);
130
+ log('focus', { related: event.relatedTarget, target: event.target });
84
131
 
85
132
  const selector = [
86
133
  '[data-attendable-id]',
@@ -88,7 +135,7 @@ const RootAttentionProvider = ({
88
135
  (el) => `[id="${el.getAttribute('aria-controls')}"]`,
89
136
  ),
90
137
  ].join(',');
91
- const prev = attention.current;
138
+ const prev = attention.getCurrent();
92
139
  const next = getAttendables(selector, event.target);
93
140
  // TODO(wittjosiah): Not allowing empty state means that the attended item is not strictly guaranteed to be in the DOM.
94
141
  // Currently this depends on the deck in order to ensure that when the attended item is removed something else is attended.
@@ -99,49 +146,9 @@ const RootAttentionProvider = ({
99
146
  }
100
147
  };
101
148
 
102
- // NOTE(thure): Use the following to debug the macOS package issue #8540:
103
-
104
- // const [startEl, setStartEl] = useState<HTMLElement | null>(null);
105
- // const [endEl, setEndEl] = useState<HTMLElement | null>(null);
106
- //
107
- // const handleEventDebug = useCallback((event: any) => {
108
- // console.log(`[${event.type}]`, event.target, event.currentTarget, [event.clientX, event.clientY]);
109
- // }, []);
110
- //
111
- // const handleStartDebug = useCallback(
112
- // (event: any) => {
113
- // setStartEl(event.target);
114
- // handleEventDebug(event);
115
- // },
116
- // [handleEventDebug],
117
- // );
118
- //
119
- // const handleEndDebug = useCallback(
120
- // (event: any) => {
121
- // setEndEl(event.target);
122
- // handleEventDebug(event);
123
- // },
124
- // [handleEventDebug],
125
- // );
126
- //
127
- // const handleClickDebug = useCallback(
128
- // (event: any) => {
129
- // console.log('[click compare]', startEl, endEl, startEl === endEl);
130
- // handleEventDebug(event);
131
- // },
132
- // [startEl, endEl, handleEventDebug],
133
- // );
134
-
135
149
  return (
136
150
  <AttentionContextProvider attention={attention} path={[]}>
137
- <div
138
- role='none'
139
- className='contents'
140
- onFocusCapture={handleFocus}
141
- // onClick={handleClickDebug}
142
- // onMouseDown={handleStartDebug}
143
- // onMouseUp={handleEndDebug}
144
- >
151
+ <div role='none' className='contents' onFocusCapture={handleFocus}>
145
152
  {children}
146
153
  </div>
147
154
  </AttentionContextProvider>
@@ -196,6 +203,6 @@ export {
196
203
  useAttentionAttributes,
197
204
  useAttentionPath,
198
205
  ATTENTION_NAME,
199
- ATTENABLE_ATTRIBUTE,
206
+ ATTENDABLE_ATTRIBUTE,
200
207
  ATTENTION_SOURCE_ATTRIBUTE,
201
208
  };