Skip to content

Commit

Permalink
feat(Popover): make opened prop update internal opened state in realtime
Browse files Browse the repository at this point in the history
  • Loading branch information
p-sw committed Jun 30, 2024
1 parent c1289b6 commit c52a884
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions packages/react/components/Popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,20 @@ interface PopoverProps extends AsChild {
}

const Popover = ({ children, opened, asChild }: PopoverProps) => {
const state = React.useState<IPopoverContext>({
const [state, setState] = React.useState<IPopoverContext>({
opened: opened ?? false,
});

useEffect(() => {
if (opened !== undefined) {
setState((p) => ({ ...p, opened: opened }));
}
}, [opened]);

const Comp = asChild ? Slot : "div";

return (
<PopoverContext.Provider value={state}>
<PopoverContext.Provider value={[state, setState]}>
<Comp className="relative">{children}</Comp>
</PopoverContext.Provider>
);
Expand Down

0 comments on commit c52a884

Please sign in to comment.