@dxos/react-ui-attention 0.6.12-main.f9d0246 → 0.6.12-staging.e11e696

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.
@@ -47,31 +47,62 @@ 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 { useControllableState } from "@radix-ui/react-use-controllable-state";
51
50
  import React2, { useMemo } from "react";
51
+ import { create } from "@dxos/echo-schema";
52
+ import { useDefaultValue } from "@dxos/react-ui";
52
53
  var ATTENTION_NAME = "Attention";
54
+ var ATTENABLE_ATTRIBUTE = "data-attendable-id";
55
+ var ATTENTION_SOURCE_ATTRIBUTE = "data-is-attention-source";
53
56
  var [AttentionContextProvider, useAttentionContext] = createContext(ATTENTION_NAME, {
54
- attended: /* @__PURE__ */ new Set()
57
+ attention: {
58
+ attended: []
59
+ },
60
+ path: []
55
61
  });
56
- var useHasAttention = (attendableId) => {
57
- const { attended } = useAttentionContext(ATTENTION_NAME);
58
- return attendableId ? attended.has(attendableId) : false;
62
+ var useAttention = (attendableId) => {
63
+ const { attention, path } = useAttentionContext(ATTENTION_NAME);
64
+ if (!attendableId) {
65
+ return {
66
+ hasAttention: false,
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];
78
+ return {
79
+ hasAttention,
80
+ isAncestor,
81
+ isRelated
82
+ };
59
83
  };
60
- var useIsDirectlyAttended = (attendableId) => {
61
- const { attended } = useAttentionContext(ATTENTION_NAME);
62
- const attendedArray = Array.from(attended);
63
- return attendedArray.length === 1 && attendedArray[0] === attendableId;
84
+ var useAttended = () => {
85
+ const { attention } = useAttentionContext(ATTENTION_NAME);
86
+ return attention.attended;
64
87
  };
65
- var useAttendedIds = () => {
66
- const { attended } = useAttentionContext(ATTENTION_NAME);
67
- return useMemo(() => Array.from(attended), [
68
- attended
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
69
101
  ]);
70
102
  };
71
- var createAttendableAttributes = (attendableId) => {
72
- return {
73
- "data-attendable-id": attendableId
74
- };
103
+ var useAttentionPath = () => {
104
+ const { path } = useAttentionContext(ATTENTION_NAME);
105
+ return path;
75
106
  };
76
107
  var getAttendables = (selector, cursor, acc = []) => {
77
108
  const closestAttendable = cursor.closest(selector);
@@ -92,42 +123,56 @@ var getAttendables = (selector, cursor, acc = []) => {
92
123
  }
93
124
  }
94
125
  };
95
- var AttentionProvider = ({ children, attended: propsAttended, defaultAttended, onChangeAttend }) => {
96
- const [attended = /* @__PURE__ */ new Set(), setAttended] = useControllableState({
97
- prop: propsAttended,
98
- defaultProp: defaultAttended,
99
- onChange: onChangeAttend
100
- });
126
+ var RootAttentionProvider = ({ children, attention: propsAttention, onChange }) => {
127
+ const attention = useDefaultValue(propsAttention, () => create({
128
+ attended: []
129
+ }));
101
130
  const handleFocus = (event) => {
102
131
  const selector = [
103
132
  "[data-attendable-id]",
104
133
  ...Array.from(document.querySelectorAll("[aria-controls]")).map((el) => `[id="${el.getAttribute("aria-controls")}"]`)
105
134
  ].join(",");
106
- const nextAttended = new Set(getAttendables(selector, event.target));
107
- const [prev, next] = [
108
- Array.from(attended),
109
- Array.from(nextAttended)
110
- ];
135
+ const prev = attention.attended;
136
+ const next = Array.from(new Set(getAttendables(selector, event.target)));
111
137
  if (next.length > 0 && (prev.length !== next.length || !!prev.find((id, index) => next[index] !== id))) {
112
- setAttended(nextAttended);
138
+ attention.attended = next;
139
+ onChange?.(next);
113
140
  }
114
141
  };
115
142
  return /* @__PURE__ */ React2.createElement(AttentionContextProvider, {
116
- attended
143
+ attention,
144
+ path: []
117
145
  }, /* @__PURE__ */ React2.createElement("div", {
118
146
  role: "none",
119
147
  className: "contents",
120
148
  onFocusCapture: handleFocus
121
149
  }, children));
122
150
  };
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
+ };
123
165
  export {
166
+ ATTENABLE_ATTRIBUTE,
124
167
  ATTENTION_NAME,
168
+ ATTENTION_SOURCE_ATTRIBUTE,
125
169
  AttentionGlyph,
126
170
  AttentionProvider,
127
- createAttendableAttributes,
128
- useAttendedIds,
171
+ RootAttentionProvider,
172
+ useAttendableAttributes,
173
+ useAttended,
174
+ useAttention,
129
175
  useAttentionContext,
130
- useHasAttention,
131
- useIsDirectlyAttended
176
+ useAttentionPath
132
177
  };
133
178
  //# 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]_&]: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 { useControllableState } from '@radix-ui/react-use-controllable-state';\n// eslint-disable-next-line unused-imports/no-unused-imports\nimport React, { type FocusEvent, type PropsWithChildren, useMemo } 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\nconst useIsDirectlyAttended = (attendableId?: string) => {\n const { attended } = useAttentionContext(ATTENTION_NAME);\n const attendedArray = Array.from(attended);\n return attendedArray.length === 1 && attendedArray[0] === attendableId;\n};\n\nconst useAttendedIds = () => {\n const { attended } = useAttentionContext(ATTENTION_NAME);\n return useMemo(() => Array.from(attended), [attended]);\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 // 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 setAttended(nextAttended);\n }\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 {\n AttentionProvider,\n useAttentionContext,\n useHasAttention,\n useIsDirectlyAttended,\n useAttendedIds,\n createAttendableAttributes,\n ATTENTION_NAME,\n};\n\nexport type { AttentionContextValue };\n"],
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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O,MAAMC,KAAKiC,YAAAA;;AAIvD,QAAIG,KAAKnC,SAAS,MAAMkC,KAAKlC,WAAWmC,KAAKnC,UAAU,CAAC,CAACkC,KAAKE,KAAK,CAACC,IAAIC,UAAUH,KAAKG,KAAAA,MAAWD,EAAAA,IAAM;AACtGhB,kBAAYW,YAAAA;IACd;EACF;AACA,SACE,gBAAAO,OAAA,cAACnD,0BAAAA;IAAyBG;KACxB,gBAAAgD,OAAA,cAACC,OAAAA;IAAIC,MAAK;IAAOC,WAAU;IAAWC,gBAAgBjB;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", "useMemo", "ATTENTION_NAME", "AttentionContextProvider", "useAttentionContext", "createContext", "attended", "Set", "useHasAttention", "attendableId", "has", "useIsDirectlyAttended", "attendedArray", "Array", "from", "length", "useAttendedIds", "useMemo", "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", "querySelectorAll", "map", "el", "join", "nextAttended", "target", "prev", "next", "find", "id", "index", "React", "div", "role", "className", "onFocusCapture"]
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]_&]: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 { create } from '@dxos/echo-schema';\nimport { useDefaultValue } from '@dxos/react-ui';\n\nconst ATTENTION_NAME = 'Attention';\nconst ATTENABLE_ATTRIBUTE = 'data-attendable-id';\nconst ATTENTION_SOURCE_ATTRIBUTE = 'data-is-attention-source';\n\ntype Attention = {\n attended: string[];\n};\n\ntype AttentionContextValue = {\n attention: Attention;\n path: string[];\n};\n\nconst [AttentionContextProvider, useAttentionContext] = createContext<AttentionContextValue>(ATTENTION_NAME, {\n attention: { attended: [] },\n path: [],\n});\n\ntype UseAttention = {\n hasAttention: boolean;\n isAncestor: boolean;\n isRelated: boolean;\n};\n\nconst useAttention = (attendableId?: string): UseAttention => {\n const { attention, path } = useAttentionContext(ATTENTION_NAME);\n if (!attendableId) {\n return { hasAttention: false, isAncestor: false, isRelated: false };\n }\n\n const current = [attendableId, ...path];\n const hasAttention =\n current.length === attention.attended.length && current.every((id, index) => attention.attended[index] === id);\n\n const isAncestor =\n current.length < attention.attended.length &&\n attention.attended.slice(-current.length).every((id, index) => current[index] === id);\n\n const isRelated = attendableId === attention.attended[0];\n\n return { hasAttention, isAncestor, isRelated };\n};\n\nconst useAttended = () => {\n const { attention } = useAttentionContext(ATTENTION_NAME);\n return attention.attended;\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\n return useMemo(() => {\n const attributes: Record<string, string | undefined> = { [ATTENABLE_ATTRIBUTE]: attendableId };\n\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\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 RootAttentionProvider = ({\n children,\n attention: propsAttention,\n onChange,\n}: PropsWithChildren<\n Partial<{\n attention: Attention;\n onChange: (nextAttended: string[]) => void;\n }>\n>) => {\n const attention = useDefaultValue(propsAttention, () => create<Attention>({ attended: [] }));\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.attended;\n const next = Array.from(new Set(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.attended = 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\nexport type { Attention, AttentionContextValue };\n"],
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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MoC,KAAKpC,WAAWqC,KAAKrC,UAAU,CAAC,CAACoC,KAAKI,KAAK,CAACtC,IAAIC,UAAUkC,KAAKlC,KAAAA,MAAWD,EAAAA,IAAM;AACtGX,gBAAUC,WAAW6C;AACrBZ,iBAAWY,IAAAA;IACb;EACF;AACA,SACE,gBAAAI,OAAA,cAACrD,0BAAAA;IAAyBG;IAAsBE,MAAM,CAAA;KACpD,gBAAAgD,OAAA,cAACC,OAAAA;IAAIC,MAAK;IAAOC,WAAU;IAAWC,gBAAgBjB;KACnDL,QAAAA,CAAAA;AAIT;AAEA,IAAMuB,oBAAoB,CAAC,EAAE5C,IAAIqB,SAAQ,MAAqC;AAC5E,QAAM,EAAEhC,WAAWE,KAAI,IAAKJ,oBAAoBJ,cAAAA;AAChD,QAAM8D,WAAWxC,QAAQ,MAAM;IAACL;OAAOT;KAAO;IAACS;IAAIT;GAAK;AAExD,SACE,gBAAAgD,OAAA,cAACrD,0BAAAA;IAAyBG;IAAsBE,MAAMsD;KACnDxB,QAAAA;AAGP;",
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", "React", "useMemo", "create", "useDefaultValue", "ATTENTION_NAME", "ATTENABLE_ATTRIBUTE", "ATTENTION_SOURCE_ATTRIBUTE", "AttentionContextProvider", "useAttentionContext", "createContext", "attention", "attended", "path", "useAttention", "attendableId", "hasAttention", "isAncestor", "isRelated", "current", "length", "every", "id", "index", "slice", "useAttended", "useAttendableAttributes", "useMemo", "attributes", "useAttentionPath", "getAttendables", "selector", "cursor", "acc", "closestAttendable", "closest", "getAttribute", "trigger", "document", "querySelector", "push", "parentElement", "RootAttentionProvider", "children", "propsAttention", "onChange", "useDefaultValue", "create", "handleFocus", "event", "Array", "from", "querySelectorAll", "map", "el", "join", "prev", "next", "Set", "target", "find", "React", "div", "role", "className", "onFocusCapture", "AttentionProvider", "nextPath"]
7
7
  }
@@ -1 +1 @@
1
- {"inputs":{"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":14373,"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":11029},"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","useAttendedIds","useAttentionContext","useHasAttention","useIsDirectlyAttended"],"entryPoint":"packages/ui/react-ui-attention/src/index.ts","inputs":{"packages/ui/react-ui-attention/src/components/AttentionGlyph.tsx":{"bytesInOutput":2349},"packages/ui/react-ui-attention/src/components/index.ts":{"bytesInOutput":0},"packages/ui/react-ui-attention/src/components/AttentionProvider.tsx":{"bytesInOutput":2739},"packages/ui/react-ui-attention/src/index.ts":{"bytesInOutput":0}},"bytes":5446}}}
1
+ {"inputs":{"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":18367,"imports":[{"path":"@radix-ui/react-context","kind":"import-statement","external":true},{"path":"react","kind":"import-statement","external":true},{"path":"@dxos/echo-schema","kind":"import-statement","external":true},{"path":"@dxos/react-ui","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":13037},"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":"react","kind":"import-statement","external":true},{"path":"@dxos/echo-schema","kind":"import-statement","external":true},{"path":"@dxos/react-ui","kind":"import-statement","external":true}],"exports":["ATTENABLE_ATTRIBUTE","ATTENTION_NAME","ATTENTION_SOURCE_ATTRIBUTE","AttentionGlyph","AttentionProvider","RootAttentionProvider","useAttendableAttributes","useAttended","useAttention","useAttentionContext","useAttentionPath"],"entryPoint":"packages/ui/react-ui-attention/src/index.ts","inputs":{"packages/ui/react-ui-attention/src/components/AttentionGlyph.tsx":{"bytesInOutput":2349},"packages/ui/react-ui-attention/src/components/index.ts":{"bytesInOutput":0},"packages/ui/react-ui-attention/src/components/AttentionProvider.tsx":{"bytesInOutput":3703},"packages/ui/react-ui-attention/src/index.ts":{"bytesInOutput":0}},"bytes":6474}}}
@@ -1,24 +1,35 @@
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
+ };
3
8
  type AttentionContextValue = {
4
- attended: Set<string>;
9
+ attention: Attention;
10
+ path: string[];
5
11
  };
6
12
  declare const useAttentionContext: (consumerName: string) => AttentionContextValue;
7
- declare const useHasAttention: (attendableId?: string) => boolean;
8
- declare const useIsDirectlyAttended: (attendableId?: string) => boolean;
9
- declare const useAttendedIds: () => string[];
13
+ type UseAttention = {
14
+ hasAttention: boolean;
15
+ isAncestor: boolean;
16
+ isRelated: boolean;
17
+ };
18
+ declare const useAttention: (attendableId?: string) => UseAttention;
19
+ declare const useAttended: () => string[];
10
20
  /**
11
21
  * Computes HTML element attributes to apply so the attention system can detect changes
12
22
  * @param attendableId
13
23
  */
14
- declare const createAttendableAttributes: (attendableId?: string) => {
15
- 'data-attendable-id': string | undefined;
16
- };
17
- declare const AttentionProvider: ({ children, attended: propsAttended, defaultAttended, onChangeAttend, }: PropsWithChildren<Partial<{
18
- attended: Set<string>;
19
- onChangeAttend: (nextAttended: Set<string>) => void;
20
- defaultAttended: Set<string>;
24
+ declare const useAttendableAttributes: (attendableId?: string) => Record<string, string | undefined>;
25
+ declare const useAttentionPath: () => string[];
26
+ declare const RootAttentionProvider: ({ children, attention: propsAttention, onChange, }: PropsWithChildren<Partial<{
27
+ attention: Attention;
28
+ onChange: (nextAttended: string[]) => void;
21
29
  }>>) => React.JSX.Element;
22
- export { AttentionProvider, useAttentionContext, useHasAttention, useIsDirectlyAttended, useAttendedIds, createAttendableAttributes, ATTENTION_NAME, };
23
- export type { AttentionContextValue };
30
+ declare const AttentionProvider: ({ id, children }: PropsWithChildren<{
31
+ id: string;
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 };
24
35
  //# sourceMappingURL=AttentionProvider.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"AttentionProvider.d.ts","sourceRoot":"","sources":["../../../../src/components/AttentionProvider.tsx"],"names":[],"mappings":"AAUA,OAAO,KAAK,EAAE,EAAmB,KAAK,iBAAiB,EAAW,MAAM,OAAO,CAAC;AAEhF,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,QAAA,MAAM,qBAAqB,kBAAmB,MAAM,YAInD,CAAC;AAEF,QAAA,MAAM,cAAc,gBAGnB,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,sBA6BA,CAAC;AAEF,OAAO,EACL,iBAAiB,EACj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,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;AAEF,YAAY,EAAE,SAAS,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.12-main.f9d0246",
3
+ "version": "0.6.12-staging.e11e696",
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,9 +28,10 @@
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/react-ui-theme": "0.6.12-main.f9d0246",
32
- "@dxos/react-ui": "0.6.12-main.f9d0246",
33
- "@dxos/util": "0.6.12-main.f9d0246"
31
+ "@dxos/echo-schema": "0.6.12-staging.e11e696",
32
+ "@dxos/react-ui": "0.6.12-staging.e11e696",
33
+ "@dxos/react-ui-theme": "0.6.12-staging.e11e696",
34
+ "@dxos/util": "0.6.12-staging.e11e696"
34
35
  },
35
36
  "devDependencies": {
36
37
  "@phosphor-icons/react": "^2.1.5",
@@ -39,9 +40,9 @@
39
40
  "react": "~18.2.0",
40
41
  "react-dom": "~18.2.0",
41
42
  "vite": "5.4.7",
42
- "@dxos/random": "0.6.12-main.f9d0246",
43
- "@dxos/app-graph": "0.6.12-main.f9d0246",
44
- "@dxos/storybook-utils": "0.6.12-main.f9d0246"
43
+ "@dxos/app-graph": "0.6.12-staging.e11e696",
44
+ "@dxos/random": "0.6.12-staging.e11e696",
45
+ "@dxos/storybook-utils": "0.6.12-staging.e11e696"
45
46
  },
46
47
  "peerDependencies": {
47
48
  "@phosphor-icons/react": "^2.1.5",
@@ -6,42 +6,80 @@
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 { useControllableState } from '@radix-ui/react-use-controllable-state';
10
- // eslint-disable-next-line unused-imports/no-unused-imports
11
- import React, { type FocusEvent, type PropsWithChildren, useMemo } from 'react';
9
+ import React, { useMemo, type FocusEvent, type PropsWithChildren } from 'react';
10
+
11
+ import { create } from '@dxos/echo-schema';
12
+ import { useDefaultValue } from '@dxos/react-ui';
12
13
 
13
14
  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
+ };
14
21
 
15
22
  type AttentionContextValue = {
16
- attended: Set<string>;
23
+ attention: Attention;
24
+ path: string[];
17
25
  };
18
26
 
19
27
  const [AttentionContextProvider, useAttentionContext] = createContext<AttentionContextValue>(ATTENTION_NAME, {
20
- attended: new Set(),
28
+ attention: { attended: [] },
29
+ path: [],
21
30
  });
22
31
 
23
- const useHasAttention = (attendableId?: string) => {
24
- const { attended } = useAttentionContext(ATTENTION_NAME);
25
- return attendableId ? attended.has(attendableId) : false;
32
+ type UseAttention = {
33
+ hasAttention: boolean;
34
+ isAncestor: boolean;
35
+ isRelated: boolean;
26
36
  };
27
37
 
28
- const useIsDirectlyAttended = (attendableId?: string) => {
29
- const { attended } = useAttentionContext(ATTENTION_NAME);
30
- const attendedArray = Array.from(attended);
31
- return attendedArray.length === 1 && attendedArray[0] === attendableId;
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 };
32
55
  };
33
56
 
34
- const useAttendedIds = () => {
35
- const { attended } = useAttentionContext(ATTENTION_NAME);
36
- return useMemo(() => Array.from(attended), [attended]);
57
+ const useAttended = () => {
58
+ const { attention } = useAttentionContext(ATTENTION_NAME);
59
+ return attention.attended;
37
60
  };
38
61
 
39
62
  /**
40
63
  * Computes HTML element attributes to apply so the attention system can detect changes
41
64
  * @param attendableId
42
65
  */
43
- const createAttendableAttributes = (attendableId?: string) => {
44
- return { 'data-attendable-id': attendableId };
66
+ const useAttendableAttributes = (attendableId?: string) => {
67
+ const { hasAttention } = useAttention(attendableId);
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;
45
83
  };
46
84
 
47
85
  /**
@@ -70,23 +108,17 @@ const getAttendables = (selector: string, cursor: Element, acc: string[] = []):
70
108
  }
71
109
  };
72
110
 
73
- const AttentionProvider = ({
111
+ const RootAttentionProvider = ({
74
112
  children,
75
- attended: propsAttended,
76
- defaultAttended,
77
- onChangeAttend,
113
+ attention: propsAttention,
114
+ onChange,
78
115
  }: PropsWithChildren<
79
116
  Partial<{
80
- attended: Set<string>;
81
- onChangeAttend: (nextAttended: Set<string>) => void;
82
- defaultAttended: Set<string>;
117
+ attention: Attention;
118
+ onChange: (nextAttended: string[]) => void;
83
119
  }>
84
120
  >) => {
85
- const [attended = new Set(), setAttended] = useControllableState<Set<string>>({
86
- prop: propsAttended,
87
- defaultProp: defaultAttended,
88
- onChange: onChangeAttend,
89
- });
121
+ const attention = useDefaultValue(propsAttention, () => create<Attention>({ attended: [] }));
90
122
  const handleFocus = (event: FocusEvent) => {
91
123
  const selector = [
92
124
  '[data-attendable-id]',
@@ -94,17 +126,18 @@ const AttentionProvider = ({
94
126
  (el) => `[id="${el.getAttribute('aria-controls')}"]`,
95
127
  ),
96
128
  ].join(',');
97
- const nextAttended = new Set(getAttendables(selector, event.target));
98
- const [prev, next] = [Array.from(attended), Array.from(nextAttended)];
129
+ const prev = attention.attended;
130
+ const next = Array.from(new Set(getAttendables(selector, event.target)));
99
131
  // TODO(wittjosiah): Not allowing empty state means that the attended item is not strictly guaranteed to be in the DOM.
100
132
  // Currently this depends on the deck in order to ensure that when the attended item is removed something else is attended.
101
133
  // Only update state if the result is different and not empty.
102
134
  if (next.length > 0 && (prev.length !== next.length || !!prev.find((id, index) => next[index] !== id))) {
103
- setAttended(nextAttended);
135
+ attention.attended = next;
136
+ onChange?.(next);
104
137
  }
105
138
  };
106
139
  return (
107
- <AttentionContextProvider attended={attended}>
140
+ <AttentionContextProvider attention={attention} path={[]}>
108
141
  <div role='none' className='contents' onFocusCapture={handleFocus}>
109
142
  {children}
110
143
  </div>
@@ -112,14 +145,28 @@ const AttentionProvider = ({
112
145
  );
113
146
  };
114
147
 
148
+ const AttentionProvider = ({ id, children }: PropsWithChildren<{ id: string }>) => {
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
+
115
159
  export {
160
+ RootAttentionProvider,
116
161
  AttentionProvider,
117
162
  useAttentionContext,
118
- useHasAttention,
119
- useIsDirectlyAttended,
120
- useAttendedIds,
121
- createAttendableAttributes,
163
+ useAttention,
164
+ useAttended,
165
+ useAttendableAttributes,
166
+ useAttentionPath,
122
167
  ATTENTION_NAME,
168
+ ATTENABLE_ATTRIBUTE,
169
+ ATTENTION_SOURCE_ATTRIBUTE,
123
170
  };
124
171
 
125
- export type { AttentionContextValue };
172
+ export type { Attention, AttentionContextValue };