58 lines
1.8 KiB
TypeScript
58 lines
1.8 KiB
TypeScript
import { type ClassValue, clsx } from "clsx";
|
|
import { extendTailwindMerge } from "tailwind-merge";
|
|
|
|
import { DESIGN_TOKENS } from "./design-tokens.generated";
|
|
|
|
/**
|
|
* 自定义工具类的 token(字号 / 圆角 / 缓动 / 时长)要告诉 tailwind-merge 它们属于哪一组:
|
|
* 不登记的话 `text-control text-muted-foreground` 会被当成两个颜色类冲突,字号那个被丢掉。
|
|
* 登记表由 scripts/gen-design-tokens.mjs 从 app.css 生成,CI 校验它是最新的。
|
|
*/
|
|
const twMerge = extendTailwindMerge({
|
|
extend: {
|
|
theme: {
|
|
radius: [...DESIGN_TOKENS.radii],
|
|
ease: [...DESIGN_TOKENS.eases],
|
|
},
|
|
classGroups: {
|
|
"font-size": [{ text: [...DESIGN_TOKENS.fontSizes] }],
|
|
duration: [{ duration: [...DESIGN_TOKENS.durations] }],
|
|
},
|
|
},
|
|
});
|
|
|
|
export function cn(...inputs: ClassValue[]) {
|
|
return twMerge(clsx(inputs));
|
|
}
|
|
|
|
export function formatBytes(bytes: number, decimals = 2) {
|
|
if (!+bytes) return "0 B";
|
|
const k = 1024;
|
|
const dm = decimals < 0 ? 0 : decimals;
|
|
const sizes = ["B", "KB", "MB", "GB", "TB", "PB"];
|
|
const i = Math.min(Math.floor(Math.log(bytes) / Math.log(k)), sizes.length - 1);
|
|
return `${Number.parseFloat((bytes / k ** i).toFixed(dm))} ${sizes[i]}`;
|
|
}
|
|
|
|
export function formatDateTime(value: string | null | undefined) {
|
|
if (!value) return "—";
|
|
const d = new Date(value);
|
|
if (Number.isNaN(d.getTime())) return value;
|
|
return d.toLocaleString("zh-CN", {
|
|
year: "numeric",
|
|
month: "2-digit",
|
|
day: "2-digit",
|
|
hour: "2-digit",
|
|
minute: "2-digit",
|
|
second: "2-digit",
|
|
hour12: false,
|
|
});
|
|
}
|
|
|
|
export function formatDate(value: string | null | undefined) {
|
|
if (!value) return "—";
|
|
const d = new Date(value);
|
|
if (Number.isNaN(d.getTime())) return value;
|
|
return d.toLocaleDateString("zh-CN", { year: "numeric", month: "2-digit", day: "2-digit" });
|
|
}
|