Last active
March 3, 2020 18:24
-
-
Save WebReflection/ac28e3a67fd9f871d8ebdf5a8abe0af3 to your computer and use it in GitHub Desktop.
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
customElements.define('any-element', class extends Element { | |
get data() { | |
return this.dataset; | |
} | |
set data(value) { | |
// more or less this operation, but previously known keys, | |
// not found in value, should be dropped from the dataset | |
Object.assign(this.dataset, value); | |
} | |
get aria() { | |
return [...this.attributes] | |
.filter(attr => /^(?:role|aria-.*)$/.test(attr.name)) | |
.reduce((o, a) => ((o[a.name.replace(/^aria-/, '')] = a.value), o), {}); | |
} | |
set aria(value) { | |
// more or less this operation, but previously known | |
// aria related attributes, not found in value, | |
// should be dropped if not present | |
Object.keys(value).forEach(key => { | |
this.setAttribute(key === 'role' ? key : `aria-${key}`, value[key]); | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
the related tweet for historical reasons: https://twitter.com/WebReflection/status/1234901261322989570