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

Controlled drawer with modal={false} does not work because of bug in useControllableState #492

Open
skirsten opened this issue Oct 19, 2024 · 4 comments

Comments

@skirsten
Copy link

skirsten commented Oct 19, 2024

This took me a while to trace down, but basically a Drawer that is controlled and has modal false e.g.:

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

return (
  <>
    <Drawer open={open} onOpenChange={setOpen} modal={false}>
      {/* ... */}
    </Drawer>
    {/* Button somewhere else outside */}
    <Button onClick={() => setOpen(true)}>Open</Button>
  </>
);

and open is set from somewhere outside, it does not get to this line to hack the pointerEvents:

document.body.style.pointerEvents = 'auto';

because if the prop changes it does not get to this line to call the handleChange function here:

if (value !== prop) handleChange(value as T);

So to conclude, under these circumstances it will not remove the pointerEvents = "none" 😢

Also I am curious why these awful hacks with the pointerEvents are necessary instead of just setting modal={false} on the DialogPrimitive.Root? Maybe this would break something else?

@skirsten
Copy link
Author

skirsten commented Oct 19, 2024

Actually, I think the useControllableState works as expected. Its probably not great if the changing of the prop would cause the handleChange to be called (which would probably cause a loop).

So I guess the handleChange function should not contain any code with side effects meaning the current logic would have to be moved to a useEffect 🤔

Edit: On first look using the upstream modal prop and removing all the hacks seems to work just fine (at least in my limited test case): https://github.com/skirsten/vaul/tree/no-hacks

@NicHaley
Copy link

Running into an issue with this as well

@xharris
Copy link

xharris commented Nov 9, 2024

Currently I'm doing this as a workaround:

<Drawer.Root
      open={isOpen}
      onOpenChange={(v) => {
        setIsOpen(v)
        document.body.style.pointerEvents = "auto"
      }}
      modal={false}
    >
</Drawer.Root>

It doesn't work reliably and feels like a hack to undo a hack. 🤷

Edit:
Nevermind, I just put this in the css instead:

body {
  pointer-events: all !important;
}

Not an ideal solution.

@BurgerBurglar
Copy link

body {
  pointer-events: all !important;
}

this worked great! thank you!

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

No branches or pull requests

4 participants