1.4.3 • Published 4 years ago

web-component-live-scoped-css-polyfill v1.4.3

Weekly downloads
28
License
MIT
Repository
github
Last release
4 years ago

Live scoped CSS polyfill for Web Components

When I started to use Web Components I discovered that the polyfills provided by the Polymer team works great except for some problems I got with the ShadyCSS polyfill. The ShadyCSS polyfill requires some manual work to get it up and running and it does not support dynamically changed styles.

This polyfill provide with an alternative to @webcomponents/shadycss that will add support for dynamically scoped CSS.

If you are using a standard library it will not require any manual setup. If you are using custom elements standalone, please read about "renderMethod" in the "Configuration" section.

How to Install

Only this polyfill

npm install web-component-live-scoped-css-polyfill

All recommended polyfills

npm install core-js @webcomponents/webcomponents-platform @webcomponents/custom-elements @webcomponents/shadydom @webcomponents/template web-component-slotchange-polyfill web-component-live-scoped-css-polyfill

Usage

This polyfill should be added last as it is dependent on other polyfills like custom-elements to be loaded.

Include as a package

Note! The bellow file can be found here: node_modules/web-component-live-scoped-css-polyfill/lib/live-scoped-css-polyfill.min.js.

<script src="live-scoped-css-polyfill.min.js"></script>

Include as a module

require('web-component-live-scoped-css-polyfill');

Include all recommended

require('core-js/stable');
require('@webcomponents/custom-elements/src/native-shim');
require('@webcomponents/webcomponents-platform');
require('@webcomponents/custom-elements/custom-elements.min');
require('@webcomponents/shadydom');
require('@webcomponents/template');
require('web-component-slotchange-polyfill');
require('web-component-live-scoped-css-polyfill');

Configuration

Example

<script>
    window.liveScopedCSSPolyfill = {
        // Name of the render method that is called on each render.
        renderMethod: 'auto',
        
        // Set to "true" if the style is not changed during render.
        onlyScopeOnConnected: false,

        // Forces the polyfill to be loaded.
        force: false,

        // Rules to disable as a RegExp or string.
        disableRules: null,

        // Set to "true" to enable debugging.
        debug: false
    };
</script>
<script src="live-scoped-css-polyfill.min.js"></script>

Options

NameTypeDefaultDescription
renderMethodstringautoName of the render method that is called on each render. This will be determined automatically by default (depending on which library you are using).
onlyScopeOnConnectedbooleanfalseIf this is set to "true", the "renderMethod" behavior will be disabled and scoping will only happen after the element has been connected once. This will improve performance drastically for applications not changing style during render.
forcebooleanfalseForces the polyfill to be loaded even if the browser has native support.
disableRulesstring|RegExpnullRules to disable as a RegExp or string. This could be useful for rules that can be applied globally instead. Example: ", :before, *:after"
disableComponentsstring[]nullA list of web component tag names to disable the polyfill on.
debugbooleanfalseSet to "true" to enable debugging.
scopeAttributeNamestring'_p'Sets the name of the attribute used for scoping.

Supported libraries

LibraryRender methodSupport
lit-elementupdate()Automatic
stenciljsNot possible to do automatically as the render method is private. You will have to declare componentDidRender() and manually trigger "this.updateScopedCSSPolyfill()".
skate.jsrenderedCallback()Automatic
slimjsrender()Automatic
hybrid.jsrender()Not tested yet
(Popular)requestRender()Automatic

Manual Update

This should only be necessary to do if your library is not supported and it is not possible to set a render method in the configuration.

class MyComponent {
   componentDidRender() {
       this.updateScopedCSSPolyfill();
   }
}

Known Limitations

Using a function argument on the ":host" selector is not supported yet (example: ":host(.special)").

If you have a need for a missing feature or if you have found a bug, please let me know, and I will do my best to fix it.

Release Notes

VersionDateDescription
1.4.32020-02-12Fixes issue with CSS rules for component tag name not being applied.
1.4.22019-11-29Fixes issue with cache key not being generated correctly, causing changes not being updated.
1.4.02019-11-01Uses attribute instead of class for scoping CSS to prevent collision with frameworks. (#3)
1.3.12019-11-01Fixes issue with attributes not being cached correctly causing class changes not being updated. (#2)
1.3.02019-10-30Adds support for "disableComponents" option.
1.2.62019-10-30Fixes issue with -moz-keyframes and -webkit-keyframe not working.
1.2.52019-10-09Fixes issue with ":host-context" selectors not being scoped. (#1)
1.2.12019-09-16Updates rules for libraries based on testing.
1.2.02019-09-12Adds support for "::slotted" CSS rule.
1.1.212019-09-12Bugfix for lit-element render method not hooked.
1.1.202019-09-12Bugfix for selectors inside a slot not working.
1.1.22019-08-29Adds option for enabling debugging.
1.1.02019-08-29Major performance improvements and minor bug fixes.
1.0.02019-08-08Stable release.
0.0.12019-07-30Alpha release.

How to Develop

Installation

npm install

Compilation

npm run compile

Run watcher

npm run watch

Debugging

It is possible to debug in Chrome or Firefox by forcing all polyfills to be enabled.

<script>
    if (window.customElements) {
        window.customElements.forcePolyfill = true;
    }
    window.ShadyDOM = {
        force: true
    };
    window.slotChangePolyfill = {
        force: true
    };
    window.liveScopedCSSPolyfill = {
        force: true
    };
</script>
<script src="all-polyfills.js"></script>
1.4.3

4 years ago

1.4.2

4 years ago

1.4.1

4 years ago

1.4.0

4 years ago

1.3.1

5 years ago

1.3.0

5 years ago

1.2.6

5 years ago

1.2.5

5 years ago

1.2.4

5 years ago

1.2.3

5 years ago

1.2.2

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.23

5 years ago

1.1.22

5 years ago

1.1.21

5 years ago

1.1.20

5 years ago

1.1.19

5 years ago

1.1.18

5 years ago

1.1.17

5 years ago

1.1.16

5 years ago

1.1.15

5 years ago

1.1.14

5 years ago

1.1.13

5 years ago

1.1.12

5 years ago

1.1.11

5 years ago

1.1.10

5 years ago

1.1.8

5 years ago

1.1.7

5 years ago

1.1.6

5 years ago

1.1.5

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago