包详细信息

@oddbird/popover-polyfill

oddbird264.2kBSD-3-Clause0.6.0

Popover Attribute Polyfill

css, polyfill, popover

自述文件

Popover Attribute Polyfill

Build Status npm version Netlify Status

This polyfills the HTML popover attribute and showPopover/hidePopover/togglePopover methods onto HTMLElement, as well as the popovertarget and popovertargetaction attributes on Button elements.

Polyfill Installation

Download a copy

The simplest, recommended way to install the polyfill is to copy it into your project.

Download popover.js (or popover.min.js) from unpkg.com and add it to the appropriate directory in your project. Then, include it where necessary with a <script> tag:

<script src="/path/to/popover.min.js" type="module"></script>

Or without JavaScript modules:

<script src="/path/to/popover.iife.min.js"></script>

Note that the JS will inject CSS styles into your document (or ShadowRoot).

With npm

For more advanced configuration, you can install with npm:

npm install @oddbird/popover-polyfill

After installing, you’ll need to use appropriate tooling to use node_modules/@oddbird/popover-polyfill/dist/popover.js.

For most tooling such as Vite, Webpack, and Parcel, that will look like this:

import '@oddbird/popover-polyfill';

If you want to manually apply the polyfill, you can instead import the isSupported and apply functions directly from node_modules/@oddbird/popover-polyfill/dist/popover-fn.js file.

With most tooling:

import { apply, isSupported } from '@oddbird/popover-polyfill/fn';

Or in CommonJS environments:

const { apply, isSupported } = require('@oddbird/popover-polyfill/fn');

An isPolyfilled function is also available, to detect if the Popover methods have been polyfilled:

import { isPolyfilled } from '@oddbird/popover-polyfill/fn';

Via CDN

For prototyping or testing, you can use the npm package via a Content Delivery Network. Avoid using JavaScript CDNs in production, for many good reasons such as performance and robustness.

<script
  src="https://cdn.jsdelivr.net/npm/@oddbird/popover-polyfill@latest"
  crossorigin="anonymous"
  defer
></script>

Usage

After installation the polyfill will automatically add the correct methods and attributes to the HTMLElement class.

Caveats

This polyfill is not a perfect replacement for the native behavior; there are some caveats which will need accommodations:

  • A native popover has a :popover-open pseudo selector when in the open state. Pseudo selectors cannot be polyfilled within CSS, and so instead the polyfill will add the .\:popover-open CSS class to any open popover. In other words a popover in the open state will have class=":popover-open". In CSS the : character must be escaped with a backslash.

    • The :popover-open selector within JavaScript methods has been polyfilled, so both .querySelector(':popover-open') and .querySelector('.\:popover-open') will work to select the same element. matches and closest have also been patched, so .matches(':popover-open') will work the same as .matches('.\:popover-open').

    • Using native :popover-open in CSS that does not support native popover results in an invalid selector, and so the entire declaration is thrown away. This is important because if you intend to style a popover using .\:popover-open it will need to be a separate declaration. For example, [popover]:popover-open, [popover].\:popover-open will not work.

  • Native popover elements use the :top-layer pseudo element which gets placed above all other elements on the page, regardless of overflow or z-index. This is not possible to polyfill, and so this library simply sets a really high z-index. This means if a popover is within an element that has overflow: or position: CSS, then there will be visual differences between the polyfill and the native behavior.

  • Native invokers (that is, buttons or inputs using the popovertarget attribute) on popover=auto will render in the accessibility tree as elements with expanded. The only way to do this in the polyfill is setting the aria-expanded attribute on those elements. This may impact mutation observers or frameworks which do DOM diffing, or it may interfere with other code which sets aria-expanded on elements.

  • The polyfill uses adoptedStyleSheets and new CSSStyleSheet() to inject CSS onto the page (and each Shadow DOM). If it can't use that it'll generate a <style> tag instead. This means you may see a <style> tag you didn't put there, and this may impact mutation observers or frameworks.

    • For browsers which don't support new CSSStyleSheet(), if you are building a ShadowRoot by setting .innerHTML, you'll remove the StyleSheet. Call injectStyles(myShadow) to add the styles back in:

      let supportsCSSStyleSheet = false;
      try {
        new CSSStyleSheet();
        supportsCSSStyleSheet = true;
      } catch {}
      const myShadow = myHost.attachShadow({ mode: 'open' });
      myShadow.innerHTML = `...`;
      if (!supportsCSSStyleSheet) {
        injectStyles(myShadow);
      }
      
    • Similarly, if you're using Declarative ShadowDOM or otherwise creating a shadow root without calling attachShadow/attachInternals, then the polyfill won't inject the styles (because it can't reference the shadowRoot). You'll need to manually inject the styles yourself with injectStyles(myShadow).

    • As a stylesheet is injected into the main document, if your host element is a popover, styling with :host gets tricky beause :host styles always take lower precedence than the main document styles. This is a limitation of CSS and there's not a reasonable workaround. The best workaround for now is to add !important to conflicting properties in your :host rule. See #147 for more.

    • Given that the CSS is injected using JavaScript, you may find that you temporarily see popovers as open while the JS is loading. To work around this, you can add the following CSS to your project:

      @supports not selector(:popover-open) {
        [popover]:not(.\:popover-open) {
          display: none;
        }
      }
      
  • When supported, the polyfill creates a cascade layer named popover-polyfill. If your styles are not in layers then this should have no impact. If your styles do use layers, you'll need to ensure the polyfill layer is declared first. (e.g. @layer popover-polyfill, other, layers;)

  • While togglePopover() supports both the force: boolean and {force: boolean, source: HTMLElement} syntax, both showPopover() and togglePopover() disregard the source parameter.

  • The polyfill will not work in browsers with partial popover support enabled, and will also not attempt to make experimental support match the final spec.

Contributing

Visit our contribution guidelines.

Sponsor OddBird's OSS Work

At OddBird, we love contributing to the languages & tools developers rely on. We're currently working on polyfills for new Popover & Anchor Positioning functionality, as well as CSS specifications for functions, mixins, and responsive typography. Help us keep this work sustainable and centered on your needs as a developer! We display sponsor logos and avatars on our website.

Sponsor OddBird's OSS Work

更新日志

Popover Attribute Polyfill Changelog

0.5.2: 2024-11-20

  • 📝 DOCS: Add favicon, sponsor text, and new demo page URL -- #230
  • 🏠 INTERNAL: Upgrade dependencies

0.5.1: 2024-11-11

  • 🐛 BUGFIX: Fix setting el.popover = null -- #229
  • 🏠 INTERNAL: Upgrade dependencies

0.5.0: 2024-10-08

  • 💥 BREAKING: main entry point (used by require("@oddbird/popover-polyfill")) now points to the default build, which automatically applies the polyfill -- #223
  • 🚀 NEW: Add CommonJS build -- #223
  • 🏠 INTERNAL: Upgrade dependencies

0.4.4: 2024-07-22

  • 🐛 BUGFIX: Add support for slotting elements into popovers -- #215
  • 📝 DOCS: Expand and style the demo page -- #187
  • 🏠 INTERNAL: Add issue templates, Code of Conduct, and Security policies -- #206 and #207
  • 🏠 INTERNAL: Upgrade dependencies

0.4.3: 2024-04-26

  • 🐛 BUGFIX: Check for window before applying polyfill -- #201
  • 🐛 BUGFIX: Expose injectStyles function -- #200

0.4.2: 2024-04-25

  • 🐛 BUGFIX: Fix support for dialog popovers -- #199
  • 🏠 INTERNAL: Upgrade dependencies

0.4.1: 2024-03-27

  • 🚀 NEW: Add isPolyfilled method to check if polyfill is applied -- #193
  • 🐛 BUGFIX: Support environments (e.g. SSR) that don't have window -- #194
  • 🐛 BUGFIX: Support nested popovers inside shadow DOM -- #190
  • 📝 DOCS: Add note about FOUC workarounds -- #182
  • 🏠 INTERNAL: Switch to Dependabot for dependency updates
  • 🏠 INTERNAL: Upgrade dependencies

0.4.0: 2024-02-08

  • 🚀 NEW: Add support for CSS cascade layers -- #178
  • 🐛 BUGFIX: Ensure click events correctly cross out of shadow DOM -- #177
  • 🏠 INTERNAL: Upgrade dependencies

0.3.8: 2024-01-16

  • 🐛 BUGFIX: Allow synthetic click events to target popovers -- #170
  • 🏠 INTERNAL: Upgrade dependencies

0.3.7: 2023-12-07

  • 🐛 BUGFIX: Fix crash in Firefox with shadowed popovers -- #160
  • 📝 DOCS: Add better ESM use examples -- #161
  • 🏠 INTERNAL: Upgrade dependencies

0.3.6: 2023-12-01

  • 🐛 BUGFIX: Allow Esc to be preventable -- #158
  • 📝 DOCS: Add caveat about :host styles -- #157

0.3.5: 2023-11-29

  • 🐛 BUGFIX: Fix incorrect isFocusable result for ShadowRoot -- #155
  • 🚀 NEW: Prepend default styles to lower specificity in the cascade -- #156

0.3.4: 2023-11-28

  • 🚀 NEW: Wrap styles in :where() to lower specificity in the cascade -- #153

0.3.3: 2023-11-28

  • 🐛 BUGFIX: Check slotted content for autofocus delegate -- #150
  • 🏠 INTERNAL: Upgrade dependencies

0.3.2: 2023-11-13

  • 🚀 NEW: Update TypeScript definitions to be compatible with v5 -- #146
  • 🏠 INTERNAL: Upgrade dependencies

0.3.1: 2023-10-28

  • 🐛 BUGFIX: Assign to adoptedStyleSheets rather than using push -- #141
  • 🏠 INTERNAL: Upgrade dependencies

0.3.0: 2023-10-19

  • 💥 BREAKING: Automatically inject styles from JS -- #137
  • 🏠 INTERNAL: Upgrade dependencies

0.2.3: 2023-09-18

  • 💥 BREAKING: Drop :open styles -- #128
  • 📝 DOCS: Fix link in README -- #108
  • 🏠 INTERNAL: Upgrade dependencies

0.2.2: 2023-06-06

  • 🚀 NEW: Add support for older browsers, e.g. Firefox 91, Chrome ~80 -- #103
  • 🐛 BUGFIX: Do not error if querySelector APIs are invoked with null or undefined -- #105
  • 🏠 INTERNAL: Upgrade dependencies

0.2.1: 2023-05-17

  • 🐛 BUGFIX: Fix infinite loop for removed popovers -- #99
  • 🐛 BUGFIX: Expose CSS entry point as @oddbird/popover-polyfill/css and @oddbird/popover-polyfill/dist/popover.css
  • 🏠 INTERNAL: Upgrade dependencies

0.2.0: 2023-04-04

  • 🚀 NEW: Support for :popover-open pseudo selector, including a polyfill for JavaScript API methods (querySelector, querySelectorAll, matches, and closest) -- #84
  • 🐛 BUGFIX: Return null for disconnected elements -- #90
  • 🏠 INTERNAL: Upgrade dependencies

0.1.1: 2023-03-24

0.1.0: 2023-03-24

  • 💥 BREAKING: Drop support for popovertoggletarget, popovershowtarget and popoverhidetarget. These are now popovertarget and popovertargetaction.
  • 💥 BREAKING: Invalid popover attribute values now default to manual, meaning popover=invalid is, in fact, a valid popover.
  • The old BeforeToggleEvent has been replaced with ToggleEvent which has a type of 'beforetoggle' or 'toggle', and the old currentState is now oldState. newState remains the same.
  • 🏠 INTERNAL: Upgrade dependencies

0.0.11: 2023-03-15

  • 🚀 NEW: Add support for Escape to dismiss auto popovers -- #82
  • 🚀 NEW: Showing an auto popover closes other auto popovers -- #83
  • 🚀 NEW: Add support for focus restoration on popover close -- #81
  • 🏠 INTERNAL: Upgrade dependencies

0.0.10: 2023-03-03

  • 🚀 NEW: Add support for aria-expanded on invokers -- #77
  • 🏠 INTERNAL: Upgrade dependencies

0.0.9: 2023-02-03

  • 🚀 NEW: Add support for JavaScript (IDL) reflections (e.g. popoverToggleTargetElement) -- #70
  • 🏠 INTERNAL: Upgrade dependencies

0.0.8: 2023-01-26

0.0.7: 2023-01-19

  • 🐛 BUGFIX: Fix display issue with native popups -- #64
  • 🐛 BUGFIX: Remove stopPropagation to allow handlers -- #63
  • 🏠 INTERNAL: Upgrade dependencies

0.0.6: 2023-01-17

0.0.5: 2023-01-14

  • 🚀 NEW: Support popovers in shadow roots -- #49
  • 🚀 NEW: Use system text and background colors -- #55, #56
  • 📝 DOCS: Include CSS in CDN section -- #52
  • 🏠 INTERNAL: Upgrade dependencies

0.0.4: 2022-12-16

  • 🐛 BUGFIX: Fix event handling of buttons -- #40
  • 🏠 INTERNAL: Upgrade dependencies

0.0.3: 2022-11-15

  • 💥 BREAKING: Drop support for hint and defaultopen
  • 📝 DOCS: Improve documentation
  • 🏠 INTERNAL: Rename popup to popover
  • 🏠 INTERNAL: Upgrade dependencies

0.0.2: 2022-10-28

  • 📝 DOCS: Fix links in documentation

0.0.1: 2022-10-26

  • 🎉 Initial release