Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for custom elements (#320)
* feat: add support for custom elements Closes #319 * Update src/type.js Co-authored-by: Kent C. Dodds <me+github@kentcdodds.com> * uncomplicate setup fn * make it untouched Co-authored-by: Pontus Lundin <pontus.lundin@ica.se> Co-authored-by: Kent C. Dodds <me+github@kentcdodds.com>
- Loading branch information
1 parent
5c11411
commit a55a657
Showing
3 changed files
with
73 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const observed = ['value'] | ||
|
||
class CustomEl extends HTMLElement { | ||
static getObservedAttributes() { | ||
return observed | ||
} | ||
|
||
constructor() { | ||
super() | ||
this.attachShadow({mode: 'open'}) | ||
this.shadowRoot.innerHTML = `<input>` | ||
this.$input = this.shadowRoot.querySelector('input') | ||
} | ||
|
||
connectedCallback() { | ||
observed.forEach(name => { | ||
this.render(name, this.getAttribute(name)) | ||
}) | ||
} | ||
|
||
attributeChangedCallback(name, oldVal, newVal) { | ||
if (oldVal === newVal) return | ||
this.render(name, newVal) | ||
} | ||
|
||
render(name, value) { | ||
if (value == null) { | ||
this.$input.removeAttribute(name) | ||
} else { | ||
this.$input.setAttribute(name, value) | ||
} | ||
} | ||
} | ||
|
||
customElements.define('custom-el', CustomEl) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters