Every upload is turned upright, stripped of its metadata (EXIF, GPS), kept at most 2400 px wide and saved at 480/960/1600 px in WebP and JPEG; the pages pick the size with <picture> and srcset. Every file counts towards storage; a photo that can't be read is refused. sharp 0.35 (prebuilt, no build step on the servers). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
87 lines
4.4 KiB
TypeScript
87 lines
4.4 KiB
TypeScript
/** Small building blocks every template uses. */
|
|
import type { Image } from "./types";
|
|
|
|
export const cx = (...c: (string | false | null | undefined)[]) => c.filter(Boolean).join(" ");
|
|
|
|
/**
|
|
* An uploaded image at the size the screen needs (§3: responsive sizes in WebP and JPEG): the
|
|
* browser picks from the `srcset`s by `sizes`. `alt` overrides the image's own (e.g. "" when it's
|
|
* only decoration behind text).
|
|
*/
|
|
export function Picture({ image, className, sizes = "100vw", priority = false, alt }: { image: Image; className?: string; sizes?: string; priority?: boolean; alt?: string }) {
|
|
const img = (
|
|
<img src={image.url} srcSet={image.srcset?.jpg} sizes={image.srcset ? sizes : undefined} alt={alt ?? image.alt}
|
|
width={image.width ?? undefined} height={image.height ?? undefined}
|
|
loading={priority ? "eager" : "lazy"} fetchPriority={priority ? "high" : undefined} decoding="async" className={className} />
|
|
);
|
|
return image.srcset ? <picture><source type="image/webp" srcSet={image.srcset.webp} sizes={sizes} />{img}</picture> : img;
|
|
}
|
|
|
|
/** An image in a frame (Heritage frames, duotone tints), or a soft placeholder when there's none. */
|
|
export function Img({ image, className, sizes = "(min-width: 768px) 50vw, 100vw", priority = false, ratio = "aspect-[4/3]" }: { image: Image | null; className?: string; sizes?: string; priority?: boolean; ratio?: string }) {
|
|
return (
|
|
<div className={cx("cc-frame", ratio, className)}>
|
|
{image ? (
|
|
<Picture image={image} sizes={sizes} priority={priority} className="cc-img" />
|
|
) : (
|
|
<div aria-hidden="true" className="h-full w-full bg-gradient-to-br from-brand/25 via-brand-tint to-accent/30" />
|
|
)}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export type ButtonData = { label: string; href: string } | null | undefined;
|
|
|
|
export function Button({ button, className }: { button: ButtonData; className?: string }) {
|
|
if (!button?.label || !button.href) return null;
|
|
const external = button.href.startsWith("https://");
|
|
return (
|
|
<a href={button.href} className={cx("cc-btn", className)} {...(external ? { rel: "noopener" } : {})}>
|
|
{button.label}
|
|
</a>
|
|
);
|
|
}
|
|
|
|
/** Text the admins wrote, as paragraphs (blank lines). Never HTML. */
|
|
export function Paragraphs({ text, className }: { text: string | null | undefined; className?: string }) {
|
|
if (!text) return null;
|
|
const paras = text.split(/\n\s*\n/).map((p) => p.trim()).filter(Boolean);
|
|
return (
|
|
<div className={cx("cc-prose", className)}>
|
|
{paras.map((p, i) => <p key={i}>{p.split("\n").map((line, j) => <span key={j}>{j > 0 && <br />}{line}</span>)}</p>)}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
const DAYS: Record<string, string> = { monday: "Monday", tuesday: "Tuesday", wednesday: "Wednesday", thursday: "Thursday", friday: "Friday", saturday: "Saturday", sunday: "Sunday" };
|
|
export const dayName = (d: string) => DAYS[d] ?? d;
|
|
|
|
export function fmtDate(iso: string, timeZone: string, opts: Intl.DateTimeFormatOptions = { weekday: "short", day: "numeric", month: "long" }) {
|
|
return new Intl.DateTimeFormat("en-ZA", { timeZone, ...opts }).format(new Date(iso));
|
|
}
|
|
|
|
export function fmtTime(iso: string, timeZone: string) {
|
|
return new Intl.DateTimeFormat("en-ZA", { timeZone, hour: "2-digit", minute: "2-digit", hour12: false }).format(new Date(iso));
|
|
}
|
|
|
|
/** Day and month in a box (event cards and lists). */
|
|
export function DateBadge({ iso, timeZone, className }: { iso: string; timeZone: string; className?: string }) {
|
|
return (
|
|
<div className={cx("flex w-16 shrink-0 flex-col items-center justify-center rounded-cc-sm bg-brand py-2 text-brand-on", className)} aria-hidden="true">
|
|
<span className="text-2xl font-bold leading-none">{fmtDate(iso, timeZone, { day: "numeric" })}</span>
|
|
<span className="text-xs uppercase tracking-wide">{fmtDate(iso, timeZone, { month: "short" })}</span>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export function SectionHeading({ eyebrow, heading, intro, centred = false, as: H = "h2" }: { eyebrow?: string | null; heading?: string | null; intro?: string | null; centred?: boolean; as?: "h1" | "h2" }) {
|
|
if (!heading && !eyebrow && !intro) return null;
|
|
return (
|
|
<div className={cx("mb-10", centred && "text-center")}>
|
|
{eyebrow && <p className="cc-eyebrow mb-2">{eyebrow}</p>}
|
|
{heading && <H className={H === "h1" ? "cc-h1" : "cc-h2"}>{heading}</H>}
|
|
{intro && <p className={cx("cc-muted mt-3 text-lg", centred && "mx-auto max-w-2xl")}>{intro}</p>}
|
|
</div>
|
|
);
|
|
}
|