192 lines
9.8 KiB
TypeScript
192 lines
9.8 KiB
TypeScript
import type { CSSProperties, ReactNode } from 'react'
|
||
import { cn } from '../../lib/cn'
|
||
|
||
/** 面板:站内唯一的「有边框的内容块」。窄屏自动收窄内边距(旧站每个页面自己写一遍)。 */
|
||
export function Panel({ children, className, style, pad = true }: {
|
||
children?: ReactNode; className?: string; style?: CSSProperties; pad?: boolean
|
||
}) {
|
||
return (
|
||
<section style={style} className={cn('bg-panel border border-line rounded-block p-[18px] max-narrow:p-[13px] min-w-0', pad && '', className)}>
|
||
{children}
|
||
</section>
|
||
)
|
||
}
|
||
|
||
/**
|
||
* 面板头:眉标 + 标题 + 右侧动作。
|
||
*
|
||
* 旧站 25 处面板头全都是 <small class="eyebrow"> + <h2>,标题走的是全局 h2(17px);
|
||
* 第一版迁移手写成 14px 的 h3,整站面板标题比正文重不了多少,扫视时找不到锚点。
|
||
* 窄列必须允许换行(旧站第二十章补的 .panel-head{flex-wrap:wrap}):两列栅格下
|
||
* 每列只有 ~400px,筛选控件的最小宽度就超过整列,不换行会把内容压到相邻面板上。
|
||
*/
|
||
export function PanelHead({ title, eyebrow, sub, right, className }: {
|
||
title?: ReactNode; eyebrow?: ReactNode; sub?: ReactNode; right?: ReactNode; className?: string
|
||
}) {
|
||
return (
|
||
<div className={cn('flex justify-between items-center gap-3 flex-wrap mb-[18px] min-w-0 [&>*]:min-w-0', className)}>
|
||
<div className="min-w-0">
|
||
{eyebrow && <Eyebrow>{eyebrow}</Eyebrow>}
|
||
{title && <h2 className="mt-[5px] mb-0 text-h2 font-bold">{title}</h2>}
|
||
{sub && <p className="mt-[5px] mb-0 text-caption text-dim leading-[1.6]">{sub}</p>}
|
||
</div>
|
||
{right && <div className="flex items-center gap-2 flex-wrap min-w-0">{right}</div>}
|
||
</div>
|
||
)
|
||
}
|
||
|
||
/** 眉标(面板上方那行全大写小字) */
|
||
export function Eyebrow({ children, className }: { children: ReactNode; className?: string }) {
|
||
return <div className={cn('text-tiny tracking-[1.6px] text-muted font-bold', className)}>{children}</div>
|
||
}
|
||
|
||
/** 小标题 + 右侧动作(.small-label / .sect-act:增删按钮一律贴在标题行右端) */
|
||
export function SectionTitle({ children, action, className }: { children: ReactNode; action?: ReactNode; className?: string }) {
|
||
return (
|
||
<div className={cn('flex items-center gap-2 flex-wrap text-tiny text-faint tracking-[.6px] font-bold mt-[14px] mb-2', className)}>
|
||
<span>{children}</span>
|
||
{action && <span className="ml-auto inline-flex items-center gap-2 [--ctl-h:var(--ctl-h-md)]">{action}</span>}
|
||
</div>
|
||
)
|
||
}
|
||
|
||
/** 指标卡 */
|
||
export function Stat({ label, value, hint, className }: { label: ReactNode; value: ReactNode; hint?: ReactNode; className?: string }) {
|
||
return (
|
||
<div className={cn('bg-panel border border-line rounded-block p-[18px] min-w-0 max-narrow:p-[13px]', className)}>
|
||
<small className="block text-muted text-tiny">{label}</small>
|
||
<b className="block text-num mt-[9px] font-bold max-narrow:text-num-sm break-words">{value}</b>
|
||
{hint && <small className="block text-faint text-tiny mt-[6px]">{hint}</small>}
|
||
</div>
|
||
)
|
||
}
|
||
|
||
/* 降列档位写成字面量:Tailwind 按源码里的字符串生成工具类,
|
||
运行时拼出来的 `max-[${w}px]:grid-cols-${c}` 根本不会被扫描到。 */
|
||
const STEP_CLS: Record<string, string> = {
|
||
'huge:3': 'max-huge:grid-cols-3', 'mid:3': 'max-mid:grid-cols-3', 'mid2:3': 'max-mid2:grid-cols-3',
|
||
'nav:2': 'max-nav:grid-cols-2', 'short:2': 'max-short:grid-cols-2', 'mid2:2': 'max-mid2:grid-cols-2',
|
||
'tiny:1': 'max-tiny:grid-cols-1', 'narrow:1': 'max-narrow:grid-cols-1', 'short:1': 'max-short:grid-cols-1'
|
||
}
|
||
export type StatStep = { at: 'huge' | 'mid' | 'mid2' | 'nav' | 'short' | 'tiny' | 'narrow'; cols: 1 | 2 | 3 }
|
||
|
||
/**
|
||
* 指标卡栅格:列数与降列节奏都由容器声明,页面不写 @media。
|
||
* 默认沿用旧站 .stats:4 列 → ≤900 两列 → ≤560 收紧间距(卡片本身跟着换小字号内边距)。
|
||
* 卡片数不是 4 的页面把节奏写进 steps(用户管理 5 张卡:≤1280 三列、≤820 两列),
|
||
* 这样例外是可见的参数,而不是又一段散回样式表、没人敢删的 @media。
|
||
*/
|
||
export function StatGrid({ children, cols = 4, steps, className }: {
|
||
children: ReactNode; cols?: number; steps?: StatStep[]; className?: string
|
||
}) {
|
||
return (
|
||
<div className={cn('grid gap-3 mb-[17px] grid-cols-[repeat(var(--cols),minmax(0,1fr))] min-w-0',
|
||
!steps && 'max-nav:grid-cols-2',
|
||
...(steps || []).map((st) => STEP_CLS[`${st.at}:${st.cols}`]),
|
||
'max-narrow:gap-2', className)}
|
||
style={{ ['--cols' as string]: cols }}>
|
||
{children}
|
||
</div>
|
||
)
|
||
}
|
||
|
||
/** 进度条(.bar / .bar i) */
|
||
export function ProgressBar({ value, bad, className }: { value: number; bad?: boolean; className?: string }) {
|
||
const pct = Math.max(0, Math.min(100, Number(value) || 0))
|
||
return (
|
||
<span className={cn('inline-block h-[5px] bg-active rounded-pill overflow-hidden min-w-[60px] align-middle', className)}>
|
||
<i className={cn('block h-full', bad ? 'bg-danger' : 'bg-accent-fill')} style={{ width: `${pct}%` }} />
|
||
</span>
|
||
)
|
||
}
|
||
|
||
export function Divider({ className }: { className?: string }) {
|
||
return <div className={cn('h-px bg-active my-4', className)} />
|
||
}
|
||
|
||
export function Empty({ children = '暂无数据', className }: { children?: ReactNode; className?: string }) {
|
||
return <div className={cn('border border-dashed border-line-strong rounded-tile p-7 text-center text-faint', className)}>{children}</div>
|
||
}
|
||
|
||
export function Loading({ children = '读取中…', className }: { children?: ReactNode; className?: string }) {
|
||
return <div className={cn('p-[26px] text-center text-faint text-body', className)}>{children}</div>
|
||
}
|
||
|
||
export function Spinner({ className }: { className?: string }) {
|
||
return <i aria-hidden className={cn('inline-block w-[11px] h-[11px] border-2 border-line-bright border-t-accent rounded-full animate-spin-slow align-[-1px]', className)} />
|
||
}
|
||
|
||
/** 提示条:成功用默认(青柠底),错误用 danger;只放一句人话,长说明收进抽屉 */
|
||
export function Notice({ children, danger, className, onClose }: {
|
||
children: ReactNode; danger?: boolean; className?: string; onClose?: () => void
|
||
}) {
|
||
if (!children) return null
|
||
return (
|
||
<div className={cn('flex items-start gap-2 p-[11px] rounded-[8px] border mb-4 text-[11.5px] leading-[1.6] break-words break-anywhere',
|
||
danger ? 'border-danger-line bg-danger-tint text-danger-bright' : 'border-accent-line bg-accent-tint-2 text-accent-bright',
|
||
className)}>
|
||
<div className="min-w-0 flex-1">{children}</div>
|
||
{onClose && (
|
||
<button onClick={onClose} aria-label="关闭提示"
|
||
className="shrink-0 border-0 bg-transparent text-inherit text-ui leading-none p-0 opacity-70 hover:opacity-100">×</button>
|
||
)}
|
||
</div>
|
||
)
|
||
}
|
||
|
||
/** 数据回显(.val)与条件告警(.linehint):规范 §7 G2 —— 版面不放成段解释文字 */
|
||
export function Value({ children, muted, className }: { children: ReactNode; muted?: boolean; className?: string }) {
|
||
return <div className={cn('text-caption text-ink-2 leading-[1.6] break-anywhere', muted && 'text-faint', className)}>{children}</div>
|
||
}
|
||
|
||
export function LineHint({ children, className }: { children: ReactNode; className?: string }) {
|
||
return <div className={cn('flex items-baseline gap-1.5 text-[10.5px] leading-[1.5] text-warn mt-[6px]', className)}>{children}</div>
|
||
}
|
||
|
||
/** 等宽小字(路径 / id / 枚举码) */
|
||
export function Mono({ children, className, title }: { children: ReactNode; className?: string; title?: string }) {
|
||
return <span title={title} className={cn('font-mono text-[10.5px] tracking-[-.2px]', className)}>{children}</span>
|
||
}
|
||
|
||
/** 文字链(.link):导航用 <Link>,这里只给「打开外部资源 / 下载」 */
|
||
export function ExtLink({ href, children, className, download }: { href: string; children: ReactNode; className?: string; download?: boolean | string }) {
|
||
return <a href={href} download={download} target="_blank" rel="noreferrer" className={cn('text-success text-caption no-underline hover:underline', className)}>{children}</a>
|
||
}
|
||
|
||
/** 卡片行(.cardrow):标题 + 说明 + 右侧动作,列表里重复项的统一骨架 */
|
||
export function CardRow({ title, children, right, className, tone = 'border' }: {
|
||
title?: ReactNode; children?: ReactNode; right?: ReactNode; className?: string; tone?: 'border' | 'plain'
|
||
}) {
|
||
return (
|
||
<div className={cn('flex justify-between gap-3 items-start py-3', tone === 'border' && 'border-b border-active last:border-b-0', className)}>
|
||
<div className="min-w-0 flex-1">
|
||
{title && <h4 className="m-0 mb-1 text-[12.5px] font-semibold">{title}</h4>}
|
||
{children && <div className="[&_p]:m-[3px]_0 [&_p]:text-caption [&_p]:text-muted [&_p]:leading-[1.55]">{children}</div>}
|
||
</div>
|
||
{right && <div className="flex items-center gap-2 shrink-0 [--ctl-h:var(--ctl-h-md)]">{right}</div>}
|
||
</div>
|
||
)
|
||
}
|
||
|
||
/** 元信息一行(.meta) */
|
||
export function Meta({ children, className }: { children: ReactNode; className?: string }) {
|
||
return <div className={cn('flex gap-[9px] items-center text-muted text-tiny flex-wrap min-w-0', className)}>{children}</div>
|
||
}
|
||
|
||
/** 行内动作组(.actions) */
|
||
export function Actions({ children, className }: { children: ReactNode; className?: string }) {
|
||
return <div className={cn('flex items-center gap-[7px] flex-wrap mt-2 min-w-0 [--ctl-h:var(--ctl-h-md)]', className)}>{children}</div>
|
||
}
|
||
|
||
/** 通用栅格:.grid(4) .grid2 .grid3 .grid4 —— 1fr 轨道配 min-w-0,防止内容把列顶宽 */
|
||
export function Grid({ children, cols = 4, gap = 12, className, min = 0 }: {
|
||
children: ReactNode; cols?: number | string; gap?: number; className?: string; min?: number
|
||
}) {
|
||
return (
|
||
<div className={cn('grid min-w-0 [&>*]:min-w-0', className)}
|
||
style={{ gridTemplateColumns: `repeat(${cols},minmax(${min}px,1fr))`, gap }}>
|
||
{children}
|
||
</div>
|
||
)
|
||
}
|