Skip to content

Instantly share code, notes, and snippets.

@robdodson
Created October 24, 2016 23:01
Show Gist options
  • Select an option

  • Save robdodson/1bf347309b8d9e20a1b88e409108ab4c to your computer and use it in GitHub Desktop.

Select an option

Save robdodson/1bf347309b8d9e20a1b88e409108ab4c to your computer and use it in GitHub Desktop.
export default class XCheckbox extends HTMLElement {
constructor() {
this._checked = false;
}
connectedCallback() {
this.addEventListener('click', this._onclick);
}
disconnectedCallback() {
this.removeEventListener('click', this._onclick);
}
_onclick(e) {
this.checked = !this.checked;
this.dispatchEvent(new CustomEvent('checkedchange', {
detail: { checked: this.checked }, bubbles: false
}));
}
set checked(value) {
this._checked = value;
value ? this.setAttribute('checked', '') : this.removeAttribute('checked');
}
get checked() {
return this._checked;
}
}
window.customElements.define('x-checkbox', XCheckbox);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment