@navikt/ds-react 7.33.0 → 7.33.2

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.
Files changed (99) hide show
  1. package/cjs/date/datepicker/hooks/useRangeDatepicker.js +2 -2
  2. package/cjs/date/datepicker/hooks/useRangeDatepicker.js.map +1 -1
  3. package/cjs/form/combobox/FilteredOptions/FilteredOptions.js +0 -1
  4. package/cjs/form/combobox/FilteredOptions/FilteredOptions.js.map +1 -1
  5. package/cjs/overlays/action-menu/ActionMenu.js +2 -5
  6. package/cjs/overlays/action-menu/ActionMenu.js.map +1 -1
  7. package/cjs/overlays/dismissablelayer/DismissableLayer.d.ts +3 -30
  8. package/cjs/overlays/dismissablelayer/DismissableLayer.js +141 -134
  9. package/cjs/overlays/dismissablelayer/DismissableLayer.js.map +1 -1
  10. package/cjs/overlays/dismissablelayer/util/sort-layers.d.ts +18 -0
  11. package/cjs/overlays/dismissablelayer/util/sort-layers.js +51 -0
  12. package/cjs/overlays/dismissablelayer/util/sort-layers.js.map +1 -0
  13. package/cjs/overlays/floating-menu/Menu.d.ts +5 -7
  14. package/cjs/overlays/floating-menu/Menu.js +7 -15
  15. package/cjs/overlays/floating-menu/Menu.js.map +1 -1
  16. package/cjs/popover/Popover.js +0 -1
  17. package/cjs/popover/Popover.js.map +1 -1
  18. package/cjs/portal/Portal.d.ts +1 -3
  19. package/cjs/portal/Portal.js +49 -17
  20. package/cjs/portal/Portal.js.map +1 -1
  21. package/cjs/timeline/Timeline.d.ts +1 -1
  22. package/cjs/timeline/Timeline.js +1 -1
  23. package/cjs/timeline/Timeline.js.map +1 -1
  24. package/cjs/timeline/index.d.ts +1 -1
  25. package/cjs/timeline/index.js +1 -1
  26. package/cjs/timeline/index.js.map +1 -1
  27. package/cjs/timeline/pin/Pin.d.ts +9 -0
  28. package/cjs/timeline/pin/Pin.js +68 -0
  29. package/cjs/timeline/pin/Pin.js.map +1 -0
  30. package/cjs/timeline/pin/PinInternal.d.ts +13 -0
  31. package/cjs/timeline/{Pin.js → pin/PinInternal.js} +9 -10
  32. package/cjs/timeline/pin/PinInternal.js.map +1 -0
  33. package/cjs/tooltip/Tooltip.js +23 -22
  34. package/cjs/tooltip/Tooltip.js.map +1 -1
  35. package/cjs/util/focus-boundary/FocusBoundary.d.ts +19 -10
  36. package/cjs/util/focus-boundary/FocusBoundary.js +107 -63
  37. package/cjs/util/focus-boundary/FocusBoundary.js.map +1 -1
  38. package/cjs/util/hooks/useScrollLock.js +17 -3
  39. package/cjs/util/hooks/useScrollLock.js.map +1 -1
  40. package/esm/date/datepicker/hooks/useRangeDatepicker.js +2 -2
  41. package/esm/date/datepicker/hooks/useRangeDatepicker.js.map +1 -1
  42. package/esm/form/combobox/FilteredOptions/FilteredOptions.js +0 -1
  43. package/esm/form/combobox/FilteredOptions/FilteredOptions.js.map +1 -1
  44. package/esm/overlays/action-menu/ActionMenu.js +2 -5
  45. package/esm/overlays/action-menu/ActionMenu.js.map +1 -1
  46. package/esm/overlays/dismissablelayer/DismissableLayer.d.ts +3 -30
  47. package/esm/overlays/dismissablelayer/DismissableLayer.js +140 -132
  48. package/esm/overlays/dismissablelayer/DismissableLayer.js.map +1 -1
  49. package/esm/overlays/dismissablelayer/util/sort-layers.d.ts +18 -0
  50. package/esm/overlays/dismissablelayer/util/sort-layers.js +49 -0
  51. package/esm/overlays/dismissablelayer/util/sort-layers.js.map +1 -0
  52. package/esm/overlays/floating-menu/Menu.d.ts +5 -7
  53. package/esm/overlays/floating-menu/Menu.js +7 -15
  54. package/esm/overlays/floating-menu/Menu.js.map +1 -1
  55. package/esm/popover/Popover.js +0 -1
  56. package/esm/popover/Popover.js.map +1 -1
  57. package/esm/portal/Portal.d.ts +1 -3
  58. package/esm/portal/Portal.js +50 -18
  59. package/esm/portal/Portal.js.map +1 -1
  60. package/esm/timeline/Timeline.d.ts +1 -1
  61. package/esm/timeline/Timeline.js +1 -1
  62. package/esm/timeline/Timeline.js.map +1 -1
  63. package/esm/timeline/index.d.ts +1 -1
  64. package/esm/timeline/index.js +1 -1
  65. package/esm/timeline/index.js.map +1 -1
  66. package/esm/timeline/pin/Pin.d.ts +9 -0
  67. package/esm/timeline/pin/Pin.js +29 -0
  68. package/esm/timeline/pin/Pin.js.map +1 -0
  69. package/esm/timeline/pin/PinInternal.d.ts +13 -0
  70. package/esm/timeline/{Pin.js → pin/PinInternal.js} +8 -9
  71. package/esm/timeline/pin/PinInternal.js.map +1 -0
  72. package/esm/tooltip/Tooltip.js +23 -22
  73. package/esm/tooltip/Tooltip.js.map +1 -1
  74. package/esm/util/focus-boundary/FocusBoundary.d.ts +19 -10
  75. package/esm/util/focus-boundary/FocusBoundary.js +108 -64
  76. package/esm/util/focus-boundary/FocusBoundary.js.map +1 -1
  77. package/esm/util/hooks/useScrollLock.js +17 -3
  78. package/esm/util/hooks/useScrollLock.js.map +1 -1
  79. package/package.json +3 -3
  80. package/src/date/datepicker/hooks/useRangeDatepicker.tsx +4 -2
  81. package/src/form/combobox/FilteredOptions/FilteredOptions.tsx +0 -1
  82. package/src/overlays/action-menu/ActionMenu.tsx +2 -4
  83. package/src/overlays/dismissablelayer/DismissableLayer.tsx +219 -194
  84. package/src/overlays/dismissablelayer/util/sort-layers.test.ts +128 -0
  85. package/src/overlays/dismissablelayer/util/sort-layers.ts +61 -0
  86. package/src/overlays/floating-menu/Menu.tsx +11 -21
  87. package/src/popover/Popover.tsx +0 -1
  88. package/src/portal/Portal.tsx +89 -31
  89. package/src/timeline/Timeline.tsx +1 -1
  90. package/src/timeline/index.ts +1 -1
  91. package/src/timeline/pin/Pin.tsx +33 -0
  92. package/src/timeline/{Pin.tsx → pin/PinInternal.tsx} +8 -18
  93. package/src/tooltip/Tooltip.tsx +4 -4
  94. package/src/util/focus-boundary/FocusBoundary.tsx +164 -93
  95. package/src/util/hooks/useScrollLock.ts +22 -3
  96. package/cjs/timeline/Pin.d.ts +0 -17
  97. package/cjs/timeline/Pin.js.map +0 -1
  98. package/esm/timeline/Pin.d.ts +0 -17
  99. package/esm/timeline/Pin.js.map +0 -1
@@ -0,0 +1,61 @@
1
+ type DismissableLayerElement = HTMLDivElement;
2
+
3
+ /**
4
+ * Returns an array of layers sorted such that parents appear before their children.
5
+ *
6
+ * **Why**:
7
+ * - mount order for parent-child relationships is unstable due to portals
8
+ * - event handling relies on parents being before children in the array
9
+ *
10
+ * This function ensures that for any parent-child relationship, the parent layer
11
+ * will always appear before its child layer in the returned array,
12
+ * resulting in consistent behavior.
13
+ *
14
+ * @param layers - A set of DismissableLayerElements to be sorted.
15
+ * @param branchedLayers - A map where each key is a parent layer and its value is a set of child layers.
16
+ * @returns An array of DismissableLayerElements sorted by parent-child relationships.
17
+ */
18
+ function getSortedLayers(
19
+ layers: Set<DismissableLayerElement>,
20
+ branchedLayers: Map<DismissableLayerElement, Set<DismissableLayerElement>>,
21
+ ): DismissableLayerElement[] {
22
+ const sorted: DismissableLayerElement[] = [];
23
+ const visited = new Set<DismissableLayerElement>();
24
+ const parentMap = new Map<DismissableLayerElement, DismissableLayerElement>();
25
+
26
+ branchedLayers.forEach((children, parent) => {
27
+ children.forEach((child) => {
28
+ if (child !== parent) {
29
+ parentMap.set(child, parent);
30
+ }
31
+ });
32
+ });
33
+
34
+ const walk = (layer: DismissableLayerElement) => {
35
+ if (visited.has(layer)) {
36
+ return;
37
+ }
38
+
39
+ const parent = parentMap.get(layer);
40
+ if (parent && !visited.has(parent)) {
41
+ walk(parent);
42
+ if (visited.has(layer)) {
43
+ return;
44
+ }
45
+ }
46
+
47
+ visited.add(layer);
48
+ sorted.push(layer);
49
+
50
+ const children = branchedLayers.get(layer);
51
+ if (children) {
52
+ children.forEach(walk);
53
+ }
54
+ };
55
+
56
+ layers.forEach(walk);
57
+
58
+ return sorted;
59
+ }
60
+
61
+ export { getSortedLayers };
@@ -243,7 +243,7 @@ type DismissableLayerProps = React.ComponentPropsWithoutRef<
243
243
  >;
244
244
 
245
245
  type MenuContentInternalPrivateProps = {
246
- onOpenAutoFocus?: FocusScopeProps["onMountAutoFocus"];
246
+ initialFocus?: FocusScopeProps["initialFocus"];
247
247
  onDismiss?: DismissableLayerProps["onDismiss"];
248
248
  disableOutsidePointerEvents?: DismissableLayerProps["disableOutsidePointerEvents"];
249
249
  };
@@ -254,11 +254,7 @@ interface MenuContentInternalProps
254
254
  React.ComponentPropsWithoutRef<typeof Floating.Content>,
255
255
  "dir" | "onPlaced"
256
256
  > {
257
- /**
258
- * Event handler called when auto-focusing after close.
259
- * Can be prevented.
260
- */
261
- onCloseAutoFocus?: FocusScopeProps["onUnmountAutoFocus"];
257
+ returnFocus?: FocusScopeProps["returnFocus"];
262
258
  onEntryFocus?: RovingFocusProps["onEntryFocus"];
263
259
  onEscapeKeyDown?: DismissableLayerProps["onEscapeKeyDown"];
264
260
  onPointerDownOutside?: DismissableLayerProps["onPointerDownOutside"];
@@ -273,8 +269,8 @@ const MenuContentInternal = forwardRef<
273
269
  >(
274
270
  (
275
271
  {
276
- onOpenAutoFocus,
277
- onCloseAutoFocus,
272
+ initialFocus,
273
+ returnFocus,
278
274
  disableOutsidePointerEvents,
279
275
  onEntryFocus,
280
276
  onEscapeKeyDown,
@@ -301,13 +297,8 @@ const MenuContentInternal = forwardRef<
301
297
 
302
298
  return (
303
299
  <FocusBoundary
304
- onMountAutoFocus={composeEventHandlers(onOpenAutoFocus, (event) => {
305
- // when opening, explicitly focus the content area only and leave
306
- // `onEntryFocus` in control of focusing first item
307
- event.preventDefault();
308
- contentRef.current?.focus({ preventScroll: true });
309
- })}
310
- onUnmountAutoFocus={onCloseAutoFocus}
300
+ initialFocus={initialFocus ?? contentRef}
301
+ returnFocus={returnFocus}
311
302
  /* Focus trapping is handled in `Floating.Content: onKeyDown */
312
303
  trapped={false}
313
304
  loop={false}
@@ -588,7 +579,7 @@ const MenuPortal = forwardRef<MenuPortalElement, MenuPortalProps>(
588
579
  }
589
580
 
590
581
  return (
591
- <Portal asChild rootElement={rootElement} ref={ref}>
582
+ <Portal rootElement={rootElement} ref={ref}>
592
583
  {children}
593
584
  </Portal>
594
585
  );
@@ -917,15 +908,14 @@ const MenuSubContent = forwardRef<
917
908
  align="start"
918
909
  side="right"
919
910
  disableOutsidePointerEvents={false}
920
- onOpenAutoFocus={(event) => {
921
- // when opening a submenu, focus content for keyboard users only
911
+ initialFocus={() => {
922
912
  if (rootContext.isUsingKeyboardRef.current) {
923
- ref.current?.focus();
913
+ return ref.current;
924
914
  }
925
- event.preventDefault();
915
+ return false;
926
916
  }}
927
917
  /* Since we manually focus Subtrigger, we prevent use of auto-focus */
928
- onCloseAutoFocus={(event) => event.preventDefault()}
918
+ returnFocus={false}
929
919
  onEscapeKeyDown={composeEventHandlers(
930
920
  props.onEscapeKeyDown,
931
921
  (event) => {
@@ -174,7 +174,6 @@ export const Popover = forwardRef<HTMLDivElement, PopoverProps>(
174
174
  asChild
175
175
  safeZone={{
176
176
  anchor: anchorEl,
177
- dismissable: refs.floating.current,
178
177
  }}
179
178
  onDismiss={() => open && onClose?.()}
180
179
  enabled={open}
@@ -1,53 +1,111 @@
1
- import React, { HTMLAttributes, forwardRef } from "react";
1
+ import React, {
2
+ HTMLAttributes,
3
+ createContext,
4
+ forwardRef,
5
+ useContext,
6
+ } from "react";
2
7
  import ReactDOM from "react-dom";
3
8
  import { useProvider } from "../provider/Provider";
4
- import { Slot } from "../slot/Slot";
5
9
  import { Theme, useThemeInternal } from "../theme/Theme";
6
- import { AsChildProps } from "../util/types";
10
+ import { useClientLayoutEffect, useMergeRefs } from "../util/hooks";
7
11
 
8
- interface PortalBaseProps extends HTMLAttributes<HTMLDivElement> {
12
+ export interface PortalProps extends HTMLAttributes<HTMLDivElement> {
9
13
  /**
10
14
  * An optional container where the portaled content should be appended.
11
15
  */
12
16
  rootElement?: HTMLElement | null;
13
17
  }
14
18
 
15
- export type PortalProps = PortalBaseProps & AsChildProps;
19
+ const PortalContext = createContext<HTMLElement | null>(null);
16
20
 
17
21
  export const Portal = forwardRef<HTMLDivElement, PortalProps>(
18
- ({ rootElement, asChild, ...rest }, ref) => {
19
- const themeContext = useThemeInternal(false);
20
- const contextRoot = useProvider()?.rootElement;
21
- const root = rootElement ?? contextRoot ?? globalThis?.document?.body;
22
+ ({ rootElement, children, ...restProps }, forwardedRef) => {
23
+ const providerRootElement = useProvider()?.rootElement;
24
+
25
+ const parentPortalNode = useContext(PortalContext);
26
+
27
+ const [containerElement, setContainerElement] = React.useState<
28
+ HTMLElement | ShadowRoot | null
29
+ >(null);
30
+ const [portalNode, setPortalNode] = React.useState<HTMLElement | null>(
31
+ null,
32
+ );
33
+
34
+ const containerRef = React.useRef<HTMLElement | ShadowRoot | null>(null);
22
35
 
23
- const Component = asChild ? Slot : "div";
36
+ const mergedRefs = useMergeRefs(forwardedRef, setPortalNode);
24
37
 
25
38
  /**
26
- * Portal can be mounted outside of theme-classNames.
27
- * If a theme is present, we want to make sure that theme cascades to portaled element.
39
+ * We update container in effect to avoid SSR mismatches.
28
40
  */
41
+ useClientLayoutEffect(() => {
42
+ /* Wait for the container to be resolved if explicitly `null`. */
43
+ if ((rootElement ?? providerRootElement) === null) {
44
+ if (containerRef.current) {
45
+ containerRef.current = null;
46
+ setPortalNode(null);
47
+ setContainerElement(null);
48
+ }
49
+ return;
50
+ }
51
+
52
+ const resolvedContainer =
53
+ rootElement ??
54
+ parentPortalNode ??
55
+ providerRootElement ??
56
+ globalThis?.document?.body;
57
+
58
+ if (resolvedContainer === null) {
59
+ if (containerRef.current) {
60
+ containerRef.current = null;
61
+ setPortalNode(null);
62
+ setContainerElement(null);
63
+ }
64
+ return;
65
+ }
66
+
67
+ if (containerRef.current !== resolvedContainer) {
68
+ containerRef.current = resolvedContainer;
69
+ setPortalNode(null);
70
+ setContainerElement(resolvedContainer);
71
+ }
72
+ }, [parentPortalNode, providerRootElement, rootElement]);
73
+
74
+ if (!containerElement) {
75
+ return null;
76
+ }
77
+
78
+ return ReactDOM.createPortal(
79
+ <PortalDiv ref={mergedRefs} {...restProps} data-aksel-portal="">
80
+ <PortalContext.Provider value={portalNode}>
81
+ {children}
82
+ </PortalContext.Provider>
83
+ </PortalDiv>,
84
+ containerElement,
85
+ );
86
+ },
87
+ );
88
+
89
+ type PortalDivProps = React.HTMLAttributes<HTMLDivElement>;
90
+
91
+ const PortalDiv = forwardRef<HTMLDivElement, PortalDivProps>(
92
+ (props: PortalDivProps, forwardedRef) => {
93
+ const themeContext = useThemeInternal(false);
94
+
29
95
  if (themeContext?.isDarkside) {
30
- return root
31
- ? ReactDOM.createPortal(
32
- <Theme
33
- theme={themeContext.theme}
34
- asChild
35
- hasBackground={false}
36
- data-color={themeContext.color}
37
- >
38
- <Component ref={ref} data-aksel-portal="" {...rest} />
39
- </Theme>,
40
- root,
41
- )
42
- : null;
96
+ return (
97
+ <Theme
98
+ theme={themeContext?.theme}
99
+ asChild
100
+ hasBackground={false}
101
+ data-color={themeContext?.color}
102
+ >
103
+ <div ref={forwardedRef} {...props} />
104
+ </Theme>
105
+ );
43
106
  }
44
107
 
45
- return root
46
- ? ReactDOM.createPortal(
47
- <Component ref={ref} data-aksel-portal="" {...rest} />,
48
- root,
49
- )
50
- : null;
108
+ return <div ref={forwardedRef} {...props} />;
51
109
  },
52
110
  );
53
111
 
@@ -2,7 +2,6 @@ import { endOfDay, isSameDay, startOfDay } from "date-fns";
2
2
  import React, { forwardRef, useMemo, useRef, useState } from "react";
3
3
  import { useRenameCSS } from "../theme/Theme";
4
4
  import { AxisLabels } from "./AxisLabels";
5
- import Pin, { PinType } from "./Pin";
6
5
  import TimelineRow, { TimelineRowType } from "./TimelineRow";
7
6
  import { RowContext } from "./hooks/useRowContext";
8
7
  import { TimelineContext } from "./hooks/useTimelineContext";
@@ -12,6 +11,7 @@ import {
12
11
  useTimelineRows,
13
12
  } from "./hooks/useTimelineRows";
14
13
  import Period, { PeriodType } from "./period";
14
+ import Pin, { PinType } from "./pin/Pin";
15
15
  import { parseRows } from "./utils/timeline";
16
16
  import { AxisLabelTemplates } from "./utils/types.external";
17
17
  import Zoom, { ZoomType } from "./zoom";
@@ -1,7 +1,7 @@
1
1
  "use client";
2
2
  export { default as Timeline, type TimelineProps } from "./Timeline";
3
3
  export { default as TimelineRow, type TimelineRowProps } from "./TimelineRow";
4
- export { default as TimelinePin, type TimelinePinProps } from "./Pin";
4
+ export { default as TimelinePin, type TimelinePinProps } from "./pin/Pin";
5
5
  export { default as TimelinePeriod, type TimelinePeriodProps } from "./period";
6
6
  export {
7
7
  default as TimelineZoomButton,
@@ -0,0 +1,33 @@
1
+ import { isBefore } from "date-fns";
2
+ import React, { forwardRef } from "react";
3
+ import { useTimelineContext } from "../hooks/useTimelineContext";
4
+ import { TimelineComponentTypes } from "../utils/types.internal";
5
+ import PinInternal, { type TimelinePinProps } from "./PinInternal";
6
+
7
+ export interface PinType
8
+ extends React.ForwardRefExoticComponent<
9
+ TimelinePinProps & React.RefAttributes<HTMLButtonElement>
10
+ > {
11
+ componentType: TimelineComponentTypes;
12
+ }
13
+
14
+ export const Pin = forwardRef<HTMLButtonElement, TimelinePinProps>(
15
+ ({ date, ...restProps }, forwardedRef) => {
16
+ const { startDate, endDate } = useTimelineContext();
17
+
18
+ /**
19
+ * Out-of-bounds pins should not be rendered
20
+ */
21
+ if (isBefore(date, startDate) || isBefore(endDate, date)) {
22
+ return null;
23
+ }
24
+
25
+ return <PinInternal ref={forwardedRef} {...restProps} date={date} />;
26
+ },
27
+ ) as PinType;
28
+
29
+ Pin.componentType = "pin";
30
+
31
+ export default Pin;
32
+
33
+ export type { TimelinePinProps };
@@ -14,12 +14,11 @@ import {
14
14
  } from "@floating-ui/react";
15
15
  import { format } from "date-fns";
16
16
  import React, { forwardRef, useRef, useState } from "react";
17
- import { useRenameCSS, useThemeInternal } from "../theme/Theme";
18
- import { useMergeRefs } from "../util/hooks/useMergeRefs";
19
- import { useI18n } from "../util/i18n/i18n.hooks";
20
- import { useTimelineContext } from "./hooks/useTimelineContext";
21
- import { position } from "./utils/calc";
22
- import { TimelineComponentTypes } from "./utils/types.internal";
17
+ import { useRenameCSS, useThemeInternal } from "../../theme/Theme";
18
+ import { useMergeRefs } from "../../util/hooks/useMergeRefs";
19
+ import { useI18n } from "../../util/i18n/i18n.hooks";
20
+ import { useTimelineContext } from "../hooks/useTimelineContext";
21
+ import { position } from "../utils/calc";
23
22
 
24
23
  export interface TimelinePinProps
25
24
  extends React.HTMLAttributes<HTMLButtonElement> {
@@ -33,14 +32,7 @@ export interface TimelinePinProps
33
32
  children?: React.ReactNode;
34
33
  }
35
34
 
36
- export interface PinType
37
- extends React.ForwardRefExoticComponent<
38
- TimelinePinProps & React.RefAttributes<HTMLButtonElement>
39
- > {
40
- componentType: TimelineComponentTypes;
41
- }
42
-
43
- export const Pin = forwardRef<HTMLButtonElement, TimelinePinProps>(
35
+ export const PinInternal = forwardRef<HTMLButtonElement, TimelinePinProps>(
44
36
  ({ date, children, ...rest }, ref) => {
45
37
  const { cn } = useRenameCSS();
46
38
  const { startDate, endDate, direction } = useTimelineContext();
@@ -157,8 +149,6 @@ export const Pin = forwardRef<HTMLButtonElement, TimelinePinProps>(
157
149
  </>
158
150
  );
159
151
  },
160
- ) as PinType;
161
-
162
- Pin.componentType = "pin";
152
+ );
163
153
 
164
- export default Pin;
154
+ export default PinInternal;
@@ -209,8 +209,8 @@ export const Tooltip = forwardRef<HTMLDivElement, TooltipProps>(
209
209
  >
210
210
  {children}
211
211
  </Slot>
212
- <Portal rootElement={rootElement} asChild>
213
- {_open && (
212
+ {_open && (
213
+ <Portal rootElement={rootElement}>
214
214
  <div
215
215
  {...getFloatingProps({
216
216
  ...rest,
@@ -267,8 +267,8 @@ export const Tooltip = forwardRef<HTMLDivElement, TooltipProps>(
267
267
  />
268
268
  )}
269
269
  </div>
270
- )}
271
- </Portal>
270
+ </Portal>
271
+ )}
272
272
  </>
273
273
  );
274
274
  },