element-call/src/Tooltip.jsx

56 lines
1.5 KiB
React
Raw Normal View History

2021-12-23 22:40:23 +00:00
import React, { forwardRef } from "react";
2021-12-15 06:00:00 +00:00
import { useTooltipTriggerState } from "@react-stately/tooltip";
import { useTooltipTrigger, useTooltip } from "@react-aria/tooltip";
2021-12-23 22:40:23 +00:00
import { mergeProps, useObjectRef } from "@react-aria/utils";
2021-12-15 06:00:00 +00:00
import styles from "./Tooltip.module.css";
import classNames from "classnames";
export function Tooltip({ position, state, ...props }) {
let { tooltipProps } = useTooltip(props, state);
return (
<div
className={classNames(styles.tooltip, styles[position || "bottom"])}
{...mergeProps(props, tooltipProps)}
>
{props.children}
</div>
);
}
export const TooltipTrigger = forwardRef(({ children, ...rest }, ref) => {
const tooltipState = useTooltipTriggerState(rest);
2021-12-23 22:40:23 +00:00
const triggerRef = useObjectRef(ref);
2021-12-15 06:00:00 +00:00
const { triggerProps, tooltipProps } = useTooltipTrigger(
rest,
tooltipState,
triggerRef
);
if (
!Array.isArray(children) ||
children.length > 2 ||
typeof children[1] !== "function"
) {
throw new Error(
"TooltipTrigger must have two props. The first being a button and the second being a render prop."
);
}
const [tooltipTrigger, tooltip] = children;
return (
<div className={styles.tooltipContainer}>
<tooltipTrigger.type
{...mergeProps(triggerProps, tooltipTrigger.props, rest)}
ref={triggerRef}
/>
{tooltipState.isOpen && tooltip({ state: tooltipState, ...tooltipProps })}
</div>
);
});
TooltipTrigger.defaultProps = {
delay: 250,
};