HF-Radio-MultiSource / static /action-feedback.js
fffiloni's picture
Add confirmed Banger, vote and report feedback animations
bb8dbe6 verified
Raw History Blame Contribute Delete
5.74 kB
(() => {
"use strict";
const colors = { banger: "#d5b0ff", up: "#9edfc5", down: "#e7a0a7", report: "#edc585" };
const labels = { banger: "BANGER!", up: "Upvote recorded", down: "Downvote recorded", report: "Report received" };
const active = new Map();
const reducedMotion = window.matchMedia("(prefers-reduced-motion: reduce)");
let announcement;
function clear() {
for (const effect of Array.from(active.values())) effect.dispose();
if (announcement) announcement.textContent = "";
}
function play(button, kind) {
if (!button || !colors[kind] || !button.isConnected) return;
active.get(button)?.dispose();
if (!announcement) {
announcement = document.createElement("div");
announcement.className = "radio-feedback-announcement";
announcement.setAttribute("role", "status");
announcement.setAttribute("aria-live", "polite");
announcement.setAttribute("aria-atomic", "true");
document.body.appendChild(announcement);
}
announcement.textContent = labels[kind];
const rect = button.getBoundingClientRect();
const layer = document.createElement("div");
layer.className = "radio-action-feedback";
layer.setAttribute("aria-hidden", "true");
layer.style.setProperty("--feedback-color", colors[kind]);
Object.assign(layer.style, { left: `${rect.left}px`, top: `${rect.top}px`, width: `${rect.width}px`, height: `${rect.height}px` });
document.body.appendChild(layer);
const animations = [];
let timer;
const effect = {
dispose() {
clearTimeout(timer);
animations.forEach(animation => animation.cancel());
layer.remove();
active.delete(button);
},
};
active.set(button, effect);
// Register cleanup before optional animation work: feedback can never block voting.
timer = setTimeout(() => effect.dispose(), kind === "banger" ? 1100 : 1600);
const label = document.createElement("span");
label.className = "radio-feedback-label";
label.textContent = labels[kind];
layer.appendChild(label);
// Keep confirmations inside even the narrowest phone viewport.
const viewport = document.documentElement.clientWidth;
const labelWidth = label.getBoundingClientRect().width;
const labelLeft = Math.max(8, Math.min(rect.left + rect.width / 2 - labelWidth / 2, viewport - labelWidth - 8));
label.style.left = `${labelLeft - rect.left}px`;
if (reducedMotion.matches || typeof button.animate !== "function") return;
const animate = (target, frames, duration) => {
if (target) animations.push(target.animate(frames, { duration, easing: "cubic-bezier(.2,.75,.25,1)", fill: "none" }));
};
const icon = button.querySelector(".radio-action-icon");
if (kind === "banger") {
animate(button, [{ transform: "scale(1)" }, { transform: "scale(.94)", offset: .18 }, { transform: "scale(1.07)", offset: .48 }, { transform: "scale(1)" }], 500);
animate(icon, [{ transform: "scale(1)" }, { transform: "scale(1.5) rotate(-12deg)", offset: .35 }, { transform: "scale(1.12) rotate(6deg)", offset: .65 }, { transform: "scale(1)" }], 650);
} else if (kind === "report") {
animate(icon, [{ transform: "scale(1)" }, { transform: "scale(1.12)" }, { transform: "scale(1)" }], 300);
} else {
const up = kind === "up";
animate(button, [{ transform: "scale(1)" }, { transform: `scale(${up ? 1.035 : .97})` }, { transform: "scale(1)" }], up ? 520 : 420);
animate(icon, [{ transform: "translateY(0)" }, { transform: `translateY(${up ? -9 : 8}px) scale(${up ? 1.16 : 1})`, offset: .4 }, { transform: `translateY(${up ? 2 : -1}px)`, offset: .75 }, { transform: "translateY(0)" }], up ? 520 : 420);
}
if (kind !== "report") {
const ring = document.createElement("span");
ring.className = "radio-feedback-ring";
layer.appendChild(ring);
animate(ring, [{ transform: "scale(1)", opacity: .7 }, { transform: kind === "banger" ? "scale(1.45, 1.9)" : `translateY(${kind === "up" ? -7 : 6}px) scale(1.13)`, opacity: 0 }], kind === "banger" ? 780 : 580);
// Keep the finished ring invisible after its animation releases styles.
ring.style.opacity = "0";
}
const particleCount = kind === "banger" ? 10 : kind === "up" ? 3 : 0;
for (let i = 0; i < particleCount; i++) {
const particle = document.createElement("span");
const heart = kind === "banger" && i % 4 === 0;
particle.className = `radio-feedback-particle${heart ? " heart" : ""}`;
if (heart) particle.textContent = "♥";
if (kind === "banger" && i % 3 === 1) particle.style.color = "#efb69f";
layer.appendChild(particle);
const angle = Math.PI * 2 * i / particleCount;
const x = kind === "up" ? (i - 1) * 11 : Math.cos(angle) * (37 + i % 3 * 9);
const y = kind === "up" ? -24 - i % 2 * 7 : Math.sin(angle) * 30 - 13;
animate(particle, [{ transform: "translate(0, 0) scale(.5)", opacity: 0 }, { opacity: 1, offset: .18 }, { transform: `translate(${x}px, ${y}px) scale(.4)`, opacity: 0 }], kind === "up" ? 520 : 620 + i % 3 * 70);
particle.style.opacity = "0";
}
animate(label, [{ opacity: 0, transform: "translateY(3px)" }, { opacity: 1, transform: "translateY(0)", offset: .15 }, { opacity: 1, offset: .7 }, { opacity: 0, transform: "translateY(-5px)" }], kind === "banger" ? 1050 : 1550);
label.style.opacity = "0";
}
window.RadioActionFeedback = { play, clear };
window.addEventListener("resize", clear, { passive: true });
window.addEventListener("scroll", clear, { passive: true, capture: true });
window.addEventListener("pagehide", clear);
reducedMotion.addEventListener?.("change", clear);
})();