function seedButton(label = "🎲 Generate seed", opts = {}) {
const { disabled, undoLabel, redoLabel } = Object.assign(
{
disabled: false,
undoLabel: "⏪ Undo",
redoLabel: "⏩ Redo"
},
opts
);
const machine = createSeedButtonMachine(generateRandomSeed);
const service = xState.interpret(machine).start();
const undoButton = html`<button
type="button"
class="${blockClass}__undo"
disabled=${disabled}
onClick=${() => service.send("UNDO")}
>${undoLabel}</button>`;
const redoButton = html`<button
type="button"
class="${blockClass}__redo"
disabled=${disabled}
onClick=${() => service.send("REDO")}
>${redoLabel}</button>`;
const form = html`<form class="${ns} ${blockClass}">
<button
type="button"
onclick=${() => service.send("GENERATE")}
disabled=${disabled}>${label}</button>
${undoButton}
${redoButton}
</form>`;
function dispatchInputEvent() {
form.dispatchEvent(new Event("input", { bubbles: true }));
}
function updateUi(state) {
undoButton.disabled = state.value === "idle" || state.value === "bigbang";
redoButton.disabled = state.value === "idle" || state.value === "present";
form.dataset.seedButtonState = state.value;
}
service.subscribe((state) => {
updateUi(state);
dispatchInputEvent();
});
attachStyles(invalidation);
invalidation.then(() => service.stop());
return Object.defineProperty(form, "value", {
get() {
const { seeds, head } = service.state.context;
return seeds[head];
},
set(value) {
service.send({ type: "GENERATE", seed: value });
}
});
}