Files
anidev-design-system/icons/catalog.js
T

73 lines
2.6 KiB
JavaScript

import { createNotifier } from "../components/toast.js";
import { icons, iconNames, iconSvg } from "./icons.js";
import { tokensFor, cssVars } from "../concepts/registry.js";
const $ = (s) => document.querySelector(s);
const categories = {
navigation: "Навигация",
account: "Аккаунт",
security: "Безопасность",
actions: "Действия",
files: "Файлы",
system: "Система",
status: "Состояния",
direction: "Направления",
};
for (const [id, label] of Object.entries(categories))
$("#category").add(new Option(label, id));
let theme = "light";
try {
theme =
JSON.parse(localStorage.getItem("anidev.preferences") || "{}").theme ===
"dark"
? "dark"
: "light";
} catch {}
function applyTheme() {
document.documentElement.style.cssText = cssVars(tokensFor("dock", theme));
document.body.dataset.theme = theme;
$("#theme").textContent = theme === "dark" ? "Светлая тема" : "Тёмная тема";
$("#theme").setAttribute("aria-pressed", String(theme === "dark"));
}
function render() {
const query = $("#search").value.toLowerCase().trim(),
category = $("#category").value,
size = Number($("#size").value);
const names = iconNames.filter(
(id) =>
(!category || icons[id].category === category) &&
`${id} ${icons[id].label}`.toLowerCase().includes(query),
);
$("#icon-grid").innerHTML = names
.map(
(id) =>
`<button class="icon-tile" type="button" data-icon="${id}" title="${icons[id].label} — копировать SVG" aria-label="${icons[id].label}: копировать SVG ${id}">${iconSvg(id, { size })}<code>${id}</code></button>`,
)
.join("");
$("#count").textContent = `${names.length} / ${iconNames.length}`;
$("#empty").hidden = names.length > 0;
}
$("#search").addEventListener("input", render);
$("#category").addEventListener("change", render);
$("#size").addEventListener("change", render);
$(".library-controls").addEventListener("submit", (e) => e.preventDefault());
$("#theme").addEventListener("click", () => {
theme = theme === "dark" ? "light" : "dark";
applyTheme();
});
const notify = createNotifier($("#toast"));
$("#icon-grid").addEventListener("click", async (e) => {
const tile = e.target.closest("[data-icon]");
if (!tile) return;
try {
await navigator.clipboard.writeText(
iconSvg(tile.dataset.icon, { size: Number($("#size").value) }),
);
notify(`SVG скопирован: ${tile.dataset.icon}`, { type: "success" });
} catch {
notify("Не удалось скопировать. Скачайте SVG-пак.", { type: "error" });
}
});
applyTheme();
render();