import * as React from "react"; import { CSSTransition } from "react-transition-group"; import styles from "./Popup.module.scss"; import classNames from "classnames"; import { useOnFocus, useOnFocusOut, useOnHover, useOnHoverOut, } from "../hooks"; import { BasePopup, BasePopupPosition as PopupPosition, } from "./BasePopup"; import { PopupLayoutContainer } from "./PopupLayoutContainer"; export interface PopupProps extends React.ComponentPropsWithoutRef<"div"> { /** * The trigger for the Popup */ children: React.ReactElement; /** * If the Popup has an indicator * * @default false */ hasIndicator?: boolean; /** * If the Popup is open * * @default false */ isOpen?: boolean; /** * The callback fired when the Popup closes */ onClose: (itemValue?: string) => void; /** * The callback fired when the Popup opens */ onOpen: () => void; /** * The position of the Popup * * @default "bottom" */ position?: PopupPosition; /** * The text for the Popup */ text: string; } /** * @public * * @description * * The Popup component is a display that shows when hovering * over an element. */ export const Popup: React.FC = (props) => { const { className, children, hasIndicator = false, isOpen = false, onClose, onOpen, position = "bottom", text, ...rest } = props; const popupRef = React.useRef(null); const triggerRef = React.useRef(null); // Show Popup on focus or hover useOnFocus(triggerRef, onOpen); useOnHover(triggerRef, onOpen); // Hide Popup on focus out or hover out useOnFocusOut(triggerRef, onClose); useOnHoverOut(triggerRef, onClose); return (
{React.cloneElement(React.Children.only(children), { ref: triggerRef, role: "button", tabIndex: 0, })}
); }; Popup.displayName = "Popup";