Tooltip

Maintainer:
amje
GitHub
placement

A simple text tip that uses its child node as an anchor. This component accepts only text content and may be an excellent alternative to the browser's title attribute with its small size and long appearance delay.

Usage

import {Tooltip} from '@gravity-ui/uikit';

<Tooltip content="Content">
  <div tabIndex={0}>Anchor</div>
</Tooltip>;

Anchor

In order for Tooltip to work you should pass a valid ReactElement as a children which accepts ref property for HTMLElement and other properties for HTMLElement.

Alternatively, you can pass function as a children to provide ref and props manually to your underlying components:

import {Tooltip} from '@gravity-ui/uikit';

<Tooltip content="Content">
  {(props, ref) => <MyCustomButton buttonProps={props} buttonRef={ref} />}
</Tooltip>;

Controlled State

By default Tooltip opens and hides by hovering the anchor. You can change this behaviour to manually set the open state. Pass your state to the open prop and change it from onOpenChange callback. onOpenChange callback has the following signature: (open: boolean, event?: Event, reason: 'hover' | 'focus') => void.

Role

Tooltip accepts the role property which changes how it should act it terms of accessibility. tooltip role should be used when anchor has its own text and label role otherwise (e.g. in icon button).

Properties

NameDescriptionTypeDefault
childrenAnchor element for the TooltipReact.ReactElement Function
classNameclass HTML attributestring
closeDelayNumber of ms to delay hiding the Tooltip after the hover endsnumber0
containerDOM element to which component is mounted via PortalHTMLElementdocument.body
contentContent that will be shown in the TooltipReact.ReactNode
disabledPrevent the Tooltip from openingboolean
disablePortalDisables using Portalbooleanfalse
offsetTooltip offset from its anchornumber4
onOpenChangeCallback to handle open state changeFunction
openControlled open stateboolean
openDelayNumber of ms to delay showing the Tooltip after the hover beginsnumber1000
placementTooltip position relative to its anchorPopupPlacementbottom
qadata-qa HTML attribute, used for testingstring
roleThe role Tooltip is used for"tooltip" "label""tooltip"
strategyThe type of CSS position property to use.absolute fixedabsolute
stylestyle HTML attributeReact.CSSProperties
triggerEvent type that should trigger opening. By default both hover and focus do."focus"

CSS API

NameDescription
--g-tooltip-text-colorText color
--g-tooltip-background-colorBackground color
--g-tooltip-paddingPadding
--g-tooltip-border-radiusBorder radius
--g-tooltip-box-shadowShadow