mx / dengtaishan-src /src /app /ui.tsx
Ethscriptions's picture
登泰山记:渲染引擎、九个篇章场景、程序化配乐音效、MP4 导出、发布文案与封面(初版)
6fff8e3 verified
Raw History Blame Contribute Delete
3.46 kB
// 轻量 UI 原件
import { ReactNode, useCallback, useEffect, useState } from 'react'
export function cx(...a: (string | false | null | undefined)[]) {
return a.filter(Boolean).join(' ')
}
export function Section({ id, kicker, title, desc, children }: { id: string; kicker: string; title: string; desc?: string; children: ReactNode }) {
return (
<section id={id} className="mx-auto w-full max-w-[1280px] px-4 sm:px-8 py-10 sm:py-14">
<div className="mb-6 sm:mb-8 flex flex-col gap-2">
<div className="text-[12px] tracking-[0.4em] text-[#c9a86a] uppercase">{kicker}</div>
<h2 className="font-serif-b text-[26px] sm:text-[34px] leading-tight text-[#f3eee4]">{title}</h2>
{desc && <p className="text-[14px] sm:text-[15px] leading-7 text-[#a39d92] max-w-[760px]">{desc}</p>}
</div>
{children}
</section>
)
}
export function Chip({ active, onClick, children, disabled, title }: { active?: boolean; onClick?: () => void; children: ReactNode; disabled?: boolean; title?: string }) {
return (
<button
type="button"
title={title}
disabled={disabled}
onClick={onClick}
className={cx(
'h-9 px-4 rounded-full border text-[14px] transition-colors whitespace-nowrap',
active ? 'border-[#c9a86a] bg-[#c9a86a] text-[#16130e] font-semibold' : 'border-white/15 text-[#ddd6c9] hover:border-[#c9a86a]/70 hover:text-white',
disabled && 'opacity-40 cursor-not-allowed',
)}
>
{children}
</button>
)
}
export function Switch({ on, onChange, label }: { on: boolean; onChange: (v: boolean) => void; label: ReactNode }) {
return (
<button type="button" onClick={() => onChange(!on)} className="flex items-center gap-3 text-[14px] text-[#ddd6c9]">
<span className={cx('relative inline-block h-6 w-11 rounded-full transition-colors', on ? 'bg-[#c9a86a]' : 'bg-white/15')}>
<span className={cx('absolute top-0.5 h-5 w-5 rounded-full bg-white shadow transition-all', on ? 'left-[22px]' : 'left-0.5')} />
</span>
{label}
</button>
)
}
let toastFn: ((s: string) => void) | null = null
export function toast(s: string) {
toastFn?.(s)
}
export function Toaster() {
const [msg, setMsg] = useState<string | null>(null)
useEffect(() => {
let tm: number | undefined
toastFn = (s) => {
setMsg(s)
clearTimeout(tm)
tm = window.setTimeout(() => setMsg(null), 1800)
}
return () => {
toastFn = null
}
}, [])
if (!msg) return null
return (
<div className="fixed left-1/2 bottom-8 -translate-x-1/2 z-[100] rounded-full bg-[#c9a86a] text-[#16130e] px-5 py-2.5 text-[14px] font-semibold shadow-2xl">{msg}</div>
)
}
export function useCopy() {
return useCallback(async (s: string, what = '内容') => {
let ok = false
try {
await navigator.clipboard.writeText(s)
ok = true
} catch {
const ta = document.createElement('textarea')
ta.value = s
ta.style.position = 'fixed'
ta.style.opacity = '0'
document.body.appendChild(ta)
ta.select()
try {
ok = document.execCommand('copy')
} catch {
ok = false
}
ta.remove()
}
toast(ok ? `已复制${what}` : '复制失败,请手动选择文本复制')
}, [])
}
export function fmtTime(t: number) {
const m = Math.floor(t / 60)
const s = Math.floor(t % 60)
return `${m}:${String(s).padStart(2, '0')}`
}