@dxos/react-ui-attention 0.7.5-staging.b81e783 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/browser/{chunk-KXRLXG2Q.mjs → chunk-CRGQHPBS.mjs} +17 -17
- package/dist/lib/browser/{chunk-KXRLXG2Q.mjs.map → chunk-CRGQHPBS.mjs.map} +3 -3
- package/dist/lib/browser/index.mjs +1 -1
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +1 -1
- package/dist/lib/node/{chunk-RYXAUIEJ.cjs → chunk-P7OLU2DK.cjs} +21 -21
- package/dist/lib/node/{chunk-RYXAUIEJ.cjs.map → chunk-P7OLU2DK.cjs.map} +3 -3
- package/dist/lib/node/index.cjs +21 -21
- package/dist/lib/node/index.cjs.map +1 -1
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node/testing/index.cjs +2 -2
- package/dist/lib/node-esm/{chunk-RUECHK2Y.mjs → chunk-2RO7IZV4.mjs} +17 -17
- package/dist/lib/node-esm/{chunk-RUECHK2Y.mjs.map → chunk-2RO7IZV4.mjs.map} +3 -3
- package/dist/lib/node-esm/index.mjs +1 -1
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/testing/index.mjs +1 -1
- package/dist/types/src/components/AttentionGlyph.stories.d.ts +1 -1
- package/dist/types/src/components/AttentionProvider.stories.d.ts +1 -1
- package/dist/types/src/components/SelectionProvider.d.ts +10 -4
- package/dist/types/src/components/SelectionProvider.d.ts.map +1 -1
- package/dist/types/src/selection.d.ts.map +1 -1
- package/package.json +11 -11
- package/src/components/SelectionProvider.tsx +25 -17
- package/src/selection.ts +1 -0
|
@@ -3,8 +3,7 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
import { createContext } from '@radix-ui/react-context';
|
|
6
|
-
import { useCallback, type PropsWithChildren } from 'react';
|
|
7
|
-
import React from 'react';
|
|
6
|
+
import React, { useCallback, useMemo, type PropsWithChildren } from 'react';
|
|
8
7
|
|
|
9
8
|
import { useDefaultValue } from '@dxos/react-ui';
|
|
10
9
|
|
|
@@ -20,6 +19,11 @@ const [SelectionContextProvider, useSelectionContext] = createContext<SelectionC
|
|
|
20
19
|
selection: new SelectionManager(),
|
|
21
20
|
});
|
|
22
21
|
|
|
22
|
+
/**
|
|
23
|
+
* Manages selection state across the app for multiple contexts.
|
|
24
|
+
*/
|
|
25
|
+
// TODO(burdon): Review/remove this.
|
|
26
|
+
// TODO(burdon): When is the selection removed?
|
|
23
27
|
export const SelectionProvider = ({
|
|
24
28
|
children,
|
|
25
29
|
selection: propsSelection,
|
|
@@ -28,40 +32,44 @@ export const SelectionProvider = ({
|
|
|
28
32
|
return <SelectionContextProvider selection={selection}>{children}</SelectionContextProvider>;
|
|
29
33
|
};
|
|
30
34
|
|
|
31
|
-
|
|
35
|
+
const emptySet = new Set<string>();
|
|
36
|
+
|
|
37
|
+
// TODO(burdon): Return array?
|
|
38
|
+
export const useSelectedItems = (contextId?: string): Set<string> => {
|
|
32
39
|
const { selection } = useSelectionContext(SELECTION_NAME);
|
|
33
|
-
return selection.getSelection(contextId);
|
|
40
|
+
return contextId ? selection.getSelection(contextId) : emptySet;
|
|
34
41
|
};
|
|
35
42
|
|
|
36
|
-
|
|
43
|
+
/**
|
|
44
|
+
* Provides functions to manage the selection state for multiple contexts.
|
|
45
|
+
*/
|
|
46
|
+
export const useSelectionActions = (contextIds: string[]) => {
|
|
47
|
+
const stableContextIds = useMemo(() => contextIds, [JSON.stringify(contextIds)]);
|
|
37
48
|
const { selection } = useSelectionContext(SELECTION_NAME);
|
|
38
49
|
|
|
39
50
|
const select = useCallback(
|
|
40
51
|
(ids: string[]) => {
|
|
41
|
-
|
|
42
|
-
|
|
52
|
+
for (const contextId of stableContextIds) {
|
|
53
|
+
selection.updateSelection(contextId, ids);
|
|
43
54
|
}
|
|
44
|
-
selection.updateSelection(contextId, ids);
|
|
45
55
|
},
|
|
46
|
-
[selection,
|
|
56
|
+
[selection, stableContextIds],
|
|
47
57
|
);
|
|
48
58
|
|
|
49
59
|
const toggle = useCallback(
|
|
50
60
|
(id: string) => {
|
|
51
|
-
|
|
52
|
-
|
|
61
|
+
for (const contextId of stableContextIds) {
|
|
62
|
+
selection.toggleSelection(contextId, id);
|
|
53
63
|
}
|
|
54
|
-
selection.toggleSelection(contextId, id);
|
|
55
64
|
},
|
|
56
|
-
[selection,
|
|
65
|
+
[selection, stableContextIds],
|
|
57
66
|
);
|
|
58
67
|
|
|
59
68
|
const clear = useCallback(() => {
|
|
60
|
-
|
|
61
|
-
|
|
69
|
+
for (const contextId of stableContextIds) {
|
|
70
|
+
selection.clearSelection(contextId);
|
|
62
71
|
}
|
|
63
|
-
|
|
64
|
-
}, [selection, contextId]);
|
|
72
|
+
}, [selection, stableContextIds]);
|
|
65
73
|
|
|
66
74
|
return { select, toggle, clear };
|
|
67
75
|
};
|
package/src/selection.ts
CHANGED
|
@@ -10,6 +10,7 @@ import { create, type ReactiveObject } from '@dxos/live-object';
|
|
|
10
10
|
* Manages selection state for different contexts.
|
|
11
11
|
* Each context maintains its own list of selected IDs.
|
|
12
12
|
*/
|
|
13
|
+
// TODO(burdon): Review/remove this abstraction. Selection should be managed by specific components.
|
|
13
14
|
export class SelectionManager {
|
|
14
15
|
private readonly _state = create<{ selections: Record<string, Set<string>> }>({ selections: {} });
|
|
15
16
|
|