HTML5 Web Component - قسمت دوم - Custom Elements
نویسنده: سالار ربال
تاریخ: ۱۳۹۹/۰۲/۰۱ ۱۹:۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
customElements.define("x-component", class extends HTMLElement {
constructor() {
super();
console.log('constructed!');
}
connectedCallback() {
console.log('connected!');
}
disconnectedCallback() {
console.log('disconnected!');
}
adoptedCallback() {
console.log('adopted!');
}
attributeChangedCallback(name, oldValue, newValue) {
console.log('attirbuteChanged!', name, oldValue, newValue);
}
static get observedAttributes() {
return ['checked','demo','label'];
}
}); // "tabs" is not a valid custom element name
document.createElement('tabs') instanceof HTMLUnknownElement === true //true
// "x-tabs" is a valid custom element name
document.createElement('x-tabs') instanceof HTMLElement === true //true customElements.whenDefined('x-component').then(() => {
console.log('x-component defined');
}); <share-buttons>
<social-button type="twitter"><a href="...">Twitter</a></social-button>
<social-button type="fb"><a href="...">Facebook</a></social-button>
<social-button type="plus"><a href="...">G+</a></social-button>
</share-buttons>
// Fetch all the children of <share-buttons> that are not defined yet.
let undefinedButtons = buttons.querySelectorAll(':not(:defined)');
let promises = [...undefinedButtons].map(socialButton => {
return customElements.whenDefined(socialButton.localName);
));
// Wait for all the social-buttons to be upgraded.
Promise.all(promises).then(() => {
// All social-button children are ready.
}); customeElements.define('x-component',...)
let XComponent = customElements.get('x-component');
document.body.appendChild(new XComponent()) const el = document.createElement('x-component');
document.body.appendChild(el);
// connectedCallback() called
el.remove();
// disconnectedCallback()
document.body.appendChild(el);
// connectedCallback() called again const iframe = document.querySelector('iframe');
const iframeImages = iframe.contentDocument.querySelectorAll('img');
const newParent = document.getElementById('images');
iframeImages.forEach(function(imgEl) {
newParent.appendChild(document.adoptNode(imgEl));
}); class XComponent extends HTMLElement {
constructor() {
super();
}
doSomething(){
console.log('doSomething');
}
} let component = document.querySelector('x-component');
component.doSomething(); div.id = 'id-value'; div.hidden = true;
<div id="id-value" hidden>
class XComponent extends HTMLElement {
constructor() {
super();
}
connectedCallback() {
this._render();
}
get disabled() {
return this.hasAttribute('disabled');
}
set disabled(val) {
// Reflect the value of `disabled` as an attribute.
if (val) {
this.setAttribute('disabled', '');
} else {
this.removeAttribute('disabled');
}
this._render();
}
_render() {
//...
}
} attributeChangedCallback(name, oldValue, newValue) {
switch (name) {
case 'checked':
// Note the attributeChangedCallback is only handling the *side effects*
// of setting the attribute.
this.setAttribute('aria-disabled', !!newValue);
break;
...
} attributeChangedCallback(name, oldValue, newValue) {
// When the component is disabled, update keyboard/screen reader behavior.
if (this.disabled) {
this.setAttribute('tabindex', '-1');
this.setAttribute('aria-disabled', 'true');
} else {
this.setAttribute('tabindex', '0');
this.setAttribute('aria-disabled', 'false');
}
// TODO: also react to the other attribute changing.
} constructor() {
super();
this._data = [];
}
get data() {
return _data;
}
set data(value) {
if (this_data === value) return;
this._data = value;
this._render();
} <x-component [disabled]="model.disabled"></x-component>
let el = document.querySelector('x-component');
el.disabled = true;
customElements.define("x-component", class extends HTMLElement {
constructor() {
super();
}
get disabled() {
return this.hasAttribute('disabled');
}
set disabled(val) {
// Reflect the value of `disabled` as an attribute.
if (val) {
this.setAttribute('disabled', '');
} else {
this.removeAttribute('disabled');
}
this._render();
}
});
let el = document.querySelector('x-component');
el.disabled = true;
customElements.define("x-component", class extends HTMLElement {
constructor() {
super();
}
connectedCallback() {
this._upgradeProp('disabled');
}
get disabled() {
return this.hasAttribute('disabled');
}
set disabled(val) {
// Reflect the value of `disabled` as an attribute.
if (val) {
this.setAttribute('disabled', '');
} else {
this.removeAttribute('disabled');
}
this._render();
}
_upgradeProp(prop) {
if (this.hasOwnProperty(prop)) {
let value = this[prop];
delete this[prop]; //delete instance property
this[prop] = value; // set prototype property
}
}
}); connectedCallback() {
if (!this.hasAttribute('role'))
this.setAttribute('role', 'checkbox');
if (!this.hasAttribute('tabindex'))
this.setAttribute('tabindex', -1); //element is not reachable via sequential keyboard navigation, but could be focused
}