项目作者: calebdwilliams

项目描述 :
Constructible style sheets/adopted style sheets polyfill
高级语言: JavaScript
项目地址: git://github.com/calebdwilliams/construct-style-sheets.git
创建时间: 2019-01-17T14:41:25Z
项目社区:https://github.com/calebdwilliams/construct-style-sheets

开源协议:MIT License

下载


Constructible style sheets polyfill

CI
npm version
codecov

This package is a polyfill for the constructible style sheets/adopted style sheets specification. The full specificaiton is enabled by default in Google Chrome as of version 73. Firefox only has support as of version 101, and Safari as of version 16.4.

Use case

The constructible style sheets proposal is intended to allow for the dynamic
creation and sharing of style sheets, even across shadow boundaries. By adopting
a style sheet into a shadow root, the same sheet can be applied to multiple
nodes, including the document.

How it works

This polyfill will create a new style element for every DocumentOrShadowRoot
into which the sheet is adopted. This is counter to the current proposal, but
updates to the style sheet using the replace or replaceSync methods should
update the relevant style elements with the updated content across all adopters.

No changes will occur in a browser that supports the feature by default.

Support

This polyfill supports all modern browsers and IE 11.

For browsers that do not support the web components specification (currently
IE 11 and Edge) only the document-level style sheets adoption works.

IE 11

To make this polyfill work with IE 11 you need the following tools:

  • Symbol polyfill (with support for Symbol.hasInstance).
  • @babel/plugin-transform-instanceof"">@babel/plugin-transform-instanceof
    applied to your code that uses instanceof against CSSStyleSheet.

Installation

This package is available on npm under the name construct-style-sheet-polyfill
and can be installed with npm,
yarn, unpkg
or however else you consume dependencies.

Example commands:

npm:

  1. npm i construct-style-sheets-polyfill

yarn:

  1. yarn add construct-style-sheets-polyfill

unpkg:

  1. import 'https://unpkg.com/construct-style-sheets-polyfill';

Usage

  1. const everythingTomato = new CSSStyleSheet();
  2. everythingTomato
  3. .replace(
  4. `
  5. * {
  6. color: tomato;
  7. }
  8. `,
  9. )
  10. .then(console.log); // will log the CSSStyleSheet object
  11. document.adoptedStyleSheets = [everythingTomato];
  12. class TestEl extends HTMLElement {
  13. constructor() {
  14. super();
  15. this.attachShadow({mode: 'open'});
  16. this.shadowRoot.adoptedStyleSheets = [everythingTomato];
  17. }
  18. connectedCallback() {
  19. this.shadowRoot.innerHTML = `<h1>This will be tomato colored, too</h1>`;
  20. }
  21. }
  22. customElements('test-el', TestEl);
  23. const testEl = new TestEl();
  24. document.body.appendChild(testEl);

The polyfill will append new style tags to the designated DocumentOrShadowRoot.
Manually removing the style node will cause a re-insertion of the styles at the
designated root. To remove a style sheet, you must remove the style element
from the element.adoptedStyleSheets array. The behavior here is supposed to
emulate a FrozenArray, so modifying the array in question will have no effect
until the value is changed using a setter.

A note about versioning

This packages doesn’t necessarily follow semantic versioning. As the spec is still under consideration and implementation by browser vendors, the features supported by this package will change (generally following Chrome’s implementation).