Tooltip
# Features
- Show tooltip on hover and focus.
- Hide on pointer down or by pressing the
Escapekey. - 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? |
booleanThe open state of the tooltip when it is initially rendered. |
false |
openDelay? |
numberThe duration from when the mouse enters a trigger until open is triggered. |
1000 |
closeDelay? |
numberThe 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 | nullWhere 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 | undefinedEvent 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 |
() => voidUtility function that shows the tooltip. |
hide |
() => voidUtility function that hides the tooltip. |
toggle |
() => voidUtility function that toggles the tooltip. |
# PositioningOptions
| Property | Description | Default |
|---|---|---|
strategy? |
absolute | fixedThe 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-endThe initial placement of the floating element. |
"top" |
offset? |
ObjectThe offset of the floating element. |
— |
gutter? |
numberThe main axis offset or gap between the reference and floating elements. |
5 |
overflowPadding? |
numberThe virtual padding around the viewport edges to check for overflow. |
8 |
flip? |
booleanWhether to flip the placement. |
true |
overlap? |
booleanWhether the floating element can overlap the reference element. |
false |
sameWidth? |
booleanWhether to make the floating element same width as the reference element. |
false |
fitViewport? |
booleanWhether the floating element should fit the viewport. |
false |
boundary? |
@floating-ui/dom.BoundaryThe overflow boundary of the reference element. |
— |