返回 CodeWhale
desktop-shot.tsx
根目录 / web / components / desktop-shot.tsx
1 import DESKTOP_MEDIA from "@/data/desktop-media.json";
2
3 export type DesktopShotId = (typeof DESKTOP_MEDIA.shots)[number]["id"];
4
5 type Output = { width: number; height: number };
6 const outputs = DESKTOP_MEDIA.outputs as Record<string, Output>;
7 const base = `/${DESKTOP_MEDIA.outDir.replace(/^public\//, "")}`;
8 const widths = [...DESKTOP_MEDIA.widths].sort((a, b) => a - b);
9 const largest = widths[widths.length - 1];
10
11 export function DesktopShot({
12 id,
13 alt,
14 sizes = "(max-width: 900px) 100vw, 1100px",
15 eager = false,
16 }: {
17 id: DesktopShotId;
18 alt: string;
19 sizes?: string;
20 eager?: boolean;
21 }) {
22 const size = outputs[`${id}-light-${largest}.webp`];
23 return (
24 <span className="desktop-shot">
25 {(["light", "dark"] as const).map((theme) => {
26 const set = (format: string) => widths.map((w) => `${base}/${id}-${theme}-${w}.${format} ${outputs[`${id}-${theme}-${w}.${format}`].width}w`).join(", ");
27 return (
28 <picture key={theme} className={`desktop-shot-${theme}`}>
29 <source type="image/avif" srcSet={set("avif")} sizes={sizes} />
30 <img
31 src={`${base}/${id}-${theme}-${largest}.webp`}
32 srcSet={set("webp")}
33 sizes={sizes}
34 alt={alt}
35 width={size.width}
36 height={size.height}
37 loading={eager && theme === "light" ? "eager" : "lazy"}
38 decoding="async"
39 />
40 </picture>
41 );
42 })}
43 </span>
44 );
45 }
46
46 lines Plain Text