# construct-style-sheets-polyfill

> Constructible style sheets/adopted style sheets polyfill

Latest version **3.1.0** (published 2022-01-30) · MIT license · 0 weekly downloads

## Install

```sh
npm install construct-style-sheets-polyfill
pnpm add construct-style-sheets-polyfill
yarn add construct-style-sheets-polyfill
bun add construct-style-sheets-polyfill
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.1.0 |
| Published | 2022-01-30 |
| First published | 2019-01-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 21.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 144 |
| Maintainers | calebdwilliams |
| Keywords | CSS, CSS in JS, constructible style sheets, constructable style sheets, shadow DOM, polyfill, web components, custom elements, CSSStyleSheet, stylesheet, polyfill, CSS modules |

## Links

- npm: https://www.npmjs.com/package/construct-style-sheets-polyfill
- Repository: https://github.com/calebdwilliams/construct-style-sheets
- Homepage: https://github.com/calebdwilliams/construct-style-sheets#readme
- Issues: https://github.com/calebdwilliams/construct-style-sheets/issues
- npm.io page: https://npm.io/package/construct-style-sheets-polyfill

## Alternatives

- [style-dictionary](https://npm.io/package/style-dictionary.md) — 2.0M weekly downloads
- [postcss-merge-idents](https://npm.io/package/postcss-merge-idents.md) — 1.7M weekly downloads
- [@fontsource/noto-sans](https://npm.io/package/@fontsource/noto-sans.md) — 93.0K weekly downloads
- [uglifycss](https://npm.io/package/uglifycss.md) — 71.6K weekly downloads
- [mat4-interpolate](https://npm.io/package/mat4-interpolate.md) — 23.3K weekly downloads

## Recent versions

- 3.1.0 (latest) — 2022-01-30
- 3.0.6 — 2022-01-30
- 3.0.5 — 2021-12-09
- 3.0.4 — 2021-10-13
- 3.0.3 — 2021-10-02
- 3.0.2 — 2021-10-01
- 3.0.1 — 2021-09-12
- 3.0.0 — 2021-08-12
- 2.4.17 — 2021-08-12
- 3.0.0-0 — 2021-06-18
- 2.4.16 — 2021-02-14
- 2.4.15 — 2021-02-14
- 2.4.14 — 2021-02-14
- 2.4.11 — 2021-02-13
- 2.4.9 — 2021-02-07
- … 39 more at https://npm.io/package/construct-style-sheets-polyfill/versions

## README

# Constructible style sheets polyfill

[![CI](https://github.com/calebdwilliams/construct-style-sheets/actions/workflows/ci.yml/badge.svg)](https://github.com/calebdwilliams/construct-style-sheets/actions)
[![npm version](https://img.shields.io/npm/v/construct-style-sheets-polyfill.svg?style=flat)](https://npmjs.org/package/construct-style-sheets-polyfill 'View this project on npm')
[![codecov](https://codecov.io/gh/calebdwilliams/construct-style-sheets/branch/main/graph/badge.svg)](https://codecov.io/gh/calebdwilliams/construct-style-sheets)

This package is a polyfill for the [constructible style sheets/adopted style sheets specification](https://github.com/WICG/construct-stylesheets/blob/gh-pages/explainer.md). The full specificaiton is enabled by default in Google Chrome as of version 73.

Currently [Mozilla is considering implementation of the feature](https://github.com/mozilla/standards-positions/issues/103), 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:
- `Symbol` polyfill (with support for `Symbol.hasInstance`).
- [@babel/plugin-transform-instanceof](https://www.npmjs.com/package/@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](https://docs.npmjs.com/getting-started),
[yarn](https://yarnpkg.com/en/docs/getting-started), [unpkg](https://unpkg.com)
or however else you consume dependencies.

### Example commands: 

npm:
```bash
npm i construct-style-sheets-polyfill
```

yarn:
```bash
yarn add construct-style-sheets-polyfill
```

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

## Usage

```javascript
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).

---
_Source: https://npm.io/package/construct-style-sheets-polyfill · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
