Tooltip

A small piece of contextual information about an element on the screen, which is displayed when a user hovers or focuses on the element it is describing.

# Features

  • Show tooltip on hover and focus.
  • Hide on pointer down or by pressing the Escape key.
  • Labeling support for screen readers.
  • Custom delay support.

# Dependencies

This module depends on @floating-ui/dom.

# Anatomy

Typically, a popover consists of:

  • Trigger: The element that triggers the popover.
  • Popover: The overlay, which is positioned relative to the trigger, and may contain
    • Close Button: An optional element to close the popover.
    • Arrow: An optional element that points to the trigger.
<script>
	import { createPopover } from '@grail-ui/svelte';

	const { usePopoverTrigger, triggerAttrs, usePopover, popoverAttrs, closeButtonAttrs, open } = createPopover();
</script>

<button use:usePopoverTrigger {...$triggerAttrs}>Click</button>

{#if $open}
	<section use:usePopover {...$popoverAttrs}>
		<div>
			Content
		</div>
		<button {...$closeButtonAttrs}>Close</button>
	</section>
{/if}

# Examples

# Customize delay

Tooltips are designed to appear either after a brief delay when the user hovers over the trigger, or immediately when the user uses keyboard focus. The openDelay property can be used to customize the delay for hover-triggered tooltips. Similarly, tooltips are programmed to disappear after a short delay once the user stops hovering over the trigger, or immediately when the user moves away from keyboard focus. The closeDelay property can be used to adjust the delay for hover-triggered tooltips.

createTooltip({
openDelay: 500,
closeDelay: 0,
})

# Changing placement

The tooltip uses floating-ui for dynamic positioning, which allows you to change its placement by passing a positioning object, that can its alignment, offset, and flip behavior. See all the available options.

createTooltip({
positioning: {
placement: "bottom",
gutter: 10
},
})

# Custom portal container

If you want to customize the element that your tooltip portals into, use the portal and pass the actual element or a CSS selector.

createTooltip({
portal: '#custom',
})

# API

# TooltipConfig

Property Description Default
open?
boolean
The open state of the tooltip when it is initially rendered.
false
openDelay?
number
The duration from when the mouse enters a trigger until open is triggered.
1000
closeDelay?
number
The duration from when the mouse leaves a trigger until hide is triggered.
500
positioning?
Partial<PositioningOptions>
Where to place the floating element relative to its reference element.
portal?
string | HTMLElement | null
Where to "portal" the floating element outside it's initial DOM position. It can be a HTMLElement or a CSS selector that points to an already existing element.
"body"
onOpenChange?
(value?: boolean) => void | undefined
Event handler called when the open state of the tooltip changes.

# TooltipReturn

Property Description
useTooltipTrigger
Action<HTMLElement, void>
Action for the trigger element.
triggerAttrs
Readable<Record<string, string | undefined>>
HTML attributes for the trigger element.
useTooltip
Action<HTMLElement, void>
Action for the tooltip element.
tooltipAttrs
Readable<Record<string, string>>
HTML attributes for the tooltip element.
arrowAttrs
Readable<Record<string, string>>
HTML attributes for the tooltip arrow element.
open
Writable<boolean>
The controlled open state of the tooltip.
show
() => void
Utility function that shows the tooltip.
hide
() => void
Utility function that hides the tooltip.
toggle
() => void
Utility function that toggles the tooltip.

# PositioningOptions

Property Description Default
strategy?
absolute | fixed
The strategy to use for positioning.
"absolute"
placement?
top | top-start | top-end | right | right-start | right-end | bottom | bottom-start | bottom-end | left | left-start | left-end
The initial placement of the floating element.
"top"
offset?
Object
The offset of the floating element.
gutter?
number
The main axis offset or gap between the reference and floating elements.
5
overflowPadding?
number
The virtual padding around the viewport edges to check for overflow.
8
flip?
boolean
Whether to flip the placement.
true
overlap?
boolean
Whether the floating element can overlap the reference element.
false
sameWidth?
boolean
Whether to make the floating element same width as the reference element.
false
fitViewport?
boolean
Whether the floating element should fit the viewport.
false
boundary?
@floating-ui/dom.Boundary
The overflow boundary of the reference element.