I want to create a Dialog using ShadCN component library, but it has a built in close button on top right, but I want it to be somewhere else, let’s say I want to make it as a button below, how can I make it happen? I tried to create a different somewhere else and gave it an asChild attribute but that didn’t work. I think I am missing something big. Thanks everyone for help.
This is my current code and how it looks in frontend.
"use client";
import { Dialog, DialogContent, DialogTrigger, DialogTitle } from "@/components/ui/dialog"; //shadCN components
import { Button } from "@/components/ui/button";
import { DialogClose } from "@radix-ui/react-dialog";
export default function Home() {
return (
<main>
<Dialog>
<DialogTrigger>
<div>XXX</div>
</DialogTrigger>
<DialogContent className="transition delay-0 duration-0">
<DialogTitle>
asda
</DialogTitle>
<DialogClose asChild={true}>
<Button>Close</Button>
</DialogClose>
</DialogContent>
</Dialog>
</main>
);
}
I try to make the x on top right disappear.
2
Answers
There is no way to remove the default close button. But you can remove it from the
dialog.tsx
file, or you use a custom prop to handle it.To create a custom close button you can use the
DialogClose
component.Here is the documentation from the shadcn docs Custom close button
You can change the styles, such as the default position of the close button, by editing the
dialog.tsx
file. To achieve this, you just need to modify this specific part of the code in theconst DialogContent
variable. Here you have acces to both the style classes for the closing icon "X" (which is being imported from lucide-react) and its according container (DialogPrimitive.Close).