Popovers

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

Examples (anchor)

Base values for default font properties are expected via reset styles so are not included, the typography provides these if required but the styles should work with any reset/normalize document used.

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)

To use the module load the StyleMods scss directory as follows (changing the path to suit the source files location as required) then include the Sass mixin(s) 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;

Source code (anchor)

See customizing for information about using the Sass and CSS variables in the source code to customize the styles, and Sass functionality (on the using StyleMods page) for other ways to use the variables to create custom styles.

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-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;
}