fix(lit-components): fix rendering and interaction issues in force-graph, html-table, and draggable-dom
Build and Deploy / build-and-deploy (push) Has been cancelled
Build and Deploy / build-and-deploy (push) Has been cancelled
- lit-force-graph: Refactored for instance reuse, better lifecycle management, and added dynamic imports for AR/VR modes. - lit-html-table: Improved visual design, readability of headers, and fixed reactivity in editable mode. - lit-draggable-dom: Fixed clipping issues by switching to relative/absolute positioning and resolved a panning speed bug caused by CSS variable inheritance.
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
import { describe, expect, test } from "vitest";
|
||||
import { AppCalculator } from "./calculator";
|
||||
import "./display-output";
|
||||
import "./keypad-input";
|
||||
|
||||
describe("Lit Calculator Component Logic", () => {
|
||||
test("registers calculator custom elements successfully", () => {
|
||||
expect(customElements.get("app-calculator")).toBeDefined();
|
||||
expect(customElements.get("display-output")).toBeDefined();
|
||||
expect(customElements.get("keypad-input")).toBeDefined();
|
||||
});
|
||||
|
||||
test("calculates addition correctly", () => {
|
||||
const calc = new AppCalculator();
|
||||
calc._value = "12";
|
||||
calc.mode = "+";
|
||||
calc._staging = "8";
|
||||
calc.calculate();
|
||||
expect(calc._value).toBe("20");
|
||||
expect(calc.mode).toBe("");
|
||||
expect(calc._staging).toBe("");
|
||||
});
|
||||
|
||||
test("calculates subtraction correctly", () => {
|
||||
const calc = new AppCalculator();
|
||||
calc._value = "15";
|
||||
calc.mode = "-";
|
||||
calc._staging = "6";
|
||||
calc.calculate();
|
||||
expect(calc._value).toBe("9");
|
||||
expect(calc.mode).toBe("");
|
||||
});
|
||||
|
||||
test("calculates multiplication correctly", () => {
|
||||
const calc = new AppCalculator();
|
||||
calc._value = "4";
|
||||
calc.mode = "*";
|
||||
calc._staging = "5";
|
||||
calc.calculate();
|
||||
expect(calc._value).toBe("20");
|
||||
});
|
||||
|
||||
test("calculates division correctly", () => {
|
||||
const calc = new AppCalculator();
|
||||
calc._value = "20";
|
||||
calc.mode = "/";
|
||||
calc._staging = "4";
|
||||
calc.calculate();
|
||||
expect(calc._value).toBe("5");
|
||||
});
|
||||
|
||||
test("handles division by zero gracefully", () => {
|
||||
const calc = new AppCalculator();
|
||||
calc._value = "10";
|
||||
calc.mode = "/";
|
||||
calc._staging = "0";
|
||||
calc.calculate();
|
||||
expect(calc._value).toBe("0");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,242 @@
|
||||
import { LitElement, html, css } from "lit";
|
||||
import { customElement, property } from "lit/decorators.js";
|
||||
import "./keypad-input.js";
|
||||
import "./key-pad.js";
|
||||
import "./display-output.js";
|
||||
|
||||
@customElement("app-calculator")
|
||||
export class AppCalculator extends LitElement {
|
||||
@property()
|
||||
width = "500px";
|
||||
|
||||
@property()
|
||||
primaryColor = "#1a1f2c";
|
||||
|
||||
@property()
|
||||
accentColor = "black";
|
||||
|
||||
static styles = css`
|
||||
:host {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
min-height: 500px;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
#base {
|
||||
border: 3px solid #1a1b1f;
|
||||
border-radius: 20px;
|
||||
padding: 24px;
|
||||
background: #2a2c33;
|
||||
box-shadow:
|
||||
0 25px 50px -12px rgba(0, 0, 0, 0.7),
|
||||
inset 0 1px 0 rgba(255, 255, 255, 0.15);
|
||||
box-sizing: border-box;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 18px;
|
||||
}
|
||||
|
||||
#actions {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(5, 1fr);
|
||||
gap: 12px;
|
||||
padding: 0 5px;
|
||||
}
|
||||
|
||||
button {
|
||||
background: #3e424b;
|
||||
border: none;
|
||||
border-bottom: 3px solid #1a1b1f;
|
||||
color: #fff;
|
||||
padding: 12px;
|
||||
font-size: 18px;
|
||||
font-weight: 700;
|
||||
border-radius: 8px;
|
||||
cursor: pointer;
|
||||
transition: all 0.08s ease;
|
||||
box-shadow: 0 4px 6px rgba(0,0,0,0.3);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
button:hover {
|
||||
background: #4a4f59;
|
||||
}
|
||||
|
||||
button:active {
|
||||
transform: translateY(2px);
|
||||
border-bottom-width: 1px;
|
||||
box-shadow: 0 1px 3px rgba(0,0,0,0.2);
|
||||
}
|
||||
|
||||
button.operator {
|
||||
background: #e76f51;
|
||||
border-bottom-color: #9d3f27;
|
||||
color: #ffffff;
|
||||
}
|
||||
|
||||
button.operator:hover {
|
||||
background: #f4a261;
|
||||
}
|
||||
|
||||
button.clear-btn {
|
||||
background: #d62828;
|
||||
border-bottom-color: #7a1212;
|
||||
color: #ffffff;
|
||||
}
|
||||
|
||||
button.clear-btn:hover {
|
||||
background: #e63946;
|
||||
}
|
||||
|
||||
button:disabled {
|
||||
opacity: 0.6;
|
||||
cursor: not-allowed;
|
||||
transform: none !important;
|
||||
border-bottom-width: 3px !important;
|
||||
box-shadow: 0 4px 6px rgba(0,0,0,0.3) !important;
|
||||
}
|
||||
`;
|
||||
|
||||
connectedCallback() {
|
||||
super.connectedCallback();
|
||||
// @ts-ignore
|
||||
window.addEventListener("select-number", this._handleNumber);
|
||||
}
|
||||
disconnectedCallback() {
|
||||
// @ts-ignore
|
||||
window.removeEventListener("select-number", this._handleNumber);
|
||||
super.disconnectedCallback();
|
||||
}
|
||||
|
||||
_staging: string = "";
|
||||
_value: string = "";
|
||||
_mode: string = "";
|
||||
|
||||
public get mode(): string {
|
||||
return this._mode;
|
||||
}
|
||||
|
||||
public set mode(v: string) {
|
||||
const oldMode = this._mode;
|
||||
const oldVal = this.value;
|
||||
this._mode = v;
|
||||
|
||||
// @ts-ignore
|
||||
this.requestUpdate("mode", oldMode);
|
||||
// @ts-ignore
|
||||
this.requestUpdate("value", oldVal);
|
||||
}
|
||||
|
||||
public get value(): string {
|
||||
if (this.mode === "") return this._value;
|
||||
return this._staging;
|
||||
}
|
||||
|
||||
public set value(v: string) {
|
||||
var oldVal = "";
|
||||
if (this.mode === "") {
|
||||
oldVal = this._value;
|
||||
this._value = v;
|
||||
} else {
|
||||
oldVal = this._staging;
|
||||
this._staging = v;
|
||||
}
|
||||
// @ts-ignore
|
||||
this.requestUpdate("value", oldVal);
|
||||
}
|
||||
|
||||
public get displayValue(): string {
|
||||
if (this.mode !== "" && this._staging === "") {
|
||||
return this._value || "0";
|
||||
}
|
||||
return this.value || "0";
|
||||
}
|
||||
|
||||
_handleNumber = (event: CustomEvent) => {
|
||||
const num = event.detail.value;
|
||||
event.stopPropagation();
|
||||
if (num === "=") {
|
||||
this.calculate();
|
||||
return;
|
||||
}
|
||||
if (this.value === "0") {
|
||||
this.value = "";
|
||||
}
|
||||
if (num === "." && this.value.includes(".")) {
|
||||
return;
|
||||
}
|
||||
this.value += num;
|
||||
};
|
||||
|
||||
clear = () => {
|
||||
this.value = "";
|
||||
this._staging = "";
|
||||
this._value = "";
|
||||
this.mode = "";
|
||||
};
|
||||
|
||||
calculate = () => {
|
||||
console.log("DEBUG: calculate entered! mode =", this.mode, "_value =", this._value, "_staging =", this._staging);
|
||||
if (this.mode === "") return;
|
||||
|
||||
const op1 = Number(this._value || "0");
|
||||
const op2 = Number(this._staging || "0");
|
||||
let result: number = 0;
|
||||
|
||||
switch (this.mode) {
|
||||
case "+":
|
||||
result = op1 + op2;
|
||||
break;
|
||||
case "-":
|
||||
result = op1 - op2;
|
||||
break;
|
||||
case "/":
|
||||
result = op2 === 0 ? 0 : op1 / op2;
|
||||
break;
|
||||
case "*":
|
||||
result = op1 * op2;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
const output = result.toString();
|
||||
console.log("DEBUG: calculation result =", result, "output =", output);
|
||||
this._value = output;
|
||||
this._staging = "";
|
||||
this.mode = "";
|
||||
};
|
||||
|
||||
render() {
|
||||
return html`<div
|
||||
id="base"
|
||||
style="width: ${this.width};"
|
||||
>
|
||||
<display-output
|
||||
value="${this.displayValue}"
|
||||
color="#8fa882"
|
||||
textColor="#172412"
|
||||
></display-output>
|
||||
<div id="actions">
|
||||
<button
|
||||
class="clear-btn"
|
||||
@click=${this.clear}
|
||||
?disabled=${this._value === "" && this._staging === "" && this.mode === ""}
|
||||
>
|
||||
C
|
||||
</button>
|
||||
<button class="operator" @click="${() => (this.mode = "+")}">+</button>
|
||||
<button class="operator" @click="${() => (this.mode = "-")}">-</button>
|
||||
<button class="operator" @click="${() => (this.mode = "/")}">/</button>
|
||||
<button class="operator" @click="${() => (this.mode = "*")}">*</button>
|
||||
</div>
|
||||
<key-pad accentColor="#1b1c20" textColor="#f0eedb" actionColor="#f4a261" actionTextColor="#ffffff"></key-pad>
|
||||
</div>`;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
import { LitElement, html, css } from "lit";
|
||||
import { customElement, property } from "lit/decorators.js";
|
||||
|
||||
@customElement("display-output")
|
||||
export class CalcDisplay extends LitElement {
|
||||
@property()
|
||||
color = "black";
|
||||
|
||||
@property()
|
||||
textColor = "white";
|
||||
|
||||
@property()
|
||||
value = "";
|
||||
|
||||
static styles = css`
|
||||
@import url('https://fonts.googleapis.com/css2?family=Share+Tech+Mono&display=swap');
|
||||
|
||||
#base {
|
||||
text-align: end;
|
||||
display: block;
|
||||
padding: 16px 20px;
|
||||
border: 3px solid #1a1b1f;
|
||||
border-radius: 8px;
|
||||
box-shadow:
|
||||
inset 0 4px 10px rgba(0, 0, 0, 0.4),
|
||||
0 1px 0 rgba(255, 255, 255, 0.08);
|
||||
position: relative;
|
||||
overflow: hidden;
|
||||
box-sizing: border-box;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
/* Glossy vintage glass glare */
|
||||
#base::after {
|
||||
content: "";
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
height: 50%;
|
||||
background: linear-gradient(
|
||||
to bottom,
|
||||
rgba(255, 255, 255, 0.12),
|
||||
rgba(255, 255, 255, 0)
|
||||
);
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
#text-display {
|
||||
font-family: 'Share Tech Mono', 'Courier New', monospace;
|
||||
font-weight: 700;
|
||||
font-size: 40px;
|
||||
letter-spacing: 2px;
|
||||
text-shadow: 1px 1px 0 rgba(255, 255, 255, 0.12);
|
||||
margin: 0;
|
||||
line-height: 1.1;
|
||||
word-wrap: break-word;
|
||||
word-break: break-all;
|
||||
}
|
||||
`;
|
||||
|
||||
// Render element DOM by returning a `lit-html` template.
|
||||
render() {
|
||||
return html`<div id="base" style="background-color: ${this.color};">
|
||||
<div id="text-display" style="color: ${this.textColor};">
|
||||
${this.value || "0"}
|
||||
</div>
|
||||
</div>`;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<link rel="stylesheet" href="style.css" />
|
||||
<title>Lit Calculator</title>
|
||||
</head>
|
||||
<body>
|
||||
<app-calculator
|
||||
width="350px"
|
||||
primaryColor="red"
|
||||
accentColor="blue"
|
||||
></app-calculator>
|
||||
<script type="module" src="./calculator.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,96 @@
|
||||
import { LitElement, html, css } from "lit";
|
||||
import { customElement, property } from "lit/decorators.js";
|
||||
|
||||
@customElement("key-pad")
|
||||
export class CalcKeyPad extends LitElement {
|
||||
@property()
|
||||
accentColor = "blue";
|
||||
|
||||
@property()
|
||||
textColor = "white";
|
||||
|
||||
@property()
|
||||
actionColor = "black";
|
||||
|
||||
@property()
|
||||
actionTextColor = "white";
|
||||
|
||||
static styles = css`
|
||||
#base {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(3, 1fr);
|
||||
gap: 12px;
|
||||
padding: 0 5px;
|
||||
}
|
||||
|
||||
keypad-input {
|
||||
display: flex;
|
||||
}
|
||||
`;
|
||||
|
||||
// Render element DOM by returning a `lit-html` template.
|
||||
render() {
|
||||
return html` <div id="base">
|
||||
<keypad-input
|
||||
number="7"
|
||||
color="${this.accentColor}"
|
||||
textColor="${this.textColor}"
|
||||
></keypad-input>
|
||||
<keypad-input
|
||||
number="8"
|
||||
color="${this.accentColor}"
|
||||
textColor="${this.textColor}"
|
||||
></keypad-input>
|
||||
<keypad-input
|
||||
number="9"
|
||||
color="${this.accentColor}"
|
||||
textColor="${this.textColor}"
|
||||
></keypad-input>
|
||||
<keypad-input
|
||||
number="4"
|
||||
color="${this.accentColor}"
|
||||
textColor="${this.textColor}"
|
||||
></keypad-input>
|
||||
<keypad-input
|
||||
number="5"
|
||||
color="${this.accentColor}"
|
||||
textColor="${this.textColor}"
|
||||
></keypad-input>
|
||||
<keypad-input
|
||||
number="6"
|
||||
color="${this.accentColor}"
|
||||
textColor="${this.textColor}"
|
||||
></keypad-input>
|
||||
<keypad-input
|
||||
number="1"
|
||||
color="${this.accentColor}"
|
||||
textColor="${this.textColor}"
|
||||
></keypad-input>
|
||||
<keypad-input
|
||||
number="2"
|
||||
color="${this.accentColor}"
|
||||
textColor="${this.textColor}"
|
||||
></keypad-input>
|
||||
<keypad-input
|
||||
number="3"
|
||||
color="${this.accentColor}"
|
||||
textColor="${this.textColor}"
|
||||
></keypad-input>
|
||||
<keypad-input
|
||||
number="0"
|
||||
color="${this.accentColor}"
|
||||
textColor="${this.textColor}"
|
||||
></keypad-input>
|
||||
<keypad-input
|
||||
number="."
|
||||
color="${this.accentColor}"
|
||||
textColor="${this.textColor}"
|
||||
></keypad-input>
|
||||
<keypad-input
|
||||
number="="
|
||||
color="${this.actionColor}"
|
||||
textColor="${this.actionTextColor}"
|
||||
></keypad-input>
|
||||
</div>`;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
import { LitElement, html, css } from "lit";
|
||||
import { customElement, property } from "lit/decorators.js";
|
||||
|
||||
@customElement("keypad-input")
|
||||
export class CalcNumber extends LitElement {
|
||||
@property()
|
||||
number = "0";
|
||||
|
||||
@property()
|
||||
color = "black";
|
||||
|
||||
@property()
|
||||
textColor = "white";
|
||||
|
||||
static styles = css`
|
||||
#base {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
border-radius: 50%;
|
||||
text-align: center;
|
||||
border: none;
|
||||
border-bottom: 3px solid rgba(0, 0, 0, 0.45);
|
||||
font-weight: 700;
|
||||
font-size: 20px;
|
||||
cursor: pointer;
|
||||
aspect-ratio: 1;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
transition: all 0.08s ease;
|
||||
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.25);
|
||||
outline: none;
|
||||
box-sizing: border-box;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
#base:hover {
|
||||
filter: brightness(1.1);
|
||||
}
|
||||
|
||||
#base:active {
|
||||
transform: translateY(2px);
|
||||
border-bottom-width: 1px;
|
||||
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
|
||||
}
|
||||
`;
|
||||
|
||||
_onTap(e: Event) {
|
||||
e.preventDefault();
|
||||
|
||||
const event = new CustomEvent("select-number", {
|
||||
detail: { value: this.number } as any,
|
||||
bubbles: true,
|
||||
cancelable: true,
|
||||
});
|
||||
|
||||
// @ts-ignore
|
||||
window.dispatchEvent(event);
|
||||
}
|
||||
|
||||
// Render element DOM by returning a `lit-html` template.
|
||||
render() {
|
||||
return html` <button
|
||||
@click="${this._onTap}"
|
||||
id="base"
|
||||
style="background-color: ${this.color}; color: ${this.textColor};"
|
||||
>
|
||||
${this.number}
|
||||
</button>`;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
body {
|
||||
margin: 0px;
|
||||
padding: 0px;
|
||||
font-family: sans-serif;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
height: 100vh;
|
||||
background-color: #f0f2f5;
|
||||
}
|
||||
Reference in New Issue
Block a user