Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to hide the X at the top of the shadcn dialog box?

Tags:

shadcnui

The shadcn/ui dialog box by default as a X at the top right hand corner. My design does not have the X. How to hide the X while retaining the esc to close functionality ?

Thanks very much.

enter image description here

like image 414
EBDS Avatar asked Dec 20 '25 21:12

EBDS


2 Answers

If you don't want to edit the Dialog component directly, you can add this class to DialogContent:

<DialogContent className="[&>button]:hidden">

This way the close button will be hidden.

like image 97
AndreVitorio Avatar answered Dec 24 '25 06:12

AndreVitorio


The current accepted answer ( and also the most upvoted) has problems depending on how you wish to implement the dialog close button.

The most upvoted answer:


<DialogContent className="[&>button]:hidden">

has some problems:

  • hides any other button element added directly inside the DialogContent as child.

  • the current accepted answer's approach gets overriden when you update shadcn ui.

This new solution:


<DialogContent className="[&>button:last-child]:hidden">

  • doesn't need you to remove <DialogPrimitive.Close> used by shadcn.

  • you can use the <DialogClose> or nest any <button> inside the <DialogContent> without worrying about it being hidden by [&>button]:hidden.

like image 39
Henry Ozoani Avatar answered Dec 24 '25 06:12

Henry Ozoani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!