Popups declaratively anchor one element to another and keep them positioned together as the page scrolls or
resizes. Primarily a low-level building block for popovers, dropdowns, and tooltips.
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 customized to your liking. Additional positioning options are available and described in more detail below.
Popup is a low-level positioning utility, not an accessible component.
Don’t use it as a tooltip or similar — it doesn’t facilitate an accessible experience. Almost every correct use builds it into another component rather than placing <cs-popup> directly in your HTML.
Don’t style a popup’s anchor with display: contents.
The coordinates won’t be eligible for calculation. If the anchor is a <slot>, popup uses the first assigned element as the anchor, so components can pass anchors through via composition.
Examples
Activating
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.
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.
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.
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.
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.
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 to match styles applied to rest of the popup element.
By default, the arrow will be aligned as close to the center of the anchor as possible, considering available space and arrow-padding. You can use the arrow-placement attribute to force the arrow to align to the start, end, or center of the popup instead.
Borders can also be added to the popup element by targeting the contents of the cs-popup element. This styling can also be extended to the arrow itself by targeting .arrow class in the popup.
When adding borders to the popup element which has an arrow, make sure to set the --popup-border-width custom property to match the width of the border of the popup. Setting this will allow the arrow to overlap the border of the popup so that they visually appear connected.
{# TODO: this example totally destroys browsers. Needs investigation.
Sync
Use the sync attribute to make the popup the same width or height as the anchor element. This is useful for controls that need the popup to stay the same width or height as the trigger.
When the popup doesn’t have enough room in its preferred placement, it can automatically flip to keep it in view and visually connected to its anchor.
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.
By default, flip takes effect when the popup would overflow the viewport.
You can use boundary="scroll" to make the popup resize when it overflows its nearest scrollable container instead.
Scroll the container to see how the popup flips to prevent clipping.
While using the flip attribute, you can customize 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, e.g. "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 it’s fallback placement to prevent clipping.
When a popup is longer than its anchor, it risks overflowing.
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.
By default, auto-size takes effect when the popup would overflow the viewport.
You can use boundary="scroll" to make the popup resize when it overflows its nearest scrollable container instead.
Use the auto-size attribute to tell the popup to resize when necessary to prevent it from overflowing.
Possible values are horizontal, vertical, and both. 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.
By default, auto-size takes effect when the popup would overflow the viewport.
You can use boundary="scroll" to make the popup resize when it overflows its nearest scrollable container instead.
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 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.
Auto-size
<divclass="popup-auto-size"><divclass="overflow"><cs-popupplacement="top"auto-size="both"auto-size-padding="10"activeboundary="scroll"><spanslot="anchor"></span><divclass="box"></div></cs-popup></div><cs-divider></cs-divider><cs-switchchecked>Auto-size</cs-switch></div><style>.popup-auto-size .overflow{position: relative;height: 300px;border: solid var(--cs-border-width-m)var(--cs-color-surface-border);overflow: auto;}.popup-auto-size span[slot='anchor']{display: inline-block;width: 150px;height: 150px;border: dashed var(--cs-border-width-m)var(--cs-color-neutral-fill-loud);margin: 250px 50px 100px 50px;}.popup-auto-size .box{background:var(--cs-color-brand-fill-loud);border-radius:var(--cs-border-radius-m);/* 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;}</style><scripttype="module">const container = document.querySelector('.popup-auto-size');const popup = container.querySelector('cs-popup');const autoSize = container.querySelector('cs-switch');
autoSize.addEventListener('change',()=>(popup.autoSize = autoSize.checked ?'both':''));</script>
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 mouseover and mouseout more sane because the pointer never technically leaves the element. The hover bridge will only be drawn when the popover is active. For demonstration purposes, the bridge in this example is shown in orange.
In most cases, popups are anchored to an actual element. Sometimes, it can be useful to anchor them to a non-element. 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.
This example anchors a popup to the mouse cursor using a virtual element. As such, a mouse is required to properly view it.
Highlight mouse cursor
<divclass="popup-virtual-element"><cs-popupplacement="right-start"><divclass="circle"></div></cs-popup><cs-divider></cs-divider><cs-switch>Highlight mouse cursor</cs-switch></div><scripttype="module">const container = document.querySelector('.popup-virtual-element');const popup = container.querySelector('cs-popup');const circle = container.querySelector('.circle');const enabled = container.querySelector('cs-switch');let clientX =0;let clientY =0;// Set the virtual element as a property
popup.anchor ={getBoundingClientRect(){return{width:0,height:0,x: clientX,y: clientY,top: clientY,left: clientX,right: clientX,bottom: clientY,};},};// Only activate the popup when the switch is checked
enabled.addEventListener('change',()=>{
popup.active = enabled.checked;});// Listen for the mouse to move
document.addEventListener('mousemove', handleMouseMove);// Update the virtual element as the mouse movesfunctionhandleMouseMove(event){
clientX = event.clientX;
clientY = event.clientY;// Reposition the popup when the virtual anchor movesif(popup.active){
popup.reposition();}}</script><style>/* If you need to set a z-index, set it on the popup part like this */.popup-virtual-element cs-popup::part(popup){z-index: 1000;pointer-events: none;}.popup-virtual-element .circle{width: 100px;height: 100px;border: solid 4px var(--cs-color-neutral-fill-loud);border-radius:var(--cs-border-radius-circle);translate: -50px -50px;animation: 1s virtual-cursor infinite;}@keyframes virtual-cursor{0%{scale: 1;}50%{scale: 1.1;}}</style>
Built-in Animations
The following classes can be applied to the popup’s popup part to animate it in or out programmatically. You can control the animation duration with the --show-duration and --hide-duration custom properties.
show / hide - Shows or hides the popover with a fade
show-with-scale / hide-with-scale - Shows or hides the popover with a fade and subtle scale effect
API
Importing
If you're using the autoloader or a hosted project, components load on demand — no manual import
needed. To cherry-pick this component, use one of the following snippets.
Activates the positioning logic and shows the popup. When this attribute is removed, the positioning logic is torn down and the popup will be hidden.
boolean
false
Yes
anchor
anchor
The element the popup will be anchored to. If the anchor lives outside of the popup, you can provide the anchor element id, a DOM element reference, or a VirtualElement. If the anchor lives inside the popup, use the anchor slot instead.
Element | string | VirtualElement
—
—
arrow
arrow
Attaches an arrow to the popup. The arrow's size and color can be customized using the --arrow-size and --arrow-color custom properties. For additional customizations, you can also target the arrow using ::part(arrow) in your stylesheet.
boolean
false
—
arrowPadding
arrow-padding
The amount of padding between the arrow and the edges of the popup. If the popup has a border-radius, for example, this will prevent it from overflowing the corners.
number
10
—
arrowPlacement
arrow-placement
The placement of the arrow. The default is anchor, which will align the arrow as close to the center of the anchor as possible, considering available space and arrow-padding. A value of start, end, or center will align the arrow to the start, end, or center of the popover instead.
'start' | 'end' | 'center' | 'anchor'
'anchor'
—
autoSize
auto-size
When set, this will cause the popup to automatically resize itself to prevent it from overflowing.
'horizontal' | 'vertical' | 'both'
—
—
autoSizeBoundary
autoSizeBoundary
The auto-size boundary describes clipping element(s) that 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.
Element | Element[]
—
—
autoSizePadding
auto-size-padding
The amount of padding, in pixels, to exceed before the auto-size behavior will occur.
number
0
—
boundary
boundary
The bounding box to use for flipping, shifting, and auto-sizing.
'viewport' | 'scroll'
'viewport'
—
distance
distance
The distance in pixels from which to offset the panel away from its anchor.
number
0
—
flip
flip
When set, placement of the popup will flip to the opposite site to keep it in view. You can use flipFallbackPlacements to further configure how the fallback placement is determined.
boolean
false
—
flipBoundary
flipBoundary
The flip boundary describes clipping element(s) that 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.
Element | Element[]
—
—
flipFallbackPlacements
flip-fallback-placements
If the preferred placement doesn't fit, popup will be tested in these fallback placements until one fits. Must be a string of any number of placements separated by a space, e.g. "top bottom left". If no placement fits, the flip fallback strategy will be used instead.
string
''
—
flipFallbackStrategy
flip-fallback-strategy
When neither the preferred placement nor the fallback placements fit, this value will be used to determine whether the popup should be positioned using the best available fit based on available space or as it was initially preferred.
'best-fit' | 'initial'
'best-fit'
—
flipPadding
flip-padding
The amount of padding, in pixels, to exceed before the flip behavior will occur.
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 pointer never technically leaves the element. The hover bridge will only be drawn when the popover is active.
boolean
false
—
placement
placement
The preferred placement of the popup. Note that the actual placement will vary as configured to keep the panel inside of the viewport.
A reference to the internal popup container. Useful for animating and styling the popup with JavaScript.
HTMLElement
—
—
shift
shift
Moves the popup along the axis to keep it in view when clipped.
boolean
false
—
shiftBoundary
shiftBoundary
The shift boundary describes clipping element(s) that 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.
Element | Element[]
—
—
shiftPadding
shift-padding
The amount of padding, in pixels, to exceed before the shift behavior will occur.
number
0
—
skidding
skidding
The distance in pixels from which to offset the panel along its anchor.
number
0
—
sync
sync
Syncs the popup's width or height to that of the anchor element.
The size of the arrow. Note that an arrow won't be shown unless the arrow attribute is used.
6px
--auto-size-available-height
A read-only custom property that determines the amount of height the popup can be before overflowing. Useful for positioning child elements that need to overflow. This property is only available when using auto-size.
—
--auto-size-available-width
A read-only custom property that determines the amount of width the popup can be before overflowing. Useful for positioning child elements that need to overflow. This property is only available when using auto-size.
—
--hide-duration
The hide duration to use when applying built-in animation classes.
var(--cs-transition-fast)
--popup-border-width
The width of any custom border applied to the popup. This is used to reposition the arrow to overlap to the inside edge of the popup border.
—
--show-duration
The show duration to use when applying built-in animation classes.
The arrow's container. Avoid setting top|bottom|left|right properties, as these values are assigned dynamically as the popup moves. This is most useful for applying a background color to match the popup, and maybe a border or box shadow.
::part(arrow)
hover-bridge
The hover bridge element. Only available when the hover-bridge option is enabled.
::part(hover-bridge)
popup
The popup's container. Useful for setting a background color, box shadow, etc.