Raft 设计系统

组件 · 浮层

Tooltip

One line that appears on hover or keyboard focus and gives something without a visible label its name, its consequence or its shortcut.

稳定也叫 Hint
import { Tooltip } from "@botiverse/raft-ui";
本页尚未翻译
译文完成之前,这里显示英文原文。 阅读英文版

A tooltip is one line that appears on hover or keyboard focus and gives something without a visible label its name, its consequence or its keyboard shortcut. Icon buttons, truncated names, runtime chips and the items of the desktop rail use it.

When to use

SituationUse
An icon button, a truncated name, a runtime chip ("Runs on Martin's MacBook")Tooltip
An item of the desktop railTooltip on the right, with the destination's name (AppRail)
A label that is already visible on the controlNothing: don't repeat it in a tooltip
Content with a link or a buttonPopover
A person or an agentPreviewCard

Anatomy

  • TooltipProvider: sets the open delay for the tooltips inside it. In raft-ui they open without delay.
  • Tooltip: the root. Required.
  • TooltipTrigger: the element the tooltip describes. Required.
  • TooltipContent: the tooltip, with a small arrow toward the trigger. A leading icon and a Kbd for the shortcut can sit inside it. Required.
  • TooltipPortal: a lower-level part.

The tooltip is layer-hud with foreground-on-hud text, like a toast (Color).

Behavior

  • It opens above its trigger, centered on it, and flips below when there is no room above.
  • side="right" puts it beside the trigger, as on the rail; side="bottom" puts it under the trigger.
  • It wraps at 20rem, or at the space left in the window.
  • It closes when the pointer leaves, when focus leaves and on Esc.

Content

  • Write one name or one short sentence, in sentence case: Search, Runs on Martin's MacBook.
  • Use the same word in the tooltip as in the trigger's aria-label.
  • Put the shortcut in a Kbd after the text: Search ⌘K.

Accessibility

  • Tooltip wraps the Base UI Tooltip. It provides the tooltip role and opens on hover and on keyboard focus.
  • A tooltip adds to a name; it doesn't replace it. Give every icon-only button its own aria-label.
  • Touch screens have no hover. Show the tooltip on press and hold, or write the explanation in the interface instead.

Rules

  • Never put a link, a button or anything people must act on in a tooltip: the pointer can't reach it.

API

Tooltip

Prop类型默认值
actionsRefA ref to imperative actions. - `unmount`: Unmounts the tooltip popup. - `close`: Closes the tooltip imperatively when called.RefObject<TooltipRootActions | null>
childrenThe content of the tooltip. This can be a regular React node or a render function that receives the `payload` of the active trigger.ReactNode | PayloadChildRenderFunction<unknown>
defaultOpenWhether the tooltip is initially open.booleanfalse
defaultTriggerIdID of the trigger that the tooltip is associated with. This is useful in conjunction with the `defaultOpen` prop to create an initially open tooltip.string
disabledWhether the tooltip is disabled.booleanfalse
disableHoverablePopupWhether the tooltip contents can be hovered without closing the tooltip.booleanfalse
handleA handle to associate the tooltip with a trigger. If specified, allows external triggers to control the tooltip's open state. Can be created with the Tooltip.createHandle() method.BaseTooltip.Handle<unknown>
onOpenChangeEvent handler called when the tooltip is opened or closed.(open: boolean, eventDetails: BaseTooltip.Root.ChangeEventDetails) => void
onOpenChangeCompleteEvent handler called after any animations complete when the tooltip is opened or closed.(open: boolean) => void
openWhether the tooltip is currently open.boolean
trackCursorAxisDetermines which axis the tooltip should track the cursor on."none" | "both" | "x" | "y"'none'
triggerIdID of the trigger that the tooltip is associated with. This is useful in conjunction with the `open` prop to create a controlled tooltip. There's no need to specify this prop when the tooltip is uncontrolled (that is, when the `open` prop is not set).string

TooltipContent

Prop类型默认值
containerHTMLElement | ShadowRoot | RefObject<HTMLElement | ShadowRoot | null>
portalPropsTooltipPortalProps
alignHow to align the popup relative to the specified side.Align"center"
alignOffsetAdditional offset along the alignment axis in pixels. Also accepts a function that returns the offset to read the dimensions of the anchor and positioner elements, along with its side and alignment.number | OffsetFunction0
anchorAn element to position the popup against. By default, the popup will be positioned against the trigger.Element | VirtualElement | RefObject<Element | null> | (() => Element | VirtualElement | null)
arrowPaddingMinimum distance to maintain between the arrow and the edges of the popup.number5
collisionAvoidanceDetermines how to handle collisions when positioning the popup.CollisionAvoidanceCONTENT_COLLISION_AVOIDANCE
collisionBoundaryAn element or a rectangle that delimits the area that the popup is confined to.Boundary'clipping-ancestors'
collisionPaddingAdditional space to maintain from the edge of the collision boundary.Padding5
disableAnchorTrackingWhether to disable the popup from tracking any layout shift of its positioning anchor.booleanfalse
positionMethodDetermines which CSS `position` property to use."fixed" | "absolute""fixed"
renderAllows you to replace the component's HTML element with a different tag, or compose it with another component.ReactElement | ((props, state) => ReactElement)
sideWhich side of the anchor element to align the popup against. May automatically change to avoid collisions.Side"top"
sideOffsetDistance between the anchor and the popup in pixels. Also accepts a function that returns the distance to read the dimensions of the anchor and positioner elements, along with its side and alignment.number | OffsetFunction0
stickyWhether to maintain the popup in the viewport after the anchor element was scrolled out of view.booleanfalse

Also takes the attributes of its element.

TooltipPortal

Prop类型默认值
containerA parent element to render the portal element into.HTMLElement | ShadowRoot | RefObject<HTMLElement | ShadowRoot | null>
keepMountedWhether to keep the portal mounted in the DOM while the popup is hidden.booleanfalse
renderAllows you to replace the component's HTML element with a different tag, or compose it with another component.ReactElement | ((props, state) => ReactElement)

Also takes the attributes of its element.

TooltipProvider

Prop类型默认值
childrenReactNode
closeDelayHow long to wait before closing a tooltip. Specified in milliseconds.number
delayHow long to wait before opening the tooltip on hover. Specified in milliseconds.number0
timeoutAnother tooltip will open instantly if the previous tooltip is closed within this timeout. Specified in milliseconds.number400

TooltipTrigger

Prop类型默认值
closeDelayHow long to wait before closing the tooltip. Specified in milliseconds.number0
closeOnClickWhether the tooltip should close when this trigger is clicked.booleantrue
delayHow long to wait before opening the tooltip on hover. Specified in milliseconds.number600
disabledIf `true`, the tooltip will not open when interacting with this trigger. Note that this doesn't apply the `disabled` attribute to the trigger element. If you want to disable the trigger element itself, you can pass the `disabled` prop to the trigger element via the `render` prop.booleanfalse
handleA handle to associate the trigger with a tooltip.BaseTooltip.Handle<unknown>
payloadA payload to pass to the tooltip when it is opened.{}
renderAllows you to replace the component's HTML element with a different tag, or compose it with another component.ReactElement | ((props, state) => ReactElement)

Also takes the attributes of its element.

改动

  • 0.10.0 · ChangedTooltip, ComboboxSelect options, Steps and Timeline use the type scale instead of fixed sizes, so they follow the desktop values.