web: improve button squishiness

This commit is contained in:
anna 2023-01-10 12:35:37 +01:00
parent 4c15d2ffc1
commit d4be43c06f
Signed by: fef
GPG key ID: EC22E476DC2D3D84

View file

@ -1,5 +1,5 @@
import {LitElement, html, css} from "lit";
import {customElement, property, state} from "lit/decorators.js";
import {customElement, property, query, state} from "lit/decorators.js";
import withSharedStyles from "../theme";
@ -11,20 +11,48 @@ export default class NyanoButton extends LitElement {
@state()
private pressed = false;
@query("#btn")
private btn: HTMLButtonElement;
protected render() {
return html`
<button
id="btn"
?disabled=${this.disabled}
class="${this.pressed ? "pressed" : ""}"
@mousedown="${() => this.pressed = true}"
@mouseup="${() => this.pressed = false}"
@mouseleave="${() => this.pressed = false}"
@mousedown="${this.handleMouseDown}"
@mouseup="${this.handleMouseLeave}"
@mouseleave="${this.handleMouseLeave}"
>
<slot/>
</button>
`;
}
private handleMouseDown(e: MouseEvent) {
this.updateTransformOrigin(e);
this.btn.addEventListener("mousemove", this.updateTransformOrigin);
this.pressed = true;
}
private handleMouseLeave() {
this.pressed = false;
this.btn.removeEventListener("mousemove", this.updateTransformOrigin);
}
private updateTransformOrigin = (e: MouseEvent) => {
const rect = this.btn.getBoundingClientRect();
const center = {
x: rect.width / 2,
y: rect.height / 2,
};
const origin = {
x: center.x + (e.clientX - rect.x - center.x) / 2,
y: center.y + (e.clientY - rect.y - center.y) / 2,
};
this.btn.style.transformOrigin = `${origin.x}px ${origin.y}px`;
}
static get styles() {
return withSharedStyles(css`
:host {