portal
efferd-/portalFreeComponent
react portal component.
Live preview
live · rendered by the registry
Rendered by efferd on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add http://efferd.com/r/portal.jsonSource
1import { cn } from "@/lib/utils";2import React from "react";3import { createPortal } from "react-dom";45function Portal({ className, ...props }: React.ComponentProps<"div">) {6 const [mounted, setMounted] = React.useState(false);78 React.useEffect(() => {9 setMounted(true);1011 const originalStyle = window.getComputedStyle(document.body).overflow;12 const scrollbarWidth =13 window.innerWidth - document.documentElement.clientWidth;14 const originalPaddingRight = document.body.style.paddingRight;1516 document.body.style.overflow = "hidden";17 if (scrollbarWidth > 0) {18 document.body.style.paddingRight = `${scrollbarWidth}px`;19 }2021 return () => {22 document.body.style.overflow = originalStyle;23 document.body.style.paddingRight = originalPaddingRight;24 };25 }, []);2627 if (!mounted) {28 return null;29 }3031 return createPortal(32 <div33 className={cn("fixed inset-0 isolate z-40 flex flex-col", className)}34 {...props}35 />,36 document.body37 );38}3940function PortalBackdrop({ className, ...props }: React.ComponentProps<"div">) {41 return (42 <div43 className={cn(44 "data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 fixed inset-0 -z-1 bg-background/95 backdrop-blur-sm duration-500 data-[state=closed]:animate-out data-[state=open]:animate-in supports-backdrop-filter:bg-background/60",45 className46 )}47 {...props}48 />49 );50}5152export { Portal, PortalBackdrop };
Files it writes
More from efferd
All 201 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| apple-iconapple-icon | efferd | Components | Free | no deps | |
| auth-dividerauth-divider | efferd | Components | Free | no deps | |
| copy-buttoncopy-button | efferd | Components | Free | no deps | |
| decor-icondecor-icon | efferd | Components | Free | no deps | |
| discord-icondiscord-icon | efferd | Components | Free | no deps | |
| facebook-iconfacebook-icon | efferd | Components | Free | no deps | |
| figma-iconfigma-icon | efferd | Components | Free | no deps | |
| formaterformater | efferd | Components | Free | no deps |
