Gathering detailed insights and metrics for @webcomponents/webcomponentsjs
Gathering detailed insights and metrics for @webcomponents/webcomponentsjs
Gathering detailed insights and metrics for @webcomponents/webcomponentsjs
Gathering detailed insights and metrics for @webcomponents/webcomponentsjs
webcomponentsjs
The popular WebComponents polyfill in a nice, frequently updated NPM package
parcel-plugin-webcomponents
A parcel plugin that autoincludes the webcomponentsjs polyfill.
3shape-global-nav
## How to use Install packages ``` npm i 3shape-global-nav @webcomponents/webcomponentsjs ``` Add polyfill import ``` import '@webcomponents/webcomponentsjs/webcomponents-bundle.js'; // or dynamical import '@webcomponents/webcomponentsjs/webcomponents-loa
webcomponentsjs-custom-element-v0
This is the v0 branch of CustomElements from the webcomponentsjs project.
Web Components Polyfills
npm install @webcomponents/webcomponentsjs
Typescript
Module System
Node Version
NPM Version
99.8
Supply Chain
100
Quality
82.6
Maintenance
100
Vulnerability
100
License
HTML (61.96%)
JavaScript (28.06%)
TypeScript (9.77%)
CSS (0.21%)
Love this project? Help keep it running — sponsor us today! 🚀
Total Downloads
58,499,772
Last Day
8,991
Last Week
215,584
Last Month
838,906
Last Year
10,312,998
BSD-3-Clause License
1,157 Stars
4,268 Commits
166 Forks
35 Watchers
67 Branches
118 Contributors
Updated on Feb 10, 2025
Latest Version
2.8.0
Package Id
@webcomponents/webcomponentsjs@2.8.0
Unpacked Size
3.40 MB
Size
911.03 kB
File Count
50
NPM Version
9.5.1
Node Version
19.8.1
Published on
Mar 30, 2023
Cumulative downloads
Total Downloads
Last Day
-13.3%
8,991
Compared to previous day
Last Week
-0%
215,584
Compared to previous week
Last Month
28%
838,906
Compared to previous month
Last Year
-7%
10,312,998
Compared to previous year
Note. For polyfills that work with the older Custom Elements and Shadow DOM v0 specs, see the v0 branch.
Note. For polyfills that include HTML Imports, see the v1 branch.
A suite of polyfills supporting the Web Components specs:
For browsers that need it, there are also some minor polyfills included:
HTMLTemplateElement
Promise
Event
, CustomEvent
, MouseEvent
constructors and Object.assign
, Array.from
(see webcomponents-platform)URL constructor
1npm install @webcomponents/webcomponentsjs
You can also load the code from a CDN such as unpkg: https://unpkg.com/@webcomponents/webcomponentsjs@^2/
webcomponents-bundle.js
The webcomponents-bundle.js
contains all of the web components polyfills and is
suitable for use on any supported browser. All of the polyfill code will be loaded
but each polyfill will only be used based on feature detection.
The bundle includes Custom Elements, Shady DOM/CSS and generic platform polyfills
(such as ES6 Promise, Constructable events, etc.) (needed by Internet Explorer 11),
and Template (needed by IE 11 and Edge).
The webcomponents-bundle.js
is very simple to use but it does load code
that is not needed on most modern browsers, slowing page load. For best performance,
use the webcomponents-loader.js
.
Here's an example:
1<!-- load webcomponents bundle, which includes all the necessary polyfills --> 2<script src="node_modules/@webcomponents/webcomponentsjs/webcomponents-bundle.js"></script> 3 4<!-- load the element --> 5<script type="module" src="my-element.js"></script> 6 7<!-- use the element --> 8<my-element></my-element>
webcomponents-loader.js
The webcomponents-loader.js
is a client-side loader that dynamically loads the
minimum polyfill bundle, using feature detection.
webcomponents-loader.js
can be loaded synchronously, or asynchronously depending on your needs.
If you have inlined the source of webcomponent-loader.js
, then you should specify window.WebComponents.root
as the root from which to load the polyfills.
For example:
1<script> 2 window.WebComponents = window.WebComponents || {}; 3 window.WebComponents.root = 'node_modules/@webcomponents/webcomponentsjs/'; 4</script>
This property is used to build the URL to the selected bundle, so you should
only set it to values that are unable to be influenced by user-controlled data.
If trusted types are enforced, this property should be a TrustedScriptURL
.
When loaded synchronously, webcomponents-loader.js
behaves similarly to webcomponents-bundle.js
.
The appropriate bundle will be loaded with document.write()
to ensure that WebComponent polyfills are available for subsequent scripts and modules.
Here's an example:
1<!-- load the webcomponents loader, which injects the necessary polyfill bundle --> 2<script src="node_modules/@webcomponents/webcomponentsjs/webcomponents-loader.js"></script> 3 4<!-- load the element --> 5<script type="module" src="my-element.js"></script> 6 7<!-- use the element --> 8<my-element></my-element>
When loaded asychronously with the defer
attribute, polyfill bundles will be loaded asynchronously,
which means that scripts and modules that depend on webcomponents APIs must be loaded
using WebComponents.waitFor
function.
The WebComponents.waitFor
function takes a callback function as an argument, and will evaluate that callback after the polyfill bundle has been loaded.
The callback function should load scripts that need the polyfills (typically via import('my-script.js')
) and
should return a promise that resolves when all scripts have loaded.
Here's an example:
1<!-- Load polyfills; note that "loader" will load these async --> 2<script 3 src="node_modules/@webcomponents/webcomponentsjs/webcomponents-loader.js" 4 defer 5></script> 6 7<!-- Load a custom element definitions in `waitFor` and return a promise --> 8<script type="module"> 9 WebComponents.waitFor(() => { 10 // At this point we are guaranteed that all required polyfills have 11 // loaded, and can use web components API's. 12 // The standard pattern is to load element definitions that call 13 // `customElements.define` here. 14 // Note: returning the import's promise causes the custom elements 15 // polyfill to wait until all definitions are loaded and then upgrade 16 // the document in one batch, for better performance. 17 return import('my-element.js'); 18 }); 19</script> 20 21<!-- Use the custom element --> 22<my-element></my-element>
The WebComponents.waitFor
function may be called multiple times, and the callback functions will be processed in order.
Here's a more complicated example:
1<!-- Load polyfills; note that "loader" will load these async --> 2<script 3 src="node_modules/@webcomponents/webcomponentsjs/webcomponents-loader.js" 4 defer 5></script> 6 7<script type="module"> 8 WebComponents.waitFor(async () => { 9 if (!window.fetch) { 10 await import('node_modules/fetch-polyfill/fetch.js'); 11 } 12 return import('my-element.js'); 13 }); 14</script> 15 16<script type="module"></script>
If you're using the loader on a page that enforces the trusted-types
Content
Security Policy, you'll need to allow the webcomponents-loader
policy name so
that the loader can dynamically create and insert a <script>
for the polyfill
bundle it selects based on feature detection. If you set WebComponents.root
(which is rare), it should be set to a TrustedScriptURL
for Trusted Types
compatibility.
The WebComponentsReady
event is fired when polyfills and user scripts have loaded and custom elements have been upgraded. This event is generally not needed; however, it may be useful in some cases like testing. If imperative code should wait until a specific custom element definition has loaded, it can use the platform customElements.whenDefined
API.
custom-elements-es5-adapter.js
According to the spec, only ES6 classes (https://html.spec.whatwg.org/multipage/scripting.html#custom-element-conformance) may be passed to the native customElements.define
API. For best performance, ES6 should be served to browsers that support it, and ES5 code should be serve to those that don't. Since this may not always be possible, it may make sense to compile and serve ES5 to all browsers. However, if you do so, ES5-style custom element classes will now not work on browsers with native Custom Elements because ES5-style classes cannot properly extend ES6 classes, like HTMLElement
.
As a workaround, if your project has been compiled to ES5, load custom-elements-es5-adapter.js
before defining Custom Elements. This adapter will automatically wrap ES5.
The adapter must NOT be compiled.
The polyfills are intended to work in the latest versions of evergreen browsers. See below for our complete browser support matrix:
Polyfill | Edge | IE11+ | Chrome* | Firefox* | Safari 9+* | Chrome Android* | Mobile Safari* |
---|---|---|---|---|---|---|---|
Custom Elements | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
Shady CSS/DOM | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
*Indicates the current version of the browser
The polyfills may work in older browsers, however require additional polyfills (such as classList, or other platform polyfills) to be used. We cannot guarantee support for browsers outside of our compatibility matrix.
The ShadowDOM polyfill does not properly support CSS in ShadowDoM out of the box:
:host
) do not work.You can fix those issues by manually calling the ShadyCSS
APIs. See ShadyCSS usage.
See #215 for background.
In Edge and IE, instances of Custom Elements have a constructor
property of HTMLUnknownElementConstructor
and HTMLUnknownElement
, respectively. It's unsafe to rely on this property for checking element types.
It's worth noting that customElement.__proto__.__proto__.constructor
is HTMLElementPrototype
and that the prototype chain isn't modified by the polyfills(onto ElementPrototype
, etc.)
ShadyCSS :host()
rules can only have (at most) 1-level of nested parentheses in its argument selector under ShadyCSS. For example, :host(.zot)
and :host(.zot:not(.bar))
both work, but :host(.zot:not(.bar:nth-child(2)))
does not.
If you wish to build the bundles yourself, you'll need node
and npm
on your system:
npm
to install gulp.js: npm install -g gulp
Now you are ready to build the polyfills with:
# install dependencies
npm install
# build
npm run build
The builds will be placed into the root directory.
See the contributing guide
Everything in this repository is BSD style license unless otherwise specified.
Copyright (c) 2015 The Polymer Authors. All rights reserved.
webcomponents-loader.js
with the defer
attribute, scripts that rely on the polyfills must be loaded using WebComponents.waitFor(loadCallback)
.No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
security policy file detected
Details
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 12/19 approved changesets -- score normalized to 6
Reason
0 commit(s) and 4 issue activity found in the last 90 days -- score normalized to 3
Reason
dependency not pinned by hash detected -- score normalized to 3
Details
Reason
SAST tool is not run on all commits -- score normalized to 1
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
83 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-02-10
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More