Tooltip Primitive
Tooltip is a Starwind Runtime primitive in the presence-floating-overlay contract family. Astro and React share one semantic component API. React coordinates reactive state through controlled and default props plus callbacks. Astro renders initial state and coordinates later changes through DOM events and Runtime methods. Raw HTML uses Runtime attributes, DOM events, and imperative methods.
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.Positioner> <Tooltip.Popup> <Tooltip.Arrow /> </Tooltip.Popup> </Tooltip.Positioner></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.Positioner> <Tooltip.Popup> <Tooltip.Arrow /> </Tooltip.Popup> </Tooltip.Positioner> </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-positioner> <div data-sw-tooltip-popup role="tooltip" hidden> <div data-sw-tooltip-arrow></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>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
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.0.0 3 releases
v1.0.0
- Promoted this Primitive's vendoring version to the stable 1.0.0 baseline. Its existing API and Runtime behavior carry forward from the previous release.
v0.1.1
- Marked generated React Primitive files as client modules so vendored installs preserve client boundaries in React server frameworks.
- Kept generated React state-change callbacks and DOM events cancelable until the Runtime accepts the proposed state, then synchronized rendered state.
v0.1.0
- Introduced hover and focus timing, floating placement, presence, and dismissal for accessible tooltips.