construct-style-sheets-polyfill
Constructible style sheets/ adopted style sheets polyfill
Last updated 6 years ago by calebdwilliams .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install construct-style-sheets-polyfill 
SYNC missed versions from official npm registry.

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.

Currently Mozilla is considering implementation of the feature, marking it as "worth prototyping" while Apple has not publically signaled, they have been active in the standards discussions surrounding it.

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:

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:

npm i construct-style-sheets-polyfill

yarn:

yarn add construct-style-sheets-polyfill

unpkg:

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

Usage

const everythingTomato = new CSSStyleSheet();
everythingTomato
  .replace(
    `
* {
    color: tomato;
}
`,
  )
  .then(console.log); // will log the CSSStyleSheet object

document.adoptedStyleSheets = [everythingTomato];

class TestEl extends HTMLElement {
  constructor() {
    super();
    this.attachShadow({mode: 'open'});
    this.shadowRoot.adoptedStyleSheets = [everythingTomato];
  }

  connectedCallback() {
    this.shadowRoot.innerHTML = `<h1>This will be tomato colored, too</h1>`;
  }
}

customElements('test-el', TestEl);

const testEl = new TestEl();
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).

Current Tags

  • 3.1.0                                ...           latest (3 years ago)

54 Versions

  • 3.1.0                                ...           3 years ago
  • 3.0.6                                ...           3 years ago
  • 3.0.5                                ...           3 years ago
  • 3.0.4                                ...           3 years ago
  • 3.0.3                                ...           3 years ago
  • 3.0.2                                ...           3 years ago
  • 3.0.1                                ...           3 years ago
  • 3.0.0                                ...           3 years ago
  • 2.4.17                                ...           3 years ago
  • 3.0.0-0                                ...           4 years ago
  • 2.4.16                                ...           4 years ago
  • 2.4.15                                ...           4 years ago
  • 2.4.14                                ...           4 years ago
  • 2.4.11                                ...           4 years ago
  • 2.4.9                                ...           4 years ago
  • 2.4.6                                ...           4 years ago
  • 2.4.5                                ...           4 years ago
  • 2.4.4                                ...           4 years ago
  • 2.4.3                                ...           4 years ago
  • 2.4.2                                ...           4 years ago
  • 2.4.1                                ...           4 years ago
  • 2.4.0                                ...           4 years ago
  • 2.3.5                                ...           5 years ago
  • 2.3.4                                ...           5 years ago
  • 2.3.3                                ...           5 years ago
  • 2.3.2                                ...           5 years ago
  • 2.3.1                                ...           5 years ago
  • 2.3.0                                ...           5 years ago
  • 2.2.11                                ...           5 years ago
  • 2.2.10                                ...           5 years ago
  • 2.2.9                                ...           5 years ago
  • 2.2.7                                ...           5 years ago
  • 2.2.6                                ...           5 years ago
  • 2.2.5                                ...           5 years ago
  • 2.2.4                                ...           5 years ago
  • 2.2.3                                ...           5 years ago
  • 2.2.2                                ...           5 years ago
  • 2.2.1                                ...           5 years ago
  • 2.2.0                                ...           5 years ago
  • 2.1.0                                ...           5 years ago
  • 2.0.3                                ...           5 years ago
  • 2.0.2                                ...           6 years ago
  • 2.0.1                                ...           6 years ago
  • 1.0.3                                ...           6 years ago
  • 1.0.2                                ...           6 years ago
  • 1.0.1                                ...           6 years ago
  • 1.0.0                                ...           6 years ago
  • 0.3.2                                ...           6 years ago
  • 0.3.1                                ...           6 years ago
  • 0.3.0                                ...           6 years ago
  • 0.2.0                                ...           6 years ago
  • 0.1.3                                ...           6 years ago
  • 0.1.2                                ...           6 years ago
  • 0.1.1                                ...           6 years ago
Maintainers (1)
Downloads
Total 0
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE