Popovers
Create contextual overlays with rich content using Chassis CSS's popover system, featuring intelligent positioning and accessible design patterns.
Introduction
Popovers in Chassis CSS provide rich contextual overlays that display detailed information, forms, or interactive content without leaving the current page context. Built on modern web standards with intelligent positioning via Popper.js, popovers integrate seamlessly with your design system while maintaining accessibility and performance excellence.
Unlike simple tooltips, popovers support complex content including headers, body sections, and interactive elements. The positioning system automatically handles viewport constraints and collision detection, while the component architecture ensures consistent behavior across devices and screen sizes.
Chassis CSS popovers operate on explicit initialization for optimal performance:
- Manual initialization prevents overhead on unused elements
- Intelligent positioning powered by Popper.js with automatic placement adjustment
- Comprehensive event integration for application logic and user interaction
- Accessibility through proper ARIA attributes and keyboard navigation support
- CSS custom properties enable seamless theming and brand consistency
Implementation requirements
Before implementing popovers, ensure these dependencies and considerations are addressed:
- Include popper.min.js before
chassis.js, or usechassis.bundle.min.js - Popovers with empty
titleandcontentvalues will not display - Hidden elements cannot trigger popovers
- Use
container: 'body'for complex component layouts (input groups, button groups) - Apply popovers only to keyboard-focusable, interactive elements for accessibility compliance
- Hide popovers before removing their trigger elements from the DOM
- Specify explicit containers for popovers within modal dialogs to ensure proper focus management
By default, this component uses the built-in content sanitizer, which strips out any HTML elements that are not explicitly allowed. See the sanitizer section in our JavaScript documentation for more details.
Chassis CSS respects user accessibility preferences by automatically disabling animations when the prefers-reduced-motion media query is detected. See the reduced motion guidelines in our accessibility documentation for implementation details.
Examples
Explore practical popover implementations demonstrating initialization patterns, positioning strategies, and customization techniques. These examples showcase Chassis CSS's flexible popover system across various use cases.
Initialization patterns
Initialize popovers via JavaScript before they can be used. Select elements by their data-cx-toggle attribute for batch initialization:
const popoverTriggerList = document.querySelectorAll('[data-cx-toggle="popover"]')
const popoverList = [...popoverTriggerList].map(popoverTriggerEl => new chassis.Popover(popoverTriggerEl))
Interactive demonstration
This example demonstrates a fully functional popover with title and body content. Titles are set via data-cx-title and body content via data-cx-content.
Feel free to use either title or data-cx-title in your HTML. When title is used, Popper will replace it automatically with data-cx-title when the element is rendered.
<button type="button" class="button large danger" data-cx-toggle="popover" data-cx-title="Popover title" data-cx-content="And here's some amazing content. It's very engaging. Right?">Click to toggle popover</button> Positioning system
Chassis CSS supports four directional placements with automatic RTL adaptation. Set data-cx-placement to control popover direction with intelligent fallback positioning.
<button type="button" class="button secondary" data-cx-container="body" data-cx-toggle="popover" data-cx-placement="top" data-cx-content="Top popover">
Popover on top
</button>
<button type="button" class="button secondary" data-cx-container="body" data-cx-toggle="popover" data-cx-placement="right" data-cx-content="Right popover">
Popover on right
</button>
<button type="button" class="button secondary" data-cx-container="body" data-cx-toggle="popover" data-cx-placement="bottom" data-cx-content="Bottom popover">
Popover on bottom
</button>
<button type="button" class="button secondary" data-cx-container="body" data-cx-toggle="popover" data-cx-placement="left" data-cx-content="Left popover">
Popover on left
</button> Container control
When parent elements interfere with popover positioning, specify a custom container to control where the popover appears in the DOM:
const popover = new chassis.Popover('.example-popover', {
container: 'body'
})
For popovers within modal dialogs, set the container to the modal to ensure proper focus management and interaction:
const popover = new chassis.Popover('.example-popover', {
container: '.modal-content'
})
Custom appearance
Customize popover appearance using CSS custom properties. Apply custom classes with data-cx-custom-class="custom-popover" to scope styling and override CSS variables for brand-specific theming.
.custom-popover {
--cx-popover-max-width: 200px;
--cx-popover-border-color: var(--cxd-subtle-bg);
--cx-popover-header-bg: var(--cxd-subtle-bg);
--cx-popover-header-color: var(--cx-white);
--cx-popover-body-padding-x: 1rem;
--cx-popover-body-padding-y: .5rem;
}
<button type="button" class="button secondary"
data-cx-toggle="popover" data-cx-placement="right"
data-cx-custom-class="custom-popover"
data-cx-title="Custom popover"
data-cx-content="This popover is themed via CSS variables.">
Custom popover
</button> Focus-based dismissal
Use the focus trigger to dismiss popovers when users interact with other elements, ideal for contextual help and secondary information.
Focus-based dismissal requires specific HTML for proper cross-browser behavior. Use <a> elements with tabindex attributes instead of <button> elements to ensure consistent keyboard and mouse interaction across all browsers and platforms.
<a tabindex="0" class="button large danger" role="button" data-cx-toggle="popover" data-cx-trigger="focus" data-cx-title="Dismissible popover" data-cx-content="And here's some amazing content. It's very engaging. Right?">Dismissible popover</a> const popover = new chassis.Popover('.popover-dismiss', {
trigger: 'focus'
})
Disabled elements
Elements with the disabled attribute cannot trigger popovers directly. Use a wrapper element with tabindex="0" to enable popover functionality while maintaining accessibility:
<span class="d-inline-block" tabindex="0" data-cx-toggle="popover" data-cx-trigger="hover focus" data-cx-content="Disabled popover">
<button class="button primary" type="button" disabled>Disabled button</button>
</span> For disabled elements, use data-cx-trigger="hover focus" to provide immediate visual feedback since users may not expect to click disabled elements.
CSS
Customize popover appearance and behavior through Chassis CSS's comprehensive styling system, featuring CSS custom properties and Sass variables for design system integration.
This component can be customized using CSS variables, allowing for styles to be modified dynamically on the page. These CSS variables are part of Chassis CSS's design token system, giving design teams control over component appearance. See the design tokens page for more details.
Custom properties
Chassis CSS popovers utilize CSS custom properties for runtime customization and design token integration. Local CSS variables on .popover enable dynamic theming while maintaining backward compatibility.
These CSS variables control the component's appearance and can be modified dynamically on the page. Components use cascading variables, allowing seamless variations in size, color, and style without redundant style declarations through component inheritance and the context class system.
--#{$prefix}zindex: var(--#{$prefix}popover-zindex, #{$zindex-popover});
--#{$prefix}max-width: var(--#{$prefix}popover-max-width, #{$popover-max-width});
@include map-font($popover-font, popover);
--#{$prefix}fg-color: var(--#{$prefix}popover-fg-color, #{$popover-fg-color});
--#{$prefix}bg-color: var(--#{$prefix}popover-bg-color, #{$popover-bg-color});
--#{$prefix}border-width: var(--#{$prefix}popover-border-width, #{$popover-border-width});
--#{$prefix}border-color: var(--#{$prefix}popover-border-color, #{$popover-border-color});
--#{$prefix}border-radius: var(--#{$prefix}popover-border-radius, #{$popover-border-radius});
--#{$prefix}inner-border-radius: var(--#{$prefix}popover-inner-border-radius, #{$popover-inner-border-radius});
--#{$prefix}box-shadow: var(--#{$prefix}popover-box-shadow, #{$popover-box-shadow});
--#{$prefix}header-padding-x: var(--#{$prefix}popover-padding-x, #{$popover-header-padding-x});
--#{$prefix}header-padding-y: var(--#{$prefix}popover-padding-y, #{$popover-header-padding-y});
@include map-font($popover-header-font, popover-header, header);
--#{$prefix}header-fg-color: var(--#{$prefix}popover-header-fg-color, #{$popover-header-fg-color});
--#{$prefix}header-bg-color: var(--#{$prefix}popover-header-bg, #{$popover-header-bg-color});
--#{$prefix}body-padding-x: var(--#{$prefix}popover-body-padding-x, #{$popover-body-padding-x});
--#{$prefix}body-padding-y: var(--#{$prefix}popover-body-padding-y, #{$popover-body-padding-y});
--#{$prefix}body-color: var(--#{$prefix}popover-body-color, #{$popover-body-color});
--#{$prefix}arrow-width: var(--#{$prefix}popover-arrow-width, #{$popover-arrow-width});
--#{$prefix}arrow-height: var(--#{$prefix}popover-arrow-height, #{$popover-arrow-height});
--#{$prefix}arrow-border: var(--#{$prefix}popover-arrow-border, var(--#{$prefix}border-color));
Sass variables
Design system integration through Sass variables provides comprehensive popover customization for build-time theming and brand alignment.
These Sass variables are also exposed as CSS custom properties using the --cx-
prefix. A Sass variable $variable-name becomes available as --cx-variable-name in CSS, allowing for styles to be modified dynamically on the
page. See the
context components
page for more details.
$popover-font: $cx-font-text-medium-normal;
$popover-fg-color: var(--#{$prefix}fg-main);
$popover-bg-color: var(--#{$prefix}bg-main);
$popover-max-width: 17.25rem; //276px;
$popover-border-width: var(--#{$prefix}border-width-medium);
$popover-border-color: var(--#{$prefix}border-subtle);
$popover-border-radius: var(--#{$prefix}border-radius-medium);
$popover-inner-border-radius: #{calc(#{$popover-border-radius} - #{$popover-border-width})};
$popover-box-shadow: var(--#{$prefix}box-shadow);
$popover-header-font: $cx-font-text-medium-strong;
$popover-header-fg-color: var(--#{$prefix}cx-main);
$popover-header-bg-color: var(--#{$prefix}bg-even);
$popover-header-padding-y: var(--#{$prefix}space-xsmall);
$popover-header-padding-x: var(--#{$prefix}space-medium);
$popover-body-color: var(--#{$prefix}fg-color);
$popover-body-padding-y: var(--#{$prefix}space-small);
$popover-body-padding-x: var(--#{$prefix}space-medium);
$popover-arrow-width: 1rem;
$popover-arrow-height: .5rem;
JavaScript API
Control popover behavior programmatically with Chassis CSS's comprehensive JavaScript interface, featuring dynamic content generation, intelligent positioning, and accessibility features.
Initialization
The popover plugin generates content and markup on demand, placing popovers relative to their trigger elements for optimal performance and accessibility. Initialize popovers via JavaScript:
const exampleEl = document.getElementById('example')
const popover = new chassis.Popover(exampleEl, options)
Maintain accessibility when using popovers by applying them only to traditionally keyboard-focusable and interactive elements (links, form inputs, buttons). While other elements can receive focus with tabindex="0", this creates confusing navigation patterns for keyboard users and may not be announced properly by assistive technologies.
Avoid excessive content in popovers with the html option. Popover content is tied to trigger elements via aria-describedby, causing all content to be announced as one continuous stream to screen reader users.
Popovers don't manage keyboard focus order automatically. Their DOM placement can be unpredictable, so exercise caution when including interactive elements like forms or links, as this may create illogical focus sequences or unreachable content for keyboard users.
Data attributes
Individual popover options can be specified through data attributes for declarative configuration without JavaScript initialization.
Popovers require minimal markup while generating semantic HTML with proper ARIA attributes for optimal accessibility and screen reader support. The required markup for a popover is only a data attribute and content attributes on the HTML element:
<!-- HTML to write -->
<button type="button" data-cx-toggle="popover" data-cx-title="Popover title" data-cx-content="Amazing content here">Click me</button>
<!-- Generated markup by the plugin -->
<div class="popover cx-popover-auto" role="tooltip">
<div class="popover-arrow"></div>
<h3 class="popover-header">Popover title</h3>
<div class="popover-body">Amazing content here</div>
</div>
For security reasons, the sanitize, sanitizeFn, and allowList options cannot be supplied using data attributes.
Configuration options
Customize popover behavior through JavaScript options or HTML data attributes for flexible integration with your application logic.
Chassis components offer flexible configuration through both data attributes and JavaScript. To use data attributes, prefix any option with data-cx- followed by the option name in kebab-case format (using hyphens instead of camelCase). For example, write data-cx-custom-class="my-class" rather than data-cx-customClass="my-class".
For more complex configurations, Chassis provides the data-cx-config attribute which accepts a JSON string of multiple settings. This approach simplifies markup when configuring several options at once. If the same option appears in both data-cx-config and as a separate data attribute (like data-cx-title), the individual attribute takes precedence. You can also use JSON values in individual attributes, such as data-cx-delay='{"show":100,"hide":200}' for more granular control.
When initializing components, Chassis merges configurations from multiple sources in this priority order: default settings, data-cx-config values, individual data-cx-* attributes, and finally any JavaScript object options. Values defined later in this sequence override earlier ones.
| Name | Type | Default | Description |
|---|---|---|---|
allowList | object | Default value | Object containing allowed attributes and tags for HTML sanitization. |
animation | boolean | true | Apply CSS fade transition to the popover. |
boundary | string, element | 'clippingParents' | Overflow constraint boundary of the popover (applies only to Popper's preventOverflow modifier). Accepts 'clippingParents' or an HTMLElement reference. For more information refer to Popper's detectOverflow docs. |
container | string, element, false | false | Appends the popover to a specific element. Example: container: 'body'. This option prevents the popover from floating away from the triggering element during window resize. |
content | string, element, function | '' | The popover's text content. If a function is given, it will be called with its this reference set to the element that the popover is attached to. |
customClass | string, function | '' | Add classes to the popover when shown. Note that these classes are added in addition to template classes. To add multiple classes, separate them with spaces: 'class-1 class-2'. Functions should return a single string containing additional class names. |
delay | number, object | 0 | Delay showing and hiding the popover (ms)—doesn't apply to manual trigger type. If a number is supplied, delay is applied to both hide/show. Object structure is: delay: { "show": 500, "hide": 100 }. |
fallbackPlacements | string, array | ['top', 'right', 'bottom', 'left'] | Define fallback placements by providing a list of placements in array (in order of preference). For more information refer to Popper's behavior docs. |
html | boolean | false | Allow HTML in the popover. If true, HTML tags in the popover's title will be rendered. If false, innerText property will be used to insert content into the DOM. Use text if you're concerned about XSS attacks. |
offset | number, string, function | [0, 8] | Offset of the popover relative to its target. You can pass a string in data attributes with comma separated values like: data-cx-offset="10,20". When a function is used to determine the offset, it is called with an object containing the popper placement, the reference, and popper rects as its first argument. The triggering element DOM node is passed as the second argument. The function must return an array with two numbers: skidding, distance. For more information refer to Popper's offset docs. |
placement | string, function | 'right' | How to position the popover: auto, top, bottom, left, right. When auto is specified, it will dynamically reorient the popover. When a function is used to determine the placement, it is called with the popover DOM node as its first argument and the triggering element DOM node as its second. The this context is set to the popover instance. |
popperConfig | null, object, function | null | To change Chassis CSS's default Popper config, see Popper's configuration. When a function is used to create the Popper configuration, it's called with an object that contains the Chassis CSS's default Popper configuration. It helps you use and merge the default with your own configuration. The function must return a configuration object for Popper. |
sanitize | boolean | true | Enable or disable the sanitization. If activated 'template', 'content' and 'title' options will be sanitized. |
sanitizeFn | null, function | null | Here you can supply your own sanitize function. This can be useful if you prefer to use a dedicated library to perform sanitization. |
selector | string, false | false | If a selector is provided, popover objects will be delegated to the specified targets. In practice, this is used to also apply popovers to dynamically added DOM elements. See this issue and an informative example. Note: title attribute must not be used as a selector. |
template | string | '<div class="popover" role="tooltip"><div class="popover-arrow"></div><h3 class="popover-header"></h3><div class="popover-body"></div></div>' | Base HTML to use when creating the popover. The popover's title will be injected into the .popover-header. The popover's content will be injected into the .popover-body. .popover-arrow will become the popover's arrow. The outermost wrapper element should have the .popover class and role="tooltip". |
title | string, element, function | '' | The popover title. If a function is given, it will be called with its this reference set to the element that the popover is attached to. |
trigger | string | 'click' | How popover is triggered: click, hover, focus, manual. You may pass multiple triggers; separate them with a space. 'manual' indicates that the popover will be triggered programmatically via the .popover('show'), .popover('hide') and .popover('toggle') methods; this value cannot be combined with any other trigger. 'hover' on its own will result in popovers that cannot be triggered via the keyboard, and should only be used if alternative methods for conveying the same information for keyboard users is present. |
Popper configuration
Advanced Popper.js configuration can be customized through a function that receives the default Chassis CSS configuration and returns a modified configuration object.
const popover = new chassis.Popover(element, {
popperConfig(defaultCxPopperConfig) {
// const newPopperConfig = {...}
// use defaultCxPopperConfig if needed...
// return newPopperConfig
}
})
Methods
Chassis CSS provides a comprehensive set of methods for programmatic popover control, enabling dynamic content updates and lifecycle management.
All Chassis CSS component methods are asynchronous and initiate CSS transitions. Methods return immediately when the transition begins, not when it completes. Calling methods on components that are already transitioning will be ignored to prevent conflicts. Learn more about Chassis JavaScript patterns.
| Method | Description |
|---|---|
disable | Removes the ability for an element's popover to be shown. The popover will only be able to be shown if it is re-enabled. |
dispose | Hides and destroys an element's popover (Removes stored data on the DOM element). Popovers that use delegation (which are created using the selector option) cannot be individually destroyed on descendant trigger elements. |
enable | Gives an element's popover the ability to be shown. Popovers are enabled by default. |
getInstance | Static method which allows you to get the popover instance associated with a DOM element. |
getOrCreateInstance | Static method which allows you to get the popover instance associated with a DOM element, or create a new one in case it wasn't initialized. |
hide | Hides an element's popover. Returns to the caller before the popover has actually been hidden (i.e. before the hidden.cx.popover event occurs). This is considered a "manual" triggering of the popover. |
setContent | Gives a way to change the popover's content after its initialization. |
show | Reveals an element's popover. Returns to the caller before the popover has actually been shown (i.e. before the shown.cx.popover event occurs). This is considered a "manual" triggering of the popover. Popovers whose title and content are both zero-length are never displayed. |
toggle | Toggles an element's popover. Returns to the caller before the popover has actually been shown or hidden (i.e. before the shown.cx.popover or hidden.cx.popover event occurs). This is considered a "manual" triggering of the popover. |
toggleEnabled | Toggles the ability for an element's popover to be shown or hidden. |
update | Updates the position of an element's popover. |
const popover = chassis.Popover.getInstance('#example') // Returns a Chassis CSS popover instance
// setContent example
popover.setContent({
'.popover-header': 'another title',
'.popover-body': 'another content'
})
The setContent method accepts an object argument, where each property-key is a valid string selector within the popover template, and each related property-value can be string | element | function | null
Events
Listen to popover lifecycle events to integrate with your application logic and create responsive user interactions.
| Event | Description |
|---|---|
hide.cx.popover | This event is fired immediately when the hide instance method has been called. |
hidden.cx.popover | This event is fired when the popover has finished being hidden from the user (will wait for CSS transitions to complete). |
inserted.cx.popover | This event is fired after the show.cx.popover event when the popover template has been added to the DOM. |
show.cx.popover | This event fires immediately when the show instance method is called. |
shown.cx.popover | This event is fired when the popover has been made visible to the user (will wait for CSS transitions to complete). |
const myPopoverTrigger = document.getElementById('myPopover')
const popover = chassis.Popover.getOrCreateInstance(myPopoverTrigger)
myPopoverTrigger.addEventListener('hidden.cx.popover', () => {
// do something...
})
popover.hide()