Replies: 3 comments 1 reply
-
I ended up doing: function RouteChangeAutoClosePopover(props: { children: ReactNode }) {
const { children } = props;
const [isOpen, setIsOpen] = useState(false);
const pathname = usePathname();
useEffect(() => {
setIsOpen(false);
}, [pathname]);
return (
<Popover open={isOpen} onOpenChange={setIsOpen}>
{children}
</Popover>
);
} But I would like to have a more generic approach. Maybe having an additional prop to RouteChangeAutoClosePopover named |
Beta Was this translation helpful? Give feedback.
-
+1 |
Beta Was this translation helpful? Give feedback.
-
When you make the popover controlled it seems to lose its accessibility, click-away and It's a little stupid but works if you put the This is useful for if you lots of a closable buttons like an emoji picker. import { Popover } from "radix-ui";
export default () => {
const closeButtonRef = useRef(null);
const closePopover = () => closeButtonRef.current?.click();
return (
<Popover.Root>
<Popover.Portal>
<Popover.Content>
<Popover.Close asChild>
<button ref={closeButtonRef} type="button" style={{ visibility: "hidden" }} />
</Popover.Close>
</Popover.Content>
</Popover.Portal>
</Popover.Root>
);
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi
I have an app with a Popover component for a menu.
Inside the popover, the user can navigate to a different section if the app with client-side navigation via the
Link
component fromnext/link
. When I navigate to the URL, the popover keeps being open. I wonder what is the best practice of closing it when the user clicks on the link. I can wrap it withPopover.Close
/ programmatically withonClick
- but I wonder if there is a better way to handle this case. What do you think?Thanks!
Beta Was this translation helpful? Give feedback.
All reactions