element-call/src/Popover.jsx

37 lines
929 B
React
Raw Normal View History

2021-12-14 15:28:54 -08:00
import React, { forwardRef, useRef } from "react";
import { DismissButton, useOverlay } from "@react-aria/overlays";
2021-12-06 17:34:10 -08:00
import { FocusScope } from "@react-aria/focus";
import classNames from "classnames";
import styles from "./Popover.module.css";
export const Popover = forwardRef(
({ isOpen = true, onClose, className, children, ...rest }, ref) => {
2021-12-14 15:28:54 -08:00
const fallbackRef = useRef();
const popoverRef = ref || fallbackRef;
2021-12-06 17:34:10 -08:00
const { overlayProps } = useOverlay(
{
isOpen,
onClose,
shouldCloseOnBlur: true,
isDismissable: true,
},
2021-12-14 15:28:54 -08:00
popoverRef
2021-12-06 17:34:10 -08:00
);
return (
2021-12-14 15:28:54 -08:00
<FocusScope restoreFocus>
<div
{...overlayProps}
{...rest}
className={classNames(styles.popover, className)}
ref={popoverRef}
>
{children}
<DismissButton onDismiss={onClose} />
</div>
</FocusScope>
2021-12-06 17:34:10 -08:00
);
}
);