-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bytter ut FloatingPortal med egen implementasjon (#2697)
* 🎉 Egen Portal-komponent * 🎨 Portal og Overlay exportert i root * ♻️ Tooltip bruker egen Portal * 📝 Changeset * 🔥 Fjernet ID på Portal-element * 🎉 Ny type WithAsChild * ♻️ Mer robust sjekk for conditional elements i WithAsChild * 📝 WithAsChild JSDOC * 📝 WithAsChild JSDOC 2 * Update .changeset/lovely-points-heal.md Co-authored-by: Halvor Haugan <83693529+HalvorHaugan@users.noreply.github.com> * ♻️ Fikset kommentarer fra git-pr --------- Co-authored-by: Halvor Haugan <83693529+HalvorHaugan@users.noreply.github.com>
- Loading branch information
1 parent
8556695
commit f44ff44
Showing
9 changed files
with
185 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@navikt/ds-react": minor | ||
--- | ||
|
||
Portal: Ny komponent `Portal` som lar deg enkelt bruke `createPortal`, også på serversiden |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { Portal } from "./portal/Portal"; | ||
export type { PortalProps } from "./portal/Portal"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import React from "react"; | ||
import { Box } from "../../layout/box"; | ||
import { Provider } from "../../provider"; | ||
import { Portal } from "./Portal"; | ||
|
||
export default { | ||
title: "Utilities/Portal", | ||
parameters: { | ||
chromatic: { disable: true }, | ||
}, | ||
}; | ||
|
||
export const Default = () => { | ||
return ( | ||
<Box background="surface-neutral-subtle" border> | ||
<h1>In regular DOM tree</h1> | ||
<p> | ||
Lorem ipsum dolor sit amet consectetur adipisicing elit. Temporibus | ||
necessitatibus quis esse nesciunt est velit voluptatibus. Distinctio eum | ||
commodi tempora unde. Nulla vel tempora incidunt? Voluptatem molestias | ||
impedit commodi. Tenetur! | ||
</p> | ||
<Portal> | ||
<h1>Inside Portal to different DOM tree</h1> | ||
<p> | ||
Lorem ipsum dolor sit amet consectetur adipisicing elit. Temporibus | ||
necessitatibus quis esse nesciunt est velit voluptatibus. Distinctio | ||
eum commodi tempora unde. Nulla vel tempora incidunt? Voluptatem | ||
molestias impedit commodi. Tenetur! | ||
</p> | ||
</Portal> | ||
</Box> | ||
); | ||
}; | ||
|
||
export const CustomPortalRoot = () => { | ||
const [portalContainer, setPortalContainer] = | ||
React.useState<HTMLDivElement | null>(null); | ||
|
||
return ( | ||
<Box background="surface-neutral-subtle"> | ||
<Box background="surface-alt-1-subtle"> | ||
<h1>Tree A</h1> | ||
<Portal rootElement={portalContainer}> | ||
<p>This is mounted to Tree B, while created inside Tree A</p> | ||
</Portal> | ||
</Box> | ||
<Box background="surface-alt-3-subtle" ref={setPortalContainer}> | ||
<h1>Tree B</h1> | ||
</Box> | ||
</Box> | ||
); | ||
}; | ||
|
||
export const CustomPortalRootFromProvider = () => { | ||
const [portalContainer, setPortalContainer] = | ||
React.useState<HTMLDivElement>(); | ||
|
||
return ( | ||
<Provider rootElement={portalContainer}> | ||
<Box background="surface-neutral-subtle"> | ||
<Box background="surface-alt-1-subtle"> | ||
<h1>Tree A</h1> | ||
<Portal> | ||
<p>This is mounted to Tree B, while created inside Tree A</p> | ||
</Portal> | ||
</Box> | ||
<Box background="surface-alt-3-subtle" ref={setPortalContainer}> | ||
<h1>Tree B</h1> | ||
</Box> | ||
</Box> | ||
</Provider> | ||
); | ||
}; | ||
|
||
export const AsChild = () => { | ||
return ( | ||
<Box background="surface-neutral-subtle" border> | ||
<h1>In regular DOM tree</h1> | ||
<p> | ||
Lorem ipsum dolor sit amet consectetur adipisicing elit. Temporibus | ||
necessitatibus quis esse nesciunt est velit voluptatibus. Distinctio eum | ||
commodi tempora unde. Nulla vel tempora incidunt? Voluptatem molestias | ||
impedit commodi. Tenetur! | ||
</p> | ||
<Portal asChild> | ||
<div data-this-is-the-child> | ||
<h1>Inside Portal to different DOM tree</h1> | ||
<p> | ||
Lorem ipsum dolor sit amet consectetur adipisicing elit. Temporibus | ||
necessitatibus quis esse nesciunt est velit voluptatibus. Distinctio | ||
eum commodi tempora unde. Nulla vel tempora incidunt? Voluptatem | ||
molestias impedit commodi. Tenetur! | ||
</p> | ||
</div> | ||
</Portal> | ||
</Box> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import React, { HTMLAttributes, forwardRef } from "react"; | ||
import ReactDOM from "react-dom"; | ||
import { useProvider } from "../../provider"; | ||
import { Slot } from "../../util/Slot"; | ||
import { AsChildProps } from "../../util/types"; | ||
|
||
interface PortalBaseProps extends HTMLAttributes<HTMLDivElement> { | ||
/** | ||
* An optional container where the portaled content should be appended. | ||
*/ | ||
rootElement?: HTMLElement | null; | ||
} | ||
|
||
export type PortalProps = PortalBaseProps & AsChildProps; | ||
|
||
export const Portal = forwardRef<HTMLDivElement, PortalProps>( | ||
({ rootElement, asChild, ...rest }, ref) => { | ||
const contextRoot = useProvider()?.rootElement; | ||
const root = rootElement ?? contextRoot ?? globalThis?.document?.body; | ||
|
||
const Component = asChild ? Slot : "div"; | ||
|
||
return root | ||
? ReactDOM.createPortal( | ||
<Component ref={ref} data-aksel-portal="" {...rest} />, | ||
root, | ||
) | ||
: null; | ||
}, | ||
); | ||
|
||
export default Portal; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
export type AsChildProps = | ||
| { | ||
children: React.ReactElement | false | null; | ||
/** | ||
* Renders the component and its child as a single element, | ||
* merging the props of the component with the props of the child. | ||
* | ||
* @example | ||
* ```tsx | ||
* <Component asChild data-prop> | ||
* <ChildComponent data-child /> | ||
* </Component> | ||
* | ||
* // Renders | ||
* <MergedComponent data-prop data-child /> | ||
* ``` | ||
*/ | ||
asChild: true; | ||
} | ||
| { | ||
children: React.ReactNode; | ||
/** | ||
* Renders the component and its child as a single element, | ||
* merging the props of the component with the props of the child. | ||
* | ||
* @example | ||
* ```tsx | ||
* <Component asChild data-prop> | ||
* <ChildComponent data-child /> | ||
* </Component> | ||
* | ||
* // Renders | ||
* <MergedComponent data-prop data-child /> | ||
* ``` | ||
*/ | ||
asChild?: false; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export type { OverridableComponent } from "./OverridableComponent"; | ||
export type { AsChildProps } from "./AsChildProps"; |