2023-03-14 12:58:11 -07:00
|
|
|
import {
|
|
|
|
arrow,
|
2023-03-28 12:37:35 -07:00
|
|
|
autoPlacement,
|
2023-03-14 12:58:11 -07:00
|
|
|
FloatingArrow,
|
|
|
|
offset,
|
|
|
|
useClick,
|
|
|
|
useDismiss,
|
|
|
|
useFloating,
|
2023-03-28 12:37:35 -07:00
|
|
|
useHover,
|
2023-03-14 12:58:11 -07:00
|
|
|
useInteractions,
|
|
|
|
useTransitionStyles,
|
|
|
|
} from '@floating-ui/react';
|
2023-03-28 12:37:35 -07:00
|
|
|
import clsx from 'clsx';
|
2023-03-14 12:58:11 -07:00
|
|
|
import React, { useRef, useState } from 'react';
|
|
|
|
|
|
|
|
interface IPopover {
|
|
|
|
children: React.ReactElement<any, string | React.JSXElementConstructor<any>>
|
2023-03-28 12:37:35 -07:00
|
|
|
/** The content of the popover */
|
2023-03-14 12:58:11 -07:00
|
|
|
content: React.ReactNode
|
2023-03-28 12:37:35 -07:00
|
|
|
/** Should we remove padding on the Popover */
|
|
|
|
isFlush?: boolean
|
|
|
|
/** Should the popover trigger via click or hover */
|
|
|
|
interaction?: 'click' | 'hover'
|
|
|
|
/** Add a class to the reference (trigger) element */
|
|
|
|
referenceElementClassName?: string
|
2023-03-14 12:58:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Popover
|
|
|
|
*
|
|
|
|
* Similar to tooltip, but requires a click and is used for larger blocks
|
|
|
|
* of information.
|
|
|
|
*/
|
|
|
|
const Popover: React.FC<IPopover> = (props) => {
|
2023-03-28 12:37:35 -07:00
|
|
|
const { children, content, referenceElementClassName, interaction = 'hover', isFlush = false } = props;
|
2023-03-14 12:58:11 -07:00
|
|
|
|
|
|
|
const [isOpen, setIsOpen] = useState<boolean>(false);
|
|
|
|
|
|
|
|
const arrowRef = useRef<SVGSVGElement>(null);
|
|
|
|
|
|
|
|
const { x, y, strategy, refs, context } = useFloating({
|
|
|
|
open: isOpen,
|
|
|
|
onOpenChange: setIsOpen,
|
|
|
|
placement: 'top',
|
|
|
|
middleware: [
|
2023-03-28 12:37:35 -07:00
|
|
|
autoPlacement({
|
|
|
|
allowedPlacements: ['top', 'bottom'],
|
|
|
|
}),
|
2023-03-14 12:58:11 -07:00
|
|
|
offset(10),
|
|
|
|
arrow({
|
|
|
|
element: arrowRef,
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
const { isMounted, styles } = useTransitionStyles(context, {
|
|
|
|
initial: {
|
|
|
|
opacity: 0,
|
|
|
|
transform: 'scale(0.8)',
|
|
|
|
},
|
|
|
|
duration: {
|
|
|
|
open: 200,
|
|
|
|
close: 200,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-03-28 12:37:35 -07:00
|
|
|
const click = useClick(context, { enabled: interaction === 'click' });
|
|
|
|
const hover = useHover(context, { enabled: interaction === 'hover' });
|
|
|
|
const dismiss = useDismiss(context);
|
|
|
|
|
2023-03-14 12:58:11 -07:00
|
|
|
const { getReferenceProps, getFloatingProps } = useInteractions([
|
|
|
|
click,
|
2023-03-28 12:37:35 -07:00
|
|
|
hover,
|
2023-03-14 12:58:11 -07:00
|
|
|
dismiss,
|
|
|
|
]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{React.cloneElement(children, {
|
|
|
|
ref: refs.setReference,
|
|
|
|
...getReferenceProps(),
|
2023-03-28 12:37:35 -07:00
|
|
|
className: clsx(children.props.className, referenceElementClassName),
|
2023-03-14 12:58:11 -07:00
|
|
|
})}
|
|
|
|
|
|
|
|
{(isMounted) && (
|
|
|
|
<div
|
|
|
|
ref={refs.setFloating}
|
|
|
|
style={{
|
|
|
|
position: strategy,
|
|
|
|
top: y ?? 0,
|
|
|
|
left: x ?? 0,
|
|
|
|
...styles,
|
|
|
|
}}
|
2023-03-28 12:37:35 -07:00
|
|
|
className={
|
|
|
|
clsx({
|
|
|
|
'z-40 rounded-lg bg-white shadow-2xl dark:bg-gray-900 dark:ring-2 dark:ring-primary-700': true,
|
|
|
|
'p-6': !isFlush,
|
|
|
|
})
|
|
|
|
}
|
2023-03-14 12:58:11 -07:00
|
|
|
{...getFloatingProps()}
|
|
|
|
>
|
|
|
|
{content}
|
|
|
|
|
2023-03-28 12:37:35 -07:00
|
|
|
<FloatingArrow
|
|
|
|
ref={arrowRef}
|
|
|
|
context={context}
|
|
|
|
className='-ml-2 fill-white dark:hidden' /** -ml-2 to fix offcenter arrow */
|
|
|
|
tipRadius={3}
|
|
|
|
/>
|
2023-03-14 12:58:11 -07:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Popover;
|