Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to replace default close button for Dialog ? #1871

Closed
apudiu opened this issue Oct 31, 2023 · 15 comments · May be fixed by #6754
Closed

How to replace default close button for Dialog ? #1871

apudiu opened this issue Oct 31, 2023 · 15 comments · May be fixed by #6754
Labels

Comments

@apudiu
Copy link

apudiu commented Oct 31, 2023

Hello, it may be so obvious but I didn't found way to replace default close button with my custom icon, In the docs I've found that custom close button can be added but didn't mentioned how to hide the existing one or replace the icon.

I really like this UI, unfortunately the docs seems very incomplete. Can I get some help on this?

Why this is an issue? becoz I think there's should be way to hide / replace default close button.

@vitorsorato
Copy link

I have the same doubt.

@servesh-chaturvedi
Copy link

Did you check this?

@vitorsorato
Copy link

The issue would be to remove the current close button in order to replace it with a custom one.

@Medkhat
Copy link

Medkhat commented Nov 6, 2023

Hi there! When you will install the Sheet component, you will also able to customize this component. For example, you have a components/ui/sheet.tsx, so open this file and customize the component below:

image

@yogiyendri
Copy link

Hi there! When you will install the Sheet component, you will also able to customize this component. For example, you have a components/ui/sheet.tsx, so open this file and customize the component below:

image

but, what if we want to use sheet on various pages and with different closed shapes.

@Medkhat
Copy link

Medkhat commented Nov 9, 2023

@yogiyendri
Hi!
You can separately declare this button and call it whenever you need ))

1. Declare like this

image

2. Use when you want

image

@shadcn shadcn added the Stale label Feb 5, 2024
@shadcn
Copy link
Collaborator

shadcn commented Feb 13, 2024

This issue has been automatically closed because it received no activity for a while. If you think it was closed by accident, please leave a comment. Thank you.

@shadcn shadcn closed this as completed Feb 13, 2024
@myketheguru
Copy link

myketheguru commented Mar 26, 2024

Hello, it may be so obvious but I didn't found way to replace default close button with my custom icon, In the docs I've found that custom close button can be added but didn't mentioned how to hide the existing one or replace the icon.

I really like this UI, unfortunately the docs seems very incomplete. Can I get some help on this?

Why this is an issue? becoz I think there's should be way to hide / replace default close button.

If you want to use your own custom icon, either in the Dialog or in the Sheet, you might need to add an extra prop to allow for this.

  1. Open the dialog file in your components folder
  • This could be @/components/ui/dialog
  1. Next, create a custom type

type ModifiedDialogContentType = React.ForwardRefExoticComponent<
(DialogPrimitive.DialogContentProps & { removeCloseIcon?: boolean }) &
React.RefAttributes

;

  1. Replace the types passed in forwardRef from typeof DialogPrimitive.Content to ModifiedDialogContentType. The DialogContent component should look like this

const DialogContent = React.forwardRef<
React.ElementRef,
React.ComponentPropsWithoutRef

(({ className, children, removeCloseIcon, ...props }, ref) => (

{children} {removeCloseIcon ? null : ( Close )} ));

Note how I used conditional rendering to hide/show the close button, by defining a prop called removeCloseIcon. Now you can disable the close icon where and when you do not need it.

...

Thanks

@SURVI1508
Copy link

Did you check this?

here i want to hide default close (x) button place on top right.

@artias13
Copy link

artias13 commented Apr 5, 2024

Did you check this?

here i want to hide default close (x) button place on top right.

you managed to?

@siradji
Copy link

siradji commented Apr 9, 2024

removeCloseIcon

@ArsenKakasyan , I did get to hide it using ref. It is the only child of type button in the dialog content that is why i used a generic selector. You can be more specific with the selector. Not the best way of doing this but it works fine for me.

import React, {useEffect, useRef} from 'react';
import {Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle} from "@/components/ui/dialog";

export const PaywallDialog: React.FC<{open: boolean}>= (props) => {
    const ref= useRef<HTMLDivElement | null>(null)

    useEffect(() => {
        if(ref?.current) {
            ref.current?.querySelector(`button`)?.remove()
        }
    }, [])
    return (
        <Dialog  open={props.open} modal={true} defaultOpen={true} >
            <DialogContent autoFocus={false} ref={ref} className="sm:max-w-[425px]">
                <DialogHeader>
                    <DialogTitle className="text-xl text-center">Account approval pending</DialogTitle>
                    <DialogDescription className="text-center">
                        Please contact Nana Admin to get your account approved
                    </DialogDescription>
                </DialogHeader>
                <div>
                </div>
            </DialogContent>
        </Dialog>
    )
};

@t2tx
Copy link

t2tx commented Apr 9, 2024

It can be controled by adding attribute to DialogContent.

const DialogContent = React.forwardRef<
  React.ElementRef<typeof DialogPrimitive.Content>,
  React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content> & {
    hideCloseButton?: boolean;
  }
>(({className, children, hideCloseButton, ...props}, ref) => (
  <DialogPortal>
    <DialogOverlay />
    <DialogPrimitive.Content
      ref={ref}
      className={cn(
        'fixed left-[50%] top-[50%] z-50 grid w-full max-w-lg translate-x-[-50%] translate-y-[-50%] gap-4 border bg-background p-6 shadow-lg duration-200 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[state=closed]:slide-out-to-left-1/2 data-[state=closed]:slide-out-to-top-[48%] data-[state=open]:slide-in-from-left-1/2 data-[state=open]:slide-in-from-top-[48%] sm:rounded-lg',
        className,
      )}
      {...props}>
      {children}
      {!hideCloseButton && (
        <DialogPrimitive.Close className="absolute right-4 top-4 rounded-sm opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:pointer-events-none data-[state=open]:bg-accent data-[state=open]:text-muted-foreground">
          <X className="h-4 w-4" />
          <span className="sr-only">Close</span>
        </DialogPrimitive.Close>
      )}
    </DialogPrimitive.Content>
  </DialogPortal>
));
DialogContent.displayName = DialogPrimitive.Content.displayName;

@CodeMode365
Copy link

Modify your dialog content code to:


interface DialogContentProps
  extends React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content> {
  showCloseIcon?: boolean; // Add the showCloseIcon prop type here
}

const DialogContent = React.forwardRef<
  React.ElementRef<typeof DialogPrimitive.Content>,
  DialogContentProps
>(({ className, showCloseIcon = true, children, ...props }, ref) => (
  <DialogPortal>
    <DialogOverlay />
    <DialogPrimitive.Content
      ref={ref}
      className={cn(
        "fixed left-[50%] top-[50%] z-50 grid w-full max-w-lg translate-x-[-50%] translate-y-[-50%] gap-4 border border-zinc-200 bg-white p-6 shadow-lg duration-200 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[state=closed]:slide-out-to-left-1/2 data-[state=closed]:slide-out-to-top-[48%] data-[state=open]:slide-in-from-left-1/2 data-[state=open]:slide-in-from-top-[48%] sm:rounded-lg dark:border-zinc-800 dark:bg-zinc-950",
        className
      )}
      {...props}
    >
      {children}
      {showCloseIcon && (
        <DialogPrimitive.Close className="absolute right-4 top-4 rounded-sm opacity-70 ring-offset-white transition-opacity hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-zinc-950 focus:ring-offset-2 disabled:pointer-events-none data-[state=open]:bg-zinc-100 data-[state=open]:text-zinc-500 dark:ring-offset-zinc-950 dark:focus:ring-zinc-300 dark:data-[state=open]:bg-zinc-800 dark:data-[state=open]:text-zinc-400">
          <X className="h-4 w-4" />
          <span className="sr-only">Close</span>
        </DialogPrimitive.Close>
      )}
    </DialogPrimitive.Content>
  </DialogPortal>
));
DialogContent.displayName = DialogPrimitive.Content.displayName;

@samir4055
Copy link

It can be controled by adding attribute to DialogContent.

const DialogContent = React.forwardRef<
  React.ElementRef<typeof DialogPrimitive.Content>,
  React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content> & {
    hideCloseButton?: boolean;
  }
>(({className, children, hideCloseButton, ...props}, ref) => (
  <DialogPortal>
    <DialogOverlay />
    <DialogPrimitive.Content
      ref={ref}
      className={cn(
        'fixed left-[50%] top-[50%] z-50 grid w-full max-w-lg translate-x-[-50%] translate-y-[-50%] gap-4 border bg-background p-6 shadow-lg duration-200 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[state=closed]:slide-out-to-left-1/2 data-[state=closed]:slide-out-to-top-[48%] data-[state=open]:slide-in-from-left-1/2 data-[state=open]:slide-in-from-top-[48%] sm:rounded-lg',
        className,
      )}
      {...props}>
      {children}
      {!hideCloseButton && (
        <DialogPrimitive.Close className="absolute right-4 top-4 rounded-sm opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:pointer-events-none data-[state=open]:bg-accent data-[state=open]:text-muted-foreground">
          <X className="h-4 w-4" />
          <span className="sr-only">Close</span>
        </DialogPrimitive.Close>
      )}
    </DialogPrimitive.Content>
  </DialogPortal>
));
DialogContent.displayName = DialogPrimitive.Content.displayName;

When we update the plugin in the future, this code may not work entire code will be replaced by shadcn component

@pranavmappoli
Copy link

@shadcn Please consider of adding the prop hideCloseButton

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.