Get the incrementer being able to be submitted, causing submits, and styling without flickering

This commit is contained in:
Oliver-Akins 2024-04-04 19:59:23 -06:00
parent d5429b7b34
commit 9f6a8e5e73
3 changed files with 58 additions and 23 deletions

View file

@ -4,27 +4,42 @@ Attributes:
@property {number} stepSize @property {number} stepSize
@property {number} largeStepSize @property {number} largeStepSize
*/ */
class DotDungeonIncrementer extends HTMLElement { export class DotDungeonIncrementer extends HTMLElement {
static elementName = `dd-incrementer`;
static styles = ``;
#input; #input;
#publicInput;
#sr;
constructor() { constructor() {
super(); super();
const value = this.getAttribute(`value`);
/*
This input exists for the sole purpose of making it so that the form data
works with this input without needing to do jank work arounds (even though
this on it's own is already a sort of jank work around).
*/
const hiddenInput = document.createElement(`input`);
hiddenInput.type = `hidden`;
hiddenInput.name = this.getAttribute(`name`);
hiddenInput.value = value;
this.#publicInput = hiddenInput;
this.appendChild(hiddenInput);
const sr = this.attachShadow({ mode: `open` }); const sr = this.attachShadow({ mode: `open` });
this.#sr = sr;
const container = document.createElement(`div`); const container = document.createElement(`div`);
if (DotDungeonIncrementer.styles) this.#embedStyles();
const style = document.createElement(`link`);
style.href = `./systems/dotdungeon/.styles/components/incrementer.css`;
style.rel = `stylesheet`;
container.appendChild(style);
const input = document.createElement(`input`); const input = document.createElement(`input`);
this.#input = input; this.#input = input;
input.type = `number`; input.type = `number`;
input.value = this.getAttribute(`value`);
input.addEventListener(`change`, this.#updateValue.bind(this)); input.addEventListener(`change`, this.#updateValue.bind(this));
input.value = value;
const increment = document.createElement(`button`); const increment = document.createElement(`button`);
increment.innerHTML = `+`; increment.innerHTML = `+`;
@ -46,26 +61,46 @@ class DotDungeonIncrementer extends HTMLElement {
sr.appendChild(container); sr.appendChild(container);
}; };
connectedCallback() {
if (!DotDungeonIncrementer.styles) {
fetch(`./systems/dotdungeon/.styles/components/incrementer.css`)
.then(r => r.text())
.then(t => {
DotDungeonIncrementer.styles = t;
this.#embedStyles();
});
};
};
#embedStyles() {
const style = document.createElement(`style`);
style.innerHTML = DotDungeonIncrementer.styles;
this.#sr.appendChild(style);
};
#updateValue() { #updateValue() {
// TODO: Emit a change event for the new value, and check for cancellation this.#publicInput.value = this.#input.value;
const event = new Event(`change`); const event = new Event(`change`, { bubbles: true });
this.dispatchEvent(event); this.#publicInput.dispatchEvent(event);
}; };
#increment() { #increment() {
console.log(`increment event`) console.log(`increment event`);
this.#input.value++; this.#input.value++;
this.#updateValue(); this.#updateValue();
}; };
#decrement() { #decrement() {
console.log(`decrement event`) console.log(`decrement event`);
this.#input.value--; this.#input.value--;
this.#updateValue(); this.#updateValue();
}; };
}; };
if (!window.customElements.get(`dd-incrementer`)) { if (!window.customElements.get(DotDungeonIncrementer.elementName)) {
window.customElements.define(`dd-incrementer`, DotDungeonIncrementer); window.customElements.define(
DotDungeonIncrementer.elementName,
DotDungeonIncrementer
);
}; };

View file

@ -4,6 +4,7 @@ the corresponding web component. Importing this into other files is forbidden.
*/ */
$default-border-radius: 4px; $default-border-radius: 4px;
$default-height: 1.25rem;
:host { :host {
min-width: max-content; min-width: max-content;
@ -11,10 +12,10 @@ $default-border-radius: 4px;
div { div {
display: grid; display: grid;
grid-template-columns: auto 50px auto; grid-template-columns: var(--height, $default-height) 50px var(--height, $default-height);
// I dunno why this is needed for the height to not be calculated as 17px, // I dunno why this is needed for the height to not be calculated as 17px,
// but it is for some arcane reason // but it is for some arcane reason
grid-template-rows: 1fr; grid-template-rows: var(--height, $default-height);
} }
input { input {
@ -23,7 +24,6 @@ input {
font-family: inherit; font-family: inherit;
text-align: center; text-align: center;
padding: 0; padding: 0;
background: green;
&::-webkit-inner-spin-button, &::-webkit-outer-spin-button { &::-webkit-inner-spin-button, &::-webkit-outer-spin-button {
-webkit-appearance: none; -webkit-appearance: none;
@ -36,11 +36,11 @@ input {
button { button {
border: none; border: none;
outline: none; outline: none;
height: 100%;
aspect-ratio: 1 / 1; aspect-ratio: 1 / 1;
padding: 4px; padding: 0;
background: darkgreen; display: flex;
max-width: 100%; justify-content: center;
align-items: center;
&:hover { &:hover {
cursor: pointer; cursor: pointer;