Popover

The popover object documents the native HTML element and attribute based API for popovers. The examples below use normal elements and attributes such as popovertarget, popovertargetaction, popover, and the styleguide placement attributes.

Change the relative element at runtime

The popover:initialized event exposes the popover instance as event.detail.popover. Call setRelativeElement() with an HTMLElement to replace the current relativeElement; the method updates the popover data and repositions immediately. Pass null to stop using a relative element and return to viewport positioning.

document.addEventListener('popover:initialized', (event) => {
const popover = event.detail.popover;

if (popover.getPopoverData().id !== 'popover-example') {
return;
}

const firstAnchor = document.getElementById('first-anchor');
const secondAnchor = document.getElementById('second-anchor');
const switchAnchorButton = document.getElementById('switch-anchor');

if (!firstAnchor || !secondAnchor || !switchAnchorButton) {
return;
}

let currentAnchor = firstAnchor;
const popoverData = popover.getPopoverData();
const popoverElement = popoverData.popoverElement;

const syncSwitchButtonInvoker = () => {
if (popoverElement.matches(':popover-open')) {
switchAnchorButton.setAttribute('popovertarget', popoverData.id);
switchAnchorButton.setAttribute('popovertargetaction', 'show');
return;
}

switchAnchorButton.removeAttribute('popovertarget');
switchAnchorButton.removeAttribute('popovertargetaction');
};

popoverElement.addEventListener('toggle', syncSwitchButtonInvoker);
syncSwitchButtonInvoker();

switchAnchorButton.addEventListener('click', () => {
currentAnchor = currentAnchor === firstAnchor ? secondAnchor : firstAnchor;
popover.setRelativeElement(currentAnchor);
});
});

Register the event listener before the popover is initialized. The positioner assigns the generated anchor name (--<popover id>) to the selected element; it does not select an existing CSS anchor name. setRelativeElement() clears that generated name from the previously selected element, updates the reference, and repositions. The switch button has no popover action while closed, so switching does not open it. While open, it temporarily acts as a show invoker so switching does not light-dismiss it.

Basic native popover

A normal button that toggles a popover element by id.

Click on the trigger to show the popover.

This is a popover.

Supported trigger actions

Buttons can explicitly show, hide, or toggle the same popover using `popovertargetaction`.

This example shows the supported native trigger actions for popovers.

This popover is controlled by native attributes.

Popover with backdrop

Adds a backdrop behind the popover content.

This example shows a popover with a backdrop behind it.

This popover has a backdrop.

Full-page cover

Lets the popover fill the whole viewport when `data-js-popover-cover` is set.

Click on the trigger to show a popover that covers the full viewport.

This popover covers the whole page.

Relative placement

Positions the popover relative to its trigger instead of the viewport.

This example positions the popover relative to the trigger.

This popover is placed relative to the trigger.

Relative placement with a separate anchor

Positions the popover relative to another element, using `data-js-popover-relative` on the anchor element.

This example positions the popover relative to a separate anchor element.

Anchor element

This popover is placed relative to the anchor element instead of the trigger.

Change the relative element at runtime

After initialization, use the popover instance's `setRelativeElement()` method to switch back and forth between two anchors.

This popover starts at the first anchor. Use the button to switch between both anchors.

First anchor
Second anchor

This popover starts at the first anchor. Use the button to switch between both anchors.

Viewport placement

Uses the placement attributes relative to the viewport when `data-js-popover-relative` is not set on the popover.

This example uses viewport-based placement.

This popover is placed relative to the viewport.

Parameters (Blade)

Key Default value Type Available values Description
Connects a trigger button to a popover by id. string Any element with a matching id can be used as a popover target -
Controls whether the trigger toggles, shows, or hides the popover. string - -
Marks the element as a popover. string - -
Optional. Makes the popover cover the full viewport and skips the regular placement logic. string - -
Optional. Add it to the popover to position the popover relative to the trigger, or set it on another element with the popover id as the value to use that element as the relative anchor instead. To change the relative element after initialization, call `setRelativeElement(element)` on the popover instance from the `popover:initialized` event. string - -
Optional. Sets horizontal placement to `left`, `center`, or `right`. string - -
Optional. Sets vertical placement to `top`, `center`, or `bottom`. string - -
Optional. Adds a backdrop behind the popover. string - -
string - The DOM id of the component.
[] array - Array containing wrapping classes array
[] array - Array containing keys and values rendered as attributes