Command palette

@yasinsensoy/web

Web · Components

Tooltip

Tooltip briefly explaining what an element does.

8 exports

Live example

Preview

Code

import { Button } from "@yasinsensoy/web";

<Button variant="outline" tooltip="Saves the changes">Kaydet</Button>

Events

  1. Events appear here when you interact with the preview.

The tooltip prop on every component

PaidAK

Monthly quota: 80 / 100

interactive: a tooltip with a link inside

VAT included

Tooltip: for your own elements

Import

import { InfoTip, Tooltip } from "@yasinsensoy/web";

Tooltip

componentClient component

Tooltip briefly explaining what an element does. Only for extra information; don't hide important information in a tooltip, because not everyone may see it.

Most of the time you don't need to write this component directly: Button, IconButton, Badge, Avatar, Checkbox, Switch, Label, fields, Menu items and Tabs tabs take a tooltip prop (see TooltipProp). Use Tooltip for your own elements.

  • Opens on mouse hover (after a short delay) and keyboard focus; closes with Esc.
  • Mobile: Since touch screens have no hover, it opens on long press and closes shortly after the finger lifts. A normal tap doesn't break the element's own function. With touch="tap" it opens on tap.
  • Disabled elements: Since disabled buttons don't get mouse events, they are wrapped in a thin <span>; the tooltip still shows (e.g. "You don't have permission").
  • Performance: The tooltip box isn't added to the DOM until first needed (hover, focus, long press); even with hundreds of tooltips on a page the cost is just a few event handlers.
  • Accessibility: The text is linked to the element as a description with aria-describedby; if it equals the element's name (e.g. IconButton) it isn't read twice.
  • Opens in the browser's top layer (popover) and is positioned with CSS anchor positioning; if it doesn't fit it flips to the other side. In unsupporting browsers the position is computed once with JavaScript.
Tooltip(props: TooltipProps): JSX.Element

Examples

<Tooltip content="Copy link">
  <MyShareButton />
</Tooltip>

InfoTip

componentClient component

Small ⓘ icon explaining the element next to it; the tooltip opens from this icon. Makes the tooltip reachable for everyone on elements that can't be focused themselves (labels, headings, table columns). The tooltip prop of Label and fields (TextField…) adds it automatically.

  • Opens on mouse hover and keyboard focus.
  • Mobile: Opens on tap and closes when tapping elsewhere; no need to know about long-pressing. The touch area is enlarged to 44×44 px.
InfoTip({ label, iconClassName, ...options }: InfoTipProps): JSX.Element

Examples

<h2 className="flex items-center gap-1.5">
  Monthly quota <InfoTip content="Resets on the 1st of every month." />
</h2>

InfoTipProps

interface

Props of the InfoTip component.

NameTypeDefaultDescription
label?stringScreen reader name of the icon. If omitted, UIProvider messages.info (default "Bilgi") is used.
iconClassName?stringClasses added to the icon button. For the tooltip box use className.
contentReactNodeText of the tooltip. Keep it short; a single line is ideal. If empty, no tooltip is shown and only children is rendered. The tooltip box is a <span>; since the trigger can be inside a paragraph (<p>) too, the content must consist of inline elements (text, <strong>, <kbd>, icons); don't put block elements like <div>, <p> or lists.
side?TooltipSide"auto"Position relative to the trigger (e.g. "bottom-left"). See TooltipSide.
delay?number400Open delay after the mouse hovers (ms).
longPressDelay?number500Long-press duration to open on touch screens (ms).
touch?TooltipTouch"longPress"How it opens on touch screens. See TooltipTouch.
interactive?booleanfalseWhether the tooltip can be entered. Turn it on if the content has a link or a button (e.g. a "Learn more" link). - Mouse: If the pointer moves from the trigger toward the tooltip, it doesn't close (a "safe area" in the gap between trigger and box); if it moves elsewhere it closes right away. It stays open while over the box, text can be selected and links clicked. - Keyboard: After the trigger, Tab moves to the links in the tooltip; it closes when focus leaves the tooltip. Esc closes it and returns focus to the trigger. - Mobile: A tooltip opened by long press or tap doesn't close by itself when the finger lifts; its link can be tapped, and tapping outside closes it. - Accessibility: Since role="tooltip" can't contain focusable elements, the box becomes role="group"; its text is still linked to the trigger as a description with aria-describedby. Block elements still shouldn't be used in the content with this option (see content). For richer, card-like content use HoverCard.
closeDelay?number300` if `interactive`, otherwise `0Close delay after the mouse leaves (ms). For an interactive tooltip this is the time allowed for the pointer to reach the box.
tone?Tone"neutral"Semantic color of the tooltip box. "neutral" is a plain box in the page's inverse color; "danger" is for error messages (e.g. the error icon of fields), "warning" for warnings, "success" for confirmations. See Tone.
className?stringClasses added to the tooltip box.

Position of the tooltip relative to the trigger. "auto" starts centered on top; if it doesn't fit it moves below, then to the sides. See Placement.

type TooltipSide = Placement

How the tooltip opens on touch screens.

  • "longPress": Opens on long press; a normal tap does the element's own job (buttons, checkboxes).
  • "tap": Opens/closes on tap; closes when tapping elsewhere (only for informational ⓘ icons).
type TooltipTouch = "longPress" | "tap"

TooltipProps

interface

Props of the Tooltip component.

NameTypeDefaultDescription
childrenReactElementThe single element triggering the tooltip, usually a button.
contentReactNodeText of the tooltip. Keep it short; a single line is ideal. If empty, no tooltip is shown and only children is rendered. The tooltip box is a <span>; since the trigger can be inside a paragraph (<p>) too, the content must consist of inline elements (text, <strong>, <kbd>, icons); don't put block elements like <div>, <p> or lists.
side?TooltipSide"auto"Position relative to the trigger (e.g. "bottom-left"). See TooltipSide.
delay?number400Open delay after the mouse hovers (ms).
longPressDelay?number500Long-press duration to open on touch screens (ms).
touch?TooltipTouch"longPress"How it opens on touch screens. See TooltipTouch.
interactive?booleanfalseWhether the tooltip can be entered. Turn it on if the content has a link or a button (e.g. a "Learn more" link). - Mouse: If the pointer moves from the trigger toward the tooltip, it doesn't close (a "safe area" in the gap between trigger and box); if it moves elsewhere it closes right away. It stays open while over the box, text can be selected and links clicked. - Keyboard: After the trigger, Tab moves to the links in the tooltip; it closes when focus leaves the tooltip. Esc closes it and returns focus to the trigger. - Mobile: A tooltip opened by long press or tap doesn't close by itself when the finger lifts; its link can be tapped, and tapping outside closes it. - Accessibility: Since role="tooltip" can't contain focusable elements, the box becomes role="group"; its text is still linked to the trigger as a description with aria-describedby. Block elements still shouldn't be used in the content with this option (see content). For richer, card-like content use HoverCard.
closeDelay?number300` if `interactive`, otherwise `0Close delay after the mouse leaves (ms). For an interactive tooltip this is the time allowed for the pointer to reach the box.
tone?Tone"neutral"Semantic color of the tooltip box. "neutral" is a plain box in the page's inverse color; "danger" is for error messages (e.g. the error icon of fields), "warning" for warnings, "success" for confirmations. See Tone.
className?stringClasses added to the tooltip box.

Settings given as an object to the tooltip prop.

type TooltipOptions = Omit<TooltipProps, "children">
NameTypeDefaultDescription
contentReactNodeText of the tooltip. Keep it short; a single line is ideal. If empty, no tooltip is shown and only children is rendered. The tooltip box is a <span>; since the trigger can be inside a paragraph (<p>) too, the content must consist of inline elements (text, <strong>, <kbd>, icons); don't put block elements like <div>, <p> or lists.
side?TooltipSide"auto"Position relative to the trigger (e.g. "bottom-left"). See TooltipSide.
delay?number400Open delay after the mouse hovers (ms).
longPressDelay?number500Long-press duration to open on touch screens (ms).
touch?TooltipTouch"longPress"How it opens on touch screens. See TooltipTouch.
interactive?booleanfalseWhether the tooltip can be entered. Turn it on if the content has a link or a button (e.g. a "Learn more" link). - Mouse: If the pointer moves from the trigger toward the tooltip, it doesn't close (a "safe area" in the gap between trigger and box); if it moves elsewhere it closes right away. It stays open while over the box, text can be selected and links clicked. - Keyboard: After the trigger, Tab moves to the links in the tooltip; it closes when focus leaves the tooltip. Esc closes it and returns focus to the trigger. - Mobile: A tooltip opened by long press or tap doesn't close by itself when the finger lifts; its link can be tapped, and tapping outside closes it. - Accessibility: Since role="tooltip" can't contain focusable elements, the box becomes role="group"; its text is still linked to the trigger as a description with aria-describedby. Block elements still shouldn't be used in the content with this option (see content). For richer, card-like content use HoverCard.
closeDelay?number300` if `interactive`, otherwise `0Close delay after the mouse leaves (ms). For an interactive tooltip this is the time allowed for the pointer to reach the box.
tone?Tone"neutral"Semantic color of the tooltip box. "neutral" is a plain box in the page's inverse color; "danger" is for error messages (e.g. the error icon of fields), "warning" for warnings, "success" for confirmations. See Tone.
className?stringClasses added to the tooltip box.

Type of components' tooltip prop. Given in two forms:

  • Content: tooltip="Copy link" or any React content (tooltip={<>Save <kbd>⌘S</kbd></>}).
  • With settings: tooltip={{ content: "Save", side: "bottom", delay: 0 }}. See TooltipOptions.

false, null or empty text turns the tooltip off (e.g. if you don't want IconButton to show its name as a tooltip).

type TooltipProp = ReactNode | TooltipOptions