@dxos/react-ui-attention 0.6.13 → 0.6.14-main.2b6a0f3
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/index.mjs +177 -43
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/types/src/attention.d.ts +37 -0
- package/dist/types/src/attention.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 +15 -10
- package/src/attention.ts +125 -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,7 +1,108 @@
|
|
|
1
|
+
// packages/ui/react-ui-attention/src/attention.ts
|
|
2
|
+
import { untracked } from "@preact/signals-core";
|
|
3
|
+
import { create, S } from "@dxos/echo-schema";
|
|
4
|
+
import { ComplexMap } from "@dxos/util";
|
|
5
|
+
var AttentionSchema = S.mutable(S.Struct({
|
|
6
|
+
hasAttention: S.Boolean,
|
|
7
|
+
isAncestor: S.Boolean,
|
|
8
|
+
isRelated: S.Boolean
|
|
9
|
+
}));
|
|
10
|
+
var stringKey = (key) => key.join(",");
|
|
11
|
+
var AttentionManager = class {
|
|
12
|
+
constructor(initial = []) {
|
|
13
|
+
// Each attention path is associated with an attention object.
|
|
14
|
+
// The lookup is not a reactive object to ensure that attention for each path is subscribable independently.
|
|
15
|
+
this._map = new ComplexMap(stringKey);
|
|
16
|
+
this._state = create({
|
|
17
|
+
current: initial
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Currently attended element.
|
|
22
|
+
*/
|
|
23
|
+
get current() {
|
|
24
|
+
return this._state.current;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* All attention paths.
|
|
28
|
+
*/
|
|
29
|
+
keys() {
|
|
30
|
+
return this._map.keys();
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Get the attention state for a given path.
|
|
34
|
+
*/
|
|
35
|
+
get(key) {
|
|
36
|
+
const object = this._map.get(key);
|
|
37
|
+
if (object) {
|
|
38
|
+
return object;
|
|
39
|
+
}
|
|
40
|
+
const newObject = create(AttentionSchema, {
|
|
41
|
+
hasAttention: false,
|
|
42
|
+
isAncestor: false,
|
|
43
|
+
isRelated: false
|
|
44
|
+
});
|
|
45
|
+
this._map.set(key, newObject);
|
|
46
|
+
return newObject;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Update the currently attended element.
|
|
50
|
+
*/
|
|
51
|
+
update(nextKey) {
|
|
52
|
+
const currentKey = untracked(() => this.current);
|
|
53
|
+
const currentRelatedTo = currentKey[0];
|
|
54
|
+
const nextRelatedTo = nextKey[0];
|
|
55
|
+
const currentRelatedKeys = this.keys().filter((key) => key[0] === currentRelatedTo);
|
|
56
|
+
const nextRelatedKeys = this.keys().filter((key) => key[0] === nextRelatedTo && stringKey(key) !== stringKey(nextKey));
|
|
57
|
+
const prevRelatedKeys = currentRelatedKeys.filter((curr) => !nextRelatedKeys.find((next) => stringKey(curr) === stringKey(next)));
|
|
58
|
+
const currentAncestorKeys = currentKey.slice(1).map((_, i) => currentKey.slice(i + 1, currentKey.length));
|
|
59
|
+
const nextAncestorKeys = nextKey.slice(1).map((_, i) => nextKey.slice(i + 1, nextKey.length));
|
|
60
|
+
const prevAncestorKeys = currentAncestorKeys.filter((curr) => !nextAncestorKeys.find((next) => stringKey(curr) === stringKey(next)));
|
|
61
|
+
this._state.current = nextKey;
|
|
62
|
+
prevRelatedKeys.forEach((key) => this._set(key, {}));
|
|
63
|
+
prevAncestorKeys.forEach((key) => this._set(key, {}));
|
|
64
|
+
nextRelatedKeys.forEach((key) => this._set(key, {
|
|
65
|
+
isRelated: true
|
|
66
|
+
}));
|
|
67
|
+
nextAncestorKeys.forEach((key) => this._set(key, {
|
|
68
|
+
isAncestor: true
|
|
69
|
+
}));
|
|
70
|
+
this._set(nextKey, {
|
|
71
|
+
hasAttention: true
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
_set(key, attention) {
|
|
75
|
+
const object = this.get(key);
|
|
76
|
+
object.hasAttention = attention.hasAttention ?? false;
|
|
77
|
+
object.isAncestor = attention.isAncestor ?? false;
|
|
78
|
+
object.isRelated = attention.isRelated ?? false;
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
var getAttendables = (selector, cursor, acc = []) => {
|
|
82
|
+
const closestAttendable = cursor.closest(selector);
|
|
83
|
+
if (closestAttendable) {
|
|
84
|
+
const attendableId = closestAttendable.getAttribute("data-attendable-id");
|
|
85
|
+
if (!attendableId) {
|
|
86
|
+
const trigger = document.querySelector(`[aria-controls="${closestAttendable.getAttribute("id")}"]`);
|
|
87
|
+
if (!trigger) {
|
|
88
|
+
return acc;
|
|
89
|
+
} else {
|
|
90
|
+
return getAttendables(selector, trigger, acc);
|
|
91
|
+
}
|
|
92
|
+
} else {
|
|
93
|
+
acc.push(attendableId);
|
|
94
|
+
return !closestAttendable.parentElement ? acc : getAttendables(selector, closestAttendable.parentElement, acc);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return [
|
|
98
|
+
...new Set(acc)
|
|
99
|
+
];
|
|
100
|
+
};
|
|
101
|
+
|
|
1
102
|
// packages/ui/react-ui-attention/src/components/AttentionGlyph.tsx
|
|
2
103
|
import React, { forwardRef } from "react";
|
|
3
104
|
import { mx } from "@dxos/react-ui-theme";
|
|
4
|
-
var attentionGlyphStyles = "inline-block rounded-sm is-3 bs-3 bg-transparent text-accentText transition-colors [[data-attention=true]_&]:bg-accentSurface [[data-attention=true]_&]:
|
|
105
|
+
var attentionGlyphStyles = "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";
|
|
5
106
|
var presenceIconStyles = "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";
|
|
6
107
|
var PresenceOne = () => {
|
|
7
108
|
return /* @__PURE__ */ React.createElement("svg", {
|
|
@@ -47,72 +148,105 @@ var AttentionGlyph = /* @__PURE__ */ forwardRef(({ presence, classNames, ...prop
|
|
|
47
148
|
|
|
48
149
|
// packages/ui/react-ui-attention/src/components/AttentionProvider.tsx
|
|
49
150
|
import { createContext } from "@radix-ui/react-context";
|
|
50
|
-
import {
|
|
51
|
-
import
|
|
151
|
+
import React2, { useMemo } from "react";
|
|
152
|
+
import { useDefaultValue } from "@dxos/react-ui";
|
|
52
153
|
var ATTENTION_NAME = "Attention";
|
|
154
|
+
var ATTENABLE_ATTRIBUTE = "data-attendable-id";
|
|
155
|
+
var ATTENTION_SOURCE_ATTRIBUTE = "data-is-attention-source";
|
|
53
156
|
var [AttentionContextProvider, useAttentionContext] = createContext(ATTENTION_NAME, {
|
|
54
|
-
|
|
157
|
+
attention: new AttentionManager(),
|
|
158
|
+
path: []
|
|
55
159
|
});
|
|
56
|
-
var
|
|
57
|
-
|
|
58
|
-
|
|
160
|
+
var UNKNOWN_ATTENDABLE = {
|
|
161
|
+
hasAttention: false,
|
|
162
|
+
isAncestor: false,
|
|
163
|
+
isRelated: false
|
|
59
164
|
};
|
|
60
|
-
var
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
165
|
+
var useAttention = (attendableId) => {
|
|
166
|
+
const { attention, path } = useAttentionContext(ATTENTION_NAME);
|
|
167
|
+
if (!attendableId) {
|
|
168
|
+
return UNKNOWN_ATTENDABLE;
|
|
169
|
+
}
|
|
170
|
+
const current = [
|
|
171
|
+
attendableId,
|
|
172
|
+
...path
|
|
173
|
+
];
|
|
174
|
+
return attention.get(current);
|
|
64
175
|
};
|
|
65
|
-
var
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
}
|
|
78
|
-
} else {
|
|
79
|
-
acc.push(attendableId);
|
|
80
|
-
return !closestAttendable.parentElement ? acc : getAttendables(selector, closestAttendable.parentElement, acc);
|
|
176
|
+
var useAttended = () => {
|
|
177
|
+
const { attention } = useAttentionContext(ATTENTION_NAME);
|
|
178
|
+
return attention.current;
|
|
179
|
+
};
|
|
180
|
+
var useAttendableAttributes = (attendableId) => {
|
|
181
|
+
const { hasAttention } = useAttention(attendableId);
|
|
182
|
+
return useMemo(() => {
|
|
183
|
+
const attributes = {
|
|
184
|
+
[ATTENABLE_ATTRIBUTE]: attendableId
|
|
185
|
+
};
|
|
186
|
+
if (hasAttention) {
|
|
187
|
+
attributes[ATTENTION_SOURCE_ATTRIBUTE] = "true";
|
|
81
188
|
}
|
|
82
|
-
|
|
189
|
+
return attributes;
|
|
190
|
+
}, [
|
|
191
|
+
attendableId,
|
|
192
|
+
hasAttention
|
|
193
|
+
]);
|
|
194
|
+
};
|
|
195
|
+
var useAttentionPath = () => {
|
|
196
|
+
const { path } = useAttentionContext(ATTENTION_NAME);
|
|
197
|
+
return path;
|
|
83
198
|
};
|
|
84
|
-
var
|
|
85
|
-
const
|
|
86
|
-
prop: propsAttended,
|
|
87
|
-
defaultProp: defaultAttended,
|
|
88
|
-
onChange: onChangeAttend
|
|
89
|
-
});
|
|
199
|
+
var RootAttentionProvider = ({ children, attention: propsAttention, onChange }) => {
|
|
200
|
+
const attention = useDefaultValue(propsAttention, () => new AttentionManager());
|
|
90
201
|
const handleFocus = (event) => {
|
|
91
202
|
const selector = [
|
|
92
203
|
"[data-attendable-id]",
|
|
93
204
|
...Array.from(document.querySelectorAll("[aria-controls]")).map((el) => `[id="${el.getAttribute("aria-controls")}"]`)
|
|
94
205
|
].join(",");
|
|
95
|
-
const
|
|
96
|
-
const
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
206
|
+
const prev = attention.current;
|
|
207
|
+
const next = getAttendables(selector, event.target);
|
|
208
|
+
if (next.length > 0 && (prev.length !== next.length || !!prev.find((id, index) => next[index] !== id))) {
|
|
209
|
+
attention.update(next);
|
|
210
|
+
onChange?.(next);
|
|
211
|
+
}
|
|
101
212
|
};
|
|
102
213
|
return /* @__PURE__ */ React2.createElement(AttentionContextProvider, {
|
|
103
|
-
|
|
214
|
+
attention,
|
|
215
|
+
path: []
|
|
104
216
|
}, /* @__PURE__ */ React2.createElement("div", {
|
|
105
217
|
role: "none",
|
|
106
218
|
className: "contents",
|
|
107
219
|
onFocusCapture: handleFocus
|
|
108
220
|
}, children));
|
|
109
221
|
};
|
|
222
|
+
var AttentionProvider = ({ id, children }) => {
|
|
223
|
+
const { attention, path } = useAttentionContext(ATTENTION_NAME);
|
|
224
|
+
const nextPath = useMemo(() => [
|
|
225
|
+
id,
|
|
226
|
+
...path
|
|
227
|
+
], [
|
|
228
|
+
id,
|
|
229
|
+
path
|
|
230
|
+
]);
|
|
231
|
+
return /* @__PURE__ */ React2.createElement(AttentionContextProvider, {
|
|
232
|
+
attention,
|
|
233
|
+
path: nextPath
|
|
234
|
+
}, children);
|
|
235
|
+
};
|
|
110
236
|
export {
|
|
237
|
+
ATTENABLE_ATTRIBUTE,
|
|
111
238
|
ATTENTION_NAME,
|
|
239
|
+
ATTENTION_SOURCE_ATTRIBUTE,
|
|
112
240
|
AttentionGlyph,
|
|
241
|
+
AttentionManager,
|
|
113
242
|
AttentionProvider,
|
|
114
|
-
|
|
243
|
+
AttentionSchema,
|
|
244
|
+
RootAttentionProvider,
|
|
245
|
+
getAttendables,
|
|
246
|
+
useAttendableAttributes,
|
|
247
|
+
useAttended,
|
|
248
|
+
useAttention,
|
|
115
249
|
useAttentionContext,
|
|
116
|
-
|
|
250
|
+
useAttentionPath
|
|
117
251
|
};
|
|
118
252
|
//# sourceMappingURL=index.mjs.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../../../src/components/AttentionGlyph.tsx", "../../../src/components/AttentionProvider.tsx"],
|
|
4
|
-
"sourcesContent": ["//\n// Copyright 2024 DXOS.org\n//\n\nimport { type Primitive } from '@radix-ui/react-primitive';\nimport React, { type ComponentPropsWithRef, forwardRef } from 'react';\n\nimport { type ThemedClassName } from '@dxos/react-ui';\nimport { mx } from '@dxos/react-ui-theme';\n\nconst attentionGlyphStyles =\n 'inline-block rounded-sm is-3 bs-3 bg-transparent text-accentText transition-colors [[data-attention=true]_&]:bg-accentSurface [[data-attention=true]_&]:fg-inverse [[aria-current][data-attention=true]_&]:bg-accentSurface [[aria-current][data-attention=true]_&]:fg-inverse';\n\nconst presenceIconStyles =\n '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';\n\nconst PresenceOne = () => {\n return (\n <svg\n width='12'\n height='12'\n viewBox='0 0 12 12'\n className={presenceIconStyles}\n fill='none'\n xmlns='http://www.w3.org/2000/svg'\n >\n <circle cx='6' cy='6' r='2.5' fill='currentColor' />\n </svg>\n );\n};\n\nconst PresenceMany = () => {\n return (\n <svg\n width='12'\n height='12'\n viewBox='0 0 12 12'\n className={presenceIconStyles}\n fill='none'\n xmlns='http://www.w3.org/2000/svg'\n >\n <path\n d='M6.75 8.27311C7.38815 7.72296 7.79212 6.90866 7.79212 6C7.79212 5.09134 7.38815 4.27704 6.75 3.72689C7.06722 3.58122 7.42019 3.5 7.79212 3.5C9.17283 3.5 10.2921 4.61929 10.2921 6C10.2921 7.38071 9.17283 8.5 7.79212 8.5C7.42019 8.5 7.06723 8.41878 6.75 8.27311Z'\n fill='currentColor'\n />\n <circle cx='4.25' cy='6' r='2.5' fill='currentColor' />\n </svg>\n );\n};\n\nexport type AttentionGlyphProps = {\n presence?: 'none' | 'one' | 'many';\n} & ThemedClassName<Omit<ComponentPropsWithRef<typeof Primitive.span>, 'children'>>;\n\nexport const AttentionGlyph = forwardRef<HTMLSpanElement, AttentionGlyphProps>(\n ({ presence, classNames, ...props }, forwardedRef) => {\n return (\n <span role='none' {...props} className={mx(attentionGlyphStyles, classNames)} ref={forwardedRef}>\n {presence === 'many' && <PresenceMany />}\n {presence === 'one' && <PresenceOne />}\n </span>\n );\n },\n);\n", "//\n// Copyright 2024 DXOS.org\n//\n\n// NOTE(thure): The following unused imports quell TS2742 (“likely not portable”).\n\n// eslint-disable-next-line unused-imports/no-unused-imports\nimport { createContext, type Scope, type CreateScope } from '@radix-ui/react-context';\nimport { useControllableState } from '@radix-ui/react-use-controllable-state';\n// eslint-disable-next-line unused-imports/no-unused-imports\nimport React, { ComponentProps, type FocusEvent, type HTMLAttributes, type PropsWithChildren, useState } from 'react';\n\nconst ATTENTION_NAME = 'Attention';\n\ntype AttentionContextValue = {\n attended: Set<string>;\n};\n\nconst [AttentionContextProvider, useAttentionContext] = createContext<AttentionContextValue>(ATTENTION_NAME, {\n attended: new Set(),\n});\n\nconst useHasAttention = (attendableId?: string) => {\n const { attended } = useAttentionContext(ATTENTION_NAME);\n return attendableId ? attended.has(attendableId) : false;\n};\n\n/**\n * Computes HTML element attributes to apply so the attention system can detect changes\n * @param attendableId\n */\nconst createAttendableAttributes = (attendableId?: string) => {\n return { 'data-attendable-id': attendableId };\n};\n\n/**\n * Accumulates all attendable id’s between the element provided and the root, inclusive.\n */\nconst getAttendables = (selector: string, cursor: Element, acc: string[] = []): string[] => {\n // Find the closest element with `data-attendable-id`, if any; start from cursor and move up the DOM tree.\n const closestAttendable = cursor.closest(selector);\n if (!closestAttendable) {\n return acc;\n } else {\n const attendableId = closestAttendable.getAttribute('data-attendable-id');\n if (!attendableId) {\n // this has an id of an aria-controls elsewhere on the page, move cursor to that trigger\n const trigger = document.querySelector(`[aria-controls=\"${closestAttendable.getAttribute('id')}\"]`);\n if (!trigger) {\n return acc;\n } else {\n return getAttendables(selector, trigger, acc);\n }\n } else {\n acc.push(attendableId);\n // (attempt tail recursion)\n return !closestAttendable.parentElement ? acc : getAttendables(selector, closestAttendable.parentElement, acc);\n }\n }\n};\n\nconst AttentionProvider = ({\n children,\n attended: propsAttended,\n defaultAttended,\n onChangeAttend,\n}: PropsWithChildren<\n Partial<{\n attended: Set<string>;\n onChangeAttend: (nextAttended: Set<string>) => void;\n defaultAttended: Set<string>;\n }>\n>) => {\n const [attended = new Set(), setAttended] = useControllableState<Set<string>>({\n prop: propsAttended,\n defaultProp: defaultAttended,\n onChange: onChangeAttend,\n });\n const handleFocus = (event: FocusEvent) => {\n const selector = [\n '[data-attendable-id]',\n ...Array.from(document.querySelectorAll('[aria-controls]')).map(\n (el) => `[id=\"${el.getAttribute('aria-controls')}\"]`,\n ),\n ].join(',');\n const nextAttended = new Set(getAttendables(selector, event.target));\n const [prev, next] = [Array.from(attended), Array.from(nextAttended)];\n // Only update state if the result is different.\n (prev.length !== next.length || !!prev.find((id, index) => next[index] !== id)) && setAttended(nextAttended);\n };\n return (\n <AttentionContextProvider attended={attended}>\n <div role='none' className='contents' onFocusCapture={handleFocus}>\n {children}\n </div>\n </AttentionContextProvider>\n );\n};\n\nexport { AttentionProvider, useAttentionContext, useHasAttention, createAttendableAttributes, ATTENTION_NAME };\n\nexport type { AttentionContextValue };\n"],
|
|
5
|
-
"mappings": ";
|
|
6
|
-
"names": ["React", "forwardRef", "mx", "attentionGlyphStyles", "presenceIconStyles", "PresenceOne", "svg", "width", "height", "viewBox", "className", "fill", "xmlns", "circle", "cx", "cy", "r", "PresenceMany", "path", "d", "AttentionGlyph", "forwardRef", "presence", "classNames", "props", "forwardedRef", "span", "role", "mx", "ref", "createContext", "
|
|
3
|
+
"sources": ["../../../src/attention.ts", "../../../src/components/AttentionGlyph.tsx", "../../../src/components/AttentionProvider.tsx"],
|
|
4
|
+
"sourcesContent": ["//\n// Copyright 2024 DXOS.org\n//\n\nimport { untracked } from '@preact/signals-core';\n\nimport { create, type ReactiveObject, S } from '@dxos/echo-schema';\nimport { ComplexMap } from '@dxos/util';\n\nexport const AttentionSchema = S.mutable(\n S.Struct({\n hasAttention: S.Boolean,\n isAncestor: S.Boolean,\n isRelated: S.Boolean,\n }),\n);\n\nexport type Attention = S.Schema.Type<typeof AttentionSchema>;\n\nconst stringKey = (key: string[]) => key.join(',');\n\n/**\n * Manages attention state for an application.\n */\nexport class AttentionManager {\n // Each attention path is associated with an attention object.\n // The lookup is not a reactive object to ensure that attention for each path is subscribable independently.\n private readonly _map = new ComplexMap<string[], ReactiveObject<Attention>>(stringKey);\n private readonly _state: ReactiveObject<{ current: string[] }>;\n\n constructor(initial: string[] = []) {\n this._state = create({ current: initial });\n }\n\n /**\n * Currently attended element.\n */\n get current() {\n return this._state.current;\n }\n\n /**\n * All attention paths.\n */\n keys() {\n return this._map.keys();\n }\n\n /**\n * Get the attention state for a given path.\n */\n get(key: string[]): ReactiveObject<Attention> {\n const object = this._map.get(key);\n if (object) {\n return object;\n }\n\n const newObject = create(AttentionSchema, { hasAttention: false, isAncestor: false, isRelated: false });\n this._map.set(key, newObject);\n return newObject;\n }\n\n /**\n * Update the currently attended element.\n */\n update(nextKey: string[]) {\n const currentKey = untracked(() => this.current);\n const currentRelatedTo = currentKey[0];\n const nextRelatedTo = nextKey[0];\n\n const currentRelatedKeys = this.keys().filter((key) => key[0] === currentRelatedTo);\n const nextRelatedKeys = this.keys().filter(\n (key) => key[0] === nextRelatedTo && stringKey(key) !== stringKey(nextKey),\n );\n const prevRelatedKeys = currentRelatedKeys.filter(\n (curr) => !nextRelatedKeys.find((next) => stringKey(curr) === stringKey(next)),\n );\n\n const currentAncestorKeys = currentKey.slice(1).map((_, i) => currentKey.slice(i + 1, currentKey.length));\n const nextAncestorKeys = nextKey.slice(1).map((_, i) => nextKey.slice(i + 1, nextKey.length));\n const prevAncestorKeys = currentAncestorKeys.filter(\n (curr) => !nextAncestorKeys.find((next) => stringKey(curr) === stringKey(next)),\n );\n\n this._state.current = nextKey;\n prevRelatedKeys.forEach((key) => this._set(key, {}));\n prevAncestorKeys.forEach((key) => this._set(key, {}));\n nextRelatedKeys.forEach((key) => this._set(key, { isRelated: true }));\n nextAncestorKeys.forEach((key) => this._set(key, { isAncestor: true }));\n this._set(nextKey, { hasAttention: true });\n }\n\n private _set(key: string[], attention: Partial<Attention>) {\n const object = this.get(key);\n object.hasAttention = attention.hasAttention ?? false;\n object.isAncestor = attention.isAncestor ?? false;\n object.isRelated = attention.isRelated ?? false;\n }\n}\n\n/**\n * Accumulates all attendable id’s between the element provided and the root, inclusive.\n */\nexport const getAttendables = (selector: string, cursor: Element, acc: string[] = []): string[] => {\n // Find the closest element with `data-attendable-id`, if any; start from cursor and move up the DOM tree.\n const closestAttendable = cursor.closest(selector);\n if (closestAttendable) {\n const attendableId = closestAttendable.getAttribute('data-attendable-id');\n if (!attendableId) {\n // this has an id of an aria-controls elsewhere on the page, move cursor to that trigger\n const trigger = document.querySelector(`[aria-controls=\"${closestAttendable.getAttribute('id')}\"]`);\n if (!trigger) {\n return acc;\n } else {\n return getAttendables(selector, trigger, acc);\n }\n } else {\n acc.push(attendableId);\n // (attempt tail recursion)\n return !closestAttendable.parentElement ? acc : getAttendables(selector, closestAttendable.parentElement, acc);\n }\n }\n\n return [...new Set(acc)];\n};\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { type Primitive } from '@radix-ui/react-primitive';\nimport React, { type ComponentPropsWithRef, forwardRef } from 'react';\n\nimport { type ThemedClassName } from '@dxos/react-ui';\nimport { mx } from '@dxos/react-ui-theme';\n\nconst attentionGlyphStyles =\n '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';\n\nconst presenceIconStyles =\n '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';\n\nconst PresenceOne = () => {\n return (\n <svg\n width='12'\n height='12'\n viewBox='0 0 12 12'\n className={presenceIconStyles}\n fill='none'\n xmlns='http://www.w3.org/2000/svg'\n >\n <circle cx='6' cy='6' r='2.5' fill='currentColor' />\n </svg>\n );\n};\n\nconst PresenceMany = () => {\n return (\n <svg\n width='12'\n height='12'\n viewBox='0 0 12 12'\n className={presenceIconStyles}\n fill='none'\n xmlns='http://www.w3.org/2000/svg'\n >\n <path\n d='M6.75 8.27311C7.38815 7.72296 7.79212 6.90866 7.79212 6C7.79212 5.09134 7.38815 4.27704 6.75 3.72689C7.06722 3.58122 7.42019 3.5 7.79212 3.5C9.17283 3.5 10.2921 4.61929 10.2921 6C10.2921 7.38071 9.17283 8.5 7.79212 8.5C7.42019 8.5 7.06723 8.41878 6.75 8.27311Z'\n fill='currentColor'\n />\n <circle cx='4.25' cy='6' r='2.5' fill='currentColor' />\n </svg>\n );\n};\n\nexport type AttentionGlyphProps = {\n presence?: 'none' | 'one' | 'many';\n} & ThemedClassName<Omit<ComponentPropsWithRef<typeof Primitive.span>, 'children'>>;\n\nexport const AttentionGlyph = forwardRef<HTMLSpanElement, AttentionGlyphProps>(\n ({ presence, classNames, ...props }, forwardedRef) => {\n return (\n <span role='none' {...props} className={mx(attentionGlyphStyles, classNames)} ref={forwardedRef}>\n {presence === 'many' && <PresenceMany />}\n {presence === 'one' && <PresenceOne />}\n </span>\n );\n },\n);\n", "//\n// Copyright 2024 DXOS.org\n//\n\n// NOTE(thure): The following unused imports quell TS2742 (“likely not portable”).\n\n// eslint-disable-next-line unused-imports/no-unused-imports\nimport { createContext, type Scope, type CreateScope } from '@radix-ui/react-context';\nimport React, { useMemo, type FocusEvent, type PropsWithChildren } from 'react';\n\nimport { useDefaultValue } from '@dxos/react-ui';\n\nimport { type Attention, AttentionManager, getAttendables } from '../attention';\n\nconst ATTENTION_NAME = 'Attention';\nconst ATTENABLE_ATTRIBUTE = 'data-attendable-id';\nconst ATTENTION_SOURCE_ATTRIBUTE = 'data-is-attention-source';\n\ntype AttentionContextValue = {\n attention: AttentionManager;\n path: string[];\n};\n\nconst [AttentionContextProvider, useAttentionContext] = createContext<AttentionContextValue>(ATTENTION_NAME, {\n attention: new AttentionManager(),\n path: [],\n});\n\nconst UNKNOWN_ATTENDABLE = { hasAttention: false, isAncestor: false, isRelated: false };\n\nconst useAttention = (attendableId?: string): Attention => {\n const { attention, path } = useAttentionContext(ATTENTION_NAME);\n if (!attendableId) {\n return UNKNOWN_ATTENDABLE;\n }\n\n const current = [attendableId, ...path];\n return attention.get(current);\n};\n\nconst useAttended = () => {\n const { attention } = useAttentionContext(ATTENTION_NAME);\n return attention.current;\n};\n\n/**\n * Computes HTML element attributes to apply so the attention system can detect changes\n * @param attendableId\n */\nconst useAttendableAttributes = (attendableId?: string) => {\n const { hasAttention } = useAttention(attendableId);\n return useMemo(() => {\n const attributes: Record<string, string | undefined> = { [ATTENABLE_ATTRIBUTE]: attendableId };\n if (hasAttention) {\n attributes[ATTENTION_SOURCE_ATTRIBUTE] = 'true';\n }\n\n return attributes;\n }, [attendableId, hasAttention]);\n};\n\nconst useAttentionPath = () => {\n const { path } = useAttentionContext(ATTENTION_NAME);\n return path;\n};\n\nconst RootAttentionProvider = ({\n children,\n attention: propsAttention,\n onChange,\n}: PropsWithChildren<\n Partial<{\n attention: AttentionManager;\n onChange: (nextAttended: string[]) => void;\n }>\n>) => {\n const attention = useDefaultValue(propsAttention, () => new AttentionManager());\n const handleFocus = (event: FocusEvent) => {\n const selector = [\n '[data-attendable-id]',\n ...Array.from(document.querySelectorAll('[aria-controls]')).map(\n (el) => `[id=\"${el.getAttribute('aria-controls')}\"]`,\n ),\n ].join(',');\n const prev = attention.current;\n const next = getAttendables(selector, event.target);\n // TODO(wittjosiah): Not allowing empty state means that the attended item is not strictly guaranteed to be in the DOM.\n // Currently this depends on the deck in order to ensure that when the attended item is removed something else is attended.\n // Only update state if the result is different and not empty.\n if (next.length > 0 && (prev.length !== next.length || !!prev.find((id, index) => next[index] !== id))) {\n attention.update(next);\n onChange?.(next);\n }\n };\n return (\n <AttentionContextProvider attention={attention} path={[]}>\n <div role='none' className='contents' onFocusCapture={handleFocus}>\n {children}\n </div>\n </AttentionContextProvider>\n );\n};\n\nconst AttentionProvider = ({ id, children }: PropsWithChildren<{ id: string }>) => {\n const { attention, path } = useAttentionContext(ATTENTION_NAME);\n const nextPath = useMemo(() => [id, ...path], [id, path]);\n\n return (\n <AttentionContextProvider attention={attention} path={nextPath}>\n {children}\n </AttentionContextProvider>\n );\n};\n\nexport {\n RootAttentionProvider,\n AttentionProvider,\n useAttentionContext,\n useAttention,\n useAttended,\n useAttendableAttributes,\n useAttentionPath,\n ATTENTION_NAME,\n ATTENABLE_ATTRIBUTE,\n ATTENTION_SOURCE_ATTRIBUTE,\n};\n"],
|
|
5
|
+
"mappings": ";AAIA,SAASA,iBAAiB;AAE1B,SAASC,QAA6BC,SAAS;AAC/C,SAASC,kBAAkB;AAEpB,IAAMC,kBAAkBC,EAAEC,QAC/BD,EAAEE,OAAO;EACPC,cAAcH,EAAEI;EAChBC,YAAYL,EAAEI;EACdE,WAAWN,EAAEI;AACf,CAAA,CAAA;AAKF,IAAMG,YAAY,CAACC,QAAkBA,IAAIC,KAAK,GAAA;AAKvC,IAAMC,mBAAN,MAAMA;EAMXC,YAAYC,UAAoB,CAAA,GAAI;AAHnBC;;gBAAO,IAAIC,WAAgDP,SAAAA;AAI1E,SAAKQ,SAASC,OAAO;MAAEC,SAASL;IAAQ,CAAA;EAC1C;;;;EAKA,IAAIK,UAAU;AACZ,WAAO,KAAKF,OAAOE;EACrB;;;;EAKAC,OAAO;AACL,WAAO,KAAKL,KAAKK,KAAI;EACvB;;;;EAKAC,IAAIX,KAA0C;AAC5C,UAAMY,SAAS,KAAKP,KAAKM,IAAIX,GAAAA;AAC7B,QAAIY,QAAQ;AACV,aAAOA;IACT;AAEA,UAAMC,YAAYL,OAAOjB,iBAAiB;MAAEI,cAAc;MAAOE,YAAY;MAAOC,WAAW;IAAM,CAAA;AACrG,SAAKO,KAAKS,IAAId,KAAKa,SAAAA;AACnB,WAAOA;EACT;;;;EAKAE,OAAOC,SAAmB;AACxB,UAAMC,aAAaC,UAAU,MAAM,KAAKT,OAAO;AA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pC;KAAO;IAACoC;IAAIpC;GAAK;AAExD,SACE,gBAAAuC,OAAA,cAAC5C,0BAAAA;IAAyBG;IAAsBE,MAAM6C;KACnD9B,QAAAA;AAGP;",
|
|
6
|
+
"names": ["untracked", "create", "S", "ComplexMap", "AttentionSchema", "S", "mutable", "Struct", "hasAttention", "Boolean", "isAncestor", "isRelated", "stringKey", "key", "join", "AttentionManager", "constructor", "initial", "_map", "ComplexMap", "_state", "create", "current", "keys", "get", "object", "newObject", "set", "update", "nextKey", "currentKey", "untracked", "currentRelatedTo", "nextRelatedTo", "currentRelatedKeys", "filter", "nextRelatedKeys", "prevRelatedKeys", "curr", "find", "next", "currentAncestorKeys", "slice", "map", "_", "i", "length", "nextAncestorKeys", "prevAncestorKeys", "forEach", "_set", "attention", "getAttendables", "selector", "cursor", "acc", "closestAttendable", "closest", "attendableId", "getAttribute", "trigger", "document", "querySelector", "push", "parentElement", "Set", "React", "forwardRef", "mx", "attentionGlyphStyles", "presenceIconStyles", "PresenceOne", "svg", "width", "height", "viewBox", "className", "fill", "xmlns", "circle", "cx", "cy", "r", "PresenceMany", "path", "d", "AttentionGlyph", "forwardRef", "presence", "classNames", "props", "forwardedRef", "span", "role", "mx", "ref", "createContext", "React", "useMemo", "useDefaultValue", "ATTENTION_NAME", "ATTENABLE_ATTRIBUTE", "ATTENTION_SOURCE_ATTRIBUTE", "AttentionContextProvider", "useAttentionContext", "createContext", "attention", "AttentionManager", "path", "UNKNOWN_ATTENDABLE", "hasAttention", "isAncestor", "isRelated", "useAttention", "attendableId", "current", "get", "useAttended", "useAttendableAttributes", "useMemo", "attributes", "useAttentionPath", "RootAttentionProvider", "children", "propsAttention", "onChange", "useDefaultValue", "handleFocus", "event", "selector", "Array", "from", "document", "querySelectorAll", "map", "el", "getAttribute", "join", "prev", "next", "getAttendables", "target", "length", "find", "id", "index", "update", "React", "div", "role", "className", "onFocusCapture", "AttentionProvider", "nextPath"]
|
|
7
7
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"inputs":{"packages/ui/react-ui-attention/src/components/AttentionGlyph.tsx":{"bytes":
|
|
1
|
+
{"inputs":{"packages/ui/react-ui-attention/src/attention.ts":{"bytes":14976,"imports":[{"path":"@preact/signals-core","kind":"import-statement","external":true},{"path":"@dxos/echo-schema","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true}],"format":"esm"},"packages/ui/react-ui-attention/src/components/AttentionGlyph.tsx":{"bytes":7599,"imports":[{"path":"react","kind":"import-statement","external":true},{"path":"@dxos/react-ui-theme","kind":"import-statement","external":true}],"format":"esm"},"packages/ui/react-ui-attention/src/components/AttentionProvider.tsx":{"bytes":13329,"imports":[{"path":"@radix-ui/react-context","kind":"import-statement","external":true},{"path":"react","kind":"import-statement","external":true},{"path":"@dxos/react-ui","kind":"import-statement","external":true},{"path":"packages/ui/react-ui-attention/src/attention.ts","kind":"import-statement","original":"../attention"}],"format":"esm"},"packages/ui/react-ui-attention/src/components/index.ts":{"bytes":644,"imports":[{"path":"packages/ui/react-ui-attention/src/components/AttentionGlyph.tsx","kind":"import-statement","original":"./AttentionGlyph"},{"path":"packages/ui/react-ui-attention/src/components/AttentionProvider.tsx","kind":"import-statement","original":"./AttentionProvider"}],"format":"esm"},"packages/ui/react-ui-attention/src/index.ts":{"bytes":597,"imports":[{"path":"packages/ui/react-ui-attention/src/attention.ts","kind":"import-statement","original":"./attention"},{"path":"packages/ui/react-ui-attention/src/components/index.ts","kind":"import-statement","original":"./components"}],"format":"esm"}},"outputs":{"packages/ui/react-ui-attention/dist/lib/browser/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":18097},"packages/ui/react-ui-attention/dist/lib/browser/index.mjs":{"imports":[{"path":"@preact/signals-core","kind":"import-statement","external":true},{"path":"@dxos/echo-schema","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true},{"path":"react","kind":"import-statement","external":true},{"path":"@dxos/react-ui-theme","kind":"import-statement","external":true},{"path":"@radix-ui/react-context","kind":"import-statement","external":true},{"path":"react","kind":"import-statement","external":true},{"path":"@dxos/react-ui","kind":"import-statement","external":true}],"exports":["ATTENABLE_ATTRIBUTE","ATTENTION_NAME","ATTENTION_SOURCE_ATTRIBUTE","AttentionGlyph","AttentionManager","AttentionProvider","AttentionSchema","RootAttentionProvider","getAttendables","useAttendableAttributes","useAttended","useAttention","useAttentionContext","useAttentionPath"],"entryPoint":"packages/ui/react-ui-attention/src/index.ts","inputs":{"packages/ui/react-ui-attention/src/attention.ts":{"bytesInOutput":3372},"packages/ui/react-ui-attention/src/index.ts":{"bytesInOutput":0},"packages/ui/react-ui-attention/src/components/AttentionGlyph.tsx":{"bytesInOutput":2349},"packages/ui/react-ui-attention/src/components/AttentionProvider.tsx":{"bytesInOutput":2605}},"bytes":8857}}}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { type ReactiveObject, S } from '@dxos/echo-schema';
|
|
2
|
+
export declare const AttentionSchema: S.mutable<S.Struct<{
|
|
3
|
+
hasAttention: typeof S.Boolean;
|
|
4
|
+
isAncestor: typeof S.Boolean;
|
|
5
|
+
isRelated: typeof S.Boolean;
|
|
6
|
+
}>>;
|
|
7
|
+
export type Attention = S.Schema.Type<typeof AttentionSchema>;
|
|
8
|
+
/**
|
|
9
|
+
* Manages attention state for an application.
|
|
10
|
+
*/
|
|
11
|
+
export declare class AttentionManager {
|
|
12
|
+
private readonly _map;
|
|
13
|
+
private readonly _state;
|
|
14
|
+
constructor(initial?: string[]);
|
|
15
|
+
/**
|
|
16
|
+
* Currently attended element.
|
|
17
|
+
*/
|
|
18
|
+
get current(): string[];
|
|
19
|
+
/**
|
|
20
|
+
* All attention paths.
|
|
21
|
+
*/
|
|
22
|
+
keys(): SetIterator<string[]>;
|
|
23
|
+
/**
|
|
24
|
+
* Get the attention state for a given path.
|
|
25
|
+
*/
|
|
26
|
+
get(key: string[]): ReactiveObject<Attention>;
|
|
27
|
+
/**
|
|
28
|
+
* Update the currently attended element.
|
|
29
|
+
*/
|
|
30
|
+
update(nextKey: string[]): void;
|
|
31
|
+
private _set;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Accumulates all attendable id’s between the element provided and the root, inclusive.
|
|
35
|
+
*/
|
|
36
|
+
export declare const getAttendables: (selector: string, cursor: Element, acc?: string[]) => string[];
|
|
37
|
+
//# sourceMappingURL=attention.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attention.d.ts","sourceRoot":"","sources":["../../../src/attention.ts"],"names":[],"mappings":"AAMA,OAAO,EAAU,KAAK,cAAc,EAAE,CAAC,EAAE,MAAM,mBAAmB,CAAC;AAGnE,eAAO,MAAM,eAAe;;;;GAM3B,CAAC;AAEF,MAAM,MAAM,SAAS,GAAG,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,eAAe,CAAC,CAAC;AAI9D;;GAEG;AACH,qBAAa,gBAAgB;IAG3B,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAkE;IACvF,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAwC;gBAEnD,OAAO,GAAE,MAAM,EAAO;IAIlC;;OAEG;IACH,IAAI,OAAO,aAEV;IAED;;OAEG;IACH,IAAI;IAIJ;;OAEG;IACH,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,cAAc,CAAC,SAAS,CAAC;IAW7C;;OAEG;IACH,MAAM,CAAC,OAAO,EAAE,MAAM,EAAE;IA2BxB,OAAO,CAAC,IAAI;CAMb;AAED;;GAEG;AACH,eAAO,MAAM,cAAc,aAAc,MAAM,UAAU,OAAO,QAAO,MAAM,EAAE,KAAQ,MAAM,EAqB5F,CAAC"}
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import '@dxos-theme';
|
|
2
2
|
import React from 'react';
|
|
3
|
-
type
|
|
3
|
+
type StoryProps = {
|
|
4
4
|
current?: boolean;
|
|
5
5
|
attention?: boolean;
|
|
6
6
|
};
|
|
7
7
|
declare const _default: {
|
|
8
8
|
title: string;
|
|
9
|
-
component: (
|
|
9
|
+
component: React.ForwardRefExoticComponent<Omit<import("./AttentionGlyph").AttentionGlyphProps, "ref"> & React.RefAttributes<HTMLSpanElement>>;
|
|
10
|
+
render: ({ current, attention }: StoryProps) => React.JSX.Element;
|
|
10
11
|
decorators: import("@storybook/react/*").Decorator[];
|
|
11
12
|
};
|
|
12
13
|
export default _default;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AttentionGlyph.stories.d.ts","sourceRoot":"","sources":["../../../../src/components/AttentionGlyph.stories.tsx"],"names":[],"mappings":"AAIA,OAAO,aAAa,CAAC;AAErB,OAAO,KAAK,MAAM,OAAO,CAAC;AAM1B,KAAK,
|
|
1
|
+
{"version":3,"file":"AttentionGlyph.stories.d.ts","sourceRoot":"","sources":["../../../../src/components/AttentionGlyph.stories.tsx"],"names":[],"mappings":"AAIA,OAAO,aAAa,CAAC;AAErB,OAAO,KAAK,MAAM,OAAO,CAAC;AAM1B,KAAK,UAAU,GAAG;IAChB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB,CAAC;;;;qCAEqC,UAAU;;;AAoBjD,wBAKE;AAEF,eAAO,MAAM,OAAO;;CAEnB,CAAC;AAEF,eAAO,MAAM,OAAO;;;;CAEnB,CAAC;AAEF,eAAO,MAAM,SAAS;;;;;CAErB,CAAC"}
|
|
@@ -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,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/react-ui-attention",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.14-main.2b6a0f3",
|
|
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": false,
|
|
9
10
|
"exports": {
|
|
10
11
|
".": {
|
|
11
12
|
"browser": "./dist/lib/browser/index.mjs",
|
|
@@ -21,6 +22,7 @@
|
|
|
21
22
|
"src"
|
|
22
23
|
],
|
|
23
24
|
"dependencies": {
|
|
25
|
+
"@preact/signals-core": "^1.6.0",
|
|
24
26
|
"@radix-ui/primitive": "^1.0.0",
|
|
25
27
|
"@radix-ui/react-compose-refs": "^1.0.0",
|
|
26
28
|
"@radix-ui/react-context": "^1.0.0",
|
|
@@ -28,9 +30,8 @@
|
|
|
28
30
|
"@radix-ui/react-primitive": "^1.0.2",
|
|
29
31
|
"@radix-ui/react-slot": "^1.0.1",
|
|
30
32
|
"@radix-ui/react-use-controllable-state": "^1.0.0",
|
|
31
|
-
"@dxos/
|
|
32
|
-
"@dxos/util": "0.6.
|
|
33
|
-
"@dxos/react-ui-theme": "0.6.13"
|
|
33
|
+
"@dxos/echo-schema": "0.6.14-main.2b6a0f3",
|
|
34
|
+
"@dxos/util": "0.6.14-main.2b6a0f3"
|
|
34
35
|
},
|
|
35
36
|
"devDependencies": {
|
|
36
37
|
"@phosphor-icons/react": "^2.1.5",
|
|
@@ -38,15 +39,19 @@
|
|
|
38
39
|
"@types/react-dom": "~18.2.0",
|
|
39
40
|
"react": "~18.2.0",
|
|
40
41
|
"react-dom": "~18.2.0",
|
|
41
|
-
"vite": "
|
|
42
|
-
"@dxos/random": "0.6.
|
|
43
|
-
"@dxos/
|
|
44
|
-
"@dxos/
|
|
42
|
+
"vite": "5.4.7",
|
|
43
|
+
"@dxos/random": "0.6.14-main.2b6a0f3",
|
|
44
|
+
"@dxos/app-graph": "0.6.14-main.2b6a0f3",
|
|
45
|
+
"@dxos/react-ui": "0.6.14-main.2b6a0f3",
|
|
46
|
+
"@dxos/react-ui-theme": "0.6.14-main.2b6a0f3",
|
|
47
|
+
"@dxos/storybook-utils": "0.6.14-main.2b6a0f3"
|
|
45
48
|
},
|
|
46
49
|
"peerDependencies": {
|
|
47
50
|
"@phosphor-icons/react": "^2.1.5",
|
|
48
|
-
"react": "
|
|
49
|
-
"react-dom": "
|
|
51
|
+
"react": "~18.2.0",
|
|
52
|
+
"react-dom": "~18.2.0",
|
|
53
|
+
"@dxos/react-ui": "0.6.14-main.2b6a0f3",
|
|
54
|
+
"@dxos/react-ui-theme": "0.6.14-main.2b6a0f3"
|
|
50
55
|
},
|
|
51
56
|
"publishConfig": {
|
|
52
57
|
"access": "public"
|
package/src/attention.ts
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
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
|
+
const stringKey = (key: string[]) => key.join(',');
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Manages attention state for an application.
|
|
24
|
+
*/
|
|
25
|
+
export class AttentionManager {
|
|
26
|
+
// Each attention path is associated with an attention object.
|
|
27
|
+
// The lookup is not a reactive object to ensure that attention for each path is subscribable independently.
|
|
28
|
+
private readonly _map = new ComplexMap<string[], ReactiveObject<Attention>>(stringKey);
|
|
29
|
+
private readonly _state: ReactiveObject<{ current: string[] }>;
|
|
30
|
+
|
|
31
|
+
constructor(initial: string[] = []) {
|
|
32
|
+
this._state = create({ current: initial });
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Currently attended element.
|
|
37
|
+
*/
|
|
38
|
+
get current() {
|
|
39
|
+
return this._state.current;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* All attention paths.
|
|
44
|
+
*/
|
|
45
|
+
keys() {
|
|
46
|
+
return this._map.keys();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Get the attention state for a given path.
|
|
51
|
+
*/
|
|
52
|
+
get(key: string[]): ReactiveObject<Attention> {
|
|
53
|
+
const object = this._map.get(key);
|
|
54
|
+
if (object) {
|
|
55
|
+
return object;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const newObject = create(AttentionSchema, { hasAttention: false, isAncestor: false, isRelated: false });
|
|
59
|
+
this._map.set(key, newObject);
|
|
60
|
+
return newObject;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Update the currently attended element.
|
|
65
|
+
*/
|
|
66
|
+
update(nextKey: string[]) {
|
|
67
|
+
const currentKey = untracked(() => this.current);
|
|
68
|
+
const currentRelatedTo = currentKey[0];
|
|
69
|
+
const nextRelatedTo = nextKey[0];
|
|
70
|
+
|
|
71
|
+
const currentRelatedKeys = this.keys().filter((key) => key[0] === currentRelatedTo);
|
|
72
|
+
const nextRelatedKeys = this.keys().filter(
|
|
73
|
+
(key) => key[0] === nextRelatedTo && stringKey(key) !== stringKey(nextKey),
|
|
74
|
+
);
|
|
75
|
+
const prevRelatedKeys = currentRelatedKeys.filter(
|
|
76
|
+
(curr) => !nextRelatedKeys.find((next) => stringKey(curr) === stringKey(next)),
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
const currentAncestorKeys = currentKey.slice(1).map((_, i) => currentKey.slice(i + 1, currentKey.length));
|
|
80
|
+
const nextAncestorKeys = nextKey.slice(1).map((_, i) => nextKey.slice(i + 1, nextKey.length));
|
|
81
|
+
const prevAncestorKeys = currentAncestorKeys.filter(
|
|
82
|
+
(curr) => !nextAncestorKeys.find((next) => stringKey(curr) === stringKey(next)),
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
this._state.current = nextKey;
|
|
86
|
+
prevRelatedKeys.forEach((key) => this._set(key, {}));
|
|
87
|
+
prevAncestorKeys.forEach((key) => this._set(key, {}));
|
|
88
|
+
nextRelatedKeys.forEach((key) => this._set(key, { isRelated: true }));
|
|
89
|
+
nextAncestorKeys.forEach((key) => this._set(key, { isAncestor: true }));
|
|
90
|
+
this._set(nextKey, { hasAttention: true });
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
private _set(key: string[], attention: Partial<Attention>) {
|
|
94
|
+
const object = this.get(key);
|
|
95
|
+
object.hasAttention = attention.hasAttention ?? false;
|
|
96
|
+
object.isAncestor = attention.isAncestor ?? false;
|
|
97
|
+
object.isRelated = attention.isRelated ?? false;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Accumulates all attendable id’s between the element provided and the root, inclusive.
|
|
103
|
+
*/
|
|
104
|
+
export const getAttendables = (selector: string, cursor: Element, acc: string[] = []): string[] => {
|
|
105
|
+
// Find the closest element with `data-attendable-id`, if any; start from cursor and move up the DOM tree.
|
|
106
|
+
const closestAttendable = cursor.closest(selector);
|
|
107
|
+
if (closestAttendable) {
|
|
108
|
+
const attendableId = closestAttendable.getAttribute('data-attendable-id');
|
|
109
|
+
if (!attendableId) {
|
|
110
|
+
// this has an id of an aria-controls elsewhere on the page, move cursor to that trigger
|
|
111
|
+
const trigger = document.querySelector(`[aria-controls="${closestAttendable.getAttribute('id')}"]`);
|
|
112
|
+
if (!trigger) {
|
|
113
|
+
return acc;
|
|
114
|
+
} else {
|
|
115
|
+
return getAttendables(selector, trigger, acc);
|
|
116
|
+
}
|
|
117
|
+
} else {
|
|
118
|
+
acc.push(attendableId);
|
|
119
|
+
// (attempt tail recursion)
|
|
120
|
+
return !closestAttendable.parentElement ? acc : getAttendables(selector, closestAttendable.parentElement, acc);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return [...new Set(acc)];
|
|
125
|
+
};
|
|
@@ -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: '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
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type Decorator } from '@storybook/react';
|
|
6
|
+
import { createElement } from 'react';
|
|
7
|
+
|
|
8
|
+
import { RootAttentionProvider } from '../../components';
|
|
9
|
+
|
|
10
|
+
export const withAttention: Decorator = (Story) => {
|
|
11
|
+
return createElement(RootAttentionProvider, { children: createElement(Story) });
|
|
12
|
+
};
|