Code Monkey home page Code Monkey logo

element-internals-polyfill's Introduction

Element Internals Polyfill

This package is a polyfill for the ElementInternals standard. The specification is supported by current releases of Google's Chrome.

Use case

The primary use case for ElementInternals right now is allowing custom elements full participation in HTML forms. To do this, it provides any element designated as formAssociated access to a handful of utilities.

Installation

This package is available on npm under the name element-internals-polyfill and can be installed with npm, yarn, unpkg or however else you consume dependencies.

Example commands:

npm:

npm i element-internals-polyfill

yarn:

yarn add element-internals-polyfill

unpkg:

import 'https://unpkg.com/element-internals-polyfill';

How it works

To do this, add the static get formAssociated to a custom element and call the attachInternals method to return a new instance of the ElementInternals interface:

class MyInput extends HTMLElement {
  static get formAssociated() {
    return true;
  }

  constructor() {
    super();
    this._internals = this.attachInternals();
  }
}

This works by doing several things under the hood. First, there is a feature check for the ElementInternals object on the window. If that does not exist, the polyfill wires up a global MutationObserver on the document to watch for additions to the DOM that the polyfill might need.

It also monkey-patches HTMLElement.prototype.attachShadow to wire up a similar listener on any created shadow roots and to remove the watcher if the shadow root is removed.

The polyfill will also monkey-patch window.FormData to attach any custom elements to that feature as well.

The currently-supported features of ElementInternals for form-associated custom elements are

Current limitations

  • Right now providing a cross-browser compliant version of ElementInternals.reportValidity is not supported. The method essentially behaves as a proxy for ElementInternals.checkValidity.
  • The polyfill does support the outcomes of the Accessibility Object Model for applying accessibility rules on the DOM object. However, the spec states that updates using AOM will not be reflected by DOM attributes, but only on the element's accesibility object. However, to emulate this behavior before it is fully supported, it is necessary to use the attributes. If you choose to use this feature, please note that behavior in polyfilled browsers and non-polyfilled browsers will be different; however, the outcome for users will be the same.

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.