Popover manages open state internally and doesn't expose a way to change this.
To keep track of the open state however, you could use a useEffect
inside the render prop.
const [isPopoverOpen, setIsPopoverOpen] = useState(false);
<Popover>
{({open}: {open: boolean}) => {
useEffect(() => {
setIsPopoverOpen(open)
}, [open]);
return (
<>
<Popover.Button>
Test
</Popover.Button>,
{open && (
<Popover.Panel static>
Test
</Popover.Panel>
)}
</>
)
}}
</Popover>
The toggling behavior is being handled by <Popover.Button>
component. Alternatively, Popover
and Popover.Panel
exposes a close()
method to close the popover. You could always use Portals to make the component available in parent for handling either toggling or executing the close()
method.
import { createPortal } from 'react-dom';
<Popover>
{({ open, close }) => {
return (
<>
{createPortal(
<Popover.Button>
Toggle popover
</Popover.Button>,
// html node where we will position this button
)}
{open && (
<Popover.Panel static>
Test
</Popover.Panel>
)}
{createPortal(
<button onClick={close()}>
Close Popover
</button>,
// html node where we will position this close button
)}
</>
)
}}
</Popover>
close()
function for you to cover most use cases. – Becalmeddocument. getElementById("#cart_button").click()
. Even Shopify default themes do the same. So it's common to do it with VanillaJS I believe. – Becalmed