|
/** |
|
* `my-custom-element` is very basic custom element. |
|
* |
|
* @param {ParentInstance} [self] Parent instance from which we want to inherit from. |
|
* @returns {MyCustomElementInstance} Your custom element instance. |
|
*/ |
|
function MyCustomElement(self) { |
|
// Equivalent of super call. |
|
// This is needed on order to create `myCustomElement` element from JS |
|
self = HTMLElement.call(self || this); |
|
|
|
// Here initialization logic can be used. |
|
... |
|
|
|
return self; |
|
} |
|
|
|
// List of observed attributes should be specified in order for `attributeChangedCallback` to be called |
|
// This is static definition of property. Static functions could be defined in the same way. |
|
MyCustomElement.observedAttributes = [‘attributeName’, ‘anotherAttributeName’]; |
|
|
|
// This is how you can create a prototype of your custom element. In this case it extends native HTMLElement. |
|
MyCustomElement.prototype = Object.create(HTMLElement.prototype, { |
|
// An instance of the element is created or upgraded. |
|
// Useful for initializing state, settings up event listeners. |
|
constructor: { |
|
// This specifies that the value for the `constructor` is `MyCustomElement`. |
|
value: MyCustomElement |
|
}, |
|
// Called every time the element is inserted into the DOM. |
|
// Useful for running setup code, such as fetching resources or rendering. |
|
// Generally, you should try to delay work until this time. |
|
connectedCallback: { |
|
// This specifies that the value of `connectedCallback` is a function with some setup logic. |
|
value: function () { |
|
... |
|
} |
|
}, |
|
// Called every time the element is removed from the DOM. |
|
// Useful for running clean up code. |
|
disconnectedCallback: { |
|
// This specifies that the value of `disconnectedCallback` is a function with some clean up logic. |
|
value: function () { |
|
... |
|
} |
|
}, |
|
// Called when an observed attribute has been added, removed, updated, or replaced. |
|
// Also called for initial values when an element is created by the parser, or upgraded. |
|
// Note: only attributes listed in the `observedAttributes` property will receive this callback. |
|
attributeChangedCallback: { |
|
// This specifies that the value of `attributeChangedCallback` is a function with some logic. |
|
value: function (name, oldValue, newValue) { |
|
... |
|
} |
|
}, |
|
// Like this yuo can define any properties of the component so that you can access them later. |
|
propertyName: { |
|
// Like this you can defined a function which will be called when you want to get a property of the component. |
|
// E.g. `element.value`, `element.src` or `element.propertyName`. |
|
get: function () { |
|
return ...; |
|
}, |
|
// Like this you can defined a function which will be called when you want to set a property of the component. |
|
// E.g. `element.value = 'new value'`, `element.src = 'new src'` or `element.propertyName = 'some property'`. |
|
set: function (value) { |
|
... |
|
} |
|
}, |
|
// Like this you can define any methods of the component so that you can call them later. |
|
// E.g. `element.hide()`, `element.print()` or `element.methodName()`. |
|
methodName: { |
|
// This specifies that the value of `methodName` is a function with some logic. |
|
value: function () { |
|
... |
|
} |
|
} |
|
}); |
|
|
|
// Defines `my-custom-element` custom element |
|
window.customElements.define(‘my-custom-element’, MyCustomElement); |