@dxos/react-ui-attention 0.6.13-main.ed424a1 → 0.6.13
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 +25 -85
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/types/src/components/AttentionProvider.d.ts +11 -24
- package/dist/types/src/components/AttentionProvider.d.ts.map +1 -1
- package/package.json +10 -11
- package/src/components/AttentionGlyph.tsx +1 -1
- package/src/components/AttentionProvider.tsx +29 -99
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// packages/ui/react-ui-attention/src/components/AttentionGlyph.tsx
|
|
2
2
|
import React, { forwardRef } from "react";
|
|
3
3
|
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]_&]:
|
|
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]_&]:fg-inverse [[aria-current][data-attention=true]_&]:bg-accentSurface [[aria-current][data-attention=true]_&]:fg-inverse";
|
|
5
5
|
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
6
|
var PresenceOne = () => {
|
|
7
7
|
return /* @__PURE__ */ React.createElement("svg", {
|
|
@@ -47,63 +47,21 @@ var AttentionGlyph = /* @__PURE__ */ forwardRef(({ presence, classNames, ...prop
|
|
|
47
47
|
|
|
48
48
|
// packages/ui/react-ui-attention/src/components/AttentionProvider.tsx
|
|
49
49
|
import { createContext } from "@radix-ui/react-context";
|
|
50
|
-
import
|
|
51
|
-
import
|
|
52
|
-
import { useDefaultValue } from "@dxos/react-ui";
|
|
50
|
+
import { useControllableState } from "@radix-ui/react-use-controllable-state";
|
|
51
|
+
import React2 from "react";
|
|
53
52
|
var ATTENTION_NAME = "Attention";
|
|
54
|
-
var ATTENABLE_ATTRIBUTE = "data-attendable-id";
|
|
55
|
-
var ATTENTION_SOURCE_ATTRIBUTE = "data-is-attention-source";
|
|
56
53
|
var [AttentionContextProvider, useAttentionContext] = createContext(ATTENTION_NAME, {
|
|
57
|
-
|
|
58
|
-
attended: []
|
|
59
|
-
},
|
|
60
|
-
path: []
|
|
54
|
+
attended: /* @__PURE__ */ new Set()
|
|
61
55
|
});
|
|
62
|
-
var
|
|
63
|
-
const {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
isAncestor: false,
|
|
68
|
-
isRelated: false
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
const current = [
|
|
72
|
-
attendableId,
|
|
73
|
-
...path
|
|
74
|
-
];
|
|
75
|
-
const hasAttention = current.length === attention.attended.length && current.every((id, index) => attention.attended[index] === id);
|
|
76
|
-
const isAncestor = current.length < attention.attended.length && attention.attended.slice(-current.length).every((id, index) => current[index] === id);
|
|
77
|
-
const isRelated = attendableId === attention.attended[0];
|
|
56
|
+
var useHasAttention = (attendableId) => {
|
|
57
|
+
const { attended } = useAttentionContext(ATTENTION_NAME);
|
|
58
|
+
return attendableId ? attended.has(attendableId) : false;
|
|
59
|
+
};
|
|
60
|
+
var createAttendableAttributes = (attendableId) => {
|
|
78
61
|
return {
|
|
79
|
-
|
|
80
|
-
isAncestor,
|
|
81
|
-
isRelated
|
|
62
|
+
"data-attendable-id": attendableId
|
|
82
63
|
};
|
|
83
64
|
};
|
|
84
|
-
var useAttended = () => {
|
|
85
|
-
const { attention } = useAttentionContext(ATTENTION_NAME);
|
|
86
|
-
return attention.attended;
|
|
87
|
-
};
|
|
88
|
-
var useAttendableAttributes = (attendableId) => {
|
|
89
|
-
const { hasAttention } = useAttention(attendableId);
|
|
90
|
-
return useMemo(() => {
|
|
91
|
-
const attributes = {
|
|
92
|
-
[ATTENABLE_ATTRIBUTE]: attendableId
|
|
93
|
-
};
|
|
94
|
-
if (hasAttention) {
|
|
95
|
-
attributes[ATTENTION_SOURCE_ATTRIBUTE] = "true";
|
|
96
|
-
}
|
|
97
|
-
return attributes;
|
|
98
|
-
}, [
|
|
99
|
-
attendableId,
|
|
100
|
-
hasAttention
|
|
101
|
-
]);
|
|
102
|
-
};
|
|
103
|
-
var useAttentionPath = () => {
|
|
104
|
-
const { path } = useAttentionContext(ATTENTION_NAME);
|
|
105
|
-
return path;
|
|
106
|
-
};
|
|
107
65
|
var getAttendables = (selector, cursor, acc = []) => {
|
|
108
66
|
const closestAttendable = cursor.closest(selector);
|
|
109
67
|
if (!closestAttendable) {
|
|
@@ -123,56 +81,38 @@ var getAttendables = (selector, cursor, acc = []) => {
|
|
|
123
81
|
}
|
|
124
82
|
}
|
|
125
83
|
};
|
|
126
|
-
var
|
|
127
|
-
const
|
|
128
|
-
|
|
129
|
-
|
|
84
|
+
var AttentionProvider = ({ children, attended: propsAttended, defaultAttended, onChangeAttend }) => {
|
|
85
|
+
const [attended = /* @__PURE__ */ new Set(), setAttended] = useControllableState({
|
|
86
|
+
prop: propsAttended,
|
|
87
|
+
defaultProp: defaultAttended,
|
|
88
|
+
onChange: onChangeAttend
|
|
89
|
+
});
|
|
130
90
|
const handleFocus = (event) => {
|
|
131
91
|
const selector = [
|
|
132
92
|
"[data-attendable-id]",
|
|
133
93
|
...Array.from(document.querySelectorAll("[aria-controls]")).map((el) => `[id="${el.getAttribute("aria-controls")}"]`)
|
|
134
94
|
].join(",");
|
|
135
|
-
const
|
|
136
|
-
const next =
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
95
|
+
const nextAttended = new Set(getAttendables(selector, event.target));
|
|
96
|
+
const [prev, next] = [
|
|
97
|
+
Array.from(attended),
|
|
98
|
+
Array.from(nextAttended)
|
|
99
|
+
];
|
|
100
|
+
(prev.length !== next.length || !!prev.find((id, index) => next[index] !== id)) && setAttended(nextAttended);
|
|
141
101
|
};
|
|
142
102
|
return /* @__PURE__ */ React2.createElement(AttentionContextProvider, {
|
|
143
|
-
|
|
144
|
-
path: []
|
|
103
|
+
attended
|
|
145
104
|
}, /* @__PURE__ */ React2.createElement("div", {
|
|
146
105
|
role: "none",
|
|
147
106
|
className: "contents",
|
|
148
107
|
onFocusCapture: handleFocus
|
|
149
108
|
}, children));
|
|
150
109
|
};
|
|
151
|
-
var AttentionProvider = ({ id, children }) => {
|
|
152
|
-
const { attention, path } = useAttentionContext(ATTENTION_NAME);
|
|
153
|
-
const nextPath = useMemo(() => [
|
|
154
|
-
id,
|
|
155
|
-
...path
|
|
156
|
-
], [
|
|
157
|
-
id,
|
|
158
|
-
path
|
|
159
|
-
]);
|
|
160
|
-
return /* @__PURE__ */ React2.createElement(AttentionContextProvider, {
|
|
161
|
-
attention,
|
|
162
|
-
path: nextPath
|
|
163
|
-
}, children);
|
|
164
|
-
};
|
|
165
110
|
export {
|
|
166
|
-
ATTENABLE_ATTRIBUTE,
|
|
167
111
|
ATTENTION_NAME,
|
|
168
|
-
ATTENTION_SOURCE_ATTRIBUTE,
|
|
169
112
|
AttentionGlyph,
|
|
170
113
|
AttentionProvider,
|
|
171
|
-
|
|
172
|
-
useAttendableAttributes,
|
|
173
|
-
useAttended,
|
|
174
|
-
useAttention,
|
|
114
|
+
createAttendableAttributes,
|
|
175
115
|
useAttentionContext,
|
|
176
|
-
|
|
116
|
+
useHasAttention
|
|
177
117
|
};
|
|
178
118
|
//# sourceMappingURL=index.mjs.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
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]_&]:
|
|
5
|
-
"mappings": ";AAKA,OAAOA,SAAqCC,kBAAkB;AAG9D,SAASC,UAAU;AAEnB,IAAMC,uBACJ;AAEF,IAAMC,qBACJ;AAEF,IAAMC,cAAc,MAAA;AAClB,SACE,sBAAA,cAACC,OAAAA;IACCC,OAAM;IACNC,QAAO;IACPC,SAAQ;IACRC,WAAWN;IACXO,MAAK;IACLC,OAAM;KAEN,sBAAA,cAACC,UAAAA;IAAOC,IAAG;IAAIC,IAAG;IAAIC,GAAE;IAAML,MAAK;;AAGzC;AAEA,IAAMM,eAAe,MAAA;AACnB,SACE,sBAAA,cAACX,OAAAA;IACCC,OAAM;IACNC,QAAO;IACPC,SAAQ;IACRC,WAAWN;IACXO,MAAK;IACLC,OAAM;KAEN,sBAAA,cAACM,QAAAA;IACCC,GAAE;IACFR,MAAK;MAEP,sBAAA,cAACE,UAAAA;IAAOC,IAAG;IAAOC,IAAG;IAAIC,GAAE;IAAML,MAAK;;AAG5C;AAMO,IAAMS,iBAAiBC,2BAC5B,CAAC,EAAEC,UAAUC,YAAY,GAAGC,MAAAA,GAASC,iBAAAA;AACnC,SACE,sBAAA,cAACC,QAAAA;IAAKC,MAAK;IAAQ,GAAGH;IAAOd,WAAWkB,GAAGzB,sBAAsBoB,UAAAA;IAAaM,KAAKJ;KAChFH,aAAa,UAAU,sBAAA,cAACL,cAAAA,IAAAA,GACxBK,aAAa,SAAS,sBAAA,cAACjB,aAAAA,IAAAA,CAAAA;AAG9B,CAAA;;;ACvDF,SAASyB,qBAAmD;AAC5D,
|
|
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", "
|
|
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": ";AAKA,OAAOA,SAAqCC,kBAAkB;AAG9D,SAASC,UAAU;AAEnB,IAAMC,uBACJ;AAEF,IAAMC,qBACJ;AAEF,IAAMC,c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gBAAAQ,OAAA,cAAC/C,0BAAAA;IAAyBG;KACxB,gBAAA4C,OAAA,cAACC,OAAAA;IAAIC,MAAK;IAAOC,WAAU;IAAWC,gBAAgBpB;KACnDT,QAAAA,CAAAA;AAIT;",
|
|
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", "useControllableState", "React", "ATTENTION_NAME", "AttentionContextProvider", "useAttentionContext", "createContext", "attended", "Set", "useHasAttention", "attendableId", "has", "createAttendableAttributes", "getAttendables", "selector", "cursor", "acc", "closestAttendable", "closest", "getAttribute", "trigger", "document", "querySelector", "push", "parentElement", "AttentionProvider", "children", "propsAttended", "defaultAttended", "onChangeAttend", "setAttended", "useControllableState", "prop", "defaultProp", "onChange", "handleFocus", "event", "Array", "from", "querySelectorAll", "map", "el", "join", "nextAttended", "target", "prev", "next", "length", "find", "id", "index", "React", "div", "role", "className", "onFocusCapture"]
|
|
7
7
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"inputs":{"packages/ui/react-ui-attention/src/components/AttentionGlyph.tsx":{"bytes":
|
|
1
|
+
{"inputs":{"packages/ui/react-ui-attention/src/components/AttentionGlyph.tsx":{"bytes":7591,"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":11985,"imports":[{"path":"@radix-ui/react-context","kind":"import-statement","external":true},{"path":"@radix-ui/react-use-controllable-state","kind":"import-statement","external":true},{"path":"react","kind":"import-statement","external":true}],"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":508,"imports":[{"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":9875},"packages/ui/react-ui-attention/dist/lib/browser/index.mjs":{"imports":[{"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":"@radix-ui/react-use-controllable-state","kind":"import-statement","external":true},{"path":"react","kind":"import-statement","external":true}],"exports":["ATTENTION_NAME","AttentionGlyph","AttentionProvider","createAttendableAttributes","useAttentionContext","useHasAttention"],"entryPoint":"packages/ui/react-ui-attention/src/index.ts","inputs":{"packages/ui/react-ui-attention/src/components/AttentionGlyph.tsx":{"bytesInOutput":2345},"packages/ui/react-ui-attention/src/components/index.ts":{"bytesInOutput":0},"packages/ui/react-ui-attention/src/components/AttentionProvider.tsx":{"bytesInOutput":2302},"packages/ui/react-ui-attention/src/index.ts":{"bytesInOutput":0}},"bytes":4962}}}
|
|
@@ -1,35 +1,22 @@
|
|
|
1
1
|
import React, { type PropsWithChildren } from 'react';
|
|
2
2
|
declare const ATTENTION_NAME = "Attention";
|
|
3
|
-
declare const ATTENABLE_ATTRIBUTE = "data-attendable-id";
|
|
4
|
-
declare const ATTENTION_SOURCE_ATTRIBUTE = "data-is-attention-source";
|
|
5
|
-
type Attention = {
|
|
6
|
-
attended: string[];
|
|
7
|
-
};
|
|
8
3
|
type AttentionContextValue = {
|
|
9
|
-
|
|
10
|
-
path: string[];
|
|
4
|
+
attended: Set<string>;
|
|
11
5
|
};
|
|
12
6
|
declare const useAttentionContext: (consumerName: string) => AttentionContextValue;
|
|
13
|
-
|
|
14
|
-
hasAttention: boolean;
|
|
15
|
-
isAncestor: boolean;
|
|
16
|
-
isRelated: boolean;
|
|
17
|
-
};
|
|
18
|
-
declare const useAttention: (attendableId?: string) => UseAttention;
|
|
19
|
-
declare const useAttended: () => string[];
|
|
7
|
+
declare const useHasAttention: (attendableId?: string) => boolean;
|
|
20
8
|
/**
|
|
21
9
|
* Computes HTML element attributes to apply so the attention system can detect changes
|
|
22
10
|
* @param attendableId
|
|
23
11
|
*/
|
|
24
|
-
declare const
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
12
|
+
declare const createAttendableAttributes: (attendableId?: string) => {
|
|
13
|
+
'data-attendable-id': string | undefined;
|
|
14
|
+
};
|
|
15
|
+
declare const AttentionProvider: ({ children, attended: propsAttended, defaultAttended, onChangeAttend, }: PropsWithChildren<Partial<{
|
|
16
|
+
attended: Set<string>;
|
|
17
|
+
onChangeAttend: (nextAttended: Set<string>) => void;
|
|
18
|
+
defaultAttended: Set<string>;
|
|
29
19
|
}>>) => React.JSX.Element;
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}>) => React.JSX.Element;
|
|
33
|
-
export { RootAttentionProvider, AttentionProvider, useAttentionContext, useAttention, useAttended, useAttendableAttributes, useAttentionPath, ATTENTION_NAME, ATTENABLE_ATTRIBUTE, ATTENTION_SOURCE_ATTRIBUTE, };
|
|
34
|
-
export type { Attention, AttentionContextValue };
|
|
20
|
+
export { AttentionProvider, useAttentionContext, useHasAttention, createAttendableAttributes, ATTENTION_NAME };
|
|
21
|
+
export type { AttentionContextValue };
|
|
35
22
|
//# 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":"AAUA,OAAO,KAAK,EAAE,EAAwD,KAAK,iBAAiB,EAAY,MAAM,OAAO,CAAC;AAEtH,QAAA,MAAM,cAAc,cAAc,CAAC;AAEnC,KAAK,qBAAqB,GAAG;IAC3B,QAAQ,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;CACvB,CAAC;AAEF,QAAA,MAAiC,mBAAmB,iDAElD,CAAC;AAEH,QAAA,MAAM,eAAe,kBAAmB,MAAM,YAG7C,CAAC;AAEF;;;GAGG;AACH,QAAA,MAAM,0BAA0B,kBAAmB,MAAM;;CAExD,CAAC;AA4BF,QAAA,MAAM,iBAAiB,4EAKpB,iBAAiB,CAClB,OAAO,CAAC;IACN,QAAQ,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;IACtB,cAAc,EAAE,CAAC,YAAY,EAAE,GAAG,CAAC,MAAM,CAAC,KAAK,IAAI,CAAC;IACpD,eAAe,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;CAC9B,CAAC,CACH,sBAyBA,CAAC;AAEF,OAAO,EAAE,iBAAiB,EAAE,mBAAmB,EAAE,eAAe,EAAE,0BAA0B,EAAE,cAAc,EAAE,CAAC;AAE/G,YAAY,EAAE,qBAAqB,EAAE,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/react-ui-attention",
|
|
3
|
-
"version": "0.6.13
|
|
3
|
+
"version": "0.6.13",
|
|
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",
|
|
@@ -28,10 +28,9 @@
|
|
|
28
28
|
"@radix-ui/react-primitive": "^1.0.2",
|
|
29
29
|
"@radix-ui/react-slot": "^1.0.1",
|
|
30
30
|
"@radix-ui/react-use-controllable-state": "^1.0.0",
|
|
31
|
-
"@dxos/
|
|
32
|
-
"@dxos/
|
|
33
|
-
"@dxos/react-ui": "0.6.13
|
|
34
|
-
"@dxos/util": "0.6.13-main.ed424a1"
|
|
31
|
+
"@dxos/react-ui": "0.6.13",
|
|
32
|
+
"@dxos/util": "0.6.13",
|
|
33
|
+
"@dxos/react-ui-theme": "0.6.13"
|
|
35
34
|
},
|
|
36
35
|
"devDependencies": {
|
|
37
36
|
"@phosphor-icons/react": "^2.1.5",
|
|
@@ -39,15 +38,15 @@
|
|
|
39
38
|
"@types/react-dom": "~18.2.0",
|
|
40
39
|
"react": "~18.2.0",
|
|
41
40
|
"react-dom": "~18.2.0",
|
|
42
|
-
"vite": "5.4
|
|
43
|
-
"@dxos/
|
|
44
|
-
"@dxos/
|
|
45
|
-
"@dxos/
|
|
41
|
+
"vite": "^5.3.4",
|
|
42
|
+
"@dxos/random": "0.6.13",
|
|
43
|
+
"@dxos/storybook-utils": "0.6.13",
|
|
44
|
+
"@dxos/app-graph": "0.6.13"
|
|
46
45
|
},
|
|
47
46
|
"peerDependencies": {
|
|
48
47
|
"@phosphor-icons/react": "^2.1.5",
|
|
49
|
-
"react": "
|
|
50
|
-
"react-dom": "
|
|
48
|
+
"react": "^18.0.0",
|
|
49
|
+
"react-dom": "^18.0.0"
|
|
51
50
|
},
|
|
52
51
|
"publishConfig": {
|
|
53
52
|
"access": "public"
|
|
@@ -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]_&]:fg-inverse [[aria-current][data-attention=true]_&]:bg-accentSurface [[aria-current][data-attention=true]_&]:fg-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';
|
|
@@ -6,80 +6,31 @@
|
|
|
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 {
|
|
12
|
-
import { useDefaultValue } from '@dxos/react-ui';
|
|
9
|
+
import { useControllableState } from '@radix-ui/react-use-controllable-state';
|
|
10
|
+
// eslint-disable-next-line unused-imports/no-unused-imports
|
|
11
|
+
import React, { ComponentProps, type FocusEvent, type HTMLAttributes, type PropsWithChildren, useState } from 'react';
|
|
13
12
|
|
|
14
13
|
const ATTENTION_NAME = 'Attention';
|
|
15
|
-
const ATTENABLE_ATTRIBUTE = 'data-attendable-id';
|
|
16
|
-
const ATTENTION_SOURCE_ATTRIBUTE = 'data-is-attention-source';
|
|
17
|
-
|
|
18
|
-
type Attention = {
|
|
19
|
-
attended: string[];
|
|
20
|
-
};
|
|
21
14
|
|
|
22
15
|
type AttentionContextValue = {
|
|
23
|
-
|
|
24
|
-
path: string[];
|
|
16
|
+
attended: Set<string>;
|
|
25
17
|
};
|
|
26
18
|
|
|
27
19
|
const [AttentionContextProvider, useAttentionContext] = createContext<AttentionContextValue>(ATTENTION_NAME, {
|
|
28
|
-
|
|
29
|
-
path: [],
|
|
20
|
+
attended: new Set(),
|
|
30
21
|
});
|
|
31
22
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
isRelated: boolean;
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
const useAttention = (attendableId?: string): UseAttention => {
|
|
39
|
-
const { attention, path } = useAttentionContext(ATTENTION_NAME);
|
|
40
|
-
if (!attendableId) {
|
|
41
|
-
return { hasAttention: false, isAncestor: false, isRelated: false };
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
const current = [attendableId, ...path];
|
|
45
|
-
const hasAttention =
|
|
46
|
-
current.length === attention.attended.length && current.every((id, index) => attention.attended[index] === id);
|
|
47
|
-
|
|
48
|
-
const isAncestor =
|
|
49
|
-
current.length < attention.attended.length &&
|
|
50
|
-
attention.attended.slice(-current.length).every((id, index) => current[index] === id);
|
|
51
|
-
|
|
52
|
-
const isRelated = attendableId === attention.attended[0];
|
|
53
|
-
|
|
54
|
-
return { hasAttention, isAncestor, isRelated };
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
const useAttended = () => {
|
|
58
|
-
const { attention } = useAttentionContext(ATTENTION_NAME);
|
|
59
|
-
return attention.attended;
|
|
23
|
+
const useHasAttention = (attendableId?: string) => {
|
|
24
|
+
const { attended } = useAttentionContext(ATTENTION_NAME);
|
|
25
|
+
return attendableId ? attended.has(attendableId) : false;
|
|
60
26
|
};
|
|
61
27
|
|
|
62
28
|
/**
|
|
63
29
|
* Computes HTML element attributes to apply so the attention system can detect changes
|
|
64
30
|
* @param attendableId
|
|
65
31
|
*/
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
return useMemo(() => {
|
|
70
|
-
const attributes: Record<string, string | undefined> = { [ATTENABLE_ATTRIBUTE]: attendableId };
|
|
71
|
-
|
|
72
|
-
if (hasAttention) {
|
|
73
|
-
attributes[ATTENTION_SOURCE_ATTRIBUTE] = 'true';
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
return attributes;
|
|
77
|
-
}, [attendableId, hasAttention]);
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
const useAttentionPath = () => {
|
|
81
|
-
const { path } = useAttentionContext(ATTENTION_NAME);
|
|
82
|
-
return path;
|
|
32
|
+
const createAttendableAttributes = (attendableId?: string) => {
|
|
33
|
+
return { 'data-attendable-id': attendableId };
|
|
83
34
|
};
|
|
84
35
|
|
|
85
36
|
/**
|
|
@@ -108,17 +59,23 @@ const getAttendables = (selector: string, cursor: Element, acc: string[] = []):
|
|
|
108
59
|
}
|
|
109
60
|
};
|
|
110
61
|
|
|
111
|
-
const
|
|
62
|
+
const AttentionProvider = ({
|
|
112
63
|
children,
|
|
113
|
-
|
|
114
|
-
|
|
64
|
+
attended: propsAttended,
|
|
65
|
+
defaultAttended,
|
|
66
|
+
onChangeAttend,
|
|
115
67
|
}: PropsWithChildren<
|
|
116
68
|
Partial<{
|
|
117
|
-
|
|
118
|
-
|
|
69
|
+
attended: Set<string>;
|
|
70
|
+
onChangeAttend: (nextAttended: Set<string>) => void;
|
|
71
|
+
defaultAttended: Set<string>;
|
|
119
72
|
}>
|
|
120
73
|
>) => {
|
|
121
|
-
const
|
|
74
|
+
const [attended = new Set(), setAttended] = useControllableState<Set<string>>({
|
|
75
|
+
prop: propsAttended,
|
|
76
|
+
defaultProp: defaultAttended,
|
|
77
|
+
onChange: onChangeAttend,
|
|
78
|
+
});
|
|
122
79
|
const handleFocus = (event: FocusEvent) => {
|
|
123
80
|
const selector = [
|
|
124
81
|
'[data-attendable-id]',
|
|
@@ -126,18 +83,13 @@ const RootAttentionProvider = ({
|
|
|
126
83
|
(el) => `[id="${el.getAttribute('aria-controls')}"]`,
|
|
127
84
|
),
|
|
128
85
|
].join(',');
|
|
129
|
-
const
|
|
130
|
-
const next = Array.from(
|
|
131
|
-
//
|
|
132
|
-
|
|
133
|
-
// Only update state if the result is different and not empty.
|
|
134
|
-
if (next.length > 0 && (prev.length !== next.length || !!prev.find((id, index) => next[index] !== id))) {
|
|
135
|
-
attention.attended = next;
|
|
136
|
-
onChange?.(next);
|
|
137
|
-
}
|
|
86
|
+
const nextAttended = new Set(getAttendables(selector, event.target));
|
|
87
|
+
const [prev, next] = [Array.from(attended), Array.from(nextAttended)];
|
|
88
|
+
// Only update state if the result is different.
|
|
89
|
+
(prev.length !== next.length || !!prev.find((id, index) => next[index] !== id)) && setAttended(nextAttended);
|
|
138
90
|
};
|
|
139
91
|
return (
|
|
140
|
-
<AttentionContextProvider
|
|
92
|
+
<AttentionContextProvider attended={attended}>
|
|
141
93
|
<div role='none' className='contents' onFocusCapture={handleFocus}>
|
|
142
94
|
{children}
|
|
143
95
|
</div>
|
|
@@ -145,28 +97,6 @@ const RootAttentionProvider = ({
|
|
|
145
97
|
);
|
|
146
98
|
};
|
|
147
99
|
|
|
148
|
-
|
|
149
|
-
const { attention, path } = useAttentionContext(ATTENTION_NAME);
|
|
150
|
-
const nextPath = useMemo(() => [id, ...path], [id, path]);
|
|
151
|
-
|
|
152
|
-
return (
|
|
153
|
-
<AttentionContextProvider attention={attention} path={nextPath}>
|
|
154
|
-
{children}
|
|
155
|
-
</AttentionContextProvider>
|
|
156
|
-
);
|
|
157
|
-
};
|
|
158
|
-
|
|
159
|
-
export {
|
|
160
|
-
RootAttentionProvider,
|
|
161
|
-
AttentionProvider,
|
|
162
|
-
useAttentionContext,
|
|
163
|
-
useAttention,
|
|
164
|
-
useAttended,
|
|
165
|
-
useAttendableAttributes,
|
|
166
|
-
useAttentionPath,
|
|
167
|
-
ATTENTION_NAME,
|
|
168
|
-
ATTENABLE_ATTRIBUTE,
|
|
169
|
-
ATTENTION_SOURCE_ATTRIBUTE,
|
|
170
|
-
};
|
|
100
|
+
export { AttentionProvider, useAttentionContext, useHasAttention, createAttendableAttributes, ATTENTION_NAME };
|
|
171
101
|
|
|
172
|
-
export type {
|
|
102
|
+
export type { AttentionContextValue };
|