Skip to content

Commit

Permalink
fix: controlled popover open on focus (#2825)
Browse files Browse the repository at this point in the history
  • Loading branch information
dawid-klosowski-at-brainly authored Dec 17, 2024
1 parent fd87a91 commit abd0665
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/components/popover/Popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,22 @@ export type PopoverPropsType = {

/**
* Determine if hover should affect popover visibility.
* @default true
*/
useHover?: boolean;

/**
* Determine if click should affect popover visibility.
* @default true
*/
useClick?: boolean;

/**
* Determine if focus should affect popover visibility.
* @default true
*/
useFocus?: boolean;

/**
* Only controlled component. Handle Popover open state change.
*/
Expand All @@ -63,6 +71,7 @@ const Popover = ({
id,
useHover,
useClick,
useFocus,
defaultOpen = false,
open,
role,
Expand All @@ -76,6 +85,7 @@ const Popover = ({
onOpenChange,
useHover,
useClick,
useFocus,
role,
});

Expand Down
8 changes: 6 additions & 2 deletions src/components/popover/usePopover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
useClick as useFloatingClick,
useHover as useFloatingHover,
useRole,
useFocus,
useFocus as useFloatingFocus,
offset,
flip,
arrow,
Expand All @@ -29,6 +29,7 @@ interface UsePopoverPropTypes {
open?: boolean;
useHover?: boolean;
useClick?: boolean;
useFocus?: boolean;
role?: PopoverRole;
onOpenChange?: (arg0: boolean) => void;
}
Expand All @@ -40,6 +41,7 @@ const usePopover = ({
open,
useHover = true,
useClick = true,
useFocus = true,
role = 'dialog',
onOpenChange,
}: UsePopoverPropTypes) => {
Expand Down Expand Up @@ -130,7 +132,9 @@ const usePopover = ({
blockPointerEvents: true,
}),
});
const focus = useFocus(data.context);
const focus = useFloatingFocus(data.context, {
enabled: useFocus,
});
const click = useFloatingClick(data.context, {
enabled: useClick,
});
Expand Down

0 comments on commit abd0665

Please sign in to comment.