fishies-ui/src/loader.tsx
2026-10-07 19:32:19 +00:00

35 lines
2.5 KiB
TypeScript

'use client'
import type { CSSProperties } from 'react'
import './loader.css'
export const SPINNER_SVG = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 48 48">' +
'<style>.a{transform-origin:24px 24px;animation:s 1.1s linear infinite}' +
'.b{opacity:0;animation:u 1.8s ease-in infinite}.c{animation-delay:.6s}.d{animation-delay:1.2s}' +
'@keyframes s{to{transform:rotate(360deg)}}' +
'@keyframes u{0%{opacity:0;transform:translateY(7px)}30%{opacity:.95}100%{opacity:0;transform:translateY(-9px)}}' +
'@media (prefers-reduced-motion:reduce){.a{animation-duration:3.6s}.b{animation:none;opacity:.8}}</style>' +
'<circle cx="24" cy="24" r="21" fill="#0b2a3c" fill-opacity=".74" stroke="#c4ecfa" stroke-opacity=".24"/>' +
'<circle cx="24" cy="24" r="16" fill="none" stroke="#c4ecfa" stroke-opacity=".13" stroke-width="3"/>' +
'<circle class="a" cx="24" cy="24" r="16" fill="none" stroke="#ffc46b" stroke-width="3" stroke-linecap="round" stroke-dasharray="25 76"/>' +
'<g fill="#dff6ff" fill-opacity=".16" stroke="#dff6ff" stroke-width="1.1">' +
'<circle class="b" cx="21.5" cy="26" r="2.7"/><circle class="b c" cx="27" cy="27.5" r="1.9"/><circle class="b d" cx="23.5" cy="30" r="1.3"/>' +
'</g></svg>'
export function svgDataUri(svg: string) {
return `url("data:image/svg+xml,${svg.replace(/"/g, "'").replace(/[<>#%{}]/g, c => encodeURIComponent(c))}")`
}
export function Spinner({ size = 32, label = 'Loading', className, style }: { size?: number; label?: string; className?: string; style?: CSSProperties }) {
return <span className={`fx-spinner${className ? ` ${className}` : ''}`} role="status" aria-label={label}
style={{ '--fx-size': `${size}px`, ...style } as CSSProperties} dangerouslySetInnerHTML={{ __html: SPINNER_SVG.replace('<svg ', '<svg aria-hidden="true" focusable="false" ') }}/>
}
/** A progress bar: a glass channel filling with gold, a bubble riding its leading edge; with no value the gold sweeps. */
export function Progress({ value, label = 'Loading', className, style }: { value?: number; label?: string; className?: string; style?: CSSProperties }) {
const known = typeof value === 'number' && Number.isFinite(value)
const percent = known ? Math.round(Math.min(1, Math.max(0, value)) * 100) : undefined
return <span className={`fx-progress${className ? ` ${className}` : ''}`} role="progressbar" aria-label={label}
aria-valuemin={0} aria-valuemax={100} aria-valuenow={percent} data-indeterminate={known ? undefined : ''}
style={{ '--fx-value': known ? percent! / 100 : 0, ...style } as CSSProperties}><i/></span>
}