Tooltips

Helpful little buggers that appear at the darndest times (usually when you hover an element). You might use them for showing extra context, keyboard shortcuts, or other low-priority information.

Tooltips can be used two different ways:

  1. As a wrapper, using the <x-slot:trigger> slot to specify the triggering element or button
  2. As a child — the <ui:tooltip /> element will use its parent element as a trigger if no trigger slot is defined

Using Tooltips

Wrap the triggering element with the <ui:tooltip> tag and the <x-slot:trigger> slot tag

Wrapper Tooltip with Trigger Slot
Copied!
1<ui:tooltip title="Have a nice day!">
2 <x-slot:trigger>
3 <ui:button color="success">
4 Hover for top secret info
5 </ui:button>
6 </x-slot:trigger>
7</ui:tooltip>
1<ui:tooltip title="Have a nice day!">
2 <x-slot:trigger>
3 <ui:button color="success">
4 Hover for top secret info
5 </ui:button>
6 </x-slot:trigger>
7</ui:tooltip>

OR

Use <ui:tooltip> inside an element to trigger the tooltip by hovering its parent element.

Child Tooltip (Parent Element Trigger)
Copied!
1<ui:button color="success">
2 <ui:tooltip title="Have a nice day!" />
3 Hover for top secret info
4</ui:button>
1<ui:button color="success">
2 <ui:tooltip title="Have a nice day!" />
3 Hover for top secret info
4</ui:button>

Tooltips will adjust their positioning if they overflow outside the viewport. The anchoring behavior and the overflow handling are made possible thanks to Alpine's Anchor plugin and Floating UI.

Properties

Use the following properties to customize your tooltips.

Property Type Default Description
title string null The main title of the tooltip.
shortcut string null Additional styled keyboard shortcut text.
offset integer 4 The offset of the tooltip from the trigger element.
position string top The position relative to the trigger element.

Title

The title prop is interchangeable with the default slot. Use whichever style you prefer.

1{{-- `title` prop or default slot (same thing) --}}
2<ui:tooltip title="Hello" />
3<ui:tooltip>Hello</ui:tooltip>
1{{-- `title` prop or default slot (same thing) --}}
2<ui:tooltip title="Hello" />
3<ui:tooltip>Hello</ui:tooltip>
Copied!

Shortcut

Use the shortcut prop (or slot) to add styled <kbd> tags to your tooltip.

1{{-- `shortcut` prop or shortcut slot (same thing) --}}
2<ui:tooltip shortcut="⌘+S" />
3<ui:tooltip>
4 <x-slot:shortcut>⌘+S</x-slot:shortcut>
5</ui:tooltip>
1{{-- `shortcut` prop or shortcut slot (same thing) --}}
2<ui:tooltip shortcut="⌘+S" />
3<ui:tooltip>
4 <x-slot:shortcut>⌘+S</x-slot:shortcut>
5</ui:tooltip>
Copied!

Offset

The offset defines how far (in pixels) the tooltip appears from the triggering element. In other words, the gap between the tooltip and what it's attached to. The default is 4 -- although any integer value will work.

1<ui:tooltip title="I'm a tooltip" offset="10" />
1<ui:tooltip title="I'm a tooltip" offset="10" />
Copied!

Position

The position prop defines where the tooltip appears relative to its triggering element. This property defaults to top, but you can also use top-start, top-end, bottom, bottom-start, bottom-end, left, left-start, left-end, right, right-start, and right-end.

Both the offset and position props are sent to Floating UI Alpine's Anchor plugin.

Size

The size prop defines the size of the tooltip. The possible sizes are sm, md (default), lg, and xl. Why would you want a tooltip to be extra large? I don't know, but you can if you want to.

Tooltip Sizes
Copied!
1<ui:button>
2 Small (sm)
3 <ui:tooltip size="sm" title="I'm a small tooltip" />
4</ui:button>
5 
6<ui:button>
7 Medium (md)
8 <ui:tooltip size="md" title="I'm a medium tooltip" />
9</ui:button>
10 
11<ui:button>
12 Large (lg)
13 <ui:tooltip size="lg" title="I'm a large tooltip" />
14</ui:button>
15 
16<ui:button>
17 Extra Large (xl)
18 <ui:tooltip size="xl" title="I'm an extra large tooltip" />
19</ui:button>
1<ui:button>
2 Small (sm)
3 <ui:tooltip size="sm" title="I'm a small tooltip" />
4</ui:button>
5 
6<ui:button>
7 Medium (md)
8 <ui:tooltip size="md" title="I'm a medium tooltip" />
9</ui:button>
10 
11<ui:button>
12 Large (lg)
13 <ui:tooltip size="lg" title="I'm a large tooltip" />
14</ui:button>
15 
16<ui:button>
17 Extra Large (xl)
18 <ui:tooltip size="xl" title="I'm an extra large tooltip" />
19</ui:button>

Examples

Here's an example of the tooltip defaults

Default Tooltip
Copied!
1<ui:button>
2 <ui:tooltip title="Have a nice day!" />
3 Hover me if you please
4</ui:button>
1<ui:button>
2 <ui:tooltip title="Have a nice day!" />
3 Hover me if you please
4</ui:button>

You can also customize the position and offset, like this.

Custom Offset and Position
Copied!
1<ui:button>
2 <ui:tooltip position="right" offset="20">Enjoy yourself, and be happy.</ui:tooltip>
3 Mouse over, and be happy
4</ui:button>
1<ui:button>
2 <ui:tooltip position="right" offset="20">Enjoy yourself, and be happy.</ui:tooltip>
3 Mouse over, and be happy
4</ui:button>
Like this project? Stop by the bear cave to stargaze.