claymorph-ui 0.0.0-stage → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +77 -2
- package/dist/attach.d.ts +115 -0
- package/dist/attach.js +328 -0
- package/dist/attach.js.map +1 -0
- package/dist/clay.css +1648 -0
- package/dist/color.d.ts +5 -0
- package/dist/color.js +44 -0
- package/dist/color.js.map +1 -0
- package/dist/components/action-button.d.ts +31 -0
- package/dist/components/action-button.js +158 -0
- package/dist/components/action-button.js.map +1 -0
- package/dist/components/bars.d.ts +33 -0
- package/dist/components/bars.js +135 -0
- package/dist/components/bars.js.map +1 -0
- package/dist/components/button.d.ts +10 -0
- package/dist/components/button.js +36 -0
- package/dist/components/button.js.map +1 -0
- package/dist/components/card.d.ts +24 -0
- package/dist/components/card.js +139 -0
- package/dist/components/card.js.map +1 -0
- package/dist/components/controls.d.ts +54 -0
- package/dist/components/controls.js +310 -0
- package/dist/components/controls.js.map +1 -0
- package/dist/components/forms.d.ts +41 -0
- package/dist/components/forms.js +266 -0
- package/dist/components/forms.js.map +1 -0
- package/dist/components/group.d.ts +26 -0
- package/dist/components/group.js +180 -0
- package/dist/components/group.js.map +1 -0
- package/dist/components/overlays.d.ts +89 -0
- package/dist/components/overlays.js +383 -0
- package/dist/components/overlays.js.map +1 -0
- package/dist/components/popups.d.ts +55 -0
- package/dist/components/popups.js +308 -0
- package/dist/components/popups.js.map +1 -0
- package/dist/components/slide.d.ts +34 -0
- package/dist/components/slide.js +234 -0
- package/dist/components/slide.js.map +1 -0
- package/dist/components/tabs.d.ts +20 -0
- package/dist/components/tabs.js +105 -0
- package/dist/components/tabs.js.map +1 -0
- package/dist/config.d.ts +48 -0
- package/dist/config.js +50 -0
- package/dist/config.js.map +1 -0
- package/dist/element.d.ts +39 -0
- package/dist/element.js +135 -0
- package/dist/element.js.map +1 -0
- package/dist/engine.d.ts +91 -0
- package/dist/engine.js +454 -0
- package/dist/engine.js.map +1 -0
- package/dist/entries/action-button.d.ts +8 -0
- package/dist/entries/action-button.js +8 -0
- package/dist/entries/action-button.js.map +1 -0
- package/dist/entries/avatar.d.ts +8 -0
- package/dist/entries/avatar.js +8 -0
- package/dist/entries/avatar.js.map +1 -0
- package/dist/entries/badge.d.ts +7 -0
- package/dist/entries/badge.js +7 -0
- package/dist/entries/badge.js.map +1 -0
- package/dist/entries/button-group.d.ts +9 -0
- package/dist/entries/button-group.js +9 -0
- package/dist/entries/button-group.js.map +1 -0
- package/dist/entries/button.d.ts +8 -0
- package/dist/entries/button.js +8 -0
- package/dist/entries/button.js.map +1 -0
- package/dist/entries/card.d.ts +7 -0
- package/dist/entries/card.js +7 -0
- package/dist/entries/card.js.map +1 -0
- package/dist/entries/checkbox.d.ts +9 -0
- package/dist/entries/checkbox.js +9 -0
- package/dist/entries/checkbox.js.map +1 -0
- package/dist/entries/dialog.d.ts +8 -0
- package/dist/entries/dialog.js +8 -0
- package/dist/entries/dialog.js.map +1 -0
- package/dist/entries/input.d.ts +9 -0
- package/dist/entries/input.js +9 -0
- package/dist/entries/input.js.map +1 -0
- package/dist/entries/menu.d.ts +8 -0
- package/dist/entries/menu.js +8 -0
- package/dist/entries/menu.js.map +1 -0
- package/dist/entries/meter.d.ts +8 -0
- package/dist/entries/meter.js +8 -0
- package/dist/entries/meter.js.map +1 -0
- package/dist/entries/popover.d.ts +8 -0
- package/dist/entries/popover.js +8 -0
- package/dist/entries/popover.js.map +1 -0
- package/dist/entries/progress.d.ts +8 -0
- package/dist/entries/progress.js +8 -0
- package/dist/entries/progress.js.map +1 -0
- package/dist/entries/radio.d.ts +9 -0
- package/dist/entries/radio.js +9 -0
- package/dist/entries/radio.js.map +1 -0
- package/dist/entries/segmented.d.ts +8 -0
- package/dist/entries/segmented.js +8 -0
- package/dist/entries/segmented.js.map +1 -0
- package/dist/entries/select.d.ts +8 -0
- package/dist/entries/select.js +8 -0
- package/dist/entries/select.js.map +1 -0
- package/dist/entries/slide-confirm.d.ts +8 -0
- package/dist/entries/slide-confirm.js +8 -0
- package/dist/entries/slide-confirm.js.map +1 -0
- package/dist/entries/slider.d.ts +9 -0
- package/dist/entries/slider.js +9 -0
- package/dist/entries/slider.js.map +1 -0
- package/dist/entries/switch.d.ts +9 -0
- package/dist/entries/switch.js +9 -0
- package/dist/entries/switch.js.map +1 -0
- package/dist/entries/tabs.d.ts +8 -0
- package/dist/entries/tabs.js +8 -0
- package/dist/entries/tabs.js.map +1 -0
- package/dist/entries/toast.d.ts +9 -0
- package/dist/entries/toast.js +8 -0
- package/dist/entries/toast.js.map +1 -0
- package/dist/entries/toggle.d.ts +8 -0
- package/dist/entries/toggle.js +8 -0
- package/dist/entries/toggle.js.map +1 -0
- package/dist/entries/tooltip.d.ts +8 -0
- package/dist/entries/tooltip.js +8 -0
- package/dist/entries/tooltip.js.map +1 -0
- package/dist/highlight.d.ts +26 -0
- package/dist/highlight.js +100 -0
- package/dist/highlight.js.map +1 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.js +37 -0
- package/dist/shared.d.ts +28 -0
- package/dist/shared.js +80 -0
- package/dist/shared.js.map +1 -0
- package/dist/spring.d.ts +14 -0
- package/dist/spring.js +50 -0
- package/dist/spring.js.map +1 -0
- package/dist/surface.d.ts +88 -0
- package/dist/surface.js +240 -0
- package/dist/surface.js.map +1 -0
- package/package.json +140 -4
package/dist/color.d.ts
ADDED
package/dist/color.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// src/clay/color.ts
|
|
2
|
+
var cache = /* @__PURE__ */ new Map();
|
|
3
|
+
var probe;
|
|
4
|
+
function resolveColor(css, fallback = [
|
|
5
|
+
.8,
|
|
6
|
+
.84,
|
|
7
|
+
.88
|
|
8
|
+
]) {
|
|
9
|
+
const key = css.trim();
|
|
10
|
+
if (!key) return fallback;
|
|
11
|
+
const hit = cache.get(key);
|
|
12
|
+
if (hit) return hit;
|
|
13
|
+
if (probe === void 0) {
|
|
14
|
+
const canvas = document.createElement("canvas");
|
|
15
|
+
canvas.width = canvas.height = 1;
|
|
16
|
+
probe = canvas.getContext("2d", { willReadFrequently: true });
|
|
17
|
+
}
|
|
18
|
+
if (!probe) return fallback;
|
|
19
|
+
probe.clearRect(0, 0, 1, 1);
|
|
20
|
+
probe.fillStyle = "#000";
|
|
21
|
+
probe.fillStyle = key;
|
|
22
|
+
probe.fillRect(0, 0, 1, 1);
|
|
23
|
+
const [r, g, b] = probe.getImageData(0, 0, 1, 1).data;
|
|
24
|
+
const rgb = [
|
|
25
|
+
r / 255,
|
|
26
|
+
g / 255,
|
|
27
|
+
b / 255
|
|
28
|
+
];
|
|
29
|
+
if (cache.size > 300) cache.clear();
|
|
30
|
+
cache.set(key, rgb);
|
|
31
|
+
return rgb;
|
|
32
|
+
}
|
|
33
|
+
var luminance = ([r, g, b]) => {
|
|
34
|
+
const lin = (c) => c <= .04045 ? c / 12.92 : ((c + .055) / 1.055) ** 2.4;
|
|
35
|
+
return .2126 * lin(r) + .7152 * lin(g) + .0722 * lin(b);
|
|
36
|
+
};
|
|
37
|
+
var mix = (a, b, t) => [
|
|
38
|
+
a[0] + (b[0] - a[0]) * t,
|
|
39
|
+
a[1] + (b[1] - a[1]) * t,
|
|
40
|
+
a[2] + (b[2] - a[2]) * t
|
|
41
|
+
];
|
|
42
|
+
export { luminance, mix, resolveColor };
|
|
43
|
+
|
|
44
|
+
//# sourceMappingURL=color.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"color.js","names":[],"sources":["../../../src/clay/color.ts"],"sourcesContent":["type RGB = [number, number, number]\n\nconst cache = new Map<string, RGB>()\nlet probe: CanvasRenderingContext2D | null | undefined\n\nfunction resolveColor(css: string, fallback: RGB = [0.8, 0.84, 0.88]): RGB {\n const key = css.trim()\n if (!key) return fallback\n const hit = cache.get(key)\n if (hit) return hit\n if (probe === undefined) {\n const canvas = document.createElement(\"canvas\")\n canvas.width = canvas.height = 1\n probe = canvas.getContext(\"2d\", { willReadFrequently: true })\n }\n if (!probe) return fallback\n probe.clearRect(0, 0, 1, 1)\n probe.fillStyle = \"#000\"\n probe.fillStyle = key\n probe.fillRect(0, 0, 1, 1)\n const [r, g, b] = probe.getImageData(0, 0, 1, 1).data\n const rgb: RGB = [r! / 255, g! / 255, b! / 255]\n if (cache.size > 300) cache.clear()\n cache.set(key, rgb)\n return rgb\n}\n\nconst luminance = ([r, g, b]: RGB) => {\n const lin = (c: number) => (c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4)\n return 0.2126 * lin(r) + 0.7152 * lin(g) + 0.0722 * lin(b)\n}\n\nconst mix = (a: RGB, b: RGB, t: number): RGB => [\n a[0] + (b[0] - a[0]) * t,\n a[1] + (b[1] - a[1]) * t,\n a[2] + (b[2] - a[2]) * t,\n]\n\nexport { luminance, mix, resolveColor, type RGB }\n"],"mappings":";AAEA,IAAM,wBAAQ,IAAI,IAAiB;AACnC,IAAI;AAEJ,SAAS,aAAa,KAAa,WAAgB;CAAC;CAAK;CAAM;AAAI,GAAQ;CACzE,MAAM,MAAM,IAAI,KAAK;CACrB,IAAI,CAAC,KAAK,OAAO;CACjB,MAAM,MAAM,MAAM,IAAI,GAAG;CACzB,IAAI,KAAK,OAAO;CAChB,IAAI,UAAU,KAAA,GAAW;EACvB,MAAM,SAAS,SAAS,cAAc,QAAQ;EAC9C,OAAO,QAAQ,OAAO,SAAS;EAC/B,QAAQ,OAAO,WAAW,MAAM,EAAE,oBAAoB,KAAK,CAAC;CAC9D;CACA,IAAI,CAAC,OAAO,OAAO;CACnB,MAAM,UAAU,GAAG,GAAG,GAAG,CAAC;CAC1B,MAAM,YAAY;CAClB,MAAM,YAAY;CAClB,MAAM,SAAS,GAAG,GAAG,GAAG,CAAC;CACzB,MAAM,CAAC,GAAG,GAAG,KAAK,MAAM,aAAa,GAAG,GAAG,GAAG,CAAC,CAAC,CAAC;CACjD,MAAM,MAAW;EAAC,IAAK;EAAK,IAAK;EAAK,IAAK;CAAG;CAC9C,IAAI,MAAM,OAAO,KAAK,MAAM,MAAM;CAClC,MAAM,IAAI,KAAK,GAAG;CAClB,OAAO;AACT;AAEA,IAAM,aAAa,CAAC,GAAG,GAAG,OAAY;CACpC,MAAM,OAAO,MAAe,KAAK,SAAU,IAAI,UAAU,IAAI,QAAS,UAAU;CAChF,OAAO,QAAS,IAAI,CAAC,IAAI,QAAS,IAAI,CAAC,IAAI,QAAS,IAAI,CAAC;AAC3D;AAEA,IAAM,OAAO,GAAQ,GAAQ,MAAmB;CAC9C,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM;CACvB,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM;CACvB,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM;AACzB"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { ClayElement } from "../element.js";
|
|
2
|
+
import type { SurfaceOptions } from "../surface.js";
|
|
3
|
+
type ActionState = "idle" | "pending" | "success" | "error";
|
|
4
|
+
/**
|
|
5
|
+
* Roll: an async action. Its bead sits apart from the body, melts into it while the
|
|
6
|
+
* action runs, and turns into a check when it lands.
|
|
7
|
+
*/
|
|
8
|
+
declare class ClayActionButton extends ClayElement {
|
|
9
|
+
static observedAttributes: string[];
|
|
10
|
+
protected readonly requires = "button";
|
|
11
|
+
/** Runs when the button is pressed. Its promise decides success or error. */
|
|
12
|
+
action: (() => unknown) | null;
|
|
13
|
+
private state;
|
|
14
|
+
private merge;
|
|
15
|
+
private pulse;
|
|
16
|
+
private shake;
|
|
17
|
+
private bead;
|
|
18
|
+
private reset;
|
|
19
|
+
private status;
|
|
20
|
+
private onAction;
|
|
21
|
+
get actionState(): ActionState;
|
|
22
|
+
protected setup(): void;
|
|
23
|
+
protected teardown(): void;
|
|
24
|
+
private run;
|
|
25
|
+
private announce;
|
|
26
|
+
private setState;
|
|
27
|
+
protected surfaceFor(): Array<[HTMLElement, SurfaceOptions]>;
|
|
28
|
+
private step;
|
|
29
|
+
private shapes;
|
|
30
|
+
}
|
|
31
|
+
export { ClayActionButton, type ActionState };
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { mix } from "../color.js";
|
|
2
|
+
import { SPRINGS } from "../config.js";
|
|
3
|
+
import { ClayElement, NEUTRAL } from "../element.js";
|
|
4
|
+
import { Spring } from "../spring.js";
|
|
5
|
+
// src/clay/components/action-button.ts
|
|
6
|
+
var ICONS = {
|
|
7
|
+
arrow: "<svg data-icon=\"idle\" viewBox=\"0 0 24 24\"><path d=\"M5 12h13M13 6l6 6-6 6\"/></svg>",
|
|
8
|
+
spinner: "<svg data-icon=\"pending\" viewBox=\"0 0 24 24\"><path d=\"M12 3a9 9 0 1 0 9 9\"/></svg>",
|
|
9
|
+
check: "<svg data-icon=\"success\" viewBox=\"0 0 24 24\"><path d=\"M5 12.5l4.5 4.5L19 7\"/></svg>",
|
|
10
|
+
cross: "<svg data-icon=\"error\" viewBox=\"0 0 24 24\"><path d=\"M7 7l10 10M17 7L7 17\"/></svg>"
|
|
11
|
+
};
|
|
12
|
+
var GAP = 6;
|
|
13
|
+
/**
|
|
14
|
+
* Roll: an async action. Its bead sits apart from the body, melts into it while the
|
|
15
|
+
* action runs, and turns into a check when it lands.
|
|
16
|
+
*/
|
|
17
|
+
var ClayActionButton = class extends ClayElement {
|
|
18
|
+
static observedAttributes = ["color", "size"];
|
|
19
|
+
requires = "button";
|
|
20
|
+
/** Runs when the button is pressed. Its promise decides success or error. */
|
|
21
|
+
action = null;
|
|
22
|
+
state = "idle";
|
|
23
|
+
merge = new Spring(0);
|
|
24
|
+
pulse = new Spring(0);
|
|
25
|
+
shake = new Spring(0);
|
|
26
|
+
bead = null;
|
|
27
|
+
reset = 0;
|
|
28
|
+
status = null;
|
|
29
|
+
onAction = (event) => {
|
|
30
|
+
if (this.state === "pending" || !this.action) return;
|
|
31
|
+
const run = this.run(this.action);
|
|
32
|
+
event.detail.waitUntil(run);
|
|
33
|
+
};
|
|
34
|
+
get actionState() {
|
|
35
|
+
return this.state;
|
|
36
|
+
}
|
|
37
|
+
setup() {
|
|
38
|
+
const button = this.querySelector("button");
|
|
39
|
+
const label = button.textContent?.trim() ?? "";
|
|
40
|
+
const text = (state, value) => {
|
|
41
|
+
const span = document.createElement("span");
|
|
42
|
+
span.dataset.label = state;
|
|
43
|
+
span.textContent = value;
|
|
44
|
+
return span;
|
|
45
|
+
};
|
|
46
|
+
const visible = document.createElement("span");
|
|
47
|
+
visible.className = "clay-action-label";
|
|
48
|
+
visible.setAttribute("aria-hidden", "true");
|
|
49
|
+
visible.append(text("idle", label), text("pending", this.getAttribute("pending-label") ?? "Working…"), text("success", this.getAttribute("success-label") ?? "Done"), text("error", this.getAttribute("error-label") ?? "Try again"));
|
|
50
|
+
const name = document.createElement("span");
|
|
51
|
+
name.className = "clay-sr";
|
|
52
|
+
name.textContent = label;
|
|
53
|
+
const bead = document.createElement("span");
|
|
54
|
+
bead.className = "clay-bead";
|
|
55
|
+
bead.setAttribute("aria-hidden", "true");
|
|
56
|
+
bead.innerHTML = Object.values(ICONS).join("");
|
|
57
|
+
button.replaceChildren(visible, name, bead);
|
|
58
|
+
this.status = document.createElement("span");
|
|
59
|
+
this.status.className = "clay-sr";
|
|
60
|
+
this.status.setAttribute("role", "status");
|
|
61
|
+
this.append(this.status);
|
|
62
|
+
const wrapper = this.querySelector("ui-button");
|
|
63
|
+
wrapper.setAttribute("pending-announcement", this.getAttribute("pending-label") ?? "Working");
|
|
64
|
+
wrapper.addEventListener("action", this.onAction);
|
|
65
|
+
this.setState("idle");
|
|
66
|
+
}
|
|
67
|
+
teardown() {
|
|
68
|
+
clearTimeout(this.reset);
|
|
69
|
+
this.querySelector("ui-button")?.removeEventListener("action", this.onAction);
|
|
70
|
+
}
|
|
71
|
+
async run(action) {
|
|
72
|
+
clearTimeout(this.reset);
|
|
73
|
+
this.setState("pending");
|
|
74
|
+
try {
|
|
75
|
+
await action();
|
|
76
|
+
this.setState("success");
|
|
77
|
+
this.announce(this.getAttribute("success-label") ?? "Done");
|
|
78
|
+
this.pulse.v = 9;
|
|
79
|
+
} catch {
|
|
80
|
+
this.setState("error");
|
|
81
|
+
this.announce(this.getAttribute("error-label") ?? "Try again");
|
|
82
|
+
this.shake.v = 160;
|
|
83
|
+
}
|
|
84
|
+
const after = Number(this.getAttribute("reset-after") ?? (this.state === "error" ? 1600 : 2400));
|
|
85
|
+
if (after > 0) this.reset = window.setTimeout(() => this.setState("idle"), after);
|
|
86
|
+
}
|
|
87
|
+
announce(message) {
|
|
88
|
+
if (this.status) this.status.textContent = message;
|
|
89
|
+
}
|
|
90
|
+
setState(state) {
|
|
91
|
+
this.state = state;
|
|
92
|
+
this.dataset.state = state;
|
|
93
|
+
this.dispatchEvent(new CustomEvent("statechange", { detail: { state } }));
|
|
94
|
+
this.invalidate();
|
|
95
|
+
this.surfaces[0]?.wake();
|
|
96
|
+
}
|
|
97
|
+
surfaceFor() {
|
|
98
|
+
return [[this, {
|
|
99
|
+
step: (dt, still) => this.step(dt, still),
|
|
100
|
+
shapes: () => this.shapes()
|
|
101
|
+
}]];
|
|
102
|
+
}
|
|
103
|
+
step(dt, still) {
|
|
104
|
+
const surface = this.surfaces[0];
|
|
105
|
+
const held = this.state === "pending" || this.state === "success" || surface.press.target > 0;
|
|
106
|
+
this.merge.target = held ? 1 : 0;
|
|
107
|
+
let moving = this.merge.step(dt, SPRINGS.morph, still);
|
|
108
|
+
moving = this.pulse.step(dt, {
|
|
109
|
+
omega: 14,
|
|
110
|
+
zeta: .35
|
|
111
|
+
}, still) || moving;
|
|
112
|
+
moving = this.shake.step(dt, {
|
|
113
|
+
omega: 30,
|
|
114
|
+
zeta: .25
|
|
115
|
+
}, still) || moving;
|
|
116
|
+
const body = surface.color("--clay-color", NEUTRAL);
|
|
117
|
+
const goal = this.state === "success" ? surface.color("--clay-success") : this.state === "error" ? surface.color("--clay-danger") : mix(body, [
|
|
118
|
+
.08,
|
|
119
|
+
.12,
|
|
120
|
+
.18
|
|
121
|
+
], .16);
|
|
122
|
+
this.bead = this.bead ? mix(this.bead, goal, still ? 1 : 1 - Math.exp(-dt * 12)) : goal;
|
|
123
|
+
if (Math.abs(this.bead[0] - goal[0]) + Math.abs(this.bead[1] - goal[1]) > .003) moving = true;
|
|
124
|
+
return moving;
|
|
125
|
+
}
|
|
126
|
+
shapes() {
|
|
127
|
+
const surface = this.surfaces[0];
|
|
128
|
+
const { width: w, height: h } = surface;
|
|
129
|
+
const m = this.merge.x;
|
|
130
|
+
const press = surface.press.x;
|
|
131
|
+
const shift = this.shake.x * .02;
|
|
132
|
+
const right = w - h - GAP + (h * .5 + GAP) * m;
|
|
133
|
+
const size = h * (1 + this.pulse.x * .12) - press * 3;
|
|
134
|
+
return [{
|
|
135
|
+
x: shift,
|
|
136
|
+
y: 0,
|
|
137
|
+
w: right,
|
|
138
|
+
h,
|
|
139
|
+
radius: h / 2,
|
|
140
|
+
color: surface.color("--clay-color", NEUTRAL),
|
|
141
|
+
height: 1 - press * .35,
|
|
142
|
+
lift: 1 - press * .5
|
|
143
|
+
}, {
|
|
144
|
+
x: w - h / 2 - size / 2 + shift,
|
|
145
|
+
y: h / 2 - size / 2,
|
|
146
|
+
w: size,
|
|
147
|
+
h: size,
|
|
148
|
+
radius: size / 2,
|
|
149
|
+
color: this.bead ?? NEUTRAL,
|
|
150
|
+
height: 1,
|
|
151
|
+
melt: 3 + 20 * m,
|
|
152
|
+
lift: 1
|
|
153
|
+
}];
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
export { ClayActionButton };
|
|
157
|
+
|
|
158
|
+
//# sourceMappingURL=action-button.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"action-button.js","names":[],"sources":["../../../../src/clay/components/action-button.ts"],"sourcesContent":["import { mix, type RGB } from \"../color\"\nimport { SPRINGS } from \"../config\"\nimport type { ClayShape } from \"../engine\"\nimport { ClayElement, NEUTRAL } from \"../element\"\nimport { Spring } from \"../spring\"\nimport type { SurfaceOptions } from \"../surface\"\n\ntype ActionState = \"idle\" | \"pending\" | \"success\" | \"error\"\n\nconst ICONS = {\n arrow: '<svg data-icon=\"idle\" viewBox=\"0 0 24 24\"><path d=\"M5 12h13M13 6l6 6-6 6\"/></svg>',\n spinner: '<svg data-icon=\"pending\" viewBox=\"0 0 24 24\"><path d=\"M12 3a9 9 0 1 0 9 9\"/></svg>',\n check: '<svg data-icon=\"success\" viewBox=\"0 0 24 24\"><path d=\"M5 12.5l4.5 4.5L19 7\"/></svg>',\n cross: '<svg data-icon=\"error\" viewBox=\"0 0 24 24\"><path d=\"M7 7l10 10M17 7L7 17\"/></svg>',\n}\n\nconst GAP = 6\n\n/**\n * Roll: an async action. Its bead sits apart from the body, melts into it while the\n * action runs, and turns into a check when it lands.\n */\nclass ClayActionButton extends ClayElement {\n static observedAttributes = [\"color\", \"size\"]\n protected readonly requires = \"button\"\n /** Runs when the button is pressed. Its promise decides success or error. */\n action: (() => unknown) | null = null\n private state: ActionState = \"idle\"\n private merge = new Spring(0)\n private pulse = new Spring(0)\n private shake = new Spring(0)\n private bead: RGB | null = null\n private reset = 0\n private status: HTMLElement | null = null\n private onAction = (event: Event) => {\n if (this.state === \"pending\" || !this.action) return\n const run = this.run(this.action)\n ;(event as CustomEvent<{ waitUntil(p: Promise<unknown>): void }>).detail.waitUntil(run)\n }\n\n get actionState() {\n return this.state\n }\n\n protected setup() {\n const button = this.querySelector(\"button\")!\n const label = button.textContent?.trim() ?? \"\"\n const text = (state: ActionState, value: string) => {\n const span = document.createElement(\"span\")\n span.dataset.label = state\n span.textContent = value\n return span\n }\n const visible = document.createElement(\"span\")\n visible.className = \"clay-action-label\"\n visible.setAttribute(\"aria-hidden\", \"true\")\n visible.append(\n text(\"idle\", label),\n text(\"pending\", this.getAttribute(\"pending-label\") ?? \"Working…\"),\n text(\"success\", this.getAttribute(\"success-label\") ?? \"Done\"),\n text(\"error\", this.getAttribute(\"error-label\") ?? \"Try again\")\n )\n const name = document.createElement(\"span\")\n name.className = \"clay-sr\"\n name.textContent = label\n const bead = document.createElement(\"span\")\n bead.className = \"clay-bead\"\n bead.setAttribute(\"aria-hidden\", \"true\")\n bead.innerHTML = Object.values(ICONS).join(\"\")\n button.replaceChildren(visible, name, bead)\n\n this.status = document.createElement(\"span\")\n this.status.className = \"clay-sr\"\n this.status.setAttribute(\"role\", \"status\")\n this.append(this.status)\n\n const wrapper = this.querySelector(\"ui-button\")!\n wrapper.setAttribute(\"pending-announcement\", this.getAttribute(\"pending-label\") ?? \"Working\")\n wrapper.addEventListener(\"action\", this.onAction)\n this.setState(\"idle\")\n }\n\n protected teardown() {\n clearTimeout(this.reset)\n this.querySelector(\"ui-button\")?.removeEventListener(\"action\", this.onAction)\n }\n\n private async run(action: () => unknown) {\n clearTimeout(this.reset)\n this.setState(\"pending\")\n try {\n await action()\n this.setState(\"success\")\n this.announce(this.getAttribute(\"success-label\") ?? \"Done\")\n this.pulse.v = 9\n } catch {\n this.setState(\"error\")\n this.announce(this.getAttribute(\"error-label\") ?? \"Try again\")\n this.shake.v = 160\n }\n const after = Number(this.getAttribute(\"reset-after\") ?? (this.state === \"error\" ? 1600 : 2400))\n if (after > 0) this.reset = window.setTimeout(() => this.setState(\"idle\"), after)\n }\n\n private announce(message: string) {\n if (this.status) this.status.textContent = message\n }\n\n private setState(state: ActionState) {\n this.state = state\n this.dataset.state = state\n this.dispatchEvent(new CustomEvent(\"statechange\", { detail: { state } }))\n this.invalidate()\n this.surfaces[0]?.wake()\n }\n\n protected surfaceFor(): Array<[HTMLElement, SurfaceOptions]> {\n return [[this, { step: (dt, still) => this.step(dt, still), shapes: () => this.shapes() }]]\n }\n\n private step(dt: number, still: boolean) {\n const surface = this.surfaces[0]!\n const held = this.state === \"pending\" || this.state === \"success\" || surface.press.target > 0\n this.merge.target = held ? 1 : 0\n let moving = this.merge.step(dt, SPRINGS.morph, still)\n moving = this.pulse.step(dt, { omega: 14, zeta: 0.35 }, still) || moving\n moving = this.shake.step(dt, { omega: 30, zeta: 0.25 }, still) || moving\n const body = surface.color(\"--clay-color\", NEUTRAL)\n const goal =\n this.state === \"success\"\n ? surface.color(\"--clay-success\")\n : this.state === \"error\"\n ? surface.color(\"--clay-danger\")\n : mix(body, [0.08, 0.12, 0.18], 0.16)\n this.bead = this.bead ? mix(this.bead, goal, still ? 1 : 1 - Math.exp(-dt * 12)) : goal\n if (Math.abs(this.bead[0] - goal[0]) + Math.abs(this.bead[1] - goal[1]) > 0.003) moving = true\n return moving\n }\n\n private shapes(): ClayShape[] {\n const surface = this.surfaces[0]!\n const { width: w, height: h } = surface\n const m = this.merge.x\n const press = surface.press.x\n const shift = this.shake.x * 0.02\n const right = w - h - GAP + (h * 0.5 + GAP) * m\n const size = h * (1 + this.pulse.x * 0.12) - press * 3\n return [\n {\n x: shift,\n y: 0,\n w: right,\n h,\n radius: h / 2,\n color: surface.color(\"--clay-color\", NEUTRAL),\n height: 1 - press * 0.35,\n lift: 1 - press * 0.5,\n },\n {\n x: w - h / 2 - size / 2 + shift,\n y: h / 2 - size / 2,\n w: size,\n h: size,\n radius: size / 2,\n color: this.bead ?? NEUTRAL,\n height: 1,\n melt: 3 + 20 * m,\n lift: 1,\n },\n ]\n }\n}\n\nexport { ClayActionButton, type ActionState }\n"],"mappings":";;;;;AASA,IAAM,QAAQ;CACZ,OAAO;CACP,SAAS;CACT,OAAO;CACP,OAAO;AACT;AAEA,IAAM,MAAM;;;;;AAMZ,IAAM,mBAAN,cAA+B,YAAY;CACzC,OAAO,qBAAqB,CAAC,SAAS,MAAM;CAC5C,WAA8B;;CAE9B,SAAiC;CACjC,QAA6B;CAC7B,QAAgB,IAAI,OAAO,CAAC;CAC5B,QAAgB,IAAI,OAAO,CAAC;CAC5B,QAAgB,IAAI,OAAO,CAAC;CAC5B,OAA2B;CAC3B,QAAgB;CAChB,SAAqC;CACrC,YAAoB,UAAiB;EACnC,IAAI,KAAK,UAAU,aAAa,CAAC,KAAK,QAAQ;EAC9C,MAAM,MAAM,KAAK,IAAI,KAAK,MAAM;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
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { ClayElement } from "../element.js";
|
|
2
|
+
import { type SurfaceOptions } from "../surface.js";
|
|
3
|
+
/** A groove that fills with a worm of clay, for progress and meters. */
|
|
4
|
+
declare abstract class ClayBar extends ClayElement {
|
|
5
|
+
static observedAttributes: string[];
|
|
6
|
+
protected abstract readonly kind: "progress" | "meter";
|
|
7
|
+
private fill;
|
|
8
|
+
private placed;
|
|
9
|
+
private time;
|
|
10
|
+
private still;
|
|
11
|
+
private track;
|
|
12
|
+
private stop;
|
|
13
|
+
private get root();
|
|
14
|
+
private get vertical();
|
|
15
|
+
private get indeterminate();
|
|
16
|
+
/** The filled share, from the ARIA the ui-sdk part keeps up to date. */
|
|
17
|
+
get fraction(): number;
|
|
18
|
+
protected setup(): void;
|
|
19
|
+
protected teardown(): void;
|
|
20
|
+
protected surfaceFor(): Array<[HTMLElement, SurfaceOptions]>;
|
|
21
|
+
private shapes;
|
|
22
|
+
}
|
|
23
|
+
/** Fill: a progress bar whose groove fills with clay. Without a value, a lump rolls back and forth. */
|
|
24
|
+
declare class ClayProgress extends ClayBar {
|
|
25
|
+
protected readonly requires = "ui-progress-track";
|
|
26
|
+
protected readonly kind: "progress";
|
|
27
|
+
}
|
|
28
|
+
/** Gauge: a meter, the same groove for a measured value, across or upright. */
|
|
29
|
+
declare class ClayMeter extends ClayBar {
|
|
30
|
+
protected readonly requires = "ui-meter-track";
|
|
31
|
+
protected readonly kind: "meter";
|
|
32
|
+
}
|
|
33
|
+
export { ClayMeter, ClayProgress };
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { SPRINGS } from "../config.js";
|
|
2
|
+
import { ClayElement, NEUTRAL } from "../element.js";
|
|
3
|
+
import { clamp, groove, watch } from "../shared.js";
|
|
4
|
+
import { Spring } from "../spring.js";
|
|
5
|
+
import { offsetWithin } from "../surface.js";
|
|
6
|
+
// src/clay/components/bars.ts
|
|
7
|
+
var number = (value, fallback) => {
|
|
8
|
+
const parsed = value === null ? NaN : Number(value);
|
|
9
|
+
return Number.isFinite(parsed) ? parsed : fallback;
|
|
10
|
+
};
|
|
11
|
+
/** A groove that fills with a worm of clay, for progress and meters. */
|
|
12
|
+
var ClayBar = class extends ClayElement {
|
|
13
|
+
static observedAttributes = ["color", "orientation"];
|
|
14
|
+
fill = new Spring(0);
|
|
15
|
+
placed = false;
|
|
16
|
+
time = 0;
|
|
17
|
+
still = false;
|
|
18
|
+
track = {
|
|
19
|
+
x: 0,
|
|
20
|
+
y: 0,
|
|
21
|
+
w: 0,
|
|
22
|
+
h: 0
|
|
23
|
+
};
|
|
24
|
+
stop = null;
|
|
25
|
+
get root() {
|
|
26
|
+
return this.querySelector(`ui-${this.kind}`);
|
|
27
|
+
}
|
|
28
|
+
get vertical() {
|
|
29
|
+
return this.getAttribute("orientation") === "vertical";
|
|
30
|
+
}
|
|
31
|
+
get indeterminate() {
|
|
32
|
+
return this.root.hasAttribute("data-indeterminate");
|
|
33
|
+
}
|
|
34
|
+
/** The filled share, from the ARIA the ui-sdk part keeps up to date. */
|
|
35
|
+
get fraction() {
|
|
36
|
+
const root = this.root;
|
|
37
|
+
const min = number(root.getAttribute("aria-valuemin"), 0);
|
|
38
|
+
const max = number(root.getAttribute("aria-valuemax"), 100);
|
|
39
|
+
const now = number(root.getAttribute("aria-valuenow"), min);
|
|
40
|
+
return max > min ? clamp((now - min) / (max - min), 0, 1) : 0;
|
|
41
|
+
}
|
|
42
|
+
setup() {
|
|
43
|
+
const sync = () => {
|
|
44
|
+
this.style.setProperty("--clay-fill", `${(this.fraction * 100).toFixed(2)}%`);
|
|
45
|
+
this.toggleAttribute("data-complete", !this.indeterminate && this.fraction >= 1);
|
|
46
|
+
this.invalidate();
|
|
47
|
+
};
|
|
48
|
+
this.stop = watch(this.root, [
|
|
49
|
+
"aria-valuenow",
|
|
50
|
+
"aria-valuemax",
|
|
51
|
+
"aria-valuemin",
|
|
52
|
+
"data-indeterminate"
|
|
53
|
+
], sync);
|
|
54
|
+
sync();
|
|
55
|
+
}
|
|
56
|
+
teardown() {
|
|
57
|
+
this.stop?.();
|
|
58
|
+
}
|
|
59
|
+
surfaceFor() {
|
|
60
|
+
return [[this, {
|
|
61
|
+
swell: false,
|
|
62
|
+
pad: 22,
|
|
63
|
+
measure: () => {
|
|
64
|
+
this.track = offsetWithin(this.querySelector(`ui-${this.kind}-track`), this);
|
|
65
|
+
this.fill.target = this.fraction;
|
|
66
|
+
if (!this.placed) {
|
|
67
|
+
this.fill.set(this.fraction);
|
|
68
|
+
this.placed = true;
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
step: (dt, still) => {
|
|
72
|
+
this.still = still;
|
|
73
|
+
const moving = this.fill.step(dt, SPRINGS.settle, still);
|
|
74
|
+
if (!this.indeterminate || still) return moving;
|
|
75
|
+
this.time += dt;
|
|
76
|
+
return true;
|
|
77
|
+
},
|
|
78
|
+
shapes: () => this.shapes()
|
|
79
|
+
}]];
|
|
80
|
+
}
|
|
81
|
+
shapes() {
|
|
82
|
+
const surface = this.surfaces[0];
|
|
83
|
+
const t = this.track;
|
|
84
|
+
const inset = Math.min(6, Math.max(t.w, t.h) / 4);
|
|
85
|
+
const shapes = groove(surface, t, inset, Math.min(t.w, t.h) / 2 + inset);
|
|
86
|
+
const thick = this.vertical ? t.w : t.h;
|
|
87
|
+
const length = this.vertical ? t.h : t.w;
|
|
88
|
+
const pad = 2;
|
|
89
|
+
let start = 0;
|
|
90
|
+
let span = clamp(this.fill.x, 0, 1.05) * length;
|
|
91
|
+
if (this.indeterminate) {
|
|
92
|
+
const still = this.still;
|
|
93
|
+
span = still ? length : length * .36;
|
|
94
|
+
const travel = length - span;
|
|
95
|
+
start = still ? 0 : travel * (.5 - .5 * Math.cos(this.time * 2.4));
|
|
96
|
+
}
|
|
97
|
+
if (span < .5) return shapes;
|
|
98
|
+
span = Math.max(span, thick - 4);
|
|
99
|
+
const color = surface.color("--clay-color", NEUTRAL);
|
|
100
|
+
const r = (thick - 4) / 2;
|
|
101
|
+
shapes.push(this.vertical ? {
|
|
102
|
+
x: t.x + pad,
|
|
103
|
+
y: t.y + t.h - start - span,
|
|
104
|
+
w: t.w - 4,
|
|
105
|
+
h: span,
|
|
106
|
+
radius: r,
|
|
107
|
+
color,
|
|
108
|
+
height: .75,
|
|
109
|
+
lift: .15
|
|
110
|
+
} : {
|
|
111
|
+
x: t.x + pad + start,
|
|
112
|
+
y: t.y + pad,
|
|
113
|
+
w: Math.min(span, t.w - 4),
|
|
114
|
+
h: t.h - 4,
|
|
115
|
+
radius: r,
|
|
116
|
+
color,
|
|
117
|
+
height: .75,
|
|
118
|
+
lift: .15
|
|
119
|
+
});
|
|
120
|
+
return shapes;
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
/** Fill: a progress bar whose groove fills with clay. Without a value, a lump rolls back and forth. */
|
|
124
|
+
var ClayProgress = class extends ClayBar {
|
|
125
|
+
requires = "ui-progress-track";
|
|
126
|
+
kind = "progress";
|
|
127
|
+
};
|
|
128
|
+
/** Gauge: a meter, the same groove for a measured value, across or upright. */
|
|
129
|
+
var ClayMeter = class extends ClayBar {
|
|
130
|
+
requires = "ui-meter-track";
|
|
131
|
+
kind = "meter";
|
|
132
|
+
};
|
|
133
|
+
export { ClayMeter, ClayProgress };
|
|
134
|
+
|
|
135
|
+
//# sourceMappingURL=bars.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bars.js","names":[],"sources":["../../../../src/clay/components/bars.ts"],"sourcesContent":["import { SPRINGS } from \"../config\"\nimport type { ClayShape } from \"../engine\"\nimport { ClayElement, NEUTRAL } from \"../element\"\nimport { clamp, groove, watch } from \"../shared\"\nimport { Spring } from \"../spring\"\nimport { offsetWithin, type SurfaceOptions } from \"../surface\"\n\nconst number = (value: string | null, fallback: number) => {\n const parsed = value === null ? NaN : Number(value)\n return Number.isFinite(parsed) ? parsed : fallback\n}\n\n/** A groove that fills with a worm of clay, for progress and meters. */\nabstract class ClayBar extends ClayElement {\n static observedAttributes = [\"color\", \"orientation\"]\n protected abstract readonly kind: \"progress\" | \"meter\"\n private fill = new Spring(0)\n private placed = false\n private time = 0\n private still = false\n private track = { x: 0, y: 0, w: 0, h: 0 }\n private stop: (() => void) | null = null\n\n private get root() {\n return this.querySelector<HTMLElement>(`ui-${this.kind}`)!\n }\n\n private get vertical() {\n return this.getAttribute(\"orientation\") === \"vertical\"\n }\n\n private get indeterminate() {\n return this.root.hasAttribute(\"data-indeterminate\")\n }\n\n /** The filled share, from the ARIA the ui-sdk part keeps up to date. */\n get fraction() {\n const root = this.root\n const min = number(root.getAttribute(\"aria-valuemin\"), 0)\n const max = number(root.getAttribute(\"aria-valuemax\"), 100)\n const now = number(root.getAttribute(\"aria-valuenow\"), min)\n return max > min ? clamp((now - min) / (max - min), 0, 1) : 0\n }\n\n protected setup() {\n const sync = () => {\n this.style.setProperty(\"--clay-fill\", `${(this.fraction * 100).toFixed(2)}%`)\n this.toggleAttribute(\"data-complete\", !this.indeterminate && this.fraction >= 1)\n this.invalidate()\n }\n this.stop = watch(this.root, [\"aria-valuenow\", \"aria-valuemax\", \"aria-valuemin\", \"data-indeterminate\"], sync)\n sync()\n }\n\n protected teardown() {\n this.stop?.()\n }\n\n protected surfaceFor(): Array<[HTMLElement, SurfaceOptions]> {\n return [\n [\n this,\n {\n swell: false,\n pad: 22,\n measure: () => {\n this.track = offsetWithin(this.querySelector(`ui-${this.kind}-track`)!, this)\n this.fill.target = this.fraction\n if (!this.placed) {\n this.fill.set(this.fraction)\n this.placed = true\n }\n },\n step: (dt, still) => {\n this.still = still\n const moving = this.fill.step(dt, SPRINGS.settle, still)\n if (!this.indeterminate || still) return moving\n this.time += dt\n return true\n },\n shapes: () => this.shapes(),\n },\n ],\n ]\n }\n\n private shapes(): ClayShape[] {\n const surface = this.surfaces[0]!\n const t = this.track\n const inset = Math.min(6, Math.max(t.w, t.h) / 4)\n const shapes = groove(surface, t, inset, Math.min(t.w, t.h) / 2 + inset)\n const thick = this.vertical ? t.w : t.h\n const length = this.vertical ? t.h : t.w\n const pad = 2\n let start = 0\n let span = clamp(this.fill.x, 0, 1.05) * length\n if (this.indeterminate) {\n const still = this.still\n span = still ? length : length * 0.36\n const travel = length - span\n start = still ? 0 : travel * (0.5 - 0.5 * Math.cos(this.time * 2.4))\n }\n if (span < 0.5) return shapes\n span = Math.max(span, thick - pad * 2)\n const color = surface.color(\"--clay-color\", NEUTRAL)\n const r = (thick - pad * 2) / 2\n shapes.push(\n this.vertical\n ? { x: t.x + pad, y: t.y + t.h - start - span, w: t.w - pad * 2, h: span, radius: r, color, height: 0.75, lift: 0.15 }\n : { x: t.x + pad + start, y: t.y + pad, w: Math.min(span, t.w - pad * 2), h: t.h - pad * 2, radius: r, color, height: 0.75, lift: 0.15 }\n )\n return shapes\n }\n}\n\n/** Fill: a progress bar whose groove fills with clay. Without a value, a lump rolls back and forth. */\nclass ClayProgress extends ClayBar {\n protected readonly requires = \"ui-progress-track\"\n protected readonly kind = \"progress\" as const\n}\n\n/** Gauge: a meter, the same groove for a measured value, across or upright. */\nclass ClayMeter extends ClayBar {\n protected readonly requires = \"ui-meter-track\"\n protected readonly kind = \"meter\" as const\n}\n\nexport { ClayMeter, ClayProgress }\n"],"mappings":";;;;;;AAOA,IAAM,UAAU,OAAsB,aAAqB;CACzD,MAAM,SAAS,UAAU,OAAO,MAAM,OAAO,KAAK;CAClD,OAAO,OAAO,SAAS,MAAM,IAAI,SAAS;AAC5C;;AAGA,IAAe,UAAf,cAA+B,YAAY;CACzC,OAAO,qBAAqB,CAAC,SAAS,aAAa;CAEnD,OAAe,IAAI,OAAO,CAAC;CAC3B,SAAiB;CACjB,OAAe;CACf,QAAgB;CAChB,QAAgB;EAAE,GAAG;EAAG,GAAG;EAAG,GAAG;EAAG,GAAG;CAAE;CACzC,OAAoC;CAEpC,IAAY,OAAO;EACjB,OAAO,KAAK,cAA2B,MAAM,KAAK,MAAM;CAC1D;CAEA,IAAY,WAAW;EACrB,OA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B,OAA0B;AAC5B;;AAGA,IAAM,YAAN,cAAwB,QAAQ;CAC9B,WAA8B;CAC9B,OAA0B;AAC5B"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ClayElement } from "../element.js";
|
|
2
|
+
import type { SurfaceOptions } from "../surface.js";
|
|
3
|
+
/** Press: a button that swells under the pointer and squashes when pressed. */
|
|
4
|
+
declare class ClayButton extends ClayElement {
|
|
5
|
+
static observedAttributes: string[];
|
|
6
|
+
protected readonly requires = "button, a";
|
|
7
|
+
protected surfaceFor(): Array<[HTMLElement, SurfaceOptions]>;
|
|
8
|
+
private shapes;
|
|
9
|
+
}
|
|
10
|
+
export { ClayButton };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import "../attach.js";
|
|
2
|
+
import { ClayElement, NEUTRAL } from "../element.js";
|
|
3
|
+
// src/clay/components/button.ts
|
|
4
|
+
/** Press: a button that swells under the pointer and squashes when pressed. */
|
|
5
|
+
var ClayButton = class extends ClayElement {
|
|
6
|
+
static observedAttributes = [
|
|
7
|
+
"color",
|
|
8
|
+
"size",
|
|
9
|
+
"shape"
|
|
10
|
+
];
|
|
11
|
+
requires = "button, a";
|
|
12
|
+
surfaceFor() {
|
|
13
|
+
return [[this, { shapes: () => this.shapes() }]];
|
|
14
|
+
}
|
|
15
|
+
shapes() {
|
|
16
|
+
const surface = this.surfaces[0];
|
|
17
|
+
const { width: w, height: h } = surface;
|
|
18
|
+
const press = surface.press.x;
|
|
19
|
+
const rounded = this.getAttribute("shape") === "rounded";
|
|
20
|
+
const joints = [...this.joints].flatMap((joint) => joint());
|
|
21
|
+
return [{
|
|
22
|
+
x: 0,
|
|
23
|
+
y: 0,
|
|
24
|
+
w,
|
|
25
|
+
h,
|
|
26
|
+
radius: rounded ? Math.min(h * .34, 18) : h / 2,
|
|
27
|
+
color: surface.color("--clay-color", NEUTRAL),
|
|
28
|
+
height: 1 - press * .4,
|
|
29
|
+
lift: 1 + surface.hover.x * .3 - press * .55,
|
|
30
|
+
blend: joints.length ? 9 : void 0
|
|
31
|
+
}, ...joints];
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
export { ClayButton };
|
|
35
|
+
|
|
36
|
+
//# sourceMappingURL=button.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"button.js","names":[],"sources":["../../../../src/clay/components/button.ts"],"sourcesContent":["import { BLEND } from \"../attach\"\nimport type { ClayShape } from \"../engine\"\nimport { ClayElement, NEUTRAL } from \"../element\"\nimport type { SurfaceOptions } from \"../surface\"\n\n/** Press: a button that swells under the pointer and squashes when pressed. */\nclass ClayButton extends ClayElement {\n static observedAttributes = [\"color\", \"size\", \"shape\"]\n protected readonly requires = \"button, a\"\n\n protected surfaceFor(): Array<[HTMLElement, SurfaceOptions]> {\n return [[this, { shapes: () => this.shapes() }]]\n }\n\n private shapes(): ClayShape[] {\n const surface = this.surfaces[0]!\n const { width: w, height: h } = surface\n const press = surface.press.x\n const rounded = this.getAttribute(\"shape\") === \"rounded\"\n const joints = [...this.joints].flatMap((joint) => joint())\n return [\n {\n x: 0,\n y: 0,\n w,\n h,\n radius: rounded ? Math.min(h * 0.34, 18) : h / 2,\n color: surface.color(\"--clay-color\", NEUTRAL),\n height: 1 - press * 0.4,\n lift: 1 + surface.hover.x * 0.3 - press * 0.55,\n blend: joints.length ? BLEND : undefined,\n },\n ...joints,\n ]\n }\n}\n\nexport { ClayButton }\n"],"mappings":";;;;AAMA,IAAM,aAAN,cAAyB,YAAY;CACnC,OAAO,qBAAqB;EAAC;EAAS;EAAQ;CAAO;CACrD,WAA8B;CAE9B,aAA6D;EAC3D,OAAO,CAAC,CAAC,MAAM,EAAE,cAAc,KAAK,OAAO,EAAE,CAAC,CAAC;CACjD;CAEA,SAA8B;EAC5B,MAAM,UAAU,KAAK,SAAS;EAC9B,MAAM,EAAE,OAAO,GAAG,QAAQ,MAAM;EAChC,MAAM,QAAQ,QAAQ,MAAM;EAC5B,MAAM,UAAU,KAAK,aAAa,OAAO,MAAM;EAC/C,MAAM,SAAS,CAAC,GAAG,KAAK,MAAM,CAAC,CAAC,SAAS,UAAU,MAAM,CAAC;EAC1D,OAAO,CACL;GACE,GAAG;GACH,GAAG;GACH;GACA;GACA,QAAQ,UAAU,KAAK,IAAI,IAAI,KAAM,EAAE,IAAI,IAAI;GAC/C,OAAO,QAAQ,MAAM,gBAAgB,OAAO;GAC5C,QAAQ,IAAI,QAAQ;GACpB,MAAM,IAAI,QAAQ,MAAM,IAAI,KAAM,QAAQ;GAC1C,OAAO,OAAO,SAAA,IAAiB,KAAA;EACjC,GACA,GAAG,MACL;CACF;AACF"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { ClayElement } from "../element.js";
|
|
2
|
+
import type { SurfaceOptions } from "../surface.js";
|
|
3
|
+
/** Hold: a soft slab that holds content. `interactive` makes it swell like a button. */
|
|
4
|
+
declare class ClayCard extends ClayElement {
|
|
5
|
+
static observedAttributes: string[];
|
|
6
|
+
protected readonly requires = "";
|
|
7
|
+
attributeChangedCallback(name: string, old: string | null, value: string | null): void;
|
|
8
|
+
protected surfaceFor(): Array<[HTMLElement, SurfaceOptions]>;
|
|
9
|
+
private shapes;
|
|
10
|
+
}
|
|
11
|
+
/** Stamp: a small pressed label. */
|
|
12
|
+
declare class ClayBadge extends ClayElement {
|
|
13
|
+
protected readonly requires = "";
|
|
14
|
+
protected surfaceFor(): Array<[HTMLElement, SurfaceOptions]>;
|
|
15
|
+
private shapes;
|
|
16
|
+
}
|
|
17
|
+
/** Frame: an avatar set in a clay ring, with an optional status bead melted onto it. */
|
|
18
|
+
declare class ClayAvatar extends ClayElement {
|
|
19
|
+
static observedAttributes: string[];
|
|
20
|
+
protected readonly requires = "ui-avatar";
|
|
21
|
+
protected surfaceFor(): Array<[HTMLElement, SurfaceOptions]>;
|
|
22
|
+
private shapes;
|
|
23
|
+
}
|
|
24
|
+
export { ClayAvatar, ClayBadge, ClayCard };
|