saladday1's picture
code: sync from git
61db3dc verified
Raw History Blame
3.28 kB
import { useEffect, useRef, useState } from "react"
import { DownloadButton } from "@/components/download-button"
/** A part of a page under a centred heading, as minimax.io sets its sections. */
export function Section({ title, children, className = "" }: { title?: string; children: React.ReactNode; className?: string }) {
return (
<section className={`mt-24 sm:mt-28 ${className}`}>
{title && <h2 className="mb-10 text-center font-display text-[30px] leading-tight font-semibold tracking-[-0.01em] sm:text-[36px]">{title}</h2>}
{children}
</section>
)
}
/** A white card with a hairline border. With `download` (the file name) it gets a download button
* on its title row (or top right, level with the legend, when untitled). */
export function Panel({ title, download, children }: { title?: string; download?: string; children: React.ReactNode }) {
const ref = useRef<HTMLDivElement>(null)
const head = title && <h3 className="font-display text-[19px] leading-tight font-semibold">{title}</h3>
const card = "relative min-w-0 rounded-[20px] border border-border bg-card p-5 shadow-[var(--shadow-card)] sm:p-8"
if (!download)
return (
<div className={card}>
{head && <div className="mb-6">{head}</div>}
{children}
</div>
)
return (
<div ref={ref} className={card}>
{title ? (
<div className="mb-6 flex items-center justify-between gap-4">
{head}
<DownloadButton target={ref} name={download} />
</div>
) : (
<DownloadButton target={ref} name={download} className="absolute top-3.5 right-5 sm:top-[26px] sm:right-8" />
)}
<div className={title ? undefined : "[&>:first-child]:pr-12"}>{children}</div>
</div>
)
}
/** A number that counts up once when it first appears. */
export function CountUp({ value, format, ms = 1600 }: { value: number; format: (n: number) => string; ms?: number }) {
const [v, setV] = useState(0)
const done = useRef(false)
useEffect(() => {
if (done.current || matchMedia("(prefers-reduced-motion: reduce)").matches) {
setV(value)
return
}
done.current = true
let raf = 0
const t0 = performance.now()
const tick = (t: number) => {
const p = Math.min(1, (t - t0) / ms)
setV(value * (1 - Math.pow(1 - p, 3)))
if (p < 1) raf = requestAnimationFrame(tick)
}
raf = requestAnimationFrame(tick)
return () => cancelAnimationFrame(raf)
}, [value, ms])
// Serif figures are proportional: the final value, hidden, holds the width while it counts.
return (
<span className="inline-grid">
<span aria-hidden className="invisible col-start-1 row-start-1">{format(value)}</span>
<span className="col-start-1 row-start-1">{format(v)}</span>
</span>
)
}
/** A number over what it counts: serif figures, as minimax.io sets its company numbers. */
export function Stat({ value, label }: { value: React.ReactNode; label: string }) {
return (
<div className="min-w-0">
<div className="font-serif text-[30px] leading-none tracking-[-0.01em] whitespace-nowrap sm:text-[36px]">{value}</div>
<div className="mt-3 text-[15px] leading-snug text-muted-foreground sm:text-base">{label}</div>
</div>
)
}