Files
crosscode-platform/packages/ui/src/index.tsx
T
joshuaandClaude Opus 5.5 8055e1ec62 events 3.9.0, website 1.9.0, accounts 1.10.0: the church's details from Accounts everywhere
- Events: the Accounts profile's name, logo, icon, colours and contact details win over
  Events' own settings while Accounts is connected (pages, emails, WhatsApp, PDFs); Settings
  shows them read-only with a link to Accounts. The Accounts logo is copied for PDFs (PNG
  and JPEG). Fixes Events keeping its old church after a demo scenario load.
- Website: a background per homepage section in the church's colours; "More info" instead
  of "Register" for events without registration; the shared colour field.
- Accounts: the shared colour field in Church profile.
- @crosscode/ui 0.4.0: ColourField and isHexColour.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 00:46:39 +02:00

172 lines
8.9 KiB
TypeScript

/**
* @crosscode/ui — the branded shell, cards, buttons, fields and notices shared by Accounts, the
* Church Website and the Church App. Colours come from `--brand` (see README).
*/
import React from "react";
export * from "./brand";
const cx = (...c: (string | false | null | undefined)[]) => c.filter(Boolean).join(" ");
export type Branding = { name: string; shortName?: string | null; logoUrl?: string | null };
/** The page frame: the church's logo and name, then the content in a centred column. */
/** The church's logo, or its first letter on its colour while it has none. */
export function ChurchMark({ branding, size = "md" }: { branding: Branding; size?: "md" | "lg" }) {
const box = size === "lg" ? "h-14 w-14 text-2xl" : "h-10 w-10 text-lg";
if (branding.logoUrl) return <img src={branding.logoUrl} alt="" className={cx(size === "lg" ? "h-14" : "h-10", "w-auto max-w-[12rem] rounded-[var(--cc-radius-sm,0.75rem)] object-contain")} />;
return <span className={cx(box, "flex shrink-0 items-center justify-center rounded-[var(--cc-radius-sm,0.75rem)] bg-brand font-semibold text-white")} aria-hidden>{(branding.shortName || branding.name || "?").trim().charAt(0).toUpperCase()}</span>;
}
/**
* The page frame (as the church's other sites): a soft wash of the church's colour, its logo and
* name across the top, the content in a centred column, and a quiet footer. `wide` for the account
* and admin pages; otherwise a sign-in column.
*/
/** `footer` replaces the quiet line at the bottom (Accounts 1.7.0 passes the `ChurchFooter`). */
export function Shell({ branding, children, wide = false, aside, footer }: { branding: Branding; children: React.ReactNode; wide?: boolean; aside?: React.ReactNode; footer?: React.ReactNode }) {
return (
<div className="min-h-screen flex flex-col bg-gradient-to-br from-brand/10 via-white to-brand/5">
<header className="border-b border-brand/10 bg-white/90 backdrop-blur">
<div className="mx-auto flex max-w-5xl items-center gap-3 px-4 py-3">
<ChurchMark branding={branding} />
<span className="text-lg font-semibold text-brand-dark [font-family:var(--cc-font-heading,inherit)] [font-variation-settings:var(--cc-heading-variation,normal)]">{branding.name}</span>
{aside && <div className="ml-auto">{aside}</div>}
</div>
</header>
<main className={cx("mx-auto w-full flex-1 px-4", wide ? "max-w-5xl py-8" : "max-w-md py-10 sm:py-16")}>{children}</main>
{footer ?? (
<footer className="px-4 py-6 text-center text-xs text-gray-500">
{branding.name} · your church account, for all its sites and apps
</footer>
)}
</div>
);
}
export function Card({ children, className }: { children: React.ReactNode; className?: string }) {
return <div className={cx("rounded-[var(--cc-radius,1rem)] border border-gray-200 border-t-4 border-t-brand bg-white p-6 shadow-sm sm:p-8", className)}>{children}</div>;
}
/** A page's title, with an optional icon in a tile of the church's colour (as on the church's other sites). */
export function Heading({ children, sub, icon }: { children: React.ReactNode; sub?: React.ReactNode; icon?: React.ReactNode }) {
return (
<div className="mb-6 flex items-start gap-3">
{icon && <span className="flex h-11 w-11 shrink-0 items-center justify-center rounded-[var(--cc-radius-sm,0.75rem)] bg-brand/10 text-brand" aria-hidden>{icon}</span>}
<div>
<h1 className="text-xl font-semibold text-gray-900 [font-family:var(--cc-font-heading,inherit)] [font-variation-settings:var(--cc-heading-variation,normal)]">{children}</h1>
{sub && <p className="mt-1 text-sm text-gray-600">{sub}</p>}
</div>
</div>
);
}
type ButtonProps = React.ButtonHTMLAttributes<HTMLButtonElement> & { variant?: "primary" | "secondary" | "danger" | "link"; busy?: boolean };
export function Button({ variant = "primary", busy, className, children, disabled, ...rest }: ButtonProps) {
const styles = {
primary: "bg-brand text-white hover:bg-brand-dark",
secondary: "border bg-white text-gray-800 hover:bg-gray-50",
danger: "bg-red-600 text-white hover:bg-red-700",
link: "text-brand underline px-0 py-0",
}[variant];
return (
<button {...rest} disabled={disabled || busy} className={cx("rounded-[var(--cc-radius-button,0.5rem)] px-4 py-2.5 text-sm font-medium transition disabled:opacity-50", styles, className)}>
{busy ? "Please wait…" : children}
</button>
);
}
type FieldProps = React.InputHTMLAttributes<HTMLInputElement> & { label: string; error?: string | null; hint?: React.ReactNode };
export function Field({ label, error, hint, id, className, ...rest }: FieldProps) {
const fieldId = id ?? `f-${label.toLowerCase().replace(/[^a-z0-9]+/g, "-")}`;
return (
<div className={cx("mb-4", className)}>
<label htmlFor={fieldId} className="mb-1 block text-sm font-medium text-gray-800">{label}</label>
<input
id={fieldId}
aria-invalid={!!error}
aria-describedby={error ? `${fieldId}-error` : undefined}
className={cx("w-full rounded-[var(--cc-radius-sm,0.5rem)] border px-3 py-2.5 text-base focus:outline-none focus:ring-2 focus:ring-brand/50", error ? "border-red-500" : "border-gray-300")}
{...rest}
/>
{hint && !error && <p className="mt-1 text-xs text-gray-500">{hint}</p>}
{error && <p id={`${fieldId}-error`} className="mt-1 text-sm text-red-700">{error}</p>}
</div>
);
}
export const isHexColour = (v: string) => /^#[0-9a-fA-F]{6}$/.test(v.trim());
type ColourFieldProps = {
label: string;
value: string;
onChange: (hex: string) => void;
hint?: React.ReactNode;
error?: string | null;
/** For a form read with FormData: the hex is sent under this name. */
name?: string;
id?: string;
disabled?: boolean;
};
/**
* A colour: the swatch to pick it, the hex code to type or paste it, and a bar showing it, as Cross
* Events' Branding settings. Controlled: `value` is what's typed, so it can be half-typed; it's a
* colour once it reads `#rrggbb` (`isHexColour`), and says so when it doesn't.
*/
export function ColourField({ label, value, onChange, hint, error, name, id, disabled }: ColourFieldProps) {
const fieldId = id ?? `f-${label.toLowerCase().replace(/[^a-z0-9]+/g, "-")}`;
const valid = isHexColour(value);
const problem = error || (valid ? null : "Enter a colour code like #1f4e79.");
return (
<div className="mb-4">
<label htmlFor={fieldId} className="mb-1 block text-sm font-medium text-gray-800">{label}</label>
<div className="flex items-center gap-3">
<input
type="color"
aria-label={`${label}: pick`}
disabled={disabled}
className="h-10 w-20 shrink-0 cursor-pointer rounded-[var(--cc-radius-sm,0.5rem)] border border-gray-300 bg-white p-1 disabled:cursor-not-allowed"
value={valid ? value.trim().toLowerCase() : "#000000"}
onChange={(e) => onChange(e.target.value)}
/>
<input
id={fieldId}
name={name}
disabled={disabled}
spellCheck={false}
autoComplete="off"
placeholder="#1f4e79"
aria-invalid={!!problem}
aria-describedby={problem ? `${fieldId}-error` : undefined}
className={cx("w-full rounded-[var(--cc-radius-sm,0.5rem)] border px-3 py-2.5 font-mono text-sm focus:outline-none focus:ring-2 focus:ring-brand/50", problem ? "border-red-500" : "border-gray-300")}
value={value}
onChange={(e) => onChange(e.target.value)}
/>
</div>
<div className="mt-2 h-6 rounded-[var(--cc-radius-sm,0.5rem)] border border-gray-200 transition-colors" style={{ background: valid ? value.trim() : "#f1f5f9" }} aria-hidden />
{hint && !problem && <p className="mt-1 text-xs text-gray-500">{hint}</p>}
{problem && <p id={`${fieldId}-error`} className="mt-1 text-sm text-red-700">{problem}</p>}
</div>
);
}
export function Checkbox({ label, hint, ...rest }: React.InputHTMLAttributes<HTMLInputElement> & { label: React.ReactNode; hint?: React.ReactNode }) {
return (
<label className="mb-3 flex items-start gap-3 text-sm">
<input type="checkbox" className="mt-0.5 h-4 w-4 accent-[rgb(var(--brand))]" {...rest} />
<span>
<span className="text-gray-900">{label}</span>
{hint && <span className="block text-gray-500">{hint}</span>}
</span>
</label>
);
}
export function Notice({ tone = "info", children }: { tone?: "info" | "error" | "success" | "warning"; children: React.ReactNode }) {
const styles = { info: "bg-blue-50 text-blue-900 border-blue-200", error: "bg-red-50 text-red-800 border-red-200", success: "bg-green-50 text-green-800 border-green-200", warning: "bg-amber-50 text-amber-900 border-amber-200" }[tone];
return <div role={tone === "error" ? "alert" : "status"} className={cx("mb-4 rounded-[var(--cc-radius-sm,0.5rem)] border px-4 py-3 text-sm", styles)}>{children}</div>;
}