|
| 1 | +import { colors } from "./preach.js"; |
| 2 | +import { kebabCase } from "https://esm.sh/change-case@5.4.4"; |
| 3 | +import Toastify from "https://esm.sh/toastify-js@1.12.0"; |
| 4 | + |
| 5 | +const copyCSSBtn = document.getElementById("copy-css"); |
| 6 | +const palette = document.getElementById("palette"); |
| 7 | + |
| 8 | +const cssStyles = await fetch("/preach.css").then((d) => d.text()); |
| 9 | + |
| 10 | +copyCSSBtn.addEventListener("click", () => { |
| 11 | + copy(cssStyles); |
| 12 | +}); |
| 13 | + |
| 14 | +for (let colorKey in colors) { |
| 15 | + const color = colors[colorKey]; |
| 16 | + const div = document.createElement("div"); |
| 17 | + div.classList.add("card"); |
| 18 | + div.dataset.color = `${color}/${colorKey}`; |
| 19 | + div.style.background = `var(--preach--${kebabCase(colorKey)})`; |
| 20 | + if (colorKey === "text") { |
| 21 | + div.style.color = `var(--preach--surface)`; |
| 22 | + } else if (colorKey.endsWith("Light")) { |
| 23 | + div.style.color = `var(--preach--${kebabCase(colorKey.replace(/Light$/, ""))})`; |
| 24 | + } else if (colors[colorKey + "Light"]) { |
| 25 | + div.style.color = `var(--preach--${kebabCase(colorKey + "Light")})`; |
| 26 | + } else { |
| 27 | + div.style.color = "var(--preach-text)"; |
| 28 | + } |
| 29 | + |
| 30 | + palette.appendChild(div); |
| 31 | +} |
| 32 | + |
| 33 | +const colorCards = palette.querySelectorAll(".card"); |
| 34 | +for (let index = 0; index < colorCards.length; index++) { |
| 35 | + const element = colorCards[index]; |
| 36 | + |
| 37 | + element.addEventListener("click", () => { |
| 38 | + const color = element.getAttribute("data-color").split("/")[0]; |
| 39 | + copy(color); |
| 40 | + }); |
| 41 | +} |
| 42 | + |
| 43 | +function copy(toCopy) { |
| 44 | + navigator.clipboard.writeText(toCopy).then(() => { |
| 45 | + toast(`Copied to clipboard`); |
| 46 | + }); |
| 47 | +} |
| 48 | + |
| 49 | +function toast(msg) { |
| 50 | + Toastify({ |
| 51 | + text: msg, |
| 52 | + duration: 3000, |
| 53 | + close: false, |
| 54 | + gravity: "top", |
| 55 | + className: "preach-toast", |
| 56 | + position: "right", |
| 57 | + }).showToast(); |
| 58 | +} |
0 commit comments