Popovers

Stylised native HTML [popover] modals with opening transition animation styles.

Examples (anchor)

Please note. The popovers will inherit whatever normalize/reset styles are present requiring only minor adjustments to suit base font property values, but as demonstrated they've been designed to work seamlessly with the StyleMods content styles.

The default button styles used in the examples below are also not included with the dialogs but are provided with the forms and buttons and/or the buttons module if required.

Default (anchor)

Popovers have light-dismiss behaviour by default so the closing button is optional.

The quick brown fox jumps over the lazy dog.

The quick brown fox jumps over the lazy dog.

Examples HTML
<button popovertarget="popover">Popover</button>
<div id="popover" popover>
  <p>The quick brown fox jumps over the lazy dog.</p>
</div>

<button popovertarget="pop-close">With close button</button>
<div id="pop-close" popover>
  <p>The quick brown fox jumps over the lazy dog.</p>
<button popovertarget="pop-close" popovertargetaction="hide">Close</button>
</div>

Transition (anchor)

The optional transition animation style is compiled with @media (prefers-reduced-motion: no-preference) to respect a users preferences. If included the default .25s transition timing can be customized inline using the CSS variable --popover-transition.

The quick brown fox jumps over the lazy dog.

The quick brown fox jumps over the lazy dog.

The quick brown fox jumps over the lazy dog.

Examples HTML

The popover animation is enabled so the 'No transition' example uses the variable to remove the default .25s transition included to demonstrate default non-animated popovers.

<button popovertarget="popover-no-transition">No transition</button>
<div id="popover-no-transition" popover style="--popover-transition: 0s;">
  <p>The quick brown fox jumps over the lazy dog.</p>
</div>

<button popovertarget="popover-transition">Enabled (default .25s)</button>
<div id="popover-transition" popover>
  <p>The quick brown fox jumps over the lazy dog.</p>
</div>

<button popovertarget="popover-custom-transition">Enabled (custom 1.5s)</button>
<div id="popover-custom-transition" popover style="--popover-transition: 1.5s;">
  <p>The quick brown fox jumps over the lazy dog.</p>
</div>

Fixed light and dark (anchor)

The quick brown fox jumps over the lazy dog.

The quick brown fox jumps over the lazy dog.

Example HTML
<button popovertarget="popover-light" class="btn-light">Light popover</button>
<div class="popover-light" id="popover-light" popover>
  <p>The quick brown fox jumps over the lazy dog.</p>
</div>

<button popovertarget="popover-dark" class="btn-dark">Dark popover</button>
<div class="popover-dark" id="popover-dark" popover>
  <p>The quick brown fox jumps over the lazy dog.</p>
</div>

Using the module (anchor)

Load StyleMods as demonstrated (change file path as required) then include the Sass mixin anywhere below.

Default with transition styles:

custom.scss
@use "stylemods/scss" as *;
@include popovers-css;

With no transition:

custom.scss
@use "stylemods/scss" as *;
@include popovers-no-transition-css;

Or individually:

custom.scss
@use "stylemods/scss" as *;
@include popover-styles-css;
@include popover-transition-css;

See the using modules page for more information including how to compile the modules in cascade layers and include and reuse them in multiple source files.

Using the framework (anchor)

Using the recommended custom setup enable the styles in an overrides document:

overrides.scss
@use "stylemods/scss/configuration" as *;
$enable-popovers:             true;
$enable-popovers-transition:  true;

Include the overrides and the framework styles with your custom document for compiling the framework:

custom.scss
@use "overrides";
@use "stylemods/scss/stylemods";

See using the framework for more information including how the styles can also be compiled within cascade layers.

Source code (anchor)

The modules and the framework follow the same methods for customizing the default values and using the CSS variables to create custom theme documents. See the customizing page for more information.

popovers.scss
// ---------------------------------------------------------- 
// Popovers
// ----------------------------------------------------------
$popover-text-color:        var(--popover-text, CanvasText) !default;
$popover-margin-top:        var(--popover-mt, 1rem) !default;
$popover-padding-block:     var(--popover-py, 0.75rem) !default;
$popover-padding-inline:    var(--popover-px, 1rem) !default;
$popover-border-color:      var(--popover-bd-color, color-mix(in srgb, CanvasText 15%, Canvas)) !default;
$popover-radius:            var(--popover-radius, 0.125em) !default;
$popover-background-color:  var(--popover-bg, Canvas) !default; 
$popover-transition:        var(--popover-transition, 0.25s) !default;
$popover-backdrop:          color-mix(in srgb, black 50%, transparent) !default;

@mixin popover-styles-css {

:where([popover]) {
  color: $popover-text-color;
  block-size: fit-content;
  max-block-size: calc(100vh - 2rem);
  inline-size: calc((100% - 6px) - 2em);
  max-inline-size: var(--popover-width, fit-content);
  margin: auto;
  margin-block-start: $popover-margin-top;
  padding-block: $popover-padding-block;
  padding-inline: $popover-padding-inline;
  border: 1px solid $popover-border-color;
  border-radius: $popover-radius;
  background-color: $popover-background-color; 
  overflow: auto;
  overscroll-behavior: contain; 
}

:where([popover]) *:last-child {
  margin-block-end: 0;
}

[popover]::backdrop {
  background-color: $popover-backdrop;
}

.popover-light {
  color-scheme: light;
}

.popover-dark {
  color-scheme: dark;
}

}

@mixin popover-transition-css {
  @media screen and (prefers-reduced-motion: no-preference) {
    [popover] {
      transition: $popover-transition;
      opacity: 1; 
    }
  }
  
  @starting-style {
    [popover] {
      opacity: 0;    
    }
  }
}

@mixin popovers-css {
  @include popover-styles-css;
  @include popover-transition-css;
}

@mixin popovers-no-transition-css {
  @include popover-styles-css;
}