Shorter imports
#9576
Replies: 1 comment
-
|
You can create a wrapper component that attaches all the subcomponents as properties: // components/ui/alert-dialog.tsx
import * as AlertDialogPrimitive from "@radix-ui/react-alert-dialog"
import { cn } from "@/lib/utils"
const AlertDialogRoot = AlertDialogPrimitive.Root
const AlertDialogTrigger = AlertDialogPrimitive.Trigger
const AlertDialogPortal = AlertDialogPrimitive.Portal
const AlertDialogOverlay = React.forwardRef
React.ElementRef<typeof AlertDialogPrimitive.Overlay>,
React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Overlay>
>(({ className, ...props }, ref) => (
<AlertDialogPrimitive.Overlay
className={cn(
"fixed inset-0 z-50 bg-black/80 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0",
className
)}
{...props}
ref={ref}
/>
))
AlertDialogOverlay.displayName = AlertDialogPrimitive.Overlay.displayName
// ... all other subcomponents (Content, Header, Footer, Title, Description, Action, Cancel)
// Create the main component with subcomponents attached
const AlertDialog = Object.assign(AlertDialogRoot, {
Trigger: AlertDialogTrigger,
Portal: AlertDialogPortal,
Overlay: AlertDialogOverlay,
Content: AlertDialogContent,
Header: AlertDialogHeader,
Footer: AlertDialogFooter,
Title: AlertDialogTitle,
Description: AlertDialogDescription,
Action: AlertDialogAction,
Cancel: AlertDialogCancel,
})
export { AlertDialog }now you can use it like you are used to: import { AlertDialog } from "@/components/ui/alert-dialog"
<AlertDialog>
<AlertDialog.Trigger>Show Dialog</AlertDialog.Trigger>
<AlertDialog.Content>
<AlertDialog.Header>
<AlertDialog.Title>Are you absolutely sure?</AlertDialog.Title>
<AlertDialog.Description>
This action cannot be undone...
</AlertDialog.Description>
</AlertDialog.Header>
<AlertDialog.Footer>
<AlertDialog.Cancel>Cancel</AlertDialog.Cancel>
<AlertDialog.Action>Continue</AlertDialog.Action>
</AlertDialog.Footer>
</AlertDialog.Content>
</AlertDialog> |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I use Mantine for all my projects, but I am looking at shadcn.
One of the features I prefer with Mantine is that subcomponents are fields of the main component. So, for example, to use the AlertDialog, instead of this import statement:
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
AlertDialogTrigger,
} from "@/components/ui/alert-dialog"
just do this:
import { AlertDialog } from "@/components/ui/alert-dialog"
and use it like this:
Show Dialog Are you absolutely sure? This action cannot be undone. This will permanently delete your account from our servers. Cancel ContinueIt's much easier to just add the one component to the import statement and reduces clutter.
Beta Was this translation helpful? Give feedback.
All reactions