Download src/components/ui.jsx from cheumk/studio: direct link, hf CLI and curl.
- Browser
- Download file 2.78 kB
-
https://huggingface.co/spaces/cheumk/studio/resolve/main/src/components/ui.jsx
- Command line
-
hf download hf://spaces/cheumk/studio/src/components/ui.jsx
-
curl -L -o ui.jsx https://huggingface.co/spaces/cheumk/studio/resolve/main/src/components/ui.jsx
2.78 kB
| /* 共用小元件 */ | |
| import { useState } from 'react' | |
| import { ChevronDown, ChevronRight } from 'lucide-react' | |
| export const inputCls = | |
| 'w-full bg-field border border-edge px-3 py-2 rounded-lg text-sm focus:outline-none focus:border-purple-500 placeholder:text-muted' | |
| export function Field({ label, hint, children }) { | |
| return ( | |
| <label className="block"> | |
| {label && <span className="block text-xs text-muted mb-1">{label}</span>} | |
| {children} | |
| {hint && <span className="block text-[11px] text-muted mt-1 leading-snug">{hint}</span>} | |
| </label> | |
| ) | |
| } | |
| export function Btn({ children, variant = 'ghost', className = '', ...props }) { | |
| const styles = { | |
| primary: 'bg-purple-600 hover:bg-purple-700 text-white', | |
| ghost: 'bg-field hover:bg-edge', | |
| danger: 'bg-red-600/90 hover:bg-red-600 text-white', | |
| subtle: 'hover:bg-field', | |
| } | |
| return ( | |
| <button | |
| type="button" | |
| className={`inline-flex items-center justify-center gap-1.5 px-3 py-2 rounded-lg text-sm transition disabled:opacity-40 disabled:cursor-not-allowed ${styles[variant]} ${className}`} | |
| {...props} | |
| > | |
| {children} | |
| </button> | |
| ) | |
| } | |
| export function Section({ title, defaultOpen = true, right, children }) { | |
| const [open, setOpen] = useState(defaultOpen) | |
| return ( | |
| <div className="border-b border-edge"> | |
| <div className="flex items-center gap-1 px-3 py-2"> | |
| <button type="button" onClick={() => setOpen(!open)} className="flex items-center gap-1 flex-1 text-left text-xs font-semibold uppercase tracking-wide text-muted"> | |
| {open ? <ChevronDown className="w-3.5 h-3.5" /> : <ChevronRight className="w-3.5 h-3.5" />} | |
| {title} | |
| </button> | |
| {right} | |
| </div> | |
| {open && <div className="px-3 pb-3 space-y-3">{children}</div>} | |
| </div> | |
| ) | |
| } | |
| /** 觸發瀏覽器下載 */ | |
| export function download(filename, text, type = 'application/json') { | |
| const blob = new Blob([text], { type }) | |
| const url = URL.createObjectURL(blob) | |
| const a = document.createElement('a') | |
| a.href = url | |
| a.download = filename | |
| document.body.appendChild(a) | |
| a.click() | |
| a.remove() | |
| setTimeout(() => URL.revokeObjectURL(url), 1000) | |
| } | |
| /** 開檔案選擇器讀 JSON */ | |
| export function pickJsonFile() { | |
| return new Promise((resolve, reject) => { | |
| const input = document.createElement('input') | |
| input.type = 'file' | |
| input.accept = 'application/json,.json' | |
| input.onchange = async () => { | |
| const file = input.files?.[0] | |
| if (!file) return reject(new Error('沒有選擇檔案')) | |
| try { resolve(JSON.parse(await file.text())) } catch { reject(new Error('不是合法的 JSON 檔')) } | |
| } | |
| input.click() | |
| }) | |
| } | |
| export const stamp = () => new Date().toISOString().slice(0, 16).replace(/[:T]/g, '-') | |