ui icon indicating copy to clipboard operation
ui copied to clipboard

[bug]: When using Popover with PopoverAnchor instead of PopoverTrigger, onOpenChange is *never* called with true, only false.

Open josheverett opened this issue 1 year ago • 1 comments

Describe the bug

See: https://github.com/shadcn-ui/ui/discussions/1684

And: https://github.com/shadcn-ui/ui/issues/2798

When using <PopoverAnchor> instead of <PopoverTrigger> in order to manually/programmatically open the Popover (via passing a boolean state to the open prop) from unrelated components, onOpenChange is only ever called with false, and at no point will be called with true.

As a workaround, I am using a 200ms timeout when calling setting the open prop state.

Affected component/components

Popover

How to reproduce

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

[...]

<Popover
  open={open}
  onOpenChange={(isOpen) => {
    console.log('isOpen', isOpen); // is ALWAYS false
  }}
>
  <PopoverAnchor>
    [...]
  </PopoverAnchor>
  <PopoverContent>[...]</PopoverContent>
</Popover>

[...]

<div onClick={() => setOpen(true)}>some div</div>

Codesandbox/StackBlitz link

Your v0 doesn't allow dependencies to be changed, and I couldn't figure out how to get shadcn up and running on codesandbox.

Logs

No response

System Info

Chrome on 2023 MacBook Pro w/ macOS 14.1, 36GB RAM, and M3 Max

Before submitting

  • [X] I've made research efforts and searched the documentation
  • [X] I've searched for existing issues

josheverett avatar Aug 05 '24 23:08 josheverett

@josheverett As mentioned in #2798 , can you try passing a setter to onOpenChange? Work fine with that.

AtharvaJoshi16 avatar Aug 06 '24 15:08 AtharvaJoshi16

This issue has been automatically marked as stale due to one year of inactivity. It will be closed in 7 days unless there’s further input. If you believe this issue is still relevant, please leave a comment or provide updated details. Thank you. (This is an automated message)

shadcn avatar Aug 06 '25 23:08 shadcn

This issue has been automatically closed due to one year of inactivity. If you’re still experiencing a similar problem or have additional details to share, please open a new issue following our current issue template. Your updated report helps us investigate and address concerns more efficiently. Thank you for your understanding! (This is an automated message)

shadcn avatar Aug 14 '25 23:08 shadcn