ui icon indicating copy to clipboard operation
ui copied to clipboard

cn doesn't override styles on DialogClose

Open Sashkan opened this issue 1 year ago • 1 comments

I'm working on a Command component, which uses the Dialog component.

The base version of the Dialog component includes a DialogClose by default in its content. I'd like to override the styling of this Close button, so I came up with this piece of code:


export default function Search() {
  const [isModalOpen, setIsModalOpen] = useState(false);

  const handleOpenSearchModal = useCallback(() => {
    setIsModalOpen(!isModalOpen);
  }, [isModalOpen]);

  return (
    <>
      {/* Search button */}
      <Button
        variant="secondary"
        className="w-full justify-start px-2.5 py-1.5 bg-white-border-gradient"
        onClick={handleOpenSearchModal}
      >
        <SearchIcon size={16} className="text-stone-600" />
        <span className="text-sm font-semibold capitalize text-stone-400">
          Search
        </span>
      </Button>

      {/* Search modal */}
      <CommandDialogWithoutClose
        open={isModalOpen}
        onOpenChange={handleOpenSearchModal}
      >
        <div className="flex items-center justify-between gap-x-4">
          <CommandInput />
          <DialogClose
            className="relative right-0 top-0"
            onClick={handleOpenSearchModal}
          />
        </div>
      </CommandDialogWithoutClose>
    </>
  );
}

The DialogClose component uses cn to handle additional CSS classes, but AFAIK, the classes I added here (relative right-0 top-0) are not applied. How can I override them ? The "ugly" workaround I found was to remove the DialogClose completely and replace it by a custom button, wut that doesn't really solve the issue.

Sashkan avatar Jan 18 '24 15:01 Sashkan

@Sashkan

It's not the problem with cn function

Property right-0 will not work when the position is said to relative. It will only work when the position is absolute, fixed

imopbuilder avatar Jan 21 '24 06:01 imopbuilder

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 avatar Feb 12 '24 23:02 shadcn

As you can see in the first screenshot, the component <DialogClose> does not overwrite the default close button of the Dialog component. How can we overwrite with DialogClose the default close button?

(in blue my custom button and under it there is the default button that I highlighted with the red arrow) Screenshot 2024-02-28 at 12 37 59 AM

Here is my code, where I am using the Dialog Button and the DialogClose component from shadcn/ui library :

Screenshot 2024-02-28 at 12 36 32 AM

steinhardt21 avatar Feb 27 '24 22:02 steinhardt21

@steinhardt21

You can change the styles of the DialogClose button from the dialog.tsx file so that they will be consistent across the app

imopbuilder avatar Feb 28 '24 03:02 imopbuilder

Thank you @imopbuilder ! I did this but I was not sure that was the best way

steinhardt21 avatar Feb 28 '24 08:02 steinhardt21