v6.0.35

Tooltip

Renders tooltip at given element on mouse over or other event
Import

Usage

import { Tooltip, Button } from '@asuikit/core';
function Demo() {
return (
<Tooltip label="Tooltip">
<Button variant="outline">Button with tooltip</Button>
</Tooltip>
);
}

Tooltip children

Tooltip requires an element or a component as a single child – strings, fragments, numbers and multiple elements/components are not supported and will throw error. Custom components must provide a prop to get root element ref, all Mantine components support ref out of the box.

import { Tooltip, Badge } from '@asuikit/core';
function Demo() {
return (
<>
<Tooltip label="OK">
<button>Native button – ok</button>
</Tooltip>
<Tooltip label="OK">
<Badge>Mantine component – ok</Badge>
</Tooltip>
<Tooltip label="Throws">Raw string, NOT OK – will throw error</Tooltip>
{/* Number, NOT OK – will throw error */}
<Tooltip label="Throws">{2}</Tooltip>
<Tooltip label="Throws">
<>Fragment, NOT OK, will throw error</>
</Tooltip>
<Tooltip label="Throws">
<div>More that one node</div>
<div>NOT OK, will throw error</div>
</Tooltip>
</>
);
}

Required ref prop

Custom components that are rendered inside Tooltip are required to support ref prop:

// Example of code that WILL NOT WORK
import { Tooltip } from '@asuikit/core';
function MyComponent() {
return <div>My component</div>;
}
// This will not work – MyComponent does not support ref
function Demo() {
return (
<Tooltip label="Does not work">
<MyComponent />
</Tooltip>
);
}

Use forwardRef function to forward ref to root element:

// Example of code that will work
import { forwardRef } from 'react';
import { Tooltip } from '@asuikit/core';
const MyComponent = forwardRef<HTMLDivElement>((props, ref) => (
<div ref={ref} {...props}>
My component
</div>
));
// Works correctly – ref is forwarded
function Demo() {
return (
<Tooltip label="Works fine">
<MyComponent />
</Tooltip>
);
}

Position and color

Color
Shades
import { Tooltip, Button } from '@asuikit/core';
function Demo() {
return (
<Tooltip
label="Tooltip"
color="blue"
withArrow
>
<Button variant="outline" size="xl">
With tooltip
</Button>
</Tooltip>
);
}

Offset

import { Tooltip, Button } from '@asuikit/core';
function Demo() {
return (
<>
<Tooltip label="Default arrow" offset={20}>
<Button variant="outline">20 offset</Button>
</Tooltip>
<Tooltip label="Arrow with size" offset={-10}>
<Button variant="outline">-10 offset</Button>
</Tooltip>
</>
);
}

Arrow

import { Tooltip, Button } from '@asuikit/core';
function Demo() {
return (
<>
<Tooltip label="Default arrow" withArrow>
<Button variant="outline">Default arrow</Button>
</Tooltip>
<Tooltip label="Arrow with size" withArrow arrowSize={6}>
<Button variant="outline">With size</Button>
</Tooltip>
<Tooltip label="Arrow with radius" withArrow arrowSize={6} arrowRadius={4}>
<Button variant="outline">With radius</Button>
</Tooltip>
</>
);
}

Controlled

import { useState } from 'react';
import { Tooltip, Button } from '@asuikit/core';
function Demo() {
const [opened, setOpened] = useState(true);
return (
<Tooltip label="Ctrl + J" opened={opened}>
<Button variant="outline" onClick={() => setOpened((o) => !o)}>
Toggle color scheme
</Button>
</Tooltip>
);
}

Change events

Events that trigger tooltip can be changed with events prop, it accepts an object with the following properties that determine which events will trigger tooltip:

  • hover – mouse hover event, true by default
  • focus – focus/blur events excluding clicks on the target element, false by default
  • touch – events for touchscreen devices, false by default
import { Tooltip } from '@asuikit/core';
function Demo() {
return (
<Tooltip label="Tooltip" events={{ hover: true, focus: true, touch: false }}>
<button>target</button>
</Tooltip>
);
}

Multiline

To enable multiline mode set multiline prop to enable line breaks and width prop to set tooltip width:

import { Tooltip, Button } from '@asuikit/core';
function Demo() {
return (
<Tooltip
multiline
width={220}
withArrow
transitionProps={{ duration: 200 }}
label="Use this button to save this information in your profile, after that you will be able to access it any time and share it via email."
>
<Button variant="outline">Multiline tooltip</Button>
</Tooltip>
);
}

Inline

Set inline prop to use Tooltip with inline elements:

Stantler’s magnificent antlers were traded at high prices as works of art. As a result, this Pokémon was hunted close to extinction by those who were after the priceless antlers. When visiting a junkyard, you may catch sight of it having an intense fight with Murkrow over shiny objects.Ho-Oh’s feathers glow in seven colors depending on the angle at which they are struck by light. These feathers are said to bring happiness to the bearers. This Pokémon is said to live at the foot of a rainbow.
import { Tooltip, Mark, Text } from '@asuikit/core';
function Demo() {
return (
<Text>
Stantler’s magnificent antlers were traded at high prices as works of art. As a result, this
Pokémon was hunted close to extinction by those who were after the priceless antlers.{' '}
<Tooltip inline label="Inline tooltip">
<Mark>When visiting a junkyard</Mark>
</Tooltip>
, you may catch sight of it having an intense fight with Murkrow over shiny objects.Ho-Oh’s
feathers glow in seven colors depending on the angle at which they are struck by light. These
feathers are said to bring happiness to the bearers. This Pokémon is said to live at the foot
of a rainbow.
</Text>
);
}

Change transition

Tooltip is built with Transition component, it supports transitionProps props:

import { Tooltip } from '@asuikit/core';
function Demo() {
return (
<Tooltip transitionProps={{ transition: 'skew-up', duration: 300 }}>
<button>target</button>
</Tooltip>
);
}

All available premade transitions:

fade
scale
scale-y
scale-x
skew-up
skew-down
rotate-left
rotate-right
slide-down
slide-up
slide-left
slide-right
pop
pop-bottom-left
pop-bottom-right
pop-top-left
pop-top-right

Close and open delay

You can delay tooltip open/close events by setting openDelay and closeDelay props in ms:

import { Button, Tooltip } from '@asuikit/core';
function Demo() {
return (
<>
<Tooltip label="Opened after 500ms" openDelay={500}>
<Button variant="outline">Delay open - 500ms</Button>
</Tooltip>
<Tooltip label="Closes after 500ms" closeDelay={500}>
<Button variant="outline">Delay close - 500ms</Button>
</Tooltip>
</>
);
};

Tooltip delay group

Tooltip.Group component can be used to sync open and close delays for multiple tooltips:

import { Tooltip, Button, Group } from '@asuikit/core';
function Demo() {
return (
<Tooltip.Group openDelay={500} closeDelay={100}>
<Group position="center">
<Tooltip label="Tooltip 1">
<Button variant="outline">Button 1</Button>
</Tooltip>
<Tooltip label="Tooltip 2">
<Button variant="outline">Button 2</Button>
</Tooltip>
<Tooltip label="Tooltip 3">
<Button variant="outline">Button 3</Button>
</Tooltip>
</Group>
</Tooltip.Group>
);
}

Floating tooltip

Tooltip.Floating component has the same API as Tooltip component but tooltip will follow mouse:

Hover over the box to see tooltip
Color
Shades
Radius
xs
sm
md
lg
xl
import { Tooltip, Box } from '@asuikit/core';
function Demo() {
return (
<Tooltip.Floating label="Tooltip" color="blue">
<Box
sx={(theme) => ({
padding: theme.spacing.xl,
cursor: 'default',
backgroundColor:
theme.colorScheme === 'dark' ? theme.colors.dark[6] : theme.colors.gray[0],
})}
>
Hover over the box to see tooltip
</Box>
</Tooltip.Floating>
);
}

Custom components with Tooltip

If you want to build a component that can be used with Tooltip use forwardRef or other prop that will allow to get root element ref. This logic is applied to Tooltip and Tooltip.Floating components:

import React, { forwardRef } from 'react';
import { Tooltip } from '@asuikit/core';
// forwardRef function will allow to get root element ref
const MyBadge = forwardRef<HTMLDivElement, { color: string }>(({ color }, ref) => (
<div ref={ref} color={color}>
Badge
</div>
));
// other props can also be used
function MyOtherBadge({
color,
innerRef,
}: {
color: string;
innerRef?: React.ForwardedRef<HTMLDivElement>;
}) {
return (
<div ref={innerRef} color={color}>
Badge
</div>
);
}
function Demo() {
return (
<>
<Tooltip label="Can be used as is">
<MyBadge color="red" />
</Tooltip>
<Tooltip label="refProp is required" refProp="innerRef">
<MyOtherBadge color="orange" />
</Tooltip>
</>
);
}

Accessibility

Tooltip follows WAI-ARIA recommendations:

  • Tooltip body has role="tooltip" attribute
  • Target element has aria-describedby attribute
  • Tooltip.Floating is ignored by screen readers

By default, Tooltip is not triggered by focus events and thus users who use screen reader or navigate with keyboard will not be able to get tooltip content. Set events prop to enable focus/blur tooltip events:

import { Tooltip } from '@asuikit/core';
// Tooltip will be visible for screen readers
function Demo() {
return (
<Tooltip label="Tooltip" events={{ hover: true, focus: true, touch: false }}>
<button>target</button>
</Tooltip>
);
}

Tooltip component props

NameTypeDescription
arrowOffset
number
Arrow offset
arrowPosition
"center" | "side"
Arrow position *
arrowRadius
number
Arrow radius
arrowSize
number
Arrow size
children *
ReactNode
Target element
closeDelay
number
Close delay in ms
color
MantineColor
Key of theme.colors
disabled
boolean
Disables tooltip
events
{ hover: boolean; focus: boolean; touch: boolean; }
Determines which events will be used to show tooltip
inline
boolean
Set if tooltip is attached to an inline element
keepMounted
boolean
If set tooltip will not be unmounted from the DOM when it is hidden, display: none styles will be added instead
label *
ReactNode
Tooltip label
multiline
boolean
Defines whether content should be wrapped on to the next line
offset
number
Space between target element and tooltip
onPositionChange
(position: FloatingPosition) => void
Called when tooltip position changes
openDelay
number
Open delay in ms
opened
boolean
Controls opened state
portalProps
Omit<PortalProps, "children" | "withinPortal">
Props to pass down to the portal when withinPortal is true
position
"bottom" | "left" | "right" | "top" | "bottom-end" | "bottom-start" | "left-end" | "left-start" | "right-end" | "right-start" | "top-end" | "top-start"
Tooltip position relative to target element (default) or mouse (floating)
positionDependencies
any[]
useEffect dependencies to force update tooltip position
radius
number | "xs" | "sm" | "md" | "lg" | "xl"
Key of theme.radius or any valid CSS value to set border-radius, theme.defaultRadius by default
refProp
string
Key of the prop that should be used to get element ref
transitionProps
Partial<Omit<TransitionProps, "mounted">>
Props added to Transition component that used to animate tooltip presence, use to configure duration and animation type, { duration: 100, transition: 'fade' } by default
width
number | "auto"
Tooltip width
withArrow
boolean
Determines whether component should have an arrow
withinPortal
boolean
Determines whether tooltip should be rendered within Portal
zIndex
ZIndex
Tooltip z-index

Tooltip.Floating component props

NameTypeDescription
children *
ReactNode
Target element
color
MantineColor
Key of theme.colors
disabled
boolean
Disables tooltip
label *
ReactNode
Tooltip label
multiline
boolean
Defines whether content should be wrapped on to the next line
offset
number
Offset from mouse
portalProps
Omit<PortalProps, "children" | "withinPortal">
Props to pass down to the portal when withinPortal is true
position
"bottom" | "left" | "right" | "top" | "bottom-end" | "bottom-start" | "left-end" | "left-start" | "right-end" | "right-start" | "top-end" | "top-start"
Tooltip position relative to target element (default) or mouse (floating)
radius
number | "xs" | "sm" | "md" | "lg" | "xl"
Key of theme.radius or any valid CSS value to set border-radius, theme.defaultRadius by default
refProp
string
Key of the prop that should be used to get element ref
width
number | "auto"
Tooltip width
withinPortal
boolean
Determines whether tooltip should be rendered within Portal
zIndex
ZIndex
Tooltip z-index

Tooltip.Group component props

NameTypeDescription
children *
ReactNode
<Tooltip /> components
closeDelay
number
Close delay in ms
openDelay
number
Open delay in ms

Tooltip component Styles API

NameStatic selectorDescription
tooltip.asuikit-Tooltip-tooltipTooltip body
arrow.asuikit-Tooltip-arrowTooltip arrow