function buttonGroup(options, {value: initialValue}) {
const group = html`<span style="display: flex"></span>`;
let value = initialValue;
function update() {
for (const button of group.children) {
button.style.background = button.value === value ? "#ddd" : null;
}
}
function input(event) {
value = event.target.value;
update();
group.dispatchEvent(new CustomEvent("input"));
}
for (const option of options) {
const button = html`<button style="flex: 1">${option.label}</button>`;
button.value = option.value;
button.onclick = input;
group.appendChild(button);
}
update();
return Object.defineProperty(group, "value", {
get() {
return value;
},
set(newValue) {
value = newValue;
update();
}
});
}