SPB Git

Repository Public

kit.tsx 6.6 KB 189 lines tsx

1// Briques de la fiche logement : section, tuile, ligne, repli, source.23import { useState, type ReactNode } from "react";4import { ChevronDown } from "lucide-react";5import { cn } from "@/lib/cn";67export type Tone = "neutral" | "good" | "warn" | "bad" | "brand";89export const TONE_TEXT: Record<Tone, string> = {10  neutral: "text-ink-2",11  good: "text-good",12  warn: "text-high",13  bad: "text-bad",14  brand: "text-brand",15};16export const TONE_BG: Record<Tone, string> = {17  neutral: "bg-surface-2",18  good: "bg-good-soft",19  warn: "bg-high-soft",20  bad: "bg-bad-soft",21  brand: "bg-brand-soft",22};23export const TONE_BAR: Record<Tone, string> = {24  neutral: "bg-ink-3",25  good: "bg-good",26  warn: "bg-high",27  bad: "bg-bad",28  brand: "bg-brand",29};3031/** Grande section de la fiche (ancre de la navigation) : filet, titre, texte. */32export function Section({33  id,34  title,35  text,36  aside,37  children,38  className,39}: {40  id: string;41  /** conservé pour compatibilité : les sur-titres ne sont plus affichés */42  eyebrow?: ReactNode;43  title: ReactNode;44  text?: ReactNode;45  aside?: ReactNode;46  children: ReactNode;47  className?: string;48}) {49  return (50    <section id={id} data-section={id} className={cn("scroll-mt-32 border-t border-line pt-8", className)}>51      <div className="mb-5 flex flex-wrap items-end justify-between gap-x-6 gap-y-3">52        <div className="min-w-0 max-w-2xl">53          <h2 className="text-[1.5rem] sm:text-[1.7rem]">{title}</h2>54          {text && <p className="mt-1.5 text-[0.94rem] leading-relaxed text-ink-3">{text}</p>}55        </div>56        {aside}57      </div>58      {children}59    </section>60  );61}6263/** Panneau : un bloc d'analyse. */64export function Panel({65  icon,66  title,67  aside,68  children,69  className,70}: {71  icon?: ReactNode;72  title?: ReactNode;73  aside?: ReactNode;74  children: ReactNode;75  className?: string;76}) {77  return (78    <div className={cn("rounded-xl border border-line bg-surface p-5 sm:p-6", className)}>79      {(title || aside) && (80        <div className="mb-4 flex flex-wrap items-center justify-between gap-3">81          <div className="flex min-w-0 items-center gap-2.5">82            {icon && <span className="shrink-0 text-ink-3">{icon}</span>}83            {title && <h3 className="text-[1.15rem] leading-snug">{title}</h3>}84          </div>85          {aside}86        </div>87      )}88      {children}89    </div>90  );91}9293/** Étiquette teintée. */94export function Pill({ tone = "neutral", children, className }: { tone?: Tone; children: ReactNode; className?: string }) {95  return (96    <span className={cn("inline-flex items-center gap-1.5 rounded-[6px] px-2 py-1 text-[0.76rem] font-medium leading-none", TONE_BG[tone], TONE_TEXT[tone], className)}>97      {children}98    </span>99  );100}101102/** Chiffre clé compact : filet haut, valeur, libellé. */103export function Tile({ value, label, hint, tone, className }: { value: ReactNode; label: ReactNode; hint?: ReactNode; tone?: Tone; className?: string }) {104  return (105    <div className={cn("min-w-0 border-t border-line pt-2.5", className)}>106      <div className={cn("num text-[1.1rem] font-semibold leading-tight sm:text-[1.2rem]", tone ? TONE_TEXT[tone] : "text-ink")}>{value}</div>107      <div className="mt-0.5 hyphens-auto break-words text-[0.76rem] leading-snug text-ink-3 sm:text-[0.8rem]">{label}</div>108      {hint && <div className="mt-0.5 text-[0.72rem] text-ink-3">{hint}</div>}109    </div>110  );111}112113/** Ligne libellé / barre / valeur. */114export function BarRow({ label, value, pct, tone = "brand", right }: { label: ReactNode; value?: ReactNode; pct: number; tone?: Tone; right?: ReactNode }) {115  return (116    <div className="flex items-center gap-3 py-1.5 text-sm">117      <span className="w-[38%] shrink-0 truncate text-ink-2">{label}</span>118      <span className="h-1.5 min-w-0 flex-1 overflow-hidden rounded-full bg-surface-3" aria-hidden>119        <span className={cn("block h-full rounded-full transition-[width] duration-300 ease-out", TONE_BAR[tone])} style={{ width: `${Math.max(2, Math.min(100, pct))}%` }} />120      </span>121      <span className="num w-20 shrink-0 text-right font-medium text-ink">122        {value}123        {right}124      </span>125    </div>126  );127}128129/** Ligne clé / valeur. */130export function KV({ k, v }: { k: ReactNode; v: ReactNode }) {131  return (132    <div className="flex items-baseline justify-between gap-4 border-b border-line py-2.5 text-[0.93rem] last:border-0">133      <span className="shrink-0 text-ink-3">{k}</span>134      <span className="min-w-0 break-words text-right font-medium text-ink">{v}</span>135    </div>136  );137}138139/** Repli accessible. */140export function Disclosure({ title, meta, children, defaultOpen }: { title: ReactNode; meta?: ReactNode; children: ReactNode; defaultOpen?: boolean }) {141  const [open, setOpen] = useState(!!defaultOpen);142  return (143    <div className="border-t border-line first:border-0">144      <button145        type="button"146        aria-expanded={open}147        onClick={() => setOpen((o) => !o)}148        className="flex w-full items-center justify-between gap-3 py-3.5 text-left text-[0.95rem] font-medium text-ink"149      >150        <span className="min-w-0">{title}</span>151        <span className="flex shrink-0 items-center gap-2.5 text-[0.82rem] font-normal text-ink-3">152          {meta}153          <ChevronDown className={cn("size-4 transition-transform duration-150", open && "rotate-180")} />154        </span>155      </button>156      {open && <div className="pb-4 text-[0.92rem] leading-relaxed text-ink-2">{children}</div>}157    </div>158  );159}160161export function SourceLine({ children }: { children: ReactNode }) {162  return <p className="mt-4 text-[0.76rem] leading-relaxed text-ink-3">{children}</p>;163}164165export function MoreLink({ onClick, children }: { onClick: () => void; children: ReactNode }) {166  return (167    <button type="button" onClick={onClick} className="mt-3 inline-flex items-center gap-1.5 text-[0.88rem] font-medium text-brand hover:underline">168      {children}169    </button>170  );171}172173export function Lines({ n = 3 }: { n?: number }) {174  return (175    <div className="space-y-2.5" aria-hidden>176      {Array.from({ length: n }, (_, i) => (177        <div key={i} className="skeleton h-4" style={{ width: `${92 - i * 14}%` }} />178      ))}179    </div>180  );181}182183export const nbsp = " ";184export const fmtDay = (ts: number | null | undefined) =>185  ts ? new Date(ts * 1000).toLocaleDateString("fr-CA", { day: "numeric", month: "short", year: "numeric" }) : "—";186export const fmtMonth = (ts: number | null | undefined) =>187  ts ? new Date(ts * 1000).toLocaleDateString("fr-CA", { month: "long", year: "numeric" }) : "—";188export const dec1 = (n: number) => n.toLocaleString("fr-CA", { minimumFractionDigits: 1, maximumFractionDigits: 1 });189
Files main
Menu

Theme