@dxos/react-ui-attention 0.8.4-main.84f28bd → 0.8.4-main.937b3ca
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/browser/chunk-HJRZWOEX.mjs +705 -0
- package/dist/lib/browser/chunk-HJRZWOEX.mjs.map +7 -0
- package/dist/lib/browser/chunk-PAZBZOO2.mjs +7 -0
- package/dist/lib/browser/chunk-PAZBZOO2.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +77 -8
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +10 -6
- package/dist/lib/browser/testing/index.mjs.map +3 -3
- package/dist/lib/browser/types.mjs +7 -0
- package/dist/lib/browser/types.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-7TQWDE4M.mjs +706 -0
- package/dist/lib/node-esm/chunk-7TQWDE4M.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-PMGNPL4M.mjs +9 -0
- package/dist/lib/node-esm/chunk-PMGNPL4M.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +77 -8
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/testing/index.mjs +10 -6
- package/dist/lib/node-esm/testing/index.mjs.map +3 -3
- package/dist/lib/node-esm/types.mjs +8 -0
- package/dist/lib/node-esm/types.mjs.map +7 -0
- package/dist/types/src/attention.d.ts +22 -17
- package/dist/types/src/attention.d.ts.map +1 -1
- package/dist/types/src/components/AttentionGlyph.d.ts.map +1 -1
- package/dist/types/src/components/AttentionGlyph.stories.d.ts +9 -20
- package/dist/types/src/components/AttentionGlyph.stories.d.ts.map +1 -1
- package/dist/types/src/components/AttentionProvider.d.ts +7 -6
- package/dist/types/src/components/AttentionProvider.d.ts.map +1 -1
- package/dist/types/src/components/AttentionProvider.stories.d.ts +6 -4
- package/dist/types/src/components/AttentionProvider.stories.d.ts.map +1 -1
- package/dist/types/src/components/SelectionProvider.d.ts +6 -5
- package/dist/types/src/components/SelectionProvider.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +1 -0
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/selection.d.ts +49 -17
- package/dist/types/src/selection.d.ts.map +1 -1
- package/dist/types/src/testing/decorators/withAttention.d.ts.map +1 -1
- package/dist/types/src/types.d.ts +10 -0
- package/dist/types/src/types.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +38 -24
- package/src/attention.test.ts +13 -8
- package/src/attention.ts +64 -41
- package/src/components/AttentionGlyph.stories.tsx +16 -13
- package/src/components/AttentionGlyph.tsx +2 -2
- package/src/components/AttentionProvider.stories.tsx +11 -7
- package/src/components/AttentionProvider.tsx +75 -65
- package/src/components/SelectionProvider.tsx +64 -19
- package/src/index.ts +1 -0
- package/src/selection.ts +135 -65
- package/src/testing/decorators/withAttention.ts +9 -3
- package/src/types.ts +16 -0
- package/dist/lib/browser/chunk-QPEFFJ4E.mjs +0 -616
- package/dist/lib/browser/chunk-QPEFFJ4E.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-F2JRKDYV.mjs +0 -618
- package/dist/lib/node-esm/chunk-F2JRKDYV.mjs.map +0 -7
package/package.json
CHANGED
|
@@ -1,20 +1,32 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/react-ui-attention",
|
|
3
|
-
"version": "0.8.4-main.
|
|
3
|
+
"version": "0.8.4-main.937b3ca",
|
|
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":
|
|
13
|
+
"sideEffects": false,
|
|
10
14
|
"type": "module",
|
|
11
15
|
"exports": {
|
|
12
16
|
".": {
|
|
17
|
+
"source": "./src/index.ts",
|
|
13
18
|
"types": "./dist/types/src/index.d.ts",
|
|
14
19
|
"browser": "./dist/lib/browser/index.mjs",
|
|
15
20
|
"node": "./dist/lib/node-esm/index.mjs"
|
|
16
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
|
+
},
|
|
17
28
|
"./testing": {
|
|
29
|
+
"source": "./src/testing/index.ts",
|
|
18
30
|
"types": "./dist/types/src/testing/index.d.ts",
|
|
19
31
|
"browser": "./dist/lib/browser/testing/index.mjs",
|
|
20
32
|
"node": "./dist/lib/node-esm/testing/index.mjs"
|
|
@@ -23,6 +35,9 @@
|
|
|
23
35
|
"types": "dist/types/src/index.d.ts",
|
|
24
36
|
"typesVersions": {
|
|
25
37
|
"*": {
|
|
38
|
+
"types": [
|
|
39
|
+
"dist/types/src/types.d.ts"
|
|
40
|
+
],
|
|
26
41
|
"testing": [
|
|
27
42
|
"dist/types/src/testing/index.d.ts"
|
|
28
43
|
]
|
|
@@ -33,8 +48,7 @@
|
|
|
33
48
|
"src"
|
|
34
49
|
],
|
|
35
50
|
"dependencies": {
|
|
36
|
-
"@
|
|
37
|
-
"@preact/signals-core": "^1.9.0",
|
|
51
|
+
"@effect-atom/atom-react": "^0.4.6",
|
|
38
52
|
"@radix-ui/primitive": "1.1.1",
|
|
39
53
|
"@radix-ui/react-compose-refs": "1.1.1",
|
|
40
54
|
"@radix-ui/react-context": "1.1.1",
|
|
@@ -42,30 +56,30 @@
|
|
|
42
56
|
"@radix-ui/react-primitive": "2.0.2",
|
|
43
57
|
"@radix-ui/react-slot": "1.1.2",
|
|
44
58
|
"@radix-ui/react-use-controllable-state": "1.1.0",
|
|
45
|
-
"effect": "3.
|
|
46
|
-
"@dxos/
|
|
47
|
-
"@dxos/
|
|
48
|
-
"@dxos/
|
|
49
|
-
"@dxos/
|
|
50
|
-
"@dxos/util": "0.8.4-main.
|
|
59
|
+
"effect": "3.19.11",
|
|
60
|
+
"@dxos/invariant": "0.8.4-main.937b3ca",
|
|
61
|
+
"@dxos/echo": "0.8.4-main.937b3ca",
|
|
62
|
+
"@dxos/log": "0.8.4-main.937b3ca",
|
|
63
|
+
"@dxos/react-hooks": "0.8.4-main.937b3ca",
|
|
64
|
+
"@dxos/util": "0.8.4-main.937b3ca"
|
|
51
65
|
},
|
|
52
66
|
"devDependencies": {
|
|
53
|
-
"@types/react": "~
|
|
54
|
-
"@types/react-dom": "~
|
|
55
|
-
"react": "~
|
|
56
|
-
"react-dom": "~
|
|
57
|
-
"vite": "
|
|
58
|
-
"@dxos/random": "0.8.4-main.
|
|
59
|
-
"@dxos/react-ui": "0.8.4-main.
|
|
60
|
-
"@dxos/
|
|
61
|
-
"@dxos/
|
|
67
|
+
"@types/react": "~19.2.7",
|
|
68
|
+
"@types/react-dom": "~19.2.3",
|
|
69
|
+
"react": "~19.2.3",
|
|
70
|
+
"react-dom": "~19.2.3",
|
|
71
|
+
"vite": "7.1.9",
|
|
72
|
+
"@dxos/random": "0.8.4-main.937b3ca",
|
|
73
|
+
"@dxos/react-ui": "0.8.4-main.937b3ca",
|
|
74
|
+
"@dxos/storybook-utils": "0.8.4-main.937b3ca",
|
|
75
|
+
"@dxos/ui-theme": "0.8.4-main.937b3ca"
|
|
62
76
|
},
|
|
63
77
|
"peerDependencies": {
|
|
64
|
-
"effect": "
|
|
65
|
-
"react": "~
|
|
66
|
-
"react-dom": "~
|
|
67
|
-
"@dxos/react-ui": "0.8.4-main.
|
|
68
|
-
"@dxos/
|
|
78
|
+
"effect": "3.19.11",
|
|
79
|
+
"react": "~19.2.3",
|
|
80
|
+
"react-dom": "~19.2.3",
|
|
81
|
+
"@dxos/react-ui": "0.8.4-main.937b3ca",
|
|
82
|
+
"@dxos/ui-theme": "0.8.4-main.937b3ca"
|
|
69
83
|
},
|
|
70
84
|
"publishConfig": {
|
|
71
85
|
"access": "public"
|
package/src/attention.test.ts
CHANGED
|
@@ -2,14 +2,16 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { Registry } from '@effect-atom/atom-react';
|
|
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
|
|
12
|
-
|
|
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
|
|
23
|
-
|
|
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.
|
|
29
|
+
expect(attention.getCurrent()).to.deep.equal(['a', 'b', 'c']);
|
|
27
30
|
});
|
|
28
31
|
|
|
29
32
|
test('keys returns all stored attendables', () => {
|
|
30
|
-
const
|
|
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
|
|
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,24 +2,11 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
import { Schema } from 'effect';
|
|
5
|
+
import { Atom, type Registry } from '@effect-atom/atom-react';
|
|
7
6
|
|
|
8
|
-
import { live, type Live } from '@dxos/live-object';
|
|
9
7
|
import { ComplexMap } from '@dxos/util';
|
|
10
8
|
|
|
11
|
-
|
|
12
|
-
export const ATTENDABLE_PATH_SEPARATOR = '~';
|
|
13
|
-
|
|
14
|
-
export const AttentionSchema = Schema.mutable(
|
|
15
|
-
Schema.Struct({
|
|
16
|
-
hasAttention: Schema.Boolean,
|
|
17
|
-
isAncestor: Schema.Boolean,
|
|
18
|
-
isRelated: Schema.Boolean,
|
|
19
|
-
}),
|
|
20
|
-
);
|
|
21
|
-
|
|
22
|
-
export type Attention = Schema.Schema.Type<typeof AttentionSchema>;
|
|
9
|
+
import { ATTENDABLE_PATH_SEPARATOR, type Attention } from './types';
|
|
23
10
|
|
|
24
11
|
// TODO(wittjosiah): Use mosaic path utility?
|
|
25
12
|
const stringKey = (key: string[]) => key.join(',');
|
|
@@ -27,26 +14,44 @@ const stringKey = (key: string[]) => key.join(',');
|
|
|
27
14
|
/**
|
|
28
15
|
* Manages attention state for an application.
|
|
29
16
|
*/
|
|
30
|
-
// TODO(wittjosiah): Write unit tests.
|
|
31
17
|
export class AttentionManager {
|
|
32
|
-
// Each attention path is associated with an attention
|
|
18
|
+
// Each attention path is associated with an attention atom.
|
|
33
19
|
// The lookup is not a reactive object to ensure that attention for each path is subscribable independently.
|
|
34
|
-
private readonly _map = new ComplexMap<string[],
|
|
35
|
-
private readonly
|
|
36
|
-
|
|
37
|
-
constructor(
|
|
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[]>([]);
|
|
38
28
|
if (initial.length > 0) {
|
|
39
29
|
this.update(initial);
|
|
40
30
|
}
|
|
41
31
|
}
|
|
42
32
|
|
|
43
33
|
/**
|
|
44
|
-
*
|
|
45
|
-
|
|
46
|
-
|
|
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.
|
|
47
49
|
*/
|
|
48
|
-
|
|
49
|
-
|
|
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
|
+
});
|
|
50
55
|
}
|
|
51
56
|
|
|
52
57
|
/**
|
|
@@ -59,15 +64,31 @@ export class AttentionManager {
|
|
|
59
64
|
/**
|
|
60
65
|
* Get the attention state for a given path.
|
|
61
66
|
*/
|
|
62
|
-
get(key: string[]):
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
|
|
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;
|
|
66
87
|
}
|
|
67
88
|
|
|
68
|
-
const
|
|
69
|
-
this._map.set(key,
|
|
70
|
-
return
|
|
89
|
+
const newAtom = Atom.make<Attention>({ hasAttention: false, isAncestor: false, isRelated: false });
|
|
90
|
+
this._map.set(key, newAtom);
|
|
91
|
+
return newAtom;
|
|
71
92
|
}
|
|
72
93
|
|
|
73
94
|
/**
|
|
@@ -76,12 +97,12 @@ export class AttentionManager {
|
|
|
76
97
|
* @internal
|
|
77
98
|
*/
|
|
78
99
|
update(nextKey: string[]): void {
|
|
79
|
-
const currentKey =
|
|
100
|
+
const currentKey = this.getCurrent();
|
|
80
101
|
const currentRelatedTo = currentKey[0];
|
|
81
102
|
const nextRelatedTo = nextKey[0];
|
|
82
103
|
|
|
83
104
|
// Ensure related attention object is initialized.
|
|
84
|
-
this.
|
|
105
|
+
this._getAtom([nextRelatedTo]);
|
|
85
106
|
|
|
86
107
|
const currentRelatedKeys = this.keys().filter((key) => key[0] === currentRelatedTo);
|
|
87
108
|
const nextRelatedKeys = this.keys().filter(
|
|
@@ -97,7 +118,7 @@ export class AttentionManager {
|
|
|
97
118
|
(curr) => !nextAncestorKeys.find((next) => stringKey(curr) === stringKey(next)),
|
|
98
119
|
);
|
|
99
120
|
|
|
100
|
-
this.
|
|
121
|
+
this._registry.set(this._currentAtom, nextKey);
|
|
101
122
|
prevRelatedKeys.forEach((key) => this._set(key, {}));
|
|
102
123
|
prevAncestorKeys.forEach((key) => this._set(key, {}));
|
|
103
124
|
nextRelatedKeys.forEach((key) => this._set(key, { isRelated: true }));
|
|
@@ -106,15 +127,17 @@ export class AttentionManager {
|
|
|
106
127
|
}
|
|
107
128
|
|
|
108
129
|
private _set(key: string[], attention: Partial<Attention>): void {
|
|
109
|
-
const
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
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
|
+
});
|
|
113
136
|
}
|
|
114
137
|
}
|
|
115
138
|
|
|
116
139
|
/**
|
|
117
|
-
* Accumulates all attendable id
|
|
140
|
+
* Accumulates all attendable id's between the element provided and the root, inclusive.
|
|
118
141
|
*/
|
|
119
142
|
export const getAttendables = (selector: string, cursor: Element, acc: string[] = []): string[] => {
|
|
120
143
|
// Find the closest element with `data-attendable-id`, if any; start from cursor and move up the DOM tree.
|
|
@@ -2,17 +2,16 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import '@
|
|
6
|
-
|
|
5
|
+
import { type Meta, type StoryObj } from '@storybook/react-vite';
|
|
7
6
|
import React, { useCallback, useEffect, useState } from 'react';
|
|
8
7
|
|
|
9
8
|
import { Button } from '@dxos/react-ui';
|
|
10
|
-
import { withTheme } from '@dxos/
|
|
9
|
+
import { withTheme } from '@dxos/react-ui/testing';
|
|
11
10
|
import { range } from '@dxos/util';
|
|
12
11
|
|
|
13
12
|
import { AttentionGlyph, type AttentionGlyphProps } from './AttentionGlyph';
|
|
14
13
|
|
|
15
|
-
const
|
|
14
|
+
const DefaultStory = (props: AttentionGlyphProps) => {
|
|
16
15
|
return (
|
|
17
16
|
<ul className='flex gap-2 mbe-2'>
|
|
18
17
|
<li>
|
|
@@ -28,26 +27,30 @@ const Story = (props: AttentionGlyphProps) => {
|
|
|
28
27
|
);
|
|
29
28
|
};
|
|
30
29
|
|
|
31
|
-
|
|
30
|
+
const meta = {
|
|
32
31
|
title: 'ui/react-ui-attention/AttentionGlyph',
|
|
33
|
-
component: AttentionGlyph,
|
|
34
|
-
render:
|
|
32
|
+
component: AttentionGlyph as any,
|
|
33
|
+
render: DefaultStory,
|
|
35
34
|
decorators: [withTheme],
|
|
36
|
-
}
|
|
35
|
+
} satisfies Meta<typeof DefaultStory>;
|
|
36
|
+
|
|
37
|
+
export default meta;
|
|
38
|
+
|
|
39
|
+
type Story = StoryObj<typeof meta>;
|
|
37
40
|
|
|
38
|
-
export const Default = {
|
|
41
|
+
export const Default: Story = {
|
|
39
42
|
args: {},
|
|
40
43
|
};
|
|
41
44
|
|
|
42
|
-
export const Attention = {
|
|
45
|
+
export const Attention: Story = {
|
|
43
46
|
args: { attended: true },
|
|
44
47
|
};
|
|
45
48
|
|
|
46
|
-
export const Contains = {
|
|
49
|
+
export const Contains: Story = {
|
|
47
50
|
args: { containsAttended: true },
|
|
48
51
|
};
|
|
49
52
|
|
|
50
|
-
export const Syncing = {
|
|
53
|
+
export const Syncing: Story = {
|
|
51
54
|
render: () => {
|
|
52
55
|
const [spaces, setSpaces] = useState(
|
|
53
56
|
new Map<string, boolean>(range(8).map((i) => [`space-${i + 1}`, Math.random() > 0.5])),
|
|
@@ -73,7 +76,7 @@ export const Syncing = {
|
|
|
73
76
|
});
|
|
74
77
|
|
|
75
78
|
return (
|
|
76
|
-
<div className='flex flex-col p-2
|
|
79
|
+
<div className='flex flex-col p-2 is-[200px]'>
|
|
77
80
|
<Button onClick={handleChangeAttended}>Change attended</Button>
|
|
78
81
|
{Array.from(spaces.entries()).map(([space, sync]) => (
|
|
79
82
|
<div key={space} className='flex items-center'>
|
|
@@ -3,10 +3,10 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
import { type Primitive } from '@radix-ui/react-primitive';
|
|
6
|
-
import React, { type
|
|
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/
|
|
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';
|
|
@@ -2,15 +2,15 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import '@
|
|
6
|
-
|
|
5
|
+
import { type Meta, type StoryObj } from '@storybook/react-vite';
|
|
7
6
|
import React from 'react';
|
|
8
7
|
|
|
9
|
-
import { withTheme } from '@dxos/
|
|
8
|
+
import { withTheme } from '@dxos/react-ui/testing';
|
|
10
9
|
|
|
11
|
-
import { useAttentionAttributes } from './AttentionProvider';
|
|
12
10
|
import { withAttention } from '../testing';
|
|
13
11
|
|
|
12
|
+
import { useAttentionAttributes } from './AttentionProvider';
|
|
13
|
+
|
|
14
14
|
const Attendable = ({ id }: { id: string }) => {
|
|
15
15
|
const attentionAttrs = useAttentionAttributes(id);
|
|
16
16
|
|
|
@@ -32,10 +32,14 @@ const Story = () => {
|
|
|
32
32
|
);
|
|
33
33
|
};
|
|
34
34
|
|
|
35
|
-
|
|
35
|
+
const meta = {
|
|
36
36
|
title: 'ui/react-ui-attention/AttentionProvider',
|
|
37
37
|
render: Story,
|
|
38
38
|
decorators: [withTheme, withAttention],
|
|
39
|
-
};
|
|
39
|
+
} satisfies Meta;
|
|
40
|
+
|
|
41
|
+
export default meta;
|
|
42
|
+
|
|
43
|
+
type Story = StoryObj<typeof meta>;
|
|
40
44
|
|
|
41
|
-
export const Default = {};
|
|
45
|
+
export const Default: Story = {};
|
|
@@ -2,21 +2,31 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
// eslint-disable-next-line unused-imports/no-unused-imports
|
|
8
|
-
import { createContext, type Scope, type CreateScope } 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, {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
|
29
|
+
const ATTENDABLE_ATTRIBUTE = 'data-attendable-id';
|
|
20
30
|
const ATTENTION_SOURCE_ATTRIBUTE = 'data-is-attention-source';
|
|
21
31
|
|
|
22
32
|
type AttentionContextValue = {
|
|
@@ -25,35 +35,70 @@ type AttentionContextValue = {
|
|
|
25
35
|
};
|
|
26
36
|
|
|
27
37
|
const [AttentionContextProvider, useAttentionContext] = createContext<AttentionContextValue>(ATTENTION_NAME, {
|
|
28
|
-
attention:
|
|
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
|
-
|
|
37
|
-
|
|
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]);
|
|
39
54
|
|
|
40
|
-
|
|
41
|
-
|
|
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;
|
|
42
71
|
};
|
|
43
72
|
|
|
44
73
|
const useAttended = () => {
|
|
45
74
|
const { attention } = useAttentionContext(ATTENTION_NAME);
|
|
46
|
-
|
|
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
|
/**
|
|
50
|
-
* Computes HTML element attributes to apply so the attention system can detect changes
|
|
95
|
+
* Computes HTML element attributes to apply so the attention system can detect changes.
|
|
51
96
|
* @param attendableId
|
|
52
97
|
*/
|
|
53
98
|
const useAttentionAttributes = (attendableId?: string) => {
|
|
54
99
|
const { hasAttention } = useAttention(attendableId);
|
|
55
100
|
return useMemo(() => {
|
|
56
|
-
const attributes: Record<string, string | undefined> = { [
|
|
101
|
+
const attributes: Record<string, string | undefined> = { [ATTENDABLE_ATTRIBUTE]: attendableId };
|
|
57
102
|
if (hasAttention) {
|
|
58
103
|
attributes[ATTENTION_SOURCE_ATTRIBUTE] = 'true';
|
|
59
104
|
}
|
|
@@ -77,15 +122,20 @@ const RootAttentionProvider = ({
|
|
|
77
122
|
onChange: (nextAttended: string[]) => void;
|
|
78
123
|
}>
|
|
79
124
|
>) => {
|
|
80
|
-
const
|
|
125
|
+
const registry = useContext(RegistryContext);
|
|
126
|
+
const attention = useDefaultValue(propsAttention, () => new AttentionManager(registry));
|
|
127
|
+
|
|
81
128
|
const handleFocus = (event: FocusEvent) => {
|
|
129
|
+
// NOTE(thure): Use the following to debug focus movement across the app:
|
|
130
|
+
log('focus', { related: event.relatedTarget, target: event.target });
|
|
131
|
+
|
|
82
132
|
const selector = [
|
|
83
133
|
'[data-attendable-id]',
|
|
84
134
|
...Array.from(document.querySelectorAll('[aria-controls]')).map(
|
|
85
135
|
(el) => `[id="${el.getAttribute('aria-controls')}"]`,
|
|
86
136
|
),
|
|
87
137
|
].join(',');
|
|
88
|
-
const prev = attention.
|
|
138
|
+
const prev = attention.getCurrent();
|
|
89
139
|
const next = getAttendables(selector, event.target);
|
|
90
140
|
// TODO(wittjosiah): Not allowing empty state means that the attended item is not strictly guaranteed to be in the DOM.
|
|
91
141
|
// Currently this depends on the deck in order to ensure that when the attended item is removed something else is attended.
|
|
@@ -96,49 +146,9 @@ const RootAttentionProvider = ({
|
|
|
96
146
|
}
|
|
97
147
|
};
|
|
98
148
|
|
|
99
|
-
// NOTE(thure): Use the following to debug the macOS package issue #8540
|
|
100
|
-
|
|
101
|
-
// const [startEl, setStartEl] = useState<HTMLElement | null>(null);
|
|
102
|
-
// const [endEl, setEndEl] = useState<HTMLElement | null>(null);
|
|
103
|
-
//
|
|
104
|
-
// const handleEventDebug = useCallback((event: any) => {
|
|
105
|
-
// console.log(`[${event.type}]`, event.target, event.currentTarget, [event.clientX, event.clientY]);
|
|
106
|
-
// }, []);
|
|
107
|
-
//
|
|
108
|
-
// const handleStartDebug = useCallback(
|
|
109
|
-
// (event: any) => {
|
|
110
|
-
// setStartEl(event.target);
|
|
111
|
-
// handleEventDebug(event);
|
|
112
|
-
// },
|
|
113
|
-
// [handleEventDebug],
|
|
114
|
-
// );
|
|
115
|
-
//
|
|
116
|
-
// const handleEndDebug = useCallback(
|
|
117
|
-
// (event: any) => {
|
|
118
|
-
// setEndEl(event.target);
|
|
119
|
-
// handleEventDebug(event);
|
|
120
|
-
// },
|
|
121
|
-
// [handleEventDebug],
|
|
122
|
-
// );
|
|
123
|
-
//
|
|
124
|
-
// const handleClickDebug = useCallback(
|
|
125
|
-
// (event: any) => {
|
|
126
|
-
// console.log('[click compare]', startEl, endEl, startEl === endEl);
|
|
127
|
-
// handleEventDebug(event);
|
|
128
|
-
// },
|
|
129
|
-
// [startEl, endEl, handleEventDebug],
|
|
130
|
-
// );
|
|
131
|
-
|
|
132
149
|
return (
|
|
133
150
|
<AttentionContextProvider attention={attention} path={[]}>
|
|
134
|
-
<div
|
|
135
|
-
role='none'
|
|
136
|
-
className='contents'
|
|
137
|
-
onFocusCapture={handleFocus}
|
|
138
|
-
// onClick={handleClickDebug}
|
|
139
|
-
// onMouseDown={handleStartDebug}
|
|
140
|
-
// onMouseUp={handleEndDebug}
|
|
141
|
-
>
|
|
151
|
+
<div role='none' className='contents' onFocusCapture={handleFocus}>
|
|
142
152
|
{children}
|
|
143
153
|
</div>
|
|
144
154
|
</AttentionContextProvider>
|
|
@@ -193,6 +203,6 @@ export {
|
|
|
193
203
|
useAttentionAttributes,
|
|
194
204
|
useAttentionPath,
|
|
195
205
|
ATTENTION_NAME,
|
|
196
|
-
|
|
206
|
+
ATTENDABLE_ATTRIBUTE,
|
|
197
207
|
ATTENTION_SOURCE_ATTRIBUTE,
|
|
198
208
|
};
|