# Popup [component-header:sl-popup] Popup is a utility that lets you declaratively anchor "popup" containers to another element. This component's name is inspired by [``](https://github.com/MicrosoftEdge/MSEdgeExplainers/blob/main/Popup/explainer.md). It uses [Floating UI](https://floating-ui.com/) 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 customized to your liking. Additional positioning options are available and described in more detail below. ```html preview ``` ```jsx react import { useState } from 'react'; import { SlPopup, SlSelect, SlMenuItem, SlInput, SlSwitch } from '@shoelace-style/shoelace/dist/react'; const css = ` .popup-overview sl-popup { --arrow-color: var(--sl-color-primary-600); } .popup-overview span[slot='anchor'] { display: inline-block; width: 150px; height: 150px; border: dashed 2px var(--sl-color-neutral-600); margin: 50px; } .popup-overview .box { width: 100px; height: 50px; background: var(--sl-color-primary-600); border-radius: var(--sl-border-radius-medium); } .popup-overview-options { display: flex; flex-wrap: wrap; align-items: end; gap: 1rem; } .popup-overview-options sl-select { width: 160px; } .popup-overview-options sl-input { width: 100px; } .popup-overview-options + .popup-overview-options { margin-top: 1rem; } `; const App = () => { const [placement, setPlacement] = useState('top'); const [distance, setDistance] = useState(0); const [skidding, setSkidding] = useState(0); const [active, setActive] = useState(true); const [arrow, setArrow] = useState(false); return ( <>
setPlacement(event.target.value)} > top top-start top-end bottom bottom-start bottom-end right right-start right-end left left-start left-end setDistance(event.target.value)} /> setSkidding(event.target.value)} />
setActive(event.target.checked)}> Active setArrow(event.target.checked)}> Arrow
); }; ``` ?> 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 `` element, popup will use the first assigned element as the anchor. This behavior allows other components to pass anchors through more easily via composition. ## Examples ### Active Popups are inactive and hidden until the `active` attribute is applied. Removing the attribute will tear down all positioning logic and listeners, meaning you can have many idle popups on the page without affecting performance. ```html preview ``` ```jsx react import { useState } from 'react'; import { SlPopup, SlSwitch } from '@shoelace-style/shoelace/dist/react'; const css = ` .popup-active span[slot='anchor'] { display: inline-block; width: 150px; height: 150px; border: dashed 2px var(--sl-color-neutral-600); margin: 50px; } .popup-active .box { width: 100px; height: 50px; background: var(--sl-color-primary-600); border-radius: var(--sl-border-radius-medium); } `; const App = () => { const [active, setActive] = useState(true); return ( <>

setActive(event.target.checked)}> Active
); }; ``` ### Placement Use the `placement` attribute to tell the popup the preferred placement of the popup. Note that the actual position will vary to ensure the panel remains in the viewport if you're using positioning features such as `flip` and `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 it will be removed when the popup is deactivated. ```html preview ``` ```jsx react import { useState } from 'react'; import { SlPopup, SlSelect, SlMenuItem } from '@shoelace-style/shoelace/dist/react'; const css = ` .popup-placement span[slot='anchor'] { display: inline-block; width: 150px; height: 150px; border: dashed 2px var(--sl-color-neutral-600); margin: 50px; } .popup-placement .box { width: 100px; height: 50px; background: var(--sl-color-primary-600); border-radius: var(--sl-border-radius-medium); } .popup-placement sl-select { max-width: 280px; } `; const App = () => { const [placement, setPlacement] = useState('top'); return (
setPlacement(event.target.value)}> top top-start top-end bottom bottom-start bottom-end right right-start right-end left left-start left-end
); }; ``` ### 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. ```html preview ``` ```jsx react import { useState } from 'react'; import { SlPopup, SlRange } from '@shoelace-style/shoelace/dist/react'; const css = ` .popup-distance span[slot='anchor'] { display: inline-block; width: 150px; height: 150px; border: dashed 2px var(--sl-color-neutral-600); margin: 50px; } .popup-distance .box { width: 100px; height: 50px; background: var(--sl-color-primary-600); border-radius: var(--sl-border-radius-medium); } .popup-distance sl-range { max-width: 260px; } `; const App = () => { const [distance, setDistance] = useState(0); return ( <>
setDistance(event.target.value)} />
); }; ``` ### 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. ```html preview ``` ```jsx react import { useState } from 'react'; import { SlPopup, SlRange } from '@shoelace-style/shoelace/dist/react'; const css = ` .popup-skidding span[slot='anchor'] { display: inline-block; width: 150px; height: 150px; border: dashed 2px var(--sl-color-neutral-600); margin: 50px; } .popup-skidding .box { width: 100px; height: 50px; background: var(--sl-color-primary-600); border-radius: var(--sl-border-radius-medium); } .popup-skidding sl-range { max-width: 260px; } `; const App = () => { const [skidding, setSkidding] = useState(0); return ( <>
setSkidding(event.target.value)} />
); }; ``` ### 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 color 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. ```html preview ``` ```jsx react import { useState } from 'react'; import { SlPopup, SlSwitch } from '@shoelace-style/shoelace/dist/react'; const css = ` .popup-arrow sl-popup { --arrow-color: var(--sl-color-primary-600); } .popup-arrow span[slot='anchor'] { display: inline-block; width: 150px; height: 150px; border: dashed 2px var(--sl-color-neutral-600); margin: 50px; } .popup-arrow .box { width: 100px; height: 50px; background: var(--sl-color-primary-600); border-radius: var(--sl-border-radius-medium); } `; const App = () => { const [arrow, setArrow] = useState(true); return ( <>

setArrow(event.target.checked)}> Arrow
); }; ``` ### Positioning Strategy By default, the popup is positioned using an absolute positioning strategy. However, if your anchor is fixed or exists within a container that has `overflow: auto|hidden`, the popup risks being clipped. To work around this, you can use a fixed positioning strategy by setting the `strategy` attribute to `fixed`. The fixed positioning strategy reduces jumpiness when the anchor is fixed and allows the popup to break out containers that clip. When using this strategy, it's important to note that the content will be positioned _relative to its containing block_, which is usually the viewport unless an ancestor uses a `transform`, `perspective`, or `filter`. [Refer to this page](https://developer.mozilla.org/en-US/docs/Web/CSS/position#fixed) for more details. In this example, you can see how the popup breaks out of the overflow container when it's fixed. The fixed positioning strategy tends to be less performant than absolute, so avoid using it unnecessarily. Toggle the switch and scroll the container to see the difference. ```html preview ``` ```jsx react import { useState } from 'react'; import { SlPopup, SlSwitch } from '@shoelace-style/shoelace/dist/react'; const css = ` .popup-strategy .overflow { position: relative; height: 300px; border: solid 2px var(--sl-color-neutral-200); overflow: auto; } .popup-strategy span[slot='anchor'] { display: inline-block; width: 150px; height: 150px; border: dashed 2px var(--sl-color-neutral-600); margin: 150px 50px; } .popup-strategy .box { width: 100px; height: 50px; background: var(--sl-color-primary-600); border-radius: var(--sl-border-radius-medium); } .popup-strategy sl-switch { margin-top: 1rem; } `; const App = () => { const [fixed, setFixed] = useState(true); return ( <>
setFixed(event.target.checked)}> Fixed
); }; ``` ### 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 behavior's boundary and padding. Scroll the container to see how the popup flips to prevent clipping. ```html preview ``` ```jsx react import { useState } from 'react'; import { SlPopup, SlSwitch } from '@shoelace-style/shoelace/dist/react'; const css = ` .popup-flip .overflow { position: relative; height: 300px; border: solid 2px var(--sl-color-neutral-200); overflow: auto; } .popup-flip span[slot='anchor'] { display: inline-block; width: 150px; height: 150px; border: dashed 2px var(--sl-color-neutral-600); margin: 150px 50px; } .popup-flip .box { width: 100px; height: 50px; background: var(--sl-color-primary-600); border-radius: var(--sl-border-radius-medium); } `; const App = () => { const [flip, setFlip] = useState(true); return ( <>

setFlip(event.target.checked)}> Flip
); }; ``` ### 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 customize the shift behavior using `shiftBoundary` and `shift-padding`. Toggle the switch to see the difference. ```html preview ``` ```jsx react import { useState } from 'react'; import { SlPopup, SlSwitch } from '@shoelace-style/shoelace/dist/react'; const css = ` .popup-shift .overflow { position: relative; border: solid 2px var(--sl-color-neutral-200); overflow: auto; } .popup-shift span[slot='anchor'] { display: inline-block; width: 150px; height: 150px; border: dashed 2px var(--sl-color-neutral-600); margin: 60px 0 0 10px; } .popup-shift .box { width: 300px; height: 50px; background: var(--sl-color-primary-600); border-radius: var(--sl-border-radius-medium); } `; const App = () => { const [shift, setShift] = useState(true); return ( <>
setShift(event.target.checked)}> Shift
); }; ``` ### Auto-size Use the `auto-size` attribute to tell the popup to resize when necessary to prevent it from getting clipped. You can use `autoSizeBoundary` and `auto-size-padding` to customize the behavior 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, two read-only custom properties called `--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 max-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. ```html preview ``` ```jsx react import { useState } from 'react'; import { SlPopup, SlSwitch } from '@shoelace-style/shoelace/dist/react'; const css = ` .popup-auto-size .overflow { position: relative; height: 300px; border: solid 2px var(--sl-color-neutral-200); overflow: auto; } .popup-auto-size span[slot='anchor'] { display: inline-block; width: 150px; height: 150px; border: dashed 2px var(--sl-color-neutral-600); margin: 250px 50px 100px 50px; } .popup-auto-size .box { background: var(--sl-color-primary-600); border-radius: var(--sl-border-radius-medium); /* This sets the preferred size of the popup's content */ width: 100px; height: 200px; /* This sets the maximum dimensions and allows scrolling when auto-size kicks in */ max-width: var(--auto-size-available-width); max-height: var(--auto-size-available-height); overflow: auto; } `; const App = () => { const [autoSize, setAutoSize] = useState(true); return ( <>

setAutoSize(event.target.checked)}> Auto-size
); }; ``` [component-metadata:sl-popup]