@navikt/ds-react 7.37.0 → 7.38.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/cjs/date/Date.Dialog.js +5 -1
- package/cjs/date/Date.Dialog.js.map +1 -1
- package/cjs/dialog/index.d.ts +1 -1
- package/cjs/dialog/index.js +4 -1
- package/cjs/dialog/index.js.map +1 -1
- package/cjs/dialog/popup/DialogPopup.js +6 -1
- package/cjs/dialog/popup/DialogPopup.js.map +1 -1
- package/cjs/dialog/root/DialogRoot.d.ts +5 -5
- package/cjs/dialog/root/DialogRoot.js +12 -11
- package/cjs/dialog/root/DialogRoot.js.map +1 -1
- package/cjs/form/combobox/Combobox.d.ts +1 -1
- package/cjs/form/combobox/Input/InputController.d.ts +1 -1
- package/cjs/form/file-upload/useFileUpload.d.ts +1 -1
- package/cjs/layout/base/PrimitiveAsChildProps.d.ts +1 -4
- package/cjs/modal/Modal.js +9 -2
- package/cjs/modal/Modal.js.map +1 -1
- package/cjs/overlays/action-menu/ActionMenu.js +3 -1
- package/cjs/overlays/action-menu/ActionMenu.js.map +1 -1
- package/cjs/overlays/dismissablelayer/DismissableLayer.d.ts +1 -0
- package/cjs/overlays/dismissablelayer/DismissableLayer.js +33 -14
- package/cjs/overlays/dismissablelayer/DismissableLayer.js.map +1 -1
- package/cjs/overlays/dismissablelayer/util/useEscapeKeydown.js +7 -2
- package/cjs/overlays/dismissablelayer/util/useEscapeKeydown.js.map +1 -1
- package/cjs/provider/Provider.d.ts +1 -5
- package/cjs/provider/Provider.js +0 -2
- package/cjs/provider/Provider.js.map +1 -1
- package/cjs/slot/Slot.js +12 -5
- package/cjs/slot/Slot.js.map +1 -1
- package/cjs/tabs/Tabs.context.d.ts +1 -1
- package/cjs/tabs/parts/tab/useTab.d.ts +1 -1
- package/cjs/tabs/parts/tab/useTab.js +2 -1
- package/cjs/tabs/parts/tab/useTab.js.map +1 -1
- package/cjs/toggle-group/ToggleGroup.context.d.ts +1 -1
- package/cjs/toggle-group/parts/useToggleItem.d.ts +1 -1
- package/cjs/toggle-group/parts/useToggleItem.js +2 -1
- package/cjs/toggle-group/parts/useToggleItem.js.map +1 -1
- package/cjs/util/hooks/descendants/useDescendant.d.ts +1 -1
- package/cjs/util/hooks/descendants/useDescendant.js +2 -1
- package/cjs/util/hooks/descendants/useDescendant.js.map +1 -1
- package/cjs/util/hooks/useMergeRefs.d.ts +15 -9
- package/cjs/util/hooks/useMergeRefs.js +94 -29
- package/cjs/util/hooks/useMergeRefs.js.map +1 -1
- package/cjs/util/types/AsChildProps.d.ts +0 -4
- package/cjs/util/virtualfocus/Context.d.ts +1 -1
- package/esm/date/Date.Dialog.js +5 -1
- package/esm/date/Date.Dialog.js.map +1 -1
- package/esm/dialog/index.d.ts +1 -1
- package/esm/dialog/index.js +1 -1
- package/esm/dialog/index.js.map +1 -1
- package/esm/dialog/popup/DialogPopup.js +6 -1
- package/esm/dialog/popup/DialogPopup.js.map +1 -1
- package/esm/dialog/root/DialogRoot.d.ts +5 -5
- package/esm/dialog/root/DialogRoot.js +5 -5
- package/esm/dialog/root/DialogRoot.js.map +1 -1
- package/esm/form/combobox/Combobox.d.ts +1 -1
- package/esm/form/combobox/Input/InputController.d.ts +1 -1
- package/esm/form/file-upload/useFileUpload.d.ts +1 -1
- package/esm/layout/base/PrimitiveAsChildProps.d.ts +1 -4
- package/esm/modal/Modal.js +9 -2
- package/esm/modal/Modal.js.map +1 -1
- package/esm/overlays/action-menu/ActionMenu.js +3 -1
- package/esm/overlays/action-menu/ActionMenu.js.map +1 -1
- package/esm/overlays/dismissablelayer/DismissableLayer.d.ts +1 -0
- package/esm/overlays/dismissablelayer/DismissableLayer.js +34 -15
- package/esm/overlays/dismissablelayer/DismissableLayer.js.map +1 -1
- package/esm/overlays/dismissablelayer/util/useEscapeKeydown.js +7 -2
- package/esm/overlays/dismissablelayer/util/useEscapeKeydown.js.map +1 -1
- package/esm/provider/Provider.d.ts +1 -5
- package/esm/provider/Provider.js +0 -2
- package/esm/provider/Provider.js.map +1 -1
- package/esm/slot/Slot.js +12 -5
- package/esm/slot/Slot.js.map +1 -1
- package/esm/tabs/Tabs.context.d.ts +1 -1
- package/esm/tabs/parts/tab/useTab.d.ts +1 -1
- package/esm/tabs/parts/tab/useTab.js +3 -2
- package/esm/tabs/parts/tab/useTab.js.map +1 -1
- package/esm/toggle-group/ToggleGroup.context.d.ts +1 -1
- package/esm/toggle-group/parts/useToggleItem.d.ts +1 -1
- package/esm/toggle-group/parts/useToggleItem.js +3 -2
- package/esm/toggle-group/parts/useToggleItem.js.map +1 -1
- package/esm/util/hooks/descendants/useDescendant.d.ts +1 -1
- package/esm/util/hooks/descendants/useDescendant.js +3 -2
- package/esm/util/hooks/descendants/useDescendant.js.map +1 -1
- package/esm/util/hooks/useMergeRefs.d.ts +15 -9
- package/esm/util/hooks/useMergeRefs.js +93 -25
- package/esm/util/hooks/useMergeRefs.js.map +1 -1
- package/esm/util/types/AsChildProps.d.ts +0 -4
- package/esm/util/virtualfocus/Context.d.ts +1 -1
- package/package.json +3 -3
- package/src/date/Date.Dialog.tsx +6 -1
- package/src/dialog/index.ts +1 -1
- package/src/dialog/popup/DialogPopup.tsx +7 -1
- package/src/dialog/root/DialogRoot.tsx +5 -5
- package/src/layout/base/PrimitiveAsChildProps.ts +1 -4
- package/src/modal/Modal.tsx +9 -1
- package/src/overlays/action-menu/ActionMenu.tsx +3 -2
- package/src/overlays/dismissablelayer/DismissableLayer.tsx +52 -16
- package/src/overlays/dismissablelayer/util/useEscapeKeydown.ts +7 -2
- package/src/provider/Provider.tsx +1 -5
- package/src/slot/Slot.tsx +14 -9
- package/src/tabs/parts/tab/useTab.ts +4 -2
- package/src/toggle-group/parts/useToggleItem.ts +4 -2
- package/src/util/__tests__/useMergeRefs.test.ts +92 -0
- package/src/util/hooks/descendants/useDescendant.tsx +4 -2
- package/src/util/hooks/useMergeRefs.ts +147 -24
- package/src/util/types/AsChildProps.ts +0 -4
|
@@ -1,34 +1,157 @@
|
|
|
1
|
-
/*
|
|
2
|
-
|
|
3
|
-
/* https://github.com/radix-ui/primitives/blob/main/packages/react/compose-refs/src/composeRefs.tsx */
|
|
1
|
+
/* https://github.com/mui/base-ui/blob/f2d7a90e3a20dee84955beb5be0d59e50f45ae7e/packages/utils/src/useMergedRefs.ts */
|
|
4
2
|
import React from "react";
|
|
3
|
+
import { useRefWithInit } from "./useRefWithInit";
|
|
4
|
+
|
|
5
|
+
type Empty = null | undefined;
|
|
6
|
+
type InputRef<I> = React.Ref<I> | Empty;
|
|
7
|
+
type Result<I> = React.RefCallback<I> | null;
|
|
8
|
+
type Cleanup = () => void;
|
|
5
9
|
|
|
6
|
-
type
|
|
10
|
+
type ForkRef<I> = {
|
|
11
|
+
callback: React.RefCallback<I> | null;
|
|
12
|
+
cleanup: Cleanup | null;
|
|
13
|
+
refs: InputRef<I>[];
|
|
14
|
+
};
|
|
7
15
|
|
|
8
|
-
// https://github.com/gregberge/react-merge-refs
|
|
9
16
|
/**
|
|
10
|
-
*
|
|
11
|
-
*
|
|
17
|
+
* Merges refs into a single memoized callback ref or `null`.
|
|
18
|
+
* This makes sure multiple refs are updated together and have the same value.
|
|
19
|
+
*
|
|
20
|
+
* This function accepts up to four refs. If you need to merge more, or have an unspecified number of refs to merge,
|
|
21
|
+
* use `useMergeRefsN` instead.
|
|
12
22
|
*/
|
|
13
|
-
export function
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
+
export function useMergeRefs<I>(a: InputRef<I>, b: InputRef<I>): Result<I>;
|
|
24
|
+
export function useMergeRefs<I>(
|
|
25
|
+
a: InputRef<I>,
|
|
26
|
+
b: InputRef<I>,
|
|
27
|
+
c: InputRef<I>,
|
|
28
|
+
): Result<I>;
|
|
29
|
+
export function useMergeRefs<I>(
|
|
30
|
+
a: InputRef<I>,
|
|
31
|
+
b: InputRef<I>,
|
|
32
|
+
c: InputRef<I>,
|
|
33
|
+
d: InputRef<I>,
|
|
34
|
+
): Result<I>;
|
|
35
|
+
export function useMergeRefs<I>(
|
|
36
|
+
a: InputRef<I>,
|
|
37
|
+
b: InputRef<I>,
|
|
38
|
+
c?: InputRef<I>,
|
|
39
|
+
d?: InputRef<I>,
|
|
40
|
+
): Result<I> {
|
|
41
|
+
const forkRef = useRefWithInit(createForkRef<I>).current!;
|
|
42
|
+
if (didChange(forkRef, a, b, c, d)) {
|
|
43
|
+
update(forkRef, [a, b, c, d]);
|
|
44
|
+
}
|
|
45
|
+
return forkRef.callback;
|
|
23
46
|
}
|
|
24
47
|
|
|
25
48
|
/**
|
|
26
|
-
* Merges refs
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
* @returns React.useCallback(mergeRefs(refs), refs)
|
|
49
|
+
* Merges an array of refs into a single memoized callback ref or `null`.
|
|
50
|
+
*
|
|
51
|
+
* If you need to merge a fixed number (up to four) of refs, use `useMergeRefs` instead for better performance.
|
|
30
52
|
*/
|
|
31
|
-
export function
|
|
32
|
-
|
|
33
|
-
|
|
53
|
+
export function useMergeRefsN<I>(refs: InputRef<I>[]): Result<I> {
|
|
54
|
+
const forkRef = useRefWithInit(createForkRef<I>).current!;
|
|
55
|
+
if (didChangeN(forkRef, refs)) {
|
|
56
|
+
update(forkRef, refs);
|
|
57
|
+
}
|
|
58
|
+
return forkRef.callback;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function createForkRef<I>(): ForkRef<I> {
|
|
62
|
+
return {
|
|
63
|
+
callback: null,
|
|
64
|
+
cleanup: null as Cleanup | null,
|
|
65
|
+
refs: [],
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function didChange<I>(
|
|
70
|
+
forkRef: ForkRef<I>,
|
|
71
|
+
a: InputRef<I>,
|
|
72
|
+
b: InputRef<I>,
|
|
73
|
+
c: InputRef<I>,
|
|
74
|
+
d: InputRef<I>,
|
|
75
|
+
) {
|
|
76
|
+
return (
|
|
77
|
+
forkRef.refs[0] !== a ||
|
|
78
|
+
forkRef.refs[1] !== b ||
|
|
79
|
+
forkRef.refs[2] !== c ||
|
|
80
|
+
forkRef.refs[3] !== d
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function didChangeN<I>(forkRef: ForkRef<I>, newRefs: InputRef<I>[]) {
|
|
85
|
+
return (
|
|
86
|
+
forkRef.refs.length !== newRefs.length ||
|
|
87
|
+
forkRef.refs.some((ref, index) => ref !== newRefs[index])
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function update<I>(forkRef: ForkRef<I>, refs: InputRef<I>[]) {
|
|
92
|
+
forkRef.refs = refs;
|
|
93
|
+
|
|
94
|
+
if (refs.every((ref) => ref == null)) {
|
|
95
|
+
forkRef.callback = null;
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
forkRef.callback = (instance: I) => {
|
|
100
|
+
if (forkRef.cleanup) {
|
|
101
|
+
forkRef.cleanup();
|
|
102
|
+
forkRef.cleanup = null;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (instance != null) {
|
|
106
|
+
const cleanupCallbacks = Array(refs.length).fill(
|
|
107
|
+
null,
|
|
108
|
+
) as (Cleanup | null)[];
|
|
109
|
+
|
|
110
|
+
for (let i = 0; i < refs.length; i += 1) {
|
|
111
|
+
const ref = refs[i];
|
|
112
|
+
if (ref == null) {
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
switch (typeof ref) {
|
|
116
|
+
case "function": {
|
|
117
|
+
const refCleanup = ref(instance);
|
|
118
|
+
if (typeof refCleanup === "function") {
|
|
119
|
+
cleanupCallbacks[i] = refCleanup;
|
|
120
|
+
}
|
|
121
|
+
break;
|
|
122
|
+
}
|
|
123
|
+
case "object": {
|
|
124
|
+
(ref as React.MutableRefObject<I | null>).current = instance;
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
default:
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
forkRef.cleanup = () => {
|
|
132
|
+
for (let i = 0; i < refs.length; i += 1) {
|
|
133
|
+
const ref = refs[i];
|
|
134
|
+
if (ref == null) {
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
switch (typeof ref) {
|
|
138
|
+
case "function": {
|
|
139
|
+
const cleanupCallback = cleanupCallbacks[i];
|
|
140
|
+
if (typeof cleanupCallback === "function") {
|
|
141
|
+
cleanupCallback();
|
|
142
|
+
} else {
|
|
143
|
+
ref(null);
|
|
144
|
+
}
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
case "object": {
|
|
148
|
+
(ref as React.MutableRefObject<I | null>).current = null;
|
|
149
|
+
break;
|
|
150
|
+
}
|
|
151
|
+
default:
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
};
|
|
34
157
|
}
|
|
@@ -6,14 +6,12 @@ export type AsChildProps =
|
|
|
6
6
|
* merging the props of the component with the props of the child.
|
|
7
7
|
*
|
|
8
8
|
* @example
|
|
9
|
-
* ```
|
|
10
9
|
* <Component asChild data-prop>
|
|
11
10
|
* <ChildComponent data-child />
|
|
12
11
|
* </Component>
|
|
13
12
|
*
|
|
14
13
|
* Out:
|
|
15
14
|
* <MergedComponent data-prop data-child />
|
|
16
|
-
* ```
|
|
17
15
|
*/
|
|
18
16
|
asChild: true;
|
|
19
17
|
as?: never;
|
|
@@ -25,14 +23,12 @@ export type AsChildProps =
|
|
|
25
23
|
* merging the props of the component with the props of the child.
|
|
26
24
|
*
|
|
27
25
|
* @example
|
|
28
|
-
* ```
|
|
29
26
|
* <Component asChild data-prop>
|
|
30
27
|
* <ChildComponent data-child />
|
|
31
28
|
* </Component>
|
|
32
29
|
*
|
|
33
30
|
* Out:
|
|
34
31
|
* <MergedComponent data-prop data-child />
|
|
35
|
-
* ```
|
|
36
32
|
*/
|
|
37
33
|
asChild?: false;
|
|
38
34
|
};
|