Tooltip Primitive
Use Tooltip to add a short hint to a control. It appears on hover or keyboard focus and closes when the person moves away or presses Escape.
Anatomy
Use the Astro primitive adapter to render Tooltip anatomy with the Runtime wiring included.
---import { Tooltip } from "@starwind-ui/astro/tooltip";---
<Tooltip.Root> <Tooltip.Trigger>Hover me</Tooltip.Trigger> <Tooltip.Portal> <Tooltip.Positioner> <Tooltip.Popup> <Tooltip.Arrow /> </Tooltip.Popup> </Tooltip.Positioner> </Tooltip.Portal></Tooltip.Root>Use the React primitive adapter when Tooltip state participates in React rendering.
import { Tooltip } from "@starwind-ui/react/tooltip";
export function Example() { return ( <Tooltip.Root> <Tooltip.Trigger>Hover me</Tooltip.Trigger> <Tooltip.Portal> <Tooltip.Positioner> <Tooltip.Popup> <Tooltip.Arrow /> </Tooltip.Popup> </Tooltip.Positioner> </Tooltip.Portal> </Tooltip.Root> );}Use the Vue components to build Tooltip.
<script setup lang="ts">import Tooltip from "@starwind-ui/vue/tooltip";</script>
<template> <Tooltip.Root> <Tooltip.Trigger>Hover me</Tooltip.Trigger> <Tooltip.Portal> <Tooltip.Positioner> <Tooltip.Popup> <Tooltip.Arrow /> </Tooltip.Popup> </Tooltip.Positioner> </Tooltip.Portal> </Tooltip.Root></template>Use the Svelte components to build Tooltip.
<script lang="ts"> import { Tooltip } from "@starwind-ui/svelte/tooltip";</script>
<Tooltip.Root> <Tooltip.Trigger>Hover me</Tooltip.Trigger> <Tooltip.Portal> <Tooltip.Positioner> <Tooltip.Popup> <Tooltip.Arrow /> </Tooltip.Popup> </Tooltip.Positioner> </Tooltip.Portal></Tooltip.Root>Render the Tooltip data-sw-* contract yourself, then initialize createTooltip.
<div data-sw-tooltip> <button data-sw-tooltip-trigger type="button">Hover me</button> <div data-sw-tooltip-portal data-sw-portal-placement="runtime"> <div data-sw-tooltip-positioner> <div data-sw-tooltip-popup role="tooltip" hidden> <div data-sw-tooltip-arrow></div> </div> </div> </div></div>
<script type="module"> import { createTooltip } from "@starwind-ui/runtime/tooltip";
const root = document.querySelector("[data-sw-tooltip]"); if (root) { createTooltip(root); }</script>Astro, React, Vue, and Svelte components use the same underlying interactions. Each framework has its own way to read and update state. See the examples and your installed packageโs types for the available props and events. Plain HTML uses DOM events and Runtime methods.
Floating Behavior
| Fact | Value |
|---|---|
| Anchor part | trigger |
| Positioner part | positioner |
| Popup part | popup |
| Portal part | portal |
| Option props | side, align, sideOffset, avoidCollisions |
API Reference
Root
The main element that owns the Tooltip Runtime instance.
- Default element
- div
- Discovery hook
- data-sw-tooltip
- Role
- -
Props
open boolean -
- Description
- Controls whether Tooltip is open.
- Kind
- control
- Targets
- root
- Full type
- boolean
- React
Use open for controlled state and defaultOpen for default state, and onOpenChange for change proposals.
opendefaultOpenonOpenChange
- Astro
Use open or defaultOpen for initial state, listen for starwind:open-change, and call setOpen for later updates.
opendefaultOpenstarwind:open-changesetOpen
- Runtime / HTML
Use data-default-open for initial state, listen for starwind:open-change, and call setOpen for later updates.
data-default-openstarwind:open-changesetOpen
defaultOpen boolean false
- Description
- Sets whether Tooltip starts open.
- Kind
- control
- Targets
- root
- Full type
- boolean
- React
Use open for controlled state and defaultOpen for default state, and onOpenChange for change proposals.
opendefaultOpenonOpenChange
- Astro
Use open or defaultOpen for initial state, listen for starwind:open-change, and call setOpen for later updates.
opendefaultOpenstarwind:open-changesetOpen
- Runtime / HTML
Use data-default-open for initial state, listen for starwind:open-change, and call setOpen for later updates.
data-default-openstarwind:open-changesetOpen
closeDelay number 200
- Description
- Sets how long Tooltip waits before closing.
- Kind
- option
- Targets
- -
- Full type
- number
closeOnEscape boolean true
- Description
- Closes Tooltip when Escape is pressed.
- Kind
- option
- Targets
- -
- Full type
- boolean
closeOnOutsideInteract boolean true
- Description
- Closes Tooltip when the user interacts outside it.
- Kind
- option
- Targets
- -
- Full type
- boolean
disabled boolean false
- Description
- Disables the Root part.
- Kind
- option
- Targets
- -
- Full type
- boolean
disableHoverableContent boolean false
- Description
- Configures the disable hoverable content option for the Root part.
- Kind
- option
- Targets
- root
- Full type
- boolean
openDelay number 200
- Description
- Sets how long Tooltip waits before opening.
- Kind
- option
- Targets
- -
- Full type
- number
onOpenChange (open: boolean, details: TooltipOpenChangeDetails) => void -
- Description
- Runs when Tooltip opens or closes.
- Kind
- callback
- Targets
- -
- Full type
- (open: boolean, details: TooltipOpenChangeDetails) => void
- React
Use open for controlled state and defaultOpen for default state, and onOpenChange for change proposals.
opendefaultOpenonOpenChange
- Astro
Use open or defaultOpen for initial state, listen for starwind:open-change, and call setOpen for later updates.
opendefaultOpenstarwind:open-changesetOpen
- Runtime / HTML
Use data-default-open for initial state, listen for starwind:open-change, and call setOpen for later updates.
data-default-openstarwind:open-changesetOpen
Events
openChange onOpenChange open: boolean
- Description
- Fires when Tooltip opens or closes.
- DOM event
- starwind:open-change
- Details type
- TooltipOpenChangeDetails
- Timing
- before-state-commit
- Cancelable
- Yes
- Cancellation sequence
- Check internal eligibility and intent.
- Create one details object for the proposal.
- Call the Runtime callback with the details object when the controller exposes one.
- Dispatch the cancelable DOM event with the same details object, including when the callback canceled it.
- Read details.isCanceled, including cancellation caused by preventDefault().
- Apply the accepted state.
- Notify Runtime subscribers and other accepted-only observers.
State
open boolean open / defaultOpen
- Description
- Tracks whether Tooltip is open.
- Initial attribute
- data-default-open
- Runtime getter
- getOpen
- Runtime setter
- setOpen
- React
Use open for controlled state and defaultOpen for default state, and onOpenChange for change proposals.
opendefaultOpenonOpenChange
- Astro
Use open or defaultOpen for initial state, listen for starwind:open-change, and call setOpen for later updates.
opendefaultOpenstarwind:open-changesetOpen
- Runtime / HTML
Use data-default-open for initial state, listen for starwind:open-change, and call setOpen for later updates.
data-default-openstarwind:open-changesetOpen
Data Attributes
Runtime hooks
State
Metadata
Trigger
The control that opens, closes, or targets the Tooltip content.
- Default element
- button
- Discovery hook
- data-sw-tooltip-trigger
- Role
- -
Props
asChild boolean -
- Description
- Merges behavior onto your child element instead of rendering the default Trigger element.
- Kind
- rendering
- Targets
- trigger
- Full type
- boolean
Data Attributes
Runtime hooks
State
Metadata
Portal
Moves Tooltip overlay content to the document body when needed.
- Default element
- div
- Discovery hook
- data-sw-tooltip-portal
- Role
- -
Data Attributes
Runtime hooks
Metadata
Positioner
Positions the Tooltip content relative to its trigger.
- Default element
- div
- Discovery hook
- data-sw-tooltip-positioner
- Role
- -
Props
side "top" | "right" | "bottom" | "left" "top"
- Description
- Sets the preferred side for Tooltip content.
- Kind
- option
- Targets
- positioner, popup
- Full type
- "top" | "right" | "bottom" | "left"
align "start" | "center" | "end" "center"
- Description
- Sets how Tooltip content aligns to its trigger.
- Kind
- option
- Targets
- positioner, popup
- Full type
- "start" | "center" | "end"
sideOffset number 8
- Description
- Sets the distance between Tooltip content and its trigger.
- Kind
- option
- Targets
- positioner, popup
- Full type
- number
avoidCollisions boolean true
- Description
- Allows Tooltip content to shift or flip to stay visible.
- Kind
- option
- Targets
- positioner, popup
- Full type
- boolean
Data Attributes
Runtime hooks
State
Metadata
Popup
The floating content container for Tooltip.
- Default element
- div
- Discovery hook
- data-sw-tooltip-popup
- Role
- tooltip
Props
side "top" | "right" | "bottom" | "left" "top"
- Description
- Sets the preferred side for Tooltip content.
- Kind
- option
- Targets
- positioner, popup
- Full type
- "top" | "right" | "bottom" | "left"
align "start" | "center" | "end" "center"
- Description
- Sets how Tooltip content aligns to its trigger.
- Kind
- option
- Targets
- positioner, popup
- Full type
- "start" | "center" | "end"
sideOffset number 8
- Description
- Sets the distance between Tooltip content and its trigger.
- Kind
- option
- Targets
- positioner, popup
- Full type
- number
avoidCollisions boolean true
- Description
- Allows Tooltip content to shift or flip to stay visible.
- Kind
- option
- Targets
- positioner, popup
- Full type
- boolean
Data Attributes
Runtime hooks
State
Metadata
Arrow
The arrow element that visually points to the trigger.
- Default element
- div
- Discovery hook
- data-sw-tooltip-arrow
- Role
- -
Data Attributes
Runtime hooks
State
Runtime API
- Factory
createTooltip- Import
@starwind-ui/runtime/tooltip- Root hook
- root
data-sw-tooltip - Option props
- closeDelay, closeOnEscape, closeOnOutsideInteract, defaultOpen, disabled, disableHoverableContent, onOpenChange, open, openDelay
Option Lifecycles
| Option | Lifecycle |
|---|---|
| closeDelay | constructor-only |
| closeOnEscape | constructor-only |
| closeOnOutsideInteract | constructor-only |
| defaultOpen | constructor-only |
| disabled | setter-backed |
| disableHoverableContent | constructor-only |
| onOpenChange | constructor-only |
| open | setter-backed |
| openDelay | constructor-only |
Runtime Setters
Related Styled Components
Changelog
View version history v1.4.0 8 releases
v1.4.0
- Tooltip content opens with the direction of its trigger and keeps it while open.
- A click during the Tooltip close animation now goes to the page below the tooltip. Before, the closing tooltip caught the click, so a quick click on a control under it did nothing.
- Tooltip now stays on screen when its trigger is at the edge of the screen, for example on a phone. Before, the tooltip kept its alignment, so part of it went off screen. Now the tooltip moves along its trigger until it is inside the viewport padding. It stays next to the trigger and keeps its side.
- The positioner now sets `--sw-floating-arrow-x` for a top or bottom side and `--sw-floating-arrow-y` for a left or right side when the Tooltip arrow has a width and a height. These values point the arrow at the center of the trigger when the arrow can reach it. Otherwise the arrow points at the nearest part of the trigger. The arrow stays inside the popup, at least 6px from each end. Each value is the distance from the left or top edge of the popup to the same edge of the arrow, in RTL too. To use it, position a custom arrow absolutely inside the popup, set `left: var(--sw-floating-arrow-x)` or `top: var(--sw-floating-arrow-y)`, and remove any centering transform on that axis.
- In Vue, this Primitive now uses the same `asChild` child handling as the other Vue Primitives. Its public API is unchanged. An invalid `asChild` child now gives one shorter error message.
- In Vue, this component now needs Vue 3.5.21 or newer.
- Internal code cleanup, such as unused code and import order. This cleanup does not change behavior.
v1.3.0
- In Astro, an attribute that you set on a Tooltip part now wins over the part's own default for the same attribute, such as `role` or `aria-label`.
- In Svelte, a defined `open` that you pass when Tooltip mounts now stays under your control, as in React and Vue. To change it, use `bind:` or update your state in the change callback. A form reset keeps it. If you pass `undefined` at mount, Tooltip keeps its own state.
- In Svelte, the `children` snippet on the Tooltip root no longer receives `open`. Use `bind:open`.
- Tooltip now opens on a native link or span in Vue and Svelte.
- In Astro, a `Tooltip.Trigger` with plain text and `asChild` now renders a button, so keyboard users can focus it.
- Fixed Tooltip in Vue and Svelte staying hidden when its popup is replaced while open, for example when the `key` on the content changes.
- Fixed a Tooltip trigger on a link or span that stayed disabled when a framework wrote `disabled="false"`. On a native form control, any `disabled` attribute still disables it.
- In Svelte, the Tooltip trigger now accepts a link in `child`. Before, it warned in development, type checks rejected the link, and `ref` did not run. Use the new `TriggerChildPayload` type for your snippet.
v1.2.0
- Vue Tooltip keeps its previous open state when an open or close action is canceled.
- Vue Tooltip respects the app-provided open value when it is re-enabled.
- Reduced background work when a React page uses multiple menus, tooltips, or other portals.
- Fixed nested Svelte overlays inside a dialog appearing behind their parent or failing to receive clicks.
- Fixed open content losing its position at the trigger when the React or Vue component updates.
- React Primitive users must wrap `Tooltip.Positioner` and `Tooltip.Popup` in `Tooltip.Portal`. Missing the wrapper now reports an error. Set `disabled` on the Portal for inline rendering.
- Reduced background work when a Vue page uses multiple menus, tooltips, or other portals.
- Fixed nested overlays appearing behind their parent in Vue and Svelte. Overlays can now render under the document body; if custom CSS or DOM queries require them to stay inside a parent, set a portal container or add `data-floating-root` to that parent.
- Disabling a tooltip now cancels a scheduled opening. Re-enabling it restores trigger interaction.
- Fixed React 18 retaining removed popup elements when the parent component stays mounted.
- Reduced repeated JavaScript when Astro Tooltip is used with other Starwind components.
v1.1.1
- Removed old comments from the Vue Primitive files that said the code was internal and not for release. The CLI also removes the blank line that these comments left in the Vue Primitive `index.ts` files that it copies into your project.
v1.1.0
- Added `container` and `disabled` to the Portal part. `container` sets the element that holds the popup, and `disabled` keeps the popup in place. Portals now place nested popups in the same way every time, and they show their placement in data attributes.
v1.0.0
- This Primitive is now stable at version `1.0.0`. Its API and behavior stay the same as in the previous release.
v0.1.1
- The React Primitive files that the CLI copies into your project are now marked with `"use client"`, so they stay client components in React server frameworks.
- In React, you can cancel a change from its change callback or DOM event before it applies. React then shows only the changes that apply.
v0.1.0
- First release. Tooltip opens after a delay on hover and focus. It places itself next to the trigger, animates when it opens and closes, and handles closing. It works with screen readers.