ui icon indicating copy to clipboard operation
ui copied to clipboard

Bug: Flickering In Command Component When Using In Dialog

Open orelhochenboym opened this issue 1 year ago • 20 comments

When using the Command component in a Dialog (the CommandDialog component) when the dialog/command flickers.

There is a video demonstrating:

https://github.com/shadcn-ui/ui/assets/45822217/dd76ec3a-0783-446a-b308-033038bbc0b3

The code of the component itself is unchanged from the cli, the usage of it in the code is the following: code

<> <Button onClick={() => setOpen(true)} variant="outline" className={cn( 'relative w-full justify-start text-sm text-muted-foreground sm:pr-12 md:w-40 lg:w-64 secondary' )} > <span className="hidden lg:inline-flex">Search ticker or cik...</span> <span className="inline-flex lg:hidden">Search...</span> <kbd className="pointer-events-none absolute right-2.5 top-auto hidden h-5 select-none items-center gap-1 rounded border bg-muted px-1.5 font-mono text-[10px] font-medium opacity-100 sm:flex"> <span className="text-xs">/</span> </kbd> </Button> <CommandDialog open={open} onOpenChange={setOpen}> <CommandInput placeholder="Search ticker or cik..." /> <CommandList> <CommandEmpty>No stocks found.</CommandEmpty> <CommandGroup> {companies.map((company) => ( <CommandItem key={company.cik}>{company.name}</CommandItem> ))} </CommandGroup> </CommandList> </CommandDialog> </>

orelhochenboym avatar Aug 26 '23 10:08 orelhochenboym

Also, I am noticing that the CommandItems are a bit blurry... any solution for that?

I have researched this issue a bit more, the flickering seems to happen only when I render a large amount of CommandItems (companies array has about 7K items. That causes lag when searching as well). When I reduce the amount of items in the companies array, it seems not to flicker...

orelhochenboym avatar Aug 26 '23 13:08 orelhochenboym

Is it possible that the data has duplicate keys ? Anything in the console ?

Joshuajrodrigues avatar Aug 30 '23 08:08 Joshuajrodrigues

The data doesn't have duplicate keys nor any errors popup in the console.

orelhochenboym avatar Aug 30 '23 09:08 orelhochenboym

I have the same problem with a Dialog containing a Form. Isn't there any solution yet?

jacovzap avatar Sep 16 '23 05:09 jacovzap

Got the same bug too with Select Component nested in Dialog. During debugging, I found out flicker disappears when 'SelectContent' is commented out.

Idan-Garay avatar Sep 27 '23 03:09 Idan-Garay

Radix-ui dialog and alert dialog seems to work without flickering (with Select Component) and I made a temporary fix by removing "data-[state=open]:animate-in" from shadcn's DialogPrimitive.Content className in my project.

Idan-Garay avatar Sep 27 '23 07:09 Idan-Garay

I've noticed that in my case, the flickering dissapears when I remove the strict mode tag, with this, the components only renders one time, so I expect the flickering won't be visible in production.

jacovzap avatar Sep 27 '23 14:09 jacovzap

Radix-ui dialog and alert dialog seems to work without flickering (with Select Component) and I made a temporary fix by removing "data-[state=open]:animate-in" from shadcn's DialogPrimitive.Content className in my project.

This was the culprit for me. Thank you!

Have you tested this in production without commenting out data-[state=open]:animate-in?

byonghun avatar Sep 27 '23 19:09 byonghun

 <DialogPrimitive.Content
      onClick={(event) => event.stopPropagation()}
      ref={ref}
      className={cn(
        "animate-in data-[state=open]:fade-in-90 data-[state=open]:slide-in-from-bottom-10 sm:zoom-in-90 data-[state=open]:sm:slide-in-from-bottom-0 fixed z-50 grid gap-4 rounded-b-lg border bg-background shadow-lg focus:outline-none dark:border-none sm:rounded-lg",
        className
      )}
      {...props}
    >
      {children}
    </DialogPrimitive.Content>
    
    
    I had a flickering issue when i had a popup that was triggered by a button on an accordion. This flickering issue kept opening the accordion in the background because of event bubbling. 
    
    I therefore added ` onClick={(event) => event.stopPropagation()}` to my dialog content


LukeM-97 avatar Oct 13 '23 13:10 LukeM-97

 <DialogPrimitive.Content
      onClick={(event) => event.stopPropagation()}
      ref={ref}
      className={cn(
        "animate-in data-[state=open]:fade-in-90 data-[state=open]:slide-in-from-bottom-10 sm:zoom-in-90 data-[state=open]:sm:slide-in-from-bottom-0 fixed z-50 grid gap-4 rounded-b-lg border bg-background shadow-lg focus:outline-none dark:border-none sm:rounded-lg",
        className
      )}
      {...props}
    >
      {children}
    </DialogPrimitive.Content>
    
    
    I had a flickering issue when i had a popup that was triggered by a button on an accordion. This flickering issue kept opening the accordion in the background because of event bubbling. 
    
    I therefore added ` onClick={(event) => event.stopPropagation()}` to my dialog content

This doesn't work for me unfortunately

orelhochenboym avatar Oct 21 '23 14:10 orelhochenboym

I've noticed that in my case, the flickering dissapears when I remove the strict mode tag, with this, the components only renders one time, so I expect the flickering won't be visible in production.

This doesn't work for me either

orelhochenboym avatar Oct 21 '23 14:10 orelhochenboym

Radix-ui dialog and alert dialog seems to work without flickering (with Select Component) and I made a temporary fix by removing "data-[state=open]:animate-in" from shadcn's DialogPrimitive.Content className in my project.

This works for me but obviously gets rid of the starting/opening animation. This solution is only temporary and should be addressed. Leaving this issue open so @shadcn can fix it

orelhochenboym avatar Oct 21 '23 14:10 orelhochenboym

I believe it is related to https://github.com/radix-ui/primitives/issues/2532.

The data-[state=open]:animate-in caused this issue because its keyframes only have the from keyword.

To temporarily fix this issue in shadcn-ui, we need to rewrite our animation keyframes that contain both the from and to keywords.

mosquitochang avatar Nov 17 '23 04:11 mosquitochang

When I execute dialog in my iPhone, I saw flickering. After I remove data-[state=open]:animate-in data-[state=closed]:animate-out, flickering is disappeared.

    <DialogPrimitive.Content
      ref={ref}
      // remove "data-[state=open]:animate-in data-[state=closed]:animate-out" to avoid flickering
      // https://github.com/shadcn-ui/ui/issues/1351
      className={cn(
        "bg-background 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%] fixed left-[50%] top-[50%] z-50 grid w-full max-w-lg translate-x-[-50%] translate-y-[-50%] gap-4 border p-6 shadow-lg duration-200 sm:rounded-lg md:w-full",
        className,
      )}
      // original
      // className={cn(
      //   "bg-background 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%] fixed left-[50%] top-[50%] z-50 grid w-full max-w-lg translate-x-[-50%] translate-y-[-50%] gap-4 border p-6 shadow-lg duration-200 sm:rounded-lg md:w-full",
      //   className,
      // )}
      {...props}
    >
      {children}
      <DialogPrimitive.Close className="ring-offset-background focus:ring-ring data-[state=open]:bg-accent data-[state=open]:text-muted-foreground absolute right-4 top-4 rounded-sm opacity-70 transition-opacity hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-offset-2 disabled:pointer-events-none">
        <X className="h-4 w-4" />
        <span className="sr-only">Close</span>
      </DialogPrimitive.Close>
    </DialogPrimitive.Content>

hayato94087 avatar Nov 17 '23 16:11 hayato94087

Also, I am noticing that the CommandItems are a bit blurry... any solution for that?

I have researched this issue a bit more, the flickering seems to happen only when I render a large amount of CommandItems (companies array has about 7K items. That causes lag when searching as well). When I reduce the amount of items in the companies array, it seems not to flicker...

I'm not sure about your specific use case but I also had the same issue with the items being blurry. It was caused by the border-b on the CommandInput. I had mine set to [0.5px] and the blurring went away when I removed it.

@shadcn - this blurring is in the example in the docs too - in the Dialog version - just fyi!

stuartakeero avatar Jan 31 '24 12:01 stuartakeero

i had a similar issue, in which the select and the popover elements where flickering when i was trying to close them. I was able to overcome that by removing data-[state=closed]:animate-out from the PopoverContent and SelectContent components.

rogue-kitten avatar Mar 19 '24 14:03 rogue-kitten

I solved this by removing animate-in from my tailwind.config.ts file. The issue was that the tailwind plugin tailwindcss-animate provides you with an animate-in class while I was defining my custom animate-in animation in my config file from before I was using shadcn-ui. So basically both were running, hence the flicker. Make sure you don't have duplicate definitions of the animate-in class.

TimurKr avatar Mar 21 '24 13:03 TimurKr

In my case, the hover to CommandItem doesn't work... any comments about it?

Edjhernandez avatar Mar 29 '24 13:03 Edjhernandez

const CommandItem = React.forwardRef< React.ElementRef<typeof CommandPrimitive.Item>, React.ComponentPropsWithoutRef<typeof CommandPrimitive.Item>

(({ className, ...props }, ref) => ( <CommandPrimitive.Item ref={ref} className={cn( "relative flex cursor-default select-none items-center rounded-sm px-2 py-1.5 text-sm outline-none aria-selected:bg-primary aria-selected:text-white aria-selected:font-bold ", // "relative flex cursor-default select-none items-center rounded-sm px-2 py-1.5 text-sm outline-none aria-selected:bg-primary aria-selected:text-white aria-selected:font-bold data-[disabled]:pointer-events-none data-[disabled]:opacity-50 ", className )} {...props} /> ));

above chages will help to working onClick function on Child of CommandItem

TejasThombare20 avatar Apr 18 '24 20:04 TejasThombare20

removing this **data-[state=open]:animate-in** works for me, thanks

satyam-agsft avatar May 08 '24 10:05 satyam-agsft

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 Jun 29 '24 23:06 shadcn

Just in case, this code on my main css file worked for me. I know that applying the style to the entire document can be unnecessary, but you can apply the style rules to your specific components.

* {
  animation-fill-mode: forwards;
  -webkit-animation-fill-mode: forwards;
}

SWatchmaker avatar Aug 03 '24 00:08 SWatchmaker