Skip to content

Commit

Permalink
feat(Popover): move role hook into Popup
Browse files Browse the repository at this point in the history
  • Loading branch information
amje committed Dec 5, 2024
1 parent 8a53684 commit b6f7912
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
15 changes: 10 additions & 5 deletions src/components/Popup/Popup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
useDismiss,
useFloating,
useInteractions,
useRole,
useTransitionStatus,
} from '@floating-ui/react';
import type {
Expand All @@ -23,6 +24,7 @@ import type {
Strategy,
UseFloatingOptions,
UseInteractionsReturn,
UseRoleProps,
} from '@floating-ui/react';

import {useForkRef} from '../../hooks';
Expand Down Expand Up @@ -110,8 +112,8 @@ export interface PopupProps extends DOMProps, AriaLabelingProps, QAProps {
disableOutsideClick?: boolean;
/** Do not use `Portal` for children */
disablePortal?: boolean;
/** `aria-role` attribute */
role?: React.AriaRole;
/** ARIA role or special component role (select, combobox) */
role?: UseRoleProps['role'];
/** HTML `id` attribute */
id?: string;
// CSS property `z-index`
Expand Down Expand Up @@ -151,7 +153,7 @@ export function Popup({
disablePortal = false,
qa,
id,
role,
role: roleProp,
zIndex = 1000,
...restProps
}: PopupProps) {
Expand Down Expand Up @@ -221,13 +223,17 @@ export function Popup({
],
});

const role = useRole(context, {
enabled: Boolean(roleProp),
role: roleProp,
});
const dismiss = useDismiss(context, {
enabled: !disableOutsideClick || !disableEscapeKeyDown,
outsidePress: !disableOutsideClick,
escapeKey: !disableEscapeKeyDown,
});

const {getReferenceProps, getFloatingProps} = useInteractions([dismiss]);
const {getReferenceProps, getFloatingProps} = useInteractions([role, dismiss]);

React.useLayoutEffect(() => {
setGetAnchorProps?.(getReferenceProps);
Expand Down Expand Up @@ -281,7 +287,6 @@ export function Popup({
style={style}
data-qa={qa}
id={id}
role={role}
{...filterDOMProps(restProps)}
>
{hasArrow && (
Expand Down
5 changes: 2 additions & 3 deletions src/components/lab/Popover/Popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
useFloatingRootContext,
useHover,
useInteractions,
useRole,
} from '@floating-ui/react';
import type {UseInteractionsReturn} from '@floating-ui/react';

Expand Down Expand Up @@ -91,9 +90,8 @@ export function Popover({
handleClose: enableSafePolygon ? safePolygon() : undefined,
});
const click = useClick(context, {enabled: !disabled});
const role = useRole(context, {role: 'dialog'});

const {getReferenceProps, getFloatingProps} = useInteractions([hover, click, role]);
const {getReferenceProps, getFloatingProps} = useInteractions([hover, click]);

const anchorRef = useForkRef(
setAnchorElement,
Expand Down Expand Up @@ -121,6 +119,7 @@ export function Popover({
floatingProps={getFloatingProps()}
autoFocus
modalFocus
role="dialog"
className={b(null, className)}
>
{content}
Expand Down

0 comments on commit b6f7912

Please sign in to comment.