Tooltip

Due to accessibility reasons with aria-labelledby, the tooltip must be the same document / shadowRoot as the item being described by the tooltip.

Tooltips are intended for re-use and are close to how you would make a native tooltip.

Styling the tooltip

Generally, the 2 things you want to pass to the tooltip are var(--background) and var(--border-color). Let’s see what that looks like in action:

API Reference

Imports

Script
HTML
<!-- Auto registers as <role-tooltip> -->
<script type="module" src="https://cdn.jsdelivr.net/npm/role-components/exports/components/tooltip/tooltip-register.js"></script>
CDN
HTML
<script type="module">
  // Auto registers as <role-tooltip>
  import "https://cdn.jsdelivr.net/npm/role-components/exports/components/tooltip/tooltip-register.js"

  // Manual Register
  import RoleTooltip from "https://cdn.jsdelivr.net/npm/role-components/exports/components/tooltip/tooltip.js"
  RoleTooltip.define()
  // => Registers as <role-tooltip>
</script>
Bundler
JavaScript
// Auto registers as <role-tooltip>
import "role-components/exports/components/tooltip/tooltip-register.js"

// Manual Register
import RoleTooltip "role-components/exports/components/tooltip/tooltip.js"
RoleTooltip.define()
// => Registers as <role-tooltip>

Slots

Name Description

default slot

Attributes

Name Description Reflects Type Default
[Attribute]


[Property]
role

The “role” attribute. Default is “tooltip” and generally shouldn’t be overriden.

string "tooltip"
[Attribute + Property]
placement

The preferred placement of the popover. Note that the actual placement will vary as configured to keep the panel inside of the viewport.

'top' | 'top-start' | 'top-end' | 'bottom' | 'bottom-start' | 'bottom-end' | 'right' | 'right-start' | 'right-end' | 'left' | 'left-start' | 'left-end'
-
[Attribute + Property]
current-placement

The currentPlacement property / current-placement attribute are where Floating UI actually positions the popup.

'top' | 'top-start' | 'top-end' | 'bottom' | 'bottom-start' | 'bottom-end' | 'right' | 'right-start' | 'right-end' | 'left' | 'left-start' | 'left-end' | null
-
[Attribute + Property]
strategy

Determines how the popover is positioned. Because you native “popover” API uses a fixed strategy, we use it as the default.

Strategy
-
[Attribute + Property]
distance

The distance in pixels from which to offset along the “main axis”. Usually its equivalent to offsetY

number
-
[Attribute + Property]
skidding

The distance in pixels from which to offset along the “cross axis”. Usually its equivalent to offsetX.

number
-
[Attribute + Property]
arrow

Attaches an arrow to the popover. The arrow’s size and color can be customized using the --arrow-size and --background-color custom properties. For additional customizations, you can also target the arrow using ::part(arrow) in your stylesheet.

boolean
-
[Attribute + Property]
arrow-placement

The placement of the arrow. The default is anchor, which will align the arrow as close to the center of the anchor as possible, considering available space and arrow-padding. A value of start, end, or center will align the arrow to the start, end, or center of the popover instead.

'start' | 'end' | 'center' | 'anchor'
-
[Attribute + Property]
arrow-padding

The amount of padding between the arrow and the edges of the popover. If the popover has a border-radius, for example, this will prevent it from overflowing the corners.

number
-
[Attribute + Property]
flip

When set, placement of the popover will flip to the opposite site to keep it in view. You can use flipFallbackPlacements to further configure how the fallback placement is determined.

boolean
-
[Attribute + Property]
flip-fallback-placements

If the preferred placement doesn’t fit, popover will be tested in these fallback placements until one fits. Must be a string of any number of placements separated by a space, e.g. “top bottom left”. If no placement fits, the flip fallback strategy will be used instead.

string
-
[Attribute + Property]
flip-fallback-strategy

When neither the preferred placement nor the fallback placements fit, this value will be used to determine whether the popover should be positioned using the best available fit based on available space or as it was initially preferred.

'best-fit' | 'initial'
-
[Attribute + Property]
flip-padding

The amount of padding, in pixels, to exceed before the flip behavior will occur.

number
-
[Attribute + Property]
shift

Moves the popover along the axis to keep it in view when clipped.

boolean
-
[Attribute + Property]
shift-padding

The amount of padding, in pixels, to exceed before the shift behavior will occur.

number
-
[Attribute + Property]
auto-size

When set, this will cause the popover to automatically resize itself to prevent it from overflowing.

null | 'horizontal' | 'vertical' | 'both'
-
[Attribute + Property]
sync

Syncs the popover’s width or height to that of the anchor element.

null | 'width' | 'height' | 'both'
-
[Attribute + Property]
auto-size-padding

The amount of padding, in pixels, to exceed before the auto-size behavior will occur.

number
-
[Attribute + Property]
hover-bridge

When a gap exists between the anchor and the popover element, this option will add a “hover bridge” that fills the gap using an invisible element. This makes listening for events such as mouseenter and mouseleave more sane because the pointer never technically leaves the element. The hover bridge will only be drawn when the popover is active.

boolean
-

Functions

Name Description Parameters
handlePopoverTriggerEvent()
-
e: RoleTooltipToggleEvent
handleReposition()
-
-
handleKeyDown()
-
e: KeyboardEvent
handleFocusChange()

This is a hack. Unfortunately, there’s no reliable way to get the active elements from just “focusin” / “focusout”

-
handleShow()

Used to show from event listeners.

event: Event
show()
-
triggerElement: Element, triggerSource: RoleTooltip["__triggerSource"]
findTriggerForTooltip()
-
e: Event
handleHide()
-
event: Event
hide()
-
triggerSource: null | RoleTooltip["__triggerSource"]
getOrAssignId()

Assign a random UUID with a prefix if no id is found. Will return the “id” of the element if its already assigned.

prefix: string, force: boolean
getTextDirection()
-
-
debounce()
-
callback: (...args: any[]) => any, options: { key: any, wait: number }
setAria()
-
key: T, value: ARIAMixin[T]

Parts

Name Description
popover
-
popover--active
-
popover--fixed
-
popover--has-arrow
-
arrow
-
hover-bridge
-
hover-bridge--visible
-