SPB Git

Repository Public

kit.tsx 7.3 KB 200 lines tsx

1// Briques de la fiche de propriété : section, panneau, tuile, ligne, repli, source.23import { useState, type ReactNode } from "react";4import { ChevronDown } from "lucide-react";5import { cn } from "@/lib/cn";6import { Collapse } from "@/lib/motion";78export type Tone = "neutral" | "good" | "warn" | "bad" | "brand";910export const TONE_TEXT: Record<Tone, string> = {11  neutral: "text-ink-2",12  good: "text-good",13  warn: "text-high",14  bad: "text-bad",15  brand: "text-brand",16};17export const TONE_BG: Record<Tone, string> = {18  neutral: "bg-surface-2",19  good: "bg-good-soft",20  warn: "bg-high-soft",21  bad: "bg-bad-soft",22  brand: "bg-brand-soft",23};24export const TONE_BAR: Record<Tone, string> = {25  neutral: "bg-ink-3",26  good: "bg-good",27  warn: "bg-high",28  bad: "bg-bad",29  brand: "bg-brand",30};3132/** Grande section de la fiche (ancre de la navigation) : filet, titre, texte. */33export function Section({34  id,35  title,36  text,37  aside,38  children,39  className,40}: {41  id: string;42  title: ReactNode;43  text?: ReactNode;44  aside?: ReactNode;45  children: ReactNode;46  className?: string;47}) {48  return (49    <section id={id} data-section={id} className={cn("scroll-mt-32 border-t border-line pt-8", className)}>50      <div className="mb-5 flex flex-wrap items-end justify-between gap-x-6 gap-y-3">51        <div className="min-w-0 max-w-2xl">52          <h2 className="text-[1.5rem] sm:text-[1.7rem]">{title}</h2>53          {text && <p className="mt-1.5 text-[0.94rem] leading-relaxed text-ink-3">{text}</p>}54        </div>55        {aside}56      </div>57      {children}58    </section>59  );60}6162/** Panneau : un bloc d'analyse. */63export function Panel({64  icon,65  title,66  aside,67  children,68  className,69}: {70  icon?: ReactNode;71  title?: ReactNode;72  aside?: ReactNode;73  children: ReactNode;74  className?: string;75}) {76  return (77    <div className={cn("rounded-xl border border-line bg-surface p-5 sm:p-6", className)}>78      {(title || aside) && (79        <div className="mb-4 flex flex-wrap items-center justify-between gap-3">80          <div className="flex min-w-0 items-center gap-2.5">81            {icon && <span className="shrink-0 text-ink-3">{icon}</span>}82            {title && <h3 className="text-[1.15rem] leading-snug">{title}</h3>}83          </div>84          {aside}85        </div>86      )}87      {children}88    </div>89  );90}9192/** Étiquette teintée. */93export function Pill({ tone = "neutral", children, className }: { tone?: Tone; children: ReactNode; className?: string }) {94  return (95    <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)}>96      {children}97    </span>98  );99}100101/** Chiffre clé compact : filet haut, valeur, libellé. */102export function Tile({ value, label, hint, tone, className }: { value: ReactNode; label: ReactNode; hint?: ReactNode; tone?: Tone; className?: string }) {103  return (104    <div className={cn("min-w-0 border-t border-line pt-2.5", className)}>105      <div className={cn("num text-[1.1rem] font-semibold leading-tight sm:text-[1.2rem]", tone ? TONE_TEXT[tone] : "text-ink")}>{value}</div>106      <div className="mt-0.5 hyphens-auto break-words text-[0.76rem] leading-snug text-ink-3 sm:text-[0.8rem]">{label}</div>107      {hint && <div className="mt-0.5 text-[0.72rem] text-ink-3">{hint}</div>}108    </div>109  );110}111112/** Ligne libellé / barre / valeur. */113export function BarRow({ label, value, pct, tone = "brand", right }: { label: ReactNode; value?: ReactNode; pct: number; tone?: Tone; right?: ReactNode }) {114  return (115    <div className="flex items-center gap-3 py-1.5 text-sm">116      <span className="w-[38%] shrink-0 truncate text-ink-2">{label}</span>117      <span className="h-1.5 min-w-0 flex-1 overflow-hidden rounded-full bg-surface-3" aria-hidden>118        <span className={cn("lk-draw-bar block h-full rounded-full", TONE_BAR[tone])} style={{ width: `${Math.max(2, Math.min(100, pct))}%` }} />119      </span>120      <span className="num w-20 shrink-0 text-right font-medium text-ink">121        {value}122        {right}123      </span>124    </div>125  );126}127128/** Ligne clé / valeur. */129export function KV({ k, v }: { k: ReactNode; v: ReactNode }) {130  return (131    <div className="flex items-baseline justify-between gap-4 border-b border-line py-2.5 text-[0.93rem] last:border-0">132      <span className="shrink-0 text-ink-3">{k}</span>133      <span className="min-w-0 break-words text-right font-medium text-ink">{v}</span>134    </div>135  );136}137138/** Repli accessible. */139export function Disclosure({ title, meta, children, defaultOpen }: { title: ReactNode; meta?: ReactNode; children: ReactNode; defaultOpen?: boolean }) {140  const [open, setOpen] = useState(!!defaultOpen);141  return (142    <div className="border-t border-line first:border-0">143      <button144        type="button"145        aria-expanded={open}146        onClick={() => setOpen((o) => !o)}147        className="flex min-h-11 w-full items-center justify-between gap-3 py-3 text-left text-[0.95rem] font-medium text-ink"148      >149        <span className="min-w-0">{title}</span>150        <span className="flex shrink-0 items-center gap-2.5 text-[0.82rem] font-normal text-ink-3">151          {meta}152          <ChevronDown className={cn("size-4 transition-transform duration-200", open && "rotate-180")} />153        </span>154      </button>155      <Collapse open={open}>156        <div className="pb-4 text-[0.92rem] leading-relaxed text-ink-2">{children}</div>157      </Collapse>158    </div>159  );160}161162export function SourceLine({ children }: { children: ReactNode }) {163  return <p className="mt-4 text-[0.76rem] leading-relaxed text-ink-3">{children}</p>;164}165166export function MoreLink({ onClick, children }: { onClick: () => void; children: ReactNode }) {167  return (168    <button type="button" onClick={onClick} className="mt-2 inline-flex min-h-11 items-center gap-1.5 text-[0.88rem] font-medium text-brand hover:underline">169      {children}170    </button>171  );172}173174export function Lines({ n = 3 }: { n?: number }) {175  return (176    <div className="space-y-2.5" aria-hidden>177      {Array.from({ length: n }, (_, i) => (178        <div key={i} className="skeleton h-4" style={{ width: `${92 - i * 14}%` }} />179      ))}180    </div>181  );182}183184export const nbsp = " ";185export const fmtDay = (ts: number | null | undefined) =>186  ts ? new Date(ts * 1000).toLocaleDateString("fr-CA", { day: "numeric", month: "short", year: "numeric" }) : "—";187export const fmtMonth = (ts: number | null | undefined) =>188  ts ? new Date(ts * 1000).toLocaleDateString("fr-CA", { month: "long", year: "numeric" }) : "—";189export const dec1 = (n: number) => n.toLocaleString("fr-CA", { minimumFractionDigits: 1, maximumFractionDigits: 1 });190191/** Montant au dollar près : « 1 234 $ ». */192export const money = (n: number | null | undefined) => (n == null || !Number.isFinite(n) ? "—" : `${Math.round(n).toLocaleString("fr-CA")}${nbsp}$`);193/** Montant avec cents : « 1 234,56 $ ». */194export const money2 = (n: number | null | undefined) =>195  n == null || !Number.isFinite(n) ? "—" : `${n.toLocaleString("fr-CA", { minimumFractionDigits: 2, maximumFractionDigits: 2 })}${nbsp}$`;196/** 4.19 → « 4,19 % » */197export const rate2 = (r: number | null | undefined) => (r == null ? "—" : `${r.toFixed(2).replace(".", ",")}${nbsp}%`);198/** 0.123 → « 12 % » (valeur absolue) */199export const pct0 = (x: number) => `${Math.abs(Math.round(x * 100))}${nbsp}%`;200
Files main
Menu

Theme