import type { CSSProperties, HTMLAttributes, ReactNode } from "react"; import { forwardRef } from "react"; import cn from "classnames"; import type { RenderConditionalPortalProps } from "@react-md/portal"; import { ConditionalPortal } from "@react-md/portal"; import type { CSSTransitionClassNames, CSSTransitionComponentProps, TransitionTimeout, } from "@react-md/transition"; import { useCSSTransition } from "@react-md/transition"; import type { SimplePosition } from "@react-md/utils"; import { bem } from "@react-md/utils"; import { DEFAULT_TOOLTIP_CLASSNAMES, DEFAULT_TOOLTIP_POSITION, DEFAULT_TOOLTIP_TIMEOUT, } from "./constants"; /** * The base props for the `Tooltip` component. This can be extended when * creating custom tooltip implementations. * * @remarks \@since 2.8.0 Supports the {@link RenderConditionalPortalProps} */ export interface TooltipProps extends HTMLAttributes, RenderConditionalPortalProps, CSSTransitionComponentProps { /** * An id for the tooltip. This is required for accessibility and finding an * element to attach event listeners to show and hide the tooltip. */ id: string; /** * An optional style to apply to the tooltip. */ style?: CSSProperties; /** * An optional class name to apply to the tooltip. */ className?: string; /** * The contents of the tooltip to display. This can be any renderable element, * but this is normally just text. * * If this is placed within a ` * * Tooltip Content * * * ); * } * ``` */ export const Tooltip = forwardRef( function Tooltip( { className, classNames = DEFAULT_TOOLTIP_CLASSNAMES, visible, timeout = DEFAULT_TOOLTIP_TIMEOUT, dense = false, lineWrap = true, position = DEFAULT_TOOLTIP_POSITION, children, onEnter, onEntering, onEntered, onExit, onExiting, onExited, portal = true, portalInto, portalIntoId, temporary = true, ...props }, nodeRef ) { const { elementProps, rendered } = useCSSTransition({ nodeRef, timeout, className: cn( block({ dense, "line-wrap": lineWrap, "dense-line-wrap": dense && lineWrap, [position]: true, }), className ), classNames, transitionIn: visible, onEnter, onEntering, onEntered, onExit, onExiting, onExited, temporary, }); return ( {rendered && ( {children} )} ); } );