@dxos/react-ui-attention 0.6.13 → 0.6.14-main.69511f5
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-GABVXGFU.mjs +259 -0
- package/dist/lib/browser/chunk-GABVXGFU.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +26 -111
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +15 -0
- package/dist/lib/browser/testing/index.mjs.map +7 -0
- package/dist/types/src/attention.d.ts +37 -0
- package/dist/types/src/attention.d.ts.map +1 -0
- package/dist/types/src/attention.test.d.ts +2 -0
- package/dist/types/src/attention.test.d.ts.map +1 -0
- package/dist/types/src/components/AttentionGlyph.stories.d.ts +3 -2
- package/dist/types/src/components/AttentionGlyph.stories.d.ts.map +1 -1
- package/dist/types/src/components/AttentionProvider.d.ts +16 -11
- package/dist/types/src/components/AttentionProvider.d.ts.map +1 -1
- package/dist/types/src/components/AttentionProvider.stories.d.ts +10 -0
- package/dist/types/src/components/AttentionProvider.stories.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +1 -0
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/testing/decorators/index.d.ts +2 -0
- package/dist/types/src/testing/decorators/index.d.ts.map +1 -0
- package/dist/types/src/testing/decorators/withAttention.d.ts +3 -0
- package/dist/types/src/testing/decorators/withAttention.d.ts.map +1 -0
- package/dist/types/src/testing/index.d.ts +2 -0
- package/dist/types/src/testing/index.d.ts.map +1 -0
- package/package.json +26 -13
- package/src/attention.test.ts +58 -0
- package/src/attention.ts +132 -0
- package/src/components/AttentionGlyph.stories.tsx +7 -4
- package/src/components/AttentionGlyph.tsx +1 -1
- package/src/components/AttentionProvider.stories.tsx +41 -0
- package/src/components/AttentionProvider.tsx +77 -53
- package/src/index.ts +1 -0
- package/src/testing/decorators/index.ts +5 -0
- package/src/testing/decorators/withAttention.ts +12 -0
- package/src/testing/index.ts +5 -0
|
@@ -1,22 +1,27 @@
|
|
|
1
1
|
import React, { type PropsWithChildren } from 'react';
|
|
2
|
+
import { type Attention, AttentionManager } from '../attention';
|
|
2
3
|
declare const ATTENTION_NAME = "Attention";
|
|
4
|
+
declare const ATTENABLE_ATTRIBUTE = "data-attendable-id";
|
|
5
|
+
declare const ATTENTION_SOURCE_ATTRIBUTE = "data-is-attention-source";
|
|
3
6
|
type AttentionContextValue = {
|
|
4
|
-
|
|
7
|
+
attention: AttentionManager;
|
|
8
|
+
path: string[];
|
|
5
9
|
};
|
|
6
10
|
declare const useAttentionContext: (consumerName: string) => AttentionContextValue;
|
|
7
|
-
declare const
|
|
11
|
+
declare const useAttention: (attendableId?: string) => Attention;
|
|
12
|
+
declare const useAttended: () => string[];
|
|
8
13
|
/**
|
|
9
14
|
* Computes HTML element attributes to apply so the attention system can detect changes
|
|
10
15
|
* @param attendableId
|
|
11
16
|
*/
|
|
12
|
-
declare const
|
|
13
|
-
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
onChangeAttend: (nextAttended: Set<string>) => void;
|
|
18
|
-
defaultAttended: Set<string>;
|
|
17
|
+
declare const useAttendableAttributes: (attendableId?: string) => Record<string, string | undefined>;
|
|
18
|
+
declare const useAttentionPath: () => string[];
|
|
19
|
+
declare const RootAttentionProvider: ({ children, attention: propsAttention, onChange, }: PropsWithChildren<Partial<{
|
|
20
|
+
attention: AttentionManager;
|
|
21
|
+
onChange: (nextAttended: string[]) => void;
|
|
19
22
|
}>>) => React.JSX.Element;
|
|
20
|
-
|
|
21
|
-
|
|
23
|
+
declare const AttentionProvider: ({ id, children }: PropsWithChildren<{
|
|
24
|
+
id: string;
|
|
25
|
+
}>) => React.JSX.Element;
|
|
26
|
+
export { RootAttentionProvider, AttentionProvider, useAttentionContext, useAttention, useAttended, useAttendableAttributes, useAttentionPath, ATTENTION_NAME, ATTENABLE_ATTRIBUTE, ATTENTION_SOURCE_ATTRIBUTE, };
|
|
22
27
|
//# sourceMappingURL=AttentionProvider.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AttentionProvider.d.ts","sourceRoot":"","sources":["../../../../src/components/AttentionProvider.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"AttentionProvider.d.ts","sourceRoot":"","sources":["../../../../src/components/AttentionProvider.tsx"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,EAA4B,KAAK,iBAAiB,EAAE,MAAM,OAAO,CAAC;AAIhF,OAAO,EAAE,KAAK,SAAS,EAAE,gBAAgB,EAAkB,MAAM,cAAc,CAAC;AAEhF,QAAA,MAAM,cAAc,cAAc,CAAC;AACnC,QAAA,MAAM,mBAAmB,uBAAuB,CAAC;AACjD,QAAA,MAAM,0BAA0B,6BAA6B,CAAC;AAE9D,KAAK,qBAAqB,GAAG;IAC3B,SAAS,EAAE,gBAAgB,CAAC;IAC5B,IAAI,EAAE,MAAM,EAAE,CAAC;CAChB,CAAC;AAEF,QAAA,MAAiC,mBAAmB,iDAGlD,CAAC;AAIH,QAAA,MAAM,YAAY,kBAAmB,MAAM,KAAG,SAQ7C,CAAC;AAEF,QAAA,MAAM,WAAW,gBAGhB,CAAC;AAEF;;;GAGG;AACH,QAAA,MAAM,uBAAuB,kBAAmB,MAAM,uCAUrD,CAAC;AAEF,QAAA,MAAM,gBAAgB,gBAGrB,CAAC;AAEF,QAAA,MAAM,qBAAqB,uDAIxB,iBAAiB,CAClB,OAAO,CAAC;IACN,SAAS,EAAE,gBAAgB,CAAC;IAC5B,QAAQ,EAAE,CAAC,YAAY,EAAE,MAAM,EAAE,KAAK,IAAI,CAAC;CAC5C,CAAC,CACH,sBA0BA,CAAC;AAEF,QAAA,MAAM,iBAAiB,qBAAsB,iBAAiB,CAAC;IAAE,EAAE,EAAE,MAAM,CAAA;CAAE,CAAC,sBAS7E,CAAC;AAEF,OAAO,EACL,qBAAqB,EACrB,iBAAiB,EACjB,mBAAmB,EACnB,YAAY,EACZ,WAAW,EACX,uBAAuB,EACvB,gBAAgB,EAChB,cAAc,EACd,mBAAmB,EACnB,0BAA0B,GAC3B,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import '@dxos-theme';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
declare const _default: {
|
|
4
|
+
title: string;
|
|
5
|
+
render: () => React.JSX.Element;
|
|
6
|
+
decorators: import("@storybook/react/*").Decorator[];
|
|
7
|
+
};
|
|
8
|
+
export default _default;
|
|
9
|
+
export declare const Default: {};
|
|
10
|
+
//# sourceMappingURL=AttentionProvider.stories.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AttentionProvider.stories.d.ts","sourceRoot":"","sources":["../../../../src/components/AttentionProvider.stories.tsx"],"names":[],"mappings":"AAIA,OAAO,aAAa,CAAC;AAErB,OAAO,KAAK,MAAM,OAAO,CAAC;;;;;;AA4B1B,wBAIE;AAEF,eAAO,MAAM,OAAO,IAAK,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAIA,cAAc,cAAc,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAIA,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/testing/decorators/index.ts"],"names":[],"mappings":"AAIA,cAAc,iBAAiB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"withAttention.d.ts","sourceRoot":"","sources":["../../../../../src/testing/decorators/withAttention.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAKlD,eAAO,MAAM,aAAa,EAAE,SAE3B,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/testing/index.ts"],"names":[],"mappings":"AAIA,cAAc,cAAc,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,26 +1,36 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/react-ui-attention",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.14-main.69511f5",
|
|
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
7
|
"license": "MIT",
|
|
8
8
|
"author": "DXOS.org",
|
|
9
|
+
"sideEffects": true,
|
|
9
10
|
"exports": {
|
|
10
11
|
".": {
|
|
11
|
-
"
|
|
12
|
-
"
|
|
12
|
+
"types": "./dist/types/src/index.d.ts",
|
|
13
|
+
"browser": "./dist/lib/browser/index.mjs"
|
|
14
|
+
},
|
|
15
|
+
"./testing": {
|
|
16
|
+
"types": "./dist/types/src/testing/index.d.ts",
|
|
17
|
+
"browser": "./dist/lib/browser/testing/index.mjs"
|
|
13
18
|
}
|
|
14
19
|
},
|
|
15
20
|
"types": "dist/types/src/index.d.ts",
|
|
16
21
|
"typesVersions": {
|
|
17
|
-
"*": {
|
|
22
|
+
"*": {
|
|
23
|
+
"testing": [
|
|
24
|
+
"dist/types/src/testing/index.d.ts"
|
|
25
|
+
]
|
|
26
|
+
}
|
|
18
27
|
},
|
|
19
28
|
"files": [
|
|
20
29
|
"dist",
|
|
21
30
|
"src"
|
|
22
31
|
],
|
|
23
32
|
"dependencies": {
|
|
33
|
+
"@preact/signals-core": "^1.6.0",
|
|
24
34
|
"@radix-ui/primitive": "^1.0.0",
|
|
25
35
|
"@radix-ui/react-compose-refs": "^1.0.0",
|
|
26
36
|
"@radix-ui/react-context": "^1.0.0",
|
|
@@ -28,9 +38,8 @@
|
|
|
28
38
|
"@radix-ui/react-primitive": "^1.0.2",
|
|
29
39
|
"@radix-ui/react-slot": "^1.0.1",
|
|
30
40
|
"@radix-ui/react-use-controllable-state": "^1.0.0",
|
|
31
|
-
"@dxos/
|
|
32
|
-
"@dxos/util": "0.6.
|
|
33
|
-
"@dxos/react-ui-theme": "0.6.13"
|
|
41
|
+
"@dxos/echo-schema": "0.6.14-main.69511f5",
|
|
42
|
+
"@dxos/util": "0.6.14-main.69511f5"
|
|
34
43
|
},
|
|
35
44
|
"devDependencies": {
|
|
36
45
|
"@phosphor-icons/react": "^2.1.5",
|
|
@@ -38,15 +47,19 @@
|
|
|
38
47
|
"@types/react-dom": "~18.2.0",
|
|
39
48
|
"react": "~18.2.0",
|
|
40
49
|
"react-dom": "~18.2.0",
|
|
41
|
-
"vite": "
|
|
42
|
-
"@dxos/
|
|
43
|
-
"@dxos/
|
|
44
|
-
"@dxos/
|
|
50
|
+
"vite": "5.4.7",
|
|
51
|
+
"@dxos/app-graph": "0.6.14-main.69511f5",
|
|
52
|
+
"@dxos/random": "0.6.14-main.69511f5",
|
|
53
|
+
"@dxos/react-ui": "0.6.14-main.69511f5",
|
|
54
|
+
"@dxos/react-ui-theme": "0.6.14-main.69511f5",
|
|
55
|
+
"@dxos/storybook-utils": "0.6.14-main.69511f5"
|
|
45
56
|
},
|
|
46
57
|
"peerDependencies": {
|
|
47
58
|
"@phosphor-icons/react": "^2.1.5",
|
|
48
|
-
"react": "
|
|
49
|
-
"react-dom": "
|
|
59
|
+
"react": "~18.2.0",
|
|
60
|
+
"react-dom": "~18.2.0",
|
|
61
|
+
"@dxos/react-ui-theme": "0.6.14-main.69511f5",
|
|
62
|
+
"@dxos/react-ui": "0.6.14-main.69511f5"
|
|
50
63
|
},
|
|
51
64
|
"publishConfig": {
|
|
52
65
|
"access": "public"
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { describe, test, expect } from 'vitest';
|
|
6
|
+
|
|
7
|
+
import { AttentionManager } from './attention';
|
|
8
|
+
|
|
9
|
+
describe('AttentionManager', () => {
|
|
10
|
+
test('takes an initial attendable', () => {
|
|
11
|
+
const attention = new AttentionManager(['a', 'b', 'c']);
|
|
12
|
+
|
|
13
|
+
expect(attention.current).to.deep.equal(['a', 'b', 'c']);
|
|
14
|
+
|
|
15
|
+
expect(attention.keys()).to.deep.equal([['a'], ['b', 'c'], ['c'], ['a', 'b', 'c']]);
|
|
16
|
+
|
|
17
|
+
expect(attention.get(['a'])).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: true });
|
|
18
|
+
expect(attention.get(['b', 'c'])).to.deep.equal({ hasAttention: false, isAncestor: true, isRelated: false });
|
|
19
|
+
expect(attention.get(['c'])).to.deep.equal({ hasAttention: false, isAncestor: true, isRelated: false });
|
|
20
|
+
expect(attention.get(['a', 'b', 'c'])).to.deep.equal({ hasAttention: true, isAncestor: false, isRelated: false });
|
|
21
|
+
|
|
22
|
+
expect(attention.get(['b'])).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: false });
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test('current returns last updated key', () => {
|
|
26
|
+
const attention = new AttentionManager();
|
|
27
|
+
|
|
28
|
+
expect(attention.current).to.deep.equal([]);
|
|
29
|
+
|
|
30
|
+
attention.update(['a', 'b', 'c']);
|
|
31
|
+
|
|
32
|
+
expect(attention.current).to.deep.equal(['a', 'b', 'c']);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test('keys returns all stored attendables', () => {
|
|
36
|
+
const attention = new AttentionManager();
|
|
37
|
+
|
|
38
|
+
expect(attention.keys()).to.deep.equal([]);
|
|
39
|
+
|
|
40
|
+
attention.update(['a']);
|
|
41
|
+
|
|
42
|
+
expect(attention.keys()).to.deep.equal([['a']]);
|
|
43
|
+
|
|
44
|
+
attention.update(['a', 'b']);
|
|
45
|
+
|
|
46
|
+
expect(attention.keys()).to.deep.equal([['a'], ['b'], ['a', 'b']]);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test('get returns attention object for a given key', () => {
|
|
50
|
+
const attention = new AttentionManager();
|
|
51
|
+
|
|
52
|
+
expect(attention.get(['a'])).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: false });
|
|
53
|
+
|
|
54
|
+
attention.update(['a']);
|
|
55
|
+
|
|
56
|
+
expect(attention.get(['a'])).to.deep.equal({ hasAttention: true, isAncestor: false, isRelated: false });
|
|
57
|
+
});
|
|
58
|
+
});
|
package/src/attention.ts
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { untracked } from '@preact/signals-core';
|
|
6
|
+
|
|
7
|
+
import { create, type ReactiveObject, S } from '@dxos/echo-schema';
|
|
8
|
+
import { ComplexMap } from '@dxos/util';
|
|
9
|
+
|
|
10
|
+
export const AttentionSchema = S.mutable(
|
|
11
|
+
S.Struct({
|
|
12
|
+
hasAttention: S.Boolean,
|
|
13
|
+
isAncestor: S.Boolean,
|
|
14
|
+
isRelated: S.Boolean,
|
|
15
|
+
}),
|
|
16
|
+
);
|
|
17
|
+
|
|
18
|
+
export type Attention = S.Schema.Type<typeof AttentionSchema>;
|
|
19
|
+
|
|
20
|
+
// TODO(wittjosiah): Use mosaic path utility?
|
|
21
|
+
const stringKey = (key: string[]) => key.join(',');
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Manages attention state for an application.
|
|
25
|
+
*/
|
|
26
|
+
// TODO(wittjosiah): Write unit tests.
|
|
27
|
+
export class AttentionManager {
|
|
28
|
+
// Each attention path is associated with an attention object.
|
|
29
|
+
// The lookup is not a reactive object to ensure that attention for each path is subscribable independently.
|
|
30
|
+
private readonly _map = new ComplexMap<string[], ReactiveObject<Attention>>(stringKey);
|
|
31
|
+
private readonly _state = create<{ current: string[] }>({ current: [] });
|
|
32
|
+
|
|
33
|
+
constructor(initial: string[] = []) {
|
|
34
|
+
if (initial.length > 0) {
|
|
35
|
+
this.update(initial);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Currently attended element.
|
|
41
|
+
*/
|
|
42
|
+
get current() {
|
|
43
|
+
return this._state.current;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* All attention paths.
|
|
48
|
+
*/
|
|
49
|
+
keys() {
|
|
50
|
+
return Array.from(this._map.keys());
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Get the attention state for a given path.
|
|
55
|
+
*/
|
|
56
|
+
get(key: string[]): ReactiveObject<Attention> {
|
|
57
|
+
const object = this._map.get(key);
|
|
58
|
+
if (object) {
|
|
59
|
+
return object;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const newObject = create(AttentionSchema, { hasAttention: false, isAncestor: false, isRelated: false });
|
|
63
|
+
this._map.set(key, newObject);
|
|
64
|
+
return newObject;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Update the currently attended element.
|
|
69
|
+
*/
|
|
70
|
+
update(nextKey: string[]) {
|
|
71
|
+
const currentKey = untracked(() => this.current);
|
|
72
|
+
const currentRelatedTo = currentKey[0];
|
|
73
|
+
const nextRelatedTo = nextKey[0];
|
|
74
|
+
|
|
75
|
+
// Ensure related attention object is initialized.
|
|
76
|
+
this.get([nextRelatedTo]);
|
|
77
|
+
|
|
78
|
+
const currentRelatedKeys = this.keys().filter((key) => key[0] === currentRelatedTo);
|
|
79
|
+
const nextRelatedKeys = this.keys().filter(
|
|
80
|
+
(key) => key[0] === nextRelatedTo && stringKey(key) !== stringKey(nextKey),
|
|
81
|
+
);
|
|
82
|
+
const prevRelatedKeys = currentRelatedKeys.filter(
|
|
83
|
+
(curr) => !nextRelatedKeys.find((next) => stringKey(curr) === stringKey(next)),
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
const currentAncestorKeys = currentKey.slice(1).map((_, i) => currentKey.slice(i + 1, currentKey.length));
|
|
87
|
+
const nextAncestorKeys = nextKey.slice(1).map((_, i) => nextKey.slice(i + 1, nextKey.length));
|
|
88
|
+
const prevAncestorKeys = currentAncestorKeys.filter(
|
|
89
|
+
(curr) => !nextAncestorKeys.find((next) => stringKey(curr) === stringKey(next)),
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
this._state.current = nextKey;
|
|
93
|
+
prevRelatedKeys.forEach((key) => this._set(key, {}));
|
|
94
|
+
prevAncestorKeys.forEach((key) => this._set(key, {}));
|
|
95
|
+
nextRelatedKeys.forEach((key) => this._set(key, { isRelated: true }));
|
|
96
|
+
nextAncestorKeys.forEach((key) => this._set(key, { isAncestor: true }));
|
|
97
|
+
this._set(nextKey, { hasAttention: true });
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
private _set(key: string[], attention: Partial<Attention>) {
|
|
101
|
+
const object = this.get(key);
|
|
102
|
+
object.hasAttention = attention.hasAttention ?? false;
|
|
103
|
+
object.isAncestor = attention.isAncestor ?? false;
|
|
104
|
+
object.isRelated = attention.isRelated ?? false;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Accumulates all attendable id’s between the element provided and the root, inclusive.
|
|
110
|
+
*/
|
|
111
|
+
export const getAttendables = (selector: string, cursor: Element, acc: string[] = []): string[] => {
|
|
112
|
+
// Find the closest element with `data-attendable-id`, if any; start from cursor and move up the DOM tree.
|
|
113
|
+
const closestAttendable = cursor.closest(selector);
|
|
114
|
+
if (closestAttendable) {
|
|
115
|
+
const attendableId = closestAttendable.getAttribute('data-attendable-id');
|
|
116
|
+
if (!attendableId) {
|
|
117
|
+
// this has an id of an aria-controls elsewhere on the page, move cursor to that trigger
|
|
118
|
+
const trigger = document.querySelector(`[aria-controls="${closestAttendable.getAttribute('id')}"]`);
|
|
119
|
+
if (!trigger) {
|
|
120
|
+
return acc;
|
|
121
|
+
} else {
|
|
122
|
+
return getAttendables(selector, trigger, acc);
|
|
123
|
+
}
|
|
124
|
+
} else {
|
|
125
|
+
acc.push(attendableId);
|
|
126
|
+
// (attempt tail recursion)
|
|
127
|
+
return !closestAttendable.parentElement ? acc : getAttendables(selector, closestAttendable.parentElement, acc);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return [...new Set(acc)];
|
|
132
|
+
};
|
|
@@ -10,12 +10,12 @@ import { withTheme } from '@dxos/storybook-utils';
|
|
|
10
10
|
|
|
11
11
|
import { AttentionGlyph } from './AttentionGlyph';
|
|
12
12
|
|
|
13
|
-
type
|
|
13
|
+
type StoryProps = {
|
|
14
14
|
current?: boolean;
|
|
15
15
|
attention?: boolean;
|
|
16
16
|
};
|
|
17
17
|
|
|
18
|
-
const
|
|
18
|
+
const Story = ({ current, attention }: StoryProps) => {
|
|
19
19
|
const itemAttrs = {
|
|
20
20
|
...(current && { 'aria-current': 'page' as const }),
|
|
21
21
|
...(attention && { 'data-attention': 'true' }),
|
|
@@ -36,17 +36,20 @@ const StorybookAttentionGlyph = ({ current, attention }: StorybookAttentionGlyph
|
|
|
36
36
|
};
|
|
37
37
|
|
|
38
38
|
export default {
|
|
39
|
-
title: 'react-ui-
|
|
40
|
-
component:
|
|
39
|
+
title: 'ui/react-ui-attention/AttentionGlyph',
|
|
40
|
+
component: AttentionGlyph,
|
|
41
|
+
render: Story,
|
|
41
42
|
decorators: [withTheme],
|
|
42
43
|
};
|
|
43
44
|
|
|
44
45
|
export const Default = {
|
|
45
46
|
args: {},
|
|
46
47
|
};
|
|
48
|
+
|
|
47
49
|
export const Current = {
|
|
48
50
|
args: { current: true },
|
|
49
51
|
};
|
|
52
|
+
|
|
50
53
|
export const Attention = {
|
|
51
54
|
args: { current: true, attention: true },
|
|
52
55
|
};
|
|
@@ -9,7 +9,7 @@ import { type ThemedClassName } from '@dxos/react-ui';
|
|
|
9
9
|
import { mx } from '@dxos/react-ui-theme';
|
|
10
10
|
|
|
11
11
|
const attentionGlyphStyles =
|
|
12
|
-
'inline-block rounded-sm is-3 bs-3 bg-transparent text-accentText transition-colors [[data-attention=true]_&]:bg-accentSurface [[data-attention=true]_&]:
|
|
12
|
+
'inline-block rounded-sm is-3 bs-3 bg-transparent text-accentText transition-colors [[data-attention=true]_&]:bg-accentSurface [[data-attention=true]_&]:text-inverse [[aria-current][data-attention=true]_&]:bg-accentSurface [[aria-current][data-attention=true]_&]:text-inverse';
|
|
13
13
|
|
|
14
14
|
const presenceIconStyles =
|
|
15
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';
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import '@dxos-theme';
|
|
6
|
+
|
|
7
|
+
import React from 'react';
|
|
8
|
+
|
|
9
|
+
import { withTheme } from '@dxos/storybook-utils';
|
|
10
|
+
|
|
11
|
+
import { useAttendableAttributes } from './AttentionProvider';
|
|
12
|
+
import { withAttention } from '../testing';
|
|
13
|
+
|
|
14
|
+
const Attendable = ({ id }: { id: string }) => {
|
|
15
|
+
const attendableAttrs = useAttendableAttributes(id);
|
|
16
|
+
|
|
17
|
+
return (
|
|
18
|
+
<div {...attendableAttrs}>
|
|
19
|
+
<textarea className='attention-surface resize-none' placeholder={id} />
|
|
20
|
+
</div>
|
|
21
|
+
);
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const Story = () => {
|
|
25
|
+
return (
|
|
26
|
+
<div className='flex justify-between'>
|
|
27
|
+
<Attendable id='1' />
|
|
28
|
+
<Attendable id='2' />
|
|
29
|
+
<Attendable id='3' />
|
|
30
|
+
<Attendable id='4' />
|
|
31
|
+
</div>
|
|
32
|
+
);
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export default {
|
|
36
|
+
title: 'ui/react-ui-attention/AttentionProvider',
|
|
37
|
+
render: Story,
|
|
38
|
+
decorators: [withTheme, withAttention],
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export const Default = {};
|
|
@@ -6,76 +6,75 @@
|
|
|
6
6
|
|
|
7
7
|
// eslint-disable-next-line unused-imports/no-unused-imports
|
|
8
8
|
import { createContext, type Scope, type CreateScope } from '@radix-ui/react-context';
|
|
9
|
-
import {
|
|
10
|
-
|
|
11
|
-
import
|
|
9
|
+
import React, { useMemo, type FocusEvent, type PropsWithChildren } from 'react';
|
|
10
|
+
|
|
11
|
+
import { useDefaultValue } from '@dxos/react-ui';
|
|
12
|
+
|
|
13
|
+
import { type Attention, AttentionManager, getAttendables } from '../attention';
|
|
12
14
|
|
|
13
15
|
const ATTENTION_NAME = 'Attention';
|
|
16
|
+
const ATTENABLE_ATTRIBUTE = 'data-attendable-id';
|
|
17
|
+
const ATTENTION_SOURCE_ATTRIBUTE = 'data-is-attention-source';
|
|
14
18
|
|
|
15
19
|
type AttentionContextValue = {
|
|
16
|
-
|
|
20
|
+
attention: AttentionManager;
|
|
21
|
+
path: string[];
|
|
17
22
|
};
|
|
18
23
|
|
|
19
24
|
const [AttentionContextProvider, useAttentionContext] = createContext<AttentionContextValue>(ATTENTION_NAME, {
|
|
20
|
-
|
|
25
|
+
attention: new AttentionManager(),
|
|
26
|
+
path: [],
|
|
21
27
|
});
|
|
22
28
|
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
|
|
29
|
+
const UNKNOWN_ATTENDABLE = { hasAttention: false, isAncestor: false, isRelated: false };
|
|
30
|
+
|
|
31
|
+
const useAttention = (attendableId?: string): Attention => {
|
|
32
|
+
const { attention, path } = useAttentionContext(ATTENTION_NAME);
|
|
33
|
+
if (!attendableId) {
|
|
34
|
+
return UNKNOWN_ATTENDABLE;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const current = [attendableId, ...path];
|
|
38
|
+
return attention.get(current);
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const useAttended = () => {
|
|
42
|
+
const { attention } = useAttentionContext(ATTENTION_NAME);
|
|
43
|
+
return attention.current;
|
|
26
44
|
};
|
|
27
45
|
|
|
28
46
|
/**
|
|
29
47
|
* Computes HTML element attributes to apply so the attention system can detect changes
|
|
30
48
|
* @param attendableId
|
|
31
49
|
*/
|
|
32
|
-
const
|
|
33
|
-
|
|
50
|
+
const useAttendableAttributes = (attendableId?: string) => {
|
|
51
|
+
const { hasAttention } = useAttention(attendableId);
|
|
52
|
+
return useMemo(() => {
|
|
53
|
+
const attributes: Record<string, string | undefined> = { [ATTENABLE_ATTRIBUTE]: attendableId };
|
|
54
|
+
if (hasAttention) {
|
|
55
|
+
attributes[ATTENTION_SOURCE_ATTRIBUTE] = 'true';
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return attributes;
|
|
59
|
+
}, [attendableId, hasAttention]);
|
|
34
60
|
};
|
|
35
61
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
const getAttendables = (selector: string, cursor: Element, acc: string[] = []): string[] => {
|
|
40
|
-
// Find the closest element with `data-attendable-id`, if any; start from cursor and move up the DOM tree.
|
|
41
|
-
const closestAttendable = cursor.closest(selector);
|
|
42
|
-
if (!closestAttendable) {
|
|
43
|
-
return acc;
|
|
44
|
-
} else {
|
|
45
|
-
const attendableId = closestAttendable.getAttribute('data-attendable-id');
|
|
46
|
-
if (!attendableId) {
|
|
47
|
-
// this has an id of an aria-controls elsewhere on the page, move cursor to that trigger
|
|
48
|
-
const trigger = document.querySelector(`[aria-controls="${closestAttendable.getAttribute('id')}"]`);
|
|
49
|
-
if (!trigger) {
|
|
50
|
-
return acc;
|
|
51
|
-
} else {
|
|
52
|
-
return getAttendables(selector, trigger, acc);
|
|
53
|
-
}
|
|
54
|
-
} else {
|
|
55
|
-
acc.push(attendableId);
|
|
56
|
-
// (attempt tail recursion)
|
|
57
|
-
return !closestAttendable.parentElement ? acc : getAttendables(selector, closestAttendable.parentElement, acc);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
62
|
+
const useAttentionPath = () => {
|
|
63
|
+
const { path } = useAttentionContext(ATTENTION_NAME);
|
|
64
|
+
return path;
|
|
60
65
|
};
|
|
61
66
|
|
|
62
|
-
const
|
|
67
|
+
const RootAttentionProvider = ({
|
|
63
68
|
children,
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
onChangeAttend,
|
|
69
|
+
attention: propsAttention,
|
|
70
|
+
onChange,
|
|
67
71
|
}: PropsWithChildren<
|
|
68
72
|
Partial<{
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
defaultAttended: Set<string>;
|
|
73
|
+
attention: AttentionManager;
|
|
74
|
+
onChange: (nextAttended: string[]) => void;
|
|
72
75
|
}>
|
|
73
76
|
>) => {
|
|
74
|
-
const
|
|
75
|
-
prop: propsAttended,
|
|
76
|
-
defaultProp: defaultAttended,
|
|
77
|
-
onChange: onChangeAttend,
|
|
78
|
-
});
|
|
77
|
+
const attention = useDefaultValue(propsAttention, () => new AttentionManager());
|
|
79
78
|
const handleFocus = (event: FocusEvent) => {
|
|
80
79
|
const selector = [
|
|
81
80
|
'[data-attendable-id]',
|
|
@@ -83,13 +82,18 @@ const AttentionProvider = ({
|
|
|
83
82
|
(el) => `[id="${el.getAttribute('aria-controls')}"]`,
|
|
84
83
|
),
|
|
85
84
|
].join(',');
|
|
86
|
-
const
|
|
87
|
-
const
|
|
88
|
-
//
|
|
89
|
-
|
|
85
|
+
const prev = attention.current;
|
|
86
|
+
const next = getAttendables(selector, event.target);
|
|
87
|
+
// TODO(wittjosiah): Not allowing empty state means that the attended item is not strictly guaranteed to be in the DOM.
|
|
88
|
+
// Currently this depends on the deck in order to ensure that when the attended item is removed something else is attended.
|
|
89
|
+
// Only update state if the result is different and not empty.
|
|
90
|
+
if (next.length > 0 && (prev.length !== next.length || !!prev.find((id, index) => next[index] !== id))) {
|
|
91
|
+
attention.update(next);
|
|
92
|
+
onChange?.(next);
|
|
93
|
+
}
|
|
90
94
|
};
|
|
91
95
|
return (
|
|
92
|
-
<AttentionContextProvider
|
|
96
|
+
<AttentionContextProvider attention={attention} path={[]}>
|
|
93
97
|
<div role='none' className='contents' onFocusCapture={handleFocus}>
|
|
94
98
|
{children}
|
|
95
99
|
</div>
|
|
@@ -97,6 +101,26 @@ const AttentionProvider = ({
|
|
|
97
101
|
);
|
|
98
102
|
};
|
|
99
103
|
|
|
100
|
-
|
|
104
|
+
const AttentionProvider = ({ id, children }: PropsWithChildren<{ id: string }>) => {
|
|
105
|
+
const { attention, path } = useAttentionContext(ATTENTION_NAME);
|
|
106
|
+
const nextPath = useMemo(() => [id, ...path], [id, path]);
|
|
107
|
+
|
|
108
|
+
return (
|
|
109
|
+
<AttentionContextProvider attention={attention} path={nextPath}>
|
|
110
|
+
{children}
|
|
111
|
+
</AttentionContextProvider>
|
|
112
|
+
);
|
|
113
|
+
};
|
|
101
114
|
|
|
102
|
-
export
|
|
115
|
+
export {
|
|
116
|
+
RootAttentionProvider,
|
|
117
|
+
AttentionProvider,
|
|
118
|
+
useAttentionContext,
|
|
119
|
+
useAttention,
|
|
120
|
+
useAttended,
|
|
121
|
+
useAttendableAttributes,
|
|
122
|
+
useAttentionPath,
|
|
123
|
+
ATTENTION_NAME,
|
|
124
|
+
ATTENABLE_ATTRIBUTE,
|
|
125
|
+
ATTENTION_SOURCE_ATTRIBUTE,
|
|
126
|
+
};
|
package/src/index.ts
CHANGED