npm install y-tagCustom Elements let authors define their own elements. Authors associate JavaScript code with custom tag names, and then use those custom tag names as they would any standard tag.
For example, after registering a special kind of button called super-button, use the super button just like this:
Custom elements are still elements. We can create, use, manipulate, and compose them just as easily as any standard As with any element, custom elements can be created in JavaScript or declared. Custom element names must always contain a dash (-). #### Element registration Before you can use a custom element, it needs to be registered. Otherwise, the browser considers it an ##### document.registerElement() To register a new custom element in JavaScript, invoke Here's the imperative version of the previous example: var XFooPrototype = Object.create(HTMLElement.prototype); var XFoo = document.registerElement('x-foo', { Note: the prototype must be chained to Extending existing elements If you want to inherit from a specialized form of var XFooButton = document.registerElement('x-foo-button', { #### Using a custom element After registration, you can construct an instance of your element just like If you've used In the declarative and var xFoo = new XFoo(); var xFooButton = document.createElement('button', 'x-foo-button'); Browser limitations require that we supply the constructor while you supply the Include the The custom elements polyfill handles element upgrades _asynchronously_. The polyfill defers upgrading elements until The Custom Elements specification is still under discussion. The polyfill implements certain features in advance of the specification. In particular, the lifecycle callback methods that get called if implemented on the element prototype: * npm install or today.$3
HTMLElement.document.registerElement() somewhere in the page.
As before, custom elements built this way work just like standard elements.
XFooPrototype.createdCallback = function() {
this.textContent = "I'm an x-foo!";
};
XFooPrototype.foo = function() {
console.log('foo() called');
};
prototype: XFooPrototype
});HTMLElement.prototype (i.e. instanceof HTMLElement.prototype).HTMLElement (e.g. HTMLButtonElement),
declare the type using the extends option when calling document.registerElement():
Example extending button:
var XFooButtonPrototype = Object.create(HTMLButtonElement.prototype);
XFooButtonPrototype.createdCallback = function() {
this.textContent = "I'm an x-foo button!";
};
prototype: XFooButtonPrototype,
extends: 'button'
});
standard DOM elements:extends to create a custom element that derives from an existing DOM element
(e.g. something other than HTMLElement), use the is syntax:document.registerElement() example above, XFoo was defined as the new element's constructor.
This can also be used to create an instance:
document.body.appendChild(xFoo);
xFooButton.foo(); // "foo() called"prototype.
Use the createdCallback to do initialization work that might otherwise be in a constructor.Polyfill details
$3
y-tag.js or y-tag.min.js (minified) file in your project.$3
DOMContentLoaded time. It does this as a performance optimization. Subsequent to the initial upgrade pass, Mutation Observers are used to discover new elements.createdCallback() is called when a custom element is created.
* attachedCallback() is called when a custom element is inserted into a DOM subtree.
* detachedCallback() is called when a custom element is removed from a DOM subtree.
* attributeChangedCallback(attributeName) is called when a custom element's attribute value has changedcreatedCallback is invoked _synchronously_ with element instantiation, the other callbacks are called _asyncronously_. The asynchronous callbacks generally use the MutationObserver timing model, which means they are called before layouts, paints, or other triggered events, so the developer need not worry about flashing content or other bad things happening before the callback has a chance to react to changes.Building
grunt