@navikt/ds-react 5.17.4 → 5.18.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.
Files changed (40) hide show
  1. package/_docs.json +544 -64
  2. package/cjs/index.js +1 -0
  3. package/cjs/overlays/index.js +5 -0
  4. package/cjs/overlays/package.json +6 -0
  5. package/cjs/overlays/portal/Portal.js +55 -0
  6. package/cjs/tooltip/Tooltip.js +3 -7
  7. package/cjs/util/Slot.js +5 -2
  8. package/cjs/util/types/AsChildProps.js +2 -0
  9. package/esm/date/hooks/useDatepicker.d.ts +1 -1
  10. package/esm/date/hooks/useMonthPicker.d.ts +1 -1
  11. package/esm/date/parts/DateInput.d.ts +1 -1
  12. package/esm/index.d.ts +1 -0
  13. package/esm/index.js +1 -0
  14. package/esm/index.js.map +1 -1
  15. package/esm/overlays/index.d.ts +2 -0
  16. package/esm/overlays/index.js +2 -0
  17. package/esm/overlays/index.js.map +1 -0
  18. package/esm/overlays/portal/Portal.d.ts +11 -0
  19. package/esm/overlays/portal/Portal.js +27 -0
  20. package/esm/overlays/portal/Portal.js.map +1 -0
  21. package/esm/tooltip/Tooltip.js +4 -8
  22. package/esm/tooltip/Tooltip.js.map +1 -1
  23. package/esm/util/Slot.js +5 -2
  24. package/esm/util/Slot.js.map +1 -1
  25. package/esm/util/types/AsChildProps.d.ts +36 -0
  26. package/esm/util/types/AsChildProps.js +2 -0
  27. package/esm/util/types/AsChildProps.js.map +1 -0
  28. package/esm/util/types/index.d.ts +1 -0
  29. package/package.json +3 -3
  30. package/src/date/hooks/useDatepicker.tsx +1 -1
  31. package/src/date/hooks/useMonthPicker.tsx +1 -1
  32. package/src/date/parts/DateInput.tsx +1 -1
  33. package/src/index.ts +1 -0
  34. package/src/overlays/index.ts +2 -0
  35. package/src/overlays/portal/Portal.stories.tsx +99 -0
  36. package/src/overlays/portal/Portal.tsx +32 -0
  37. package/src/tooltip/Tooltip.tsx +4 -8
  38. package/src/util/Slot.tsx +4 -2
  39. package/src/util/types/AsChildProps.ts +37 -0
  40. package/src/util/types/index.ts +1 -0
@@ -1,5 +1,4 @@
1
1
  import {
2
- FloatingPortal,
3
2
  autoUpdate,
4
3
  arrow as flArrow,
5
4
  flip,
@@ -21,7 +20,7 @@ import React, {
21
20
  useRef,
22
21
  } from "react";
23
22
  import { ModalContext } from "../modal/ModalContext";
24
- import { useProvider } from "../provider";
23
+ import Portal from "../overlays/portal/Portal";
25
24
  import { Detail } from "../typography";
26
25
  import { useId } from "../util/hooks";
27
26
  import { useControllableState } from "../util/hooks/useControllableState";
@@ -123,10 +122,7 @@ export const Tooltip = forwardRef<HTMLDivElement, TooltipProps>(
123
122
 
124
123
  const arrowRef = useRef<HTMLDivElement | null>(null);
125
124
  const modalContext = useContext(ModalContext);
126
- const providerRootElement = useProvider()?.rootElement;
127
- const rootElement = modalContext
128
- ? modalContext.ref.current
129
- : providerRootElement;
125
+ const rootElement = modalContext ? modalContext.ref.current : undefined;
130
126
 
131
127
  const {
132
128
  x,
@@ -199,7 +195,7 @@ export const Tooltip = forwardRef<HTMLDivElement, TooltipProps>(
199
195
  : children?.props["aria-describedby"],
200
196
  }),
201
197
  )}
202
- <FloatingPortal root={rootElement}>
198
+ <Portal rootElement={rootElement}>
203
199
  {_open && (
204
200
  <div
205
201
  {...getFloatingProps({
@@ -253,7 +249,7 @@ export const Tooltip = forwardRef<HTMLDivElement, TooltipProps>(
253
249
  )}
254
250
  </div>
255
251
  )}
256
- </FloatingPortal>
252
+ </Portal>
257
253
  </>
258
254
  );
259
255
  },
package/src/util/Slot.tsx CHANGED
@@ -20,10 +20,12 @@ export const Slot = React.forwardRef<HTMLElement, SlotProps>(
20
20
  }
21
21
 
22
22
  if (React.Children.count(children) > 1) {
23
- console.error(
23
+ const error = new Error(
24
24
  "Aksel: Components using 'asChild' expects to recieve a single React element child.",
25
25
  );
26
- return React.Children.only(null);
26
+ error.name = "SlotError";
27
+ Error.captureStackTrace?.(error, Slot);
28
+ throw error;
27
29
  }
28
30
 
29
31
  return null;
@@ -0,0 +1,37 @@
1
+ export type AsChildProps =
2
+ | {
3
+ children: React.ReactElement | false | null;
4
+ /**
5
+ * Renders the component and its child as a single element,
6
+ * merging the props of the component with the props of the child.
7
+ *
8
+ * @example
9
+ * ```tsx
10
+ * <Component asChild data-prop>
11
+ * <ChildComponent data-child />
12
+ * </Component>
13
+ *
14
+ * // Renders
15
+ * <MergedComponent data-prop data-child />
16
+ * ```
17
+ */
18
+ asChild: true;
19
+ }
20
+ | {
21
+ children: React.ReactNode;
22
+ /**
23
+ * Renders the component and its child as a single element,
24
+ * merging the props of the component with the props of the child.
25
+ *
26
+ * @example
27
+ * ```tsx
28
+ * <Component asChild data-prop>
29
+ * <ChildComponent data-child />
30
+ * </Component>
31
+ *
32
+ * // Renders
33
+ * <MergedComponent data-prop data-child />
34
+ * ```
35
+ */
36
+ asChild?: false;
37
+ };
@@ -1 +1,2 @@
1
1
  export type { OverridableComponent } from "./OverridableComponent";
2
+ export type { AsChildProps } from "./AsChildProps";