primitives icon indicating copy to clipboard operation
primitives copied to clipboard

Popover opened inside a Dialog closes immediately under certain circumstances

Open leofavre opened this issue 3 years ago β€’ 14 comments

Bug report

Current Behavior

A Popover opened inside a Dialog closes immediately when:

  • @radix-ui/react-dialog version is 1.0.4;
  • @radix-ui/react-popover version is 1.0.6;
  • Popover.Trigger re-renders according to the Popover.Root state;
  • Popover.Portal is present;

https://github.com/radix-ui/primitives/assets/125764/9df60724-6096-45c2-9b75-797a7701e575

Expected behavior

The Popover should stay opened when the trigger is clicked.

Reproducible example

Code Sandbox

Additional context

  • Fails on Chrome, Safari and Firefox;

Your environment

Software Name(s) Version
Radix Package(s) @radix-ui/react-dialog; @radix-ui/react-popover 1.0.4 and 1.0.6 respectively
React n/a 18.2.0
Browser Chrome 115.0.5790.170 (Official Build) (arm64)
Assistive tech n/a
Node n/a 16.19.0
npm/yarn yarn 1.22.19
Operating System MacOS 13.4.1 (c) (22F770820d)

leofavre avatar Aug 18 '23 16:08 leofavre

It looks like to be https://github.com/radix-ui/primitives/issues/2180. Upgrade to RC version should work

Codesandbox

joaom00 avatar Aug 19 '23 00:08 joaom00

@joaom00, any idea when the official release will be?

leofavre avatar Aug 22 '23 17:08 leofavre

Not sure

joaom00 avatar Aug 22 '23 17:08 joaom00

@leofavre facing the same issue too – looks like downgrading to react-dialog to 1.0.3 and react-popover to 1.0.5 is a good temporary fix – this bug doesn't happen in those version :)

steven-tey avatar Sep 21 '23 16:09 steven-tey

Hey @steven-tey! Have you tested this fix on Safari? We are having the same issue with popovers and dialogs with safari browsers in https://github.com/open-sauced/app/issues/1144. Chrome and firefox works as expected.

Thanks in advance πŸ™

OgDev-01 avatar Oct 20 '23 12:10 OgDev-01

We have this problem also. Implemented an ugly temporary fix by using a cooldown like this:

const [cooldown, setCooldown] = useState(false);
  const [open, setOpen] = useState(false);
  function handleSet() {
    if (cooldown) return;
    console.log(β€˜rex’);
    setOpen(!open);
    setCooldown(true);
    setTimeout(() => {
      setCooldown(false);
    }, 200);
  }

CarelessCourage avatar Oct 25 '23 10:10 CarelessCourage

Can confirm that the fix from @CarelessCourage works as well! Ended up needing to upgrade react-dialog and react-popover to the latest version so this fix came in handy – thank you!

steven-tey avatar Nov 03 '23 01:11 steven-tey

Am seeing this as well. In our case we have an onclick handler inside a DropdownMenu item (from react-dropdown-menu) setting an isPopoverOpen state variable. But as soon as the PopOver is rendered, the Popover is triggering onOpenChange(false)

nzjoel1234 avatar Nov 21 '23 23:11 nzjoel1234

@nzjoel1234 I had the same problem and for me setting modal=true on popover fix this.

function StatusItem({ status }: StatusItemProps) {
 const [openChangeColor, setOpenChangeColor] = useState(false);

 return (
   <div className="border bg-body-background md:bg-background h-16 rounded-[20px] px-7 flex flex-row items-center shrink-0">
     <Popover
       open={openChangeColor}
       onOpenChange={setOpenChangeColor}
       modal={true}
     >
       <PopoverAnchor>
         <div
           className="w-[15px] h-[15px] rounded-full mx-4"
           style={{ backgroundColor: status.color }}
         />
       </PopoverAnchor>
       <PopoverContent>Color Picker</PopoverContent>
     </Popover>
     <span className="text-foreground font-medium text-sm">{status.name}</span>
     <div className="flex-grow flex-1"></div>
     <DropdownMenu>
       <DropdownMenuTrigger asChild>
         <button type="button" className="outline-none">
           <MoreVertical className="h-[18px] w-[18px] text-gray1" />
         </button>
       </DropdownMenuTrigger>
       <DropdownMenuContent className="md:w-52">
         <DropdownMenuItem>
           <Pencil className="mr-[10px] h-5 w-5" />
           <span>Change Name</span>
         </DropdownMenuItem>
         <DropdownMenuItem onSelect={() => setOpenChangeColor(true)}>
           <Palette className="mr-[10px] h-5 w-5" />
           <span>Change Color</span>
         </DropdownMenuItem>
       </DropdownMenuContent>
     </DropdownMenu>
   </div>
 );
}

Garwend avatar Dec 03 '23 12:12 Garwend

Running into this as well, but haven't yet been able to figure our a workaround that works for us.

jasonyavorsky avatar Dec 05 '23 14:12 jasonyavorsky

My issue was caused by the fact that closing the DropdownMenu auto focuses the DropdownMenu.Trigger.

Since the DropdownMenu.Trigger is outside of my Popover the focus of the Trigger causes the Popover to close.

To resolve this I added the following:

  <DropdownMenu.Content
    onCloseAutoFocus={(e) => {
      e.preventDefault();
    }}

nzjoel1234 avatar Dec 06 '23 01:12 nzjoel1234

@leofavre facing the same issue too – looks like downgrading to react-dialog to 1.0.3 and react-popover to 1.0.5 is a good temporary fix – this bug doesn't happen in those version :)

Thanks!! For me working in both browsers firefox and chrome

ycaruso avatar Dec 15 '23 09:12 ycaruso

Same happened for me. I have a popover command dropdown UX inside an alert dialog.

<AlertDialog... <Popover... <Command...

and basically my popover was auto closing.

Solution is to set <Popover modal={true} ... > AND <Popover.Content onOpenAutoFocus={e => e.preventDefault()} ... >

@nzjoel1234's solution worked but then triggered infinite recursion like https://github.com/radix-ui/primitives/issues/2390 as something fought over auto-focus. I had to prevent that by stopping the auto focus on the popover content πŸ€·β€β™‚οΈ.

desduggan avatar Dec 18 '23 19:12 desduggan

Should this be closed? What if one wants to focus inside the popover in a modal?

sohrab- avatar Dec 31 '24 09:12 sohrab-