|
|
|
|
|
by atirip
1802 days ago
|
|
Oh please, if you want to compare, use modern vanilla too... <!doctype html>
<body>
<custom-element></custom-element>
<script>
const CHECKBOX_ID = "my-checkbox";
const defaultLabelContent = "Toggle me, you newbies";
const beforeDiscountText = "You have not availabled discount";
const afterDiscountText = "Discount Availed!";
const beforeLabelText = "Click on me to remove fake discount"
const afterLabelText = "Click me to apply fake discount!"
const state = new WeakMap();
class CustomElement extends HTMLElement {
connectedCallback() {
this.render();
this.shadowRoot.addEventListener('change', (event) => {
state.set(this, event.target.checked);
this.render();
})
}
constructor() {
super();
this.attachShadow({ mode: 'open' });
}
render() {
let isChecked = state.get(this);
const discountText = isChecked
? afterDiscountText
: beforeDiscountText;
const labelText = isChecked
? beforeLabelText
: afterLabelText;
this.shadowRoot.innerHTML = `
<input
${isChecked ? 'checked ' : ''}
type="checkbox"
id=${CHECKBOX_ID}
>
<label for=${CHECKBOX_ID}>
${labelText}
</label>
<div>${discountText}</div>
`;
}
}
customElements.define('custom-element', CustomElement);
</script>
|
|
https://codepen.io/uwwgo/pen/GRmEKJz
(nothing revolutionary, does the exact same thing.)