Popups
Create floating popups, menus, and tooltips using the power of Floating UI.
import { popup } from '@skeletonlabs/skeleton';
import type { PopupSettings } from '@skeletonlabs/skeleton';
Demo
Installation
RequiredTo begin, install Floating UI from NPM. This is required for popups to function.
npm install @floating-ui/dom
Import Floating UI into your application's root layout /src/routes/+layout.svelte
.
import { computePosition, autoUpdate, offset, shift, flip, arrow } from '@floating-ui/dom';
Import storePopup
in your root layout, then pass an object containing the required Floating UI modules shown
below.
import { storePopup } from '@skeletonlabs/skeleton';
storePopup.set({ computePosition, autoUpdate, offset, shift, flip, arrow });
Events
You can control how the popup is opened and closed by using the event
setting.
Click
Tap the trigger element to open the popup, then outside to close it. Supports the closeQuery
feature.
Hover
The popup shows only while hovering the trigger element. Great for creating tooltips.
Be sure to disable pointer events for children (ex: icons) within your trigger element. These may cause the popup to flash or close
early. We recommend applying [&>*]:pointer-events-none
to your trigger element to resolve this.
Focus-Blur
Shows the popup only while the trigger element has focus. Useful for showing tooltips while focusing an input.
Focus-Click
Show the popup on focus, closed when clicking outside. Useful for autocomplete popups. Supports the closeQuery
feature.
Settings
In addition to event
, let's explore what other popupSettings
are available.
Placement
Defines which side of the trigger the popup will appear. This will automatically flip when near the edge of the screen.
Close Query
Use the closeQuery
setting to indicate what child elements within the popup can trigger the popup to
close. By default this uses
'a[href], button'
to denote anchors and buttons. You may provide a custom query or set
''
to disable this feature.
State Callback
Provide a callback function to be notified when a particular popup is opened or closed.
Middleware
Use middleware
to configure Floating UI middleware such as shift, offset, and more.
This includes support for the optional Floating UI middleware shown below. To use these, import the Floating UI modules and pass
them to the storePopup
in your root layout as shown below. Note that these may alter the default behavior
of your popups. We recommend these only for advanced users.
import { /* ... */ size, autoPlacement, hide, inline } from '@floating-ui/dom';
storePopup.set({ /* ... */ size, autoPlacement, hide, inline });
Combobox
The goal of Skeleton is to combine primative elements and components to build more complex UI. For example, by combining a Button, Popup, and ListBox you can create a highly customizable combobox.
Z-Index Stacking
Please note that neither Skelton nor Floating-UI define a default z-index for popups.
Accessibility
Use click
or focus
events when targeting mobile. Touch screens do not fully support hover
.