themes icon indicating copy to clipboard operation
themes copied to clipboard

Application completely freezes when trying to close dropdown-menu from another component with useState.

Open Jailawi opened this issue 2 years ago • 11 comments

Problem

I'm using a dropdown-menu component with a select-option (button) when selected opens a dialog component. I would like the drop-menu to close when the dialog is closed. I've tried with

const [open, setOpenDropMenu] = useState(false);

Used in drop-menu like:

<DropdownMenu.Root open={open} onOpenChange={setOpenDropMenu}>

The setOpenDropMenu is sent in as a prop to the dialog component and is used when the dialog is closed like:

setOpenDropMenu(false);

This works, but not all the time. It sometimes freezes the whole application and no button or link becomes clickable unless the whole website is refreshed.

Could it be a race condition problem? How can I resolve this in the best manner?

Jailawi avatar Nov 17 '23 15:11 Jailawi

Could you create a CodeSandbox reproduction?

vladmoroz avatar Nov 17 '23 15:11 vladmoroz

Could you create a CodeSandbox reproduction?

Created this: CodeSandbox

  1. Click on OpenDrop-down menu
  2. Click on Open Dialog
  3. Click on Cancel
  4. Repeat 1-3 until the application freezes and it won't be possible to click on OpenDrop-down menu (it usually happens within maximum 10 tries)

Jailawi avatar Nov 18 '23 11:11 Jailawi

Not sure why this happens, but either way it's the best way to expose a handler like onCancel and call the state setter in the dropdown component itself like so:

// In your dropdown menu component
<Dialog onCancel={() => setDropdownMenuOpen(false)} />

I checked this in your CodeSandbox and it resolves the issue you mentioned https://codesandbox.io/p/devbox/ecstatic-dawn-fmq9vq

vladmoroz avatar Nov 22 '23 10:11 vladmoroz

I tried the same steps in the CodeSandBox you linked however, the issue remains where the app freezes after 10-20 tries.

Jailawi avatar Nov 22 '23 10:11 Jailawi

Looks like this has not been fixed yet. Is there a workaround for this?

henryobiaraije avatar May 17 '24 16:05 henryobiaraije

@benoitgrelard do you have an idea for what's going on? Feels like I'm missing something obvious

vladmoroz avatar May 17 '24 17:05 vladmoroz

It seems that when closing the dialog or dropdown menu it is not cleaning correctly and pointer-events: none remains in the body. you can use setOpen(false) in the onCloseAutoFocus callback in Dialog.Content but this will wait for the Dialog exit animation. Another alternative I believe using setTimeout(() => setOpen(false)) will fix it

joaom00 avatar May 18 '24 22:05 joaom00

@joaom00 The setTimeout solves my problem when I try to close the dropdown menu after closing the dialog! Thanks!

Lexachoc avatar Sep 05 '24 12:09 Lexachoc