Coder Social home page Coder Social logo

alkadia / custom-elements Goto Github PK

View Code? Open in Web Editor NEW

This project forked from webreflection/custom-elements

0.0 1.0 0.0 3.86 MB

An unobtrusive customElements V1 polyfill

Home Page: https://medium.com/@WebReflection/some-custom-elements-update-376317dd7862

License: ISC License

JavaScript 97.78% HTML 2.22%

custom-elements's Introduction

Custom Elements V1 Polyfill

Social Media Photo by Joanna Kosinska on Unsplash

An unobtrusive customElements V1 polyfill.

Warning: @ungap/custom-elements is what you are looking for!

This module is a ponyfill but the official module for CE is not here.

Use this module only if you understand what a ponyfill here, and only if you are capable of not invoking this more than once in your project.


Compatibility

The polyfill gracefully enhances the following minimum versions of at least these browsers, up to their latest version:

  • Chrome 38
  • Firefox 14
  • Opera 25
  • Internet Explorer 11 and Edge 12
  • Safari 8 and WebKit based
  • Samsung Internet 3

How To Polyfill

This polyfill provides both global fixes and ESM or CJS modules.

As global polyfill

The index.js and its min.js versions are dedicated for usage within HTML pages.

<!-- simply include this script on top of your HTML files -->
<script src="//unpkg.com/@webreflection/custom-elements"></script>

Patches are applied after feature detections to bring customElements V1 to legacy browsers, as well as modern browsers that are not fully specs compliant (i.e. Safari / WebKit).

As ESM

The esm/index.js is the default export. The module does not patch anything until explicitly invoked.

import cePolyfill from '@webreflection/custom-elements';
const customElements = cePolyfill(self || window || global);
const {define, get, whenDefined} = customElements;

As CJS

Everything said for ESM, except the file is in cjs/index.js.

const cePolyfill = require('@webreflection/custom-elements');
const customElements = cePolyfill(self || window || global);
const {define, get, whenDefined} = customElements;

Extra note about ESM/CJS modules

If you'd like to not pollute the global object with all the patches, you need to pass an object that contains all needed globals.

However, this is strongly discouraged unless you are doing this to test/cover something via node.js only, not browsers.

Example

const customElements = cePolyfill({
  // pass a bound version of customElements if available
  customElements: self.customElements && {
    // only define and get are needed
    define: customElements.define.bind(customElements),
    get: customElements.get.bind(customElements)
  },

  // needed globals through this `self` namespace
  Map, MutationObserver, Object, Set, WeakMap,
  HTMLElement, Error, TypeError,

  // the `document.createElement` might be patched regardless
  document,
  // the `Element.prototype.attachShadow` might be patched regardless
  Element,
  // the `Node.prototype.isConnected` might be patched regardless
  Node,

  // one or more globals you'd like to ake available for builtin extends
  HTMLButtonElement,
  HTMLDivElement,
});

To Keep In Mind

This is not a ShadowDOM polyfill, this is just the current Custom Elements V1 as specified by standard bodies.

If you are looking for a reasonable attempt to polyfill ShadowDOM too, ShadyDOM would be my recommendation.

Please note, once this polyfill has been tested enough it will be moved to under @ungap.

Please also note that:

custom-elements's People

Contributors

webreflection avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.