, 'children'>;
const TooltipTrigger = React.forwardRef<
HTMLDivElement,
TooltipTriggerPropsType
>((props: TooltipTriggerPropsType, ref) => {
const {children, ...rest} = props;
const context = useTooltipContext();
const childrenRef = (children as any).ref;
const triggerRef = useMergeRefs([
// @ts-ignore TS18047
context.refs.setReference,
ref,
childrenRef,
]);
const className = 'sg-tooltip-trigger';
// @ts-ignore TS18047
const tooltipId = context.getFloatingProps().id as string;
// @ts-ignore TS18047
const ariaLink = context.asLabel
? {'aria-labeledby': tooltipId}
: {'aria-describedby': tooltipId};
// If children is valid element, i.e. , etc.
// and if the element is forward ref type (otherwise we cannot pass ref).
if (React.isValidElement(children) && isReactForwardRefType(children)) {
return React.cloneElement(children, {
// @ts-ignore TS18047
...context.getReferenceProps({
className,
tabIndex: 0, // ensure the element tabindex is set, but allow overriding with children props
...rest,
...children.props,
ref: triggerRef, // override forwarded ref with merged refs
// @ts-ignore TS18047
'data-state': context.isOpen ? 'open' : 'closed',
}),
...ariaLink,
});
}
return (
{children}
);
});
export default TooltipTrigger;