Zum Hauptinhalt springen Seitenleiste
Auf dieser Seite
Zum Inhaltsverzeichnis springen

Popup

<pc-popup> 0.4.0 experimental

Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.

Popup is a utility that lets you declaratively anchor “popup” containers to another element.

This component’s name is inspired by <popup>. It uses Floating UI under the hood to provide a well‐tested, lightweight and fully declarative positioning utility for tooltips, dropdowns and more.

Popup doesn’t provide any styles—just positioning! The popup’s preferred placement, distance and skidding (offset) can be configured using attributes. An arrow that points to the anchor can be shown and customised to your liking. Additional positioning options are available and described in more detail below.

Popup is a low‐level utility built specifically for positioning elements. Do not mistake it for a tooltip or similar because it does not facilitate an accessible experience! Almost every correct usage of it will involve building other components. It should rarely, if ever, occur directly in your HTML.

<div class="popup-overview">
    <pc-popup placement="top" active>
        <span slot="anchor"></span>
        <div class="box"></div>
    </pc-popup>

    <div class="popup-overview-options">
        <pc-select
            class="popup-overview-select"
            label="Placement"
            name="placement"
            value="top"
        >
            <pc-option value="top">top</pc-option>
            <pc-option value="top-start">top-start</pc-option>
            <pc-option value="top-end">top-end</pc-option>
            <pc-option value="bottom">bottom</pc-option>
            <pc-option value="bottom-start">bottom-start</pc-option>
            <pc-option value="bottom-end">bottom-end</pc-option>
            <pc-option value="right">right</pc-option>
            <pc-option value="right-start">right-start</pc-option>
            <pc-option value="right-end">right-end</pc-option>
            <pc-option value="left">left</pc-option>
            <pc-option value="left-start">left-start</pc-option>
            <pc-option value="left-end">left-end</pc-option>
        </pc-select>
        <pc-input
            label="Distance"
            type="number"
            name="distance"
            value="0"
        ></pc-input>
        <pc-input
            label="Skidding"
            type="number"
            name="skidding"
            value="0"
        ></pc-input>
    </div>

    <div class="popup-overview-options">
        <pc-switch name="active" checked>Active</pc-switch>
        <pc-switch name="arrow">Arrow</pc-switch>
    </div>
</div>

<script>
    const container = document.querySelector(".popup-overview");
    const popup = container.querySelector("pc-popup");
    const select = container.querySelector('pc-select[name="placement"]');
    const distance = container.querySelector('pc-input[name="distance"]');
    const skidding = container.querySelector('pc-input[name="skidding"]');
    const active = container.querySelector('pc-switch[name="active"]');
    const arrow = container.querySelector('pc-switch[name="arrow"]');

    select.addEventListener("pc-change", () => (popup.placement = select.value));
    distance.addEventListener("pc-input", () => (popup.distance = distance.value));
    skidding.addEventListener("pc-input", () => (popup.skidding = skidding.value));
    active.addEventListener("pc-change", () => (popup.active = active.checked));
    arrow.addEventListener("pc-change", () => (popup.arrow = arrow.checked));
</script>

<style>
    .popup-overview pc-popup {
        --arrow-color: var(--pc-color-primary-fill-loud);
    }

    .popup-overview span[slot="anchor"] {
        display: inline-block;
        margin: var(--pc-spacing-xxxxl);
        inline-size: 150px;
        block-size: 150px;
        border: var(--pc-border-width-m) dashed
            var(--pc-color-neutral-border-normal);
        border-radius: var(--pc-border-radius-l);
    }

    .popup-overview .box {
        inline-size: 100px;
        block-size: 55px;
        background-color: var(--pc-color-primary-fill-loud);
        border-radius: var(--pc-border-radius-m);
    }

    .popup-overview-options {
        display: flex;
        align-items: center;
        justify-content: flex-start;
        flex-wrap: wrap;
        gap: var(--pc-spacing-s);
    }

    .popup-overview-options pc-select {
        inline-size: 200px;
    }

    .popup-overview-options pc-input {
        inline-size: 100px;
    }

    .popup-overview-options + .popup-overview-options {
        margin-block-start: var(--pc-spacing-m);
    }
</style>
Code Edit

A popup’s anchor should not be styled with display: contents since the coordinates will not be eligible for calculation. However, if the anchor is a <slot> element, the Popup component will use the first assigned element as the anchor. This behaviour allows other components to pass anchors through more easily via composition.

Demos

Activation

Popups are inactive and hidden until the active attribute is applied. Removing the attribute will tear down all positioning logic and event listeners, meaning you can have many idle popups on the page without affecting performance.

Edit

External anchors

By default, anchors are slotted into the popup using the anchor slot. If your anchor needs to live outside of the popup, you can pass the anchor’s id to the anchor attribute. Alternatively, you can pass an element reference to the anchor property to achieve the same effect without using an id.

Edit

Placement

Use the placement attribute to tell the popup the preferred placement of the popup. The actual position will vary to ensure the panel remains in the viewport if you’re using positioning features like flip or shift.

Since placement is preferred when using flip, you can observe the popup’s current placement when it’s active by looking at the data-current-placement attribute. This attribute will update as the popup flips to find available space and will be removed when the popup is deactivated.

Edit

Distance

Use the distance attribute to change the distance between the popup and its anchor. A positive value will move the popup further away and a negative value will move it closer to the anchor.

Edit

Skidding

The skidding attribute is similar to distance, but instead allows you to offset the popup along the anchor’s axis. Both positive and negative values are allowed.

Edit

Arrows

Add an arrow to your popup with the arrow attribute. It’s usually a good idea to set a distance to make room for the arrow. To adjust the arrow’s colour and size, use the --arrow-color and --arrow-size custom properties, respectively. You can also target the arrow part to add additional styles such as shadows and borders.

By default, the arrow will be aligned as close to the centre of the anchor as possible, considering available space and arrow-padding. You can the arrow-placement attribute to force the arrow to align to the start, centre or end of the popup instead.

Edit

Syncing with the anchor’s dimensions

Use the sync attribute to make the popup the same with or height as the anchor element. This is useful for controls that need the popup to stay the same width or height as the anchor element.

Edit

Flip

When the popup doesn’t have enough room in its preferred placement, it can automatically flip to keep it in view. To enable this, use the flip attribute. By default, the popup will flip to the opposite placement, but you can configure preferred fallback placements using flip-fallback-placement and flip-fallback-strategy. Additional options are available to control the flip behaviour’s boundary and padding.

Scroll the container to see how the popup flips to prevent clipping.

Edit

Flip fallbacks

While using the flip attribute, you can customise the placement of the popup when the preferred placement doesn’t have room. For this, use flip-fallback-placements and flip-fallback-strategy.

If the preferred placement doesn’t have room, the first suitable placement found in flip-fallback-placement will be used. The value of this attribute must be a string including any number of placements separated by a space, like "right bottom".

If no fallback placement works, the final placement will be determined by flip-fallback-strategy. This value can be either initial (default), where the placement reverts to the position in placement, or best-fit, where the placement is chosen based on available space.

Scroll the container to see how the popup changes its fallback placement to prevent clipping.

Edit

Shift

When a popup is longer than its anchor, it risks being clipped by an overflowing container. In this case, use the shift attribute to shift the popup along its axis and back into view. You can customise the shift behaviour using shiftBoundary and shift-padding.

Toggle the switch to see the demo in action.

Edit

Auto‐size

Use the auto-size attribute to tell the popup to resize when necessary to prevent it from getting clipped. Possible values are horizontal, vertical and both. You can use autoSizeBoundary and auto-size-padding to customise the behaviour of this option. Auto‐size works well with flip, but if you’re using auto-size-padding, make sure flip-padding is the same value.

When using auto-size, one or both of --auto-size-available-width and --auto-size-available-height will be applied to the host element. These values determine the available space the popover has before clipping will occur. Since they cascade, you can use them to set a maximum width/height on your popup’s content and easily control its overflow.

Scroll the container to see the popup resize as its available space changes.

Edit

Hover bridge

When a gap exists between the anchor and the popup, this option will add a “hover bridge” that fills the gap using an invisible element. This makes listening for events such as mouseover and mouseout more sane because the cursor technically never leaves the element. The hover bridge will only be drawn when the popup is active. For demo purposes, the hover bridge will be shown in a transparent blue colour.

Edit

Virtual elements

In most cases, popups are anchored to an actual element. Sometimes, it can be useful to anchor them to non‐DOM elements. To do this, you can pass a VirtualElement to the anchor property. A virtual element must contain a function called getBoundingClientRect() that returns a DOMRect object as shown below.

const virtualElement = {
    getBoundingClientRect() {
        // …
        return { width, height, x, y, top, left, right, bottom };
    },
};

This demo anchors a popup to the mouse cursor using a virtual element. As such, a mouse is required to properly view it.

Edit

Sometimes, the getBoundingClientRects might be derived from a real element. In this case, provide the anchor element as context to ensure clipping and position updates for the popup work well.

const virtualElement = {
    getBoundingClientRect() {
        // …
        return { width, height, x, y, top, left, right, bottom };
    },
    contextElement: anchorElement,
};

Eigenschaften

NameBeschreibungReflektiertStandard
anchorThe element the popup will be anchored to. If the anchor lives outside of the popup, you can provide the anchor element’s id, a DOM element reference or a VirtualElement. If the anchor lives inside the popup, use the anchor slot instead.
Typ: Element | string | VirtualElement | undefined
activeActivates the positioning logic and shows the popup. When this attribute is removed, the positioning logic is torn down and the popup will be hidden.
Typ: boolean
false
placementThe preferred placement of the popup. Note that the actual placement will vary as configured to keep the panel inside of the viewport.
Typ: "top" | "top-start" | "top-end" | "bottom" | "bottom-start" | "bottom-end" | "right" | "right-start" | "right-end" | "left" | "left-start" | "left-end"
"top"
boundaryThe bounding box to use for flipping, shifting and auto‐sizing.
Typ: "viewport" | "scroll"
"viewport"
distanceThe distance in pixels from which to offset the panel away from its anchor.
Typ: number
0
skiddingThe distance in pixels from which to offset the panel alongside its anchor.
Typ: number
0
arrowAdds an arrow to the popup. The arrow’s size and colour can be customised using the --arrow-size and --arrow-color custom properties. For additional customisation, you can also target the arrow using the arrow part in your stylesheet.
Typ: boolean
false
arrowPlacement
arrow-placement
The placement of the arrow. The default is anchor, which will align the arrow as close to the centre of the anchor as possible, considering available space and arrow padding. The alignment values will align the arrow to the popup instead.
Typ: "start" | "end" | "center" | "anchor"
"anchor"
arrowPadding
arrow-padding
The amount of padding between the arrow and the popup’s edges.
Typ: number
10
flipIf the popup doesn’t fit in the boundary with the placement anymore, the popup will flip to the opposite side to keep it in view. You can also use the flip-fallback-placements attribute to further configure how the fallback placement is determined.
Typ: boolean
false
flipFallbackPlacements
flip-fallback-placements
If the preferred placement doesn’t fit in the boundary anymore, the popup will try to fit in the fallback placements you specify until one fits. It must be a string of any number of placements separated by a space (e.g., top right bottom). If no placement fits, the flip fallback strategy will be used instead.
Typ: string
""
flipFallbackStrategy
flip-fallback-strategy
When neither the preferred placement nor the fallback placements fit, this value will be use to determine whether the popup should be positioned using the best available fit based on available space or as it was initially preferred.
Typ: "best-fit" | "initial"
"best-fit"
flipBoundaryThe flip boundary describes one or more clipping elements where overflow will be checked relative to when flipping. By default, the boundary includes overflow ancestors that will cause the element to be clipped. If needed, you can change the boundary by passing a reference to one or more elements to this property.
Typ: Element | Element[] | undefined
flipPadding
flip-padding
The amount of padding in pixels to exceed before the flip behaviour will occur.
Typ: number
0
shiftMoves the popup along the axis to keep it in view when clipped.
Typ: boolean
false
shiftBoundaryThe shift boundary describes one or more clipping elements where overflow will be checked relative to when shifting. By default, the boundary includes overflow ancestors that will cause the element to be clipped. If needed, you can change the boundary by passing a reference to one or more elements to this property.
Typ: Element | Element[] | undefined
shiftPadding
shift-padding
The amount of padding in pixels to exceed before the shift behaviour will occur.
Typ: number
0
autoSize
auto-size
If set, the popup will automatically resize itself to prevent it from overflowing.
Typ: "horizontal" | "vertical" | "both" | undefined
syncSyncs the popup’s width or height to that of the anchor element.
Typ: "width" | "height" | "both" | undefined
autoSizeBoundaryThe auto‐size boundary describes one or more clipping elements where overflow will be checked relative to when resizing. By default, the boundary includes overflow ancestors that will cause the element to be clipped. If needed, you can change the boundary by passing a reference to one or more elements to this property.
Typ: Element | Element[] | undefined
autoSizePadding
auto-size-padding
The amount of padding in pixels to exceed before the auto‐size behaviour will occur.
Typ: number
0
hoverBridge
hover-bridge
When a gap exists between the anchor and the popup element, this option will add a “hover bridge” that fills the gap using an invisible element. This makes listening for events such as mouseenter and mouseleave more sane because the cursor technically never leaves the element. The hover bridge is only drawn if the popup is active.
Typ: boolean
false
updateComplete Ein schreibgeschütztes Promise, das erfüllt ist, sobald die Komponente fertig aktualisiert wurde.

Erfahre mehr über Attribute und Eigenschaften.

Slots

NameBeschreibung
(Standard)The popup’s content.
anchorThe element the popup will be anchored to. If the anchor lives outside of the popup, you can use the anchor attribute/property instead.

Erfahre mehr über die Benutzung von Slots.

Methoden

NameBeschreibungArgumente
reposition()Forces the popup to recalculate and reposition itself.

Erfahre mehr über Methoden.

Events

NameBeschreibungEvent‐Detail
pc-repositionEmitted when the popup is repositioned. This even can fire a lot, so avoid putting expensive operations in your event listener or consider debouncing it.

Erfahre mehr über Events.

Benutzerdefinierte Eigenschaften

NameBeschreibungStandard
--arrow-sizeThe arrow’s size. The arrow won’t be shown unless the arrow attribute is used.var(--pc-tooltip-arrow-size)
--arrow-colorThe arrow’s colour.var(--pc-color-neutral-fill-normal)

Erfahre mehr über das Anpassen von benutzerdefinierten Eigenschaften.

Parts

NameBeschreibung
arrowThe arrow’s container. Avoid setting positioning properties, as these values are assigned dynamically as the popup moves.
popupThe popup’s container.
hover-bridgeThe hover bridge element. This is only available if the hover-bridge attribute is set.

Erfahre mehr über das Anpassen von CSS‐Parts.

Importieren

Wenn du den Autoloader oder den Standard‐Loader nutzt, kannst du diesen Abschnitt überspringen. Falls du „Cherry Picking“ betreibst, kannst du die folgenden Snippets verwenden, um diese Komponente zu importieren.

CDN (Skript‐Tag)CDN (Import)npm (Import)

Um diese Komponente manuell vom CDN zu importieren, kannst du dieses Code‐Snippet kopieren und es in dein HTML einfügen.

<script type="module" src="https://cdn.jsdelivr.net/npm/placer-toolkit@1.0.0-alpha.3/cdn/components/popup/popup.js"></script>

Um diese Komponente manuell vom CDN zu importieren, kannst du dieses Code‐Snippet kopieren und es in deine JavaScript‐Datei einfügen.

import "https://cdn.jsdelivr.net/npm/placer-toolkit@1.0.0-alpha.3/cdn/components/popup/popup.js";

Um diese Komponente manuell von npm zu importieren, kannst du dieses Code‐Snippet kopieren und es in deine JavaScript‐Datei einfügen.

import "placer-toolkit/dist/components/popup/popup.js";

Wir würden uns freuen, von dir zu hören. Kontaktiere uns bitte bei Fragen oder Anliegen, die du hast.

Du kannst uns per E‐Mail unter placer.coc.reports+contact@gmail.com erreichen.

Wir freuen uns darauf, von dir zu hören!

Alles klar!
Gefährliche Lande

Ui! Du bist in die gefährlichen Lande von Placer Toolkit geraten. Version 0 ist veraltet und entspricht nicht den EU‐Datenschutzstandards, einschließlich DSGVO.

Willst du die neuesten Kräfte, Sicherheit und Compliance? Bleib bei der aktuellen Version von Placer Toolkit!

Ups! Aufladen!

Unsere Website ist zu 100 % frei von Cookies! Wir legen großen Wert auf deine Privatsphäre und speichern daher keinerlei Cookies oder persönliche Informationen über dich.

Dein Browserverlauf bleibt sauber und deine Daten sind geschützt, ganz im Einklang mit modernen Datenschutzstandards wie der DSGVO. Genieße deinen Besuch ganz ohne digitale Cookies! 🍪🚫

Weitere Informationen findest du in unserer Datenschutzerklärung.

Alles klar!