import * as React from 'react'; import { Box, sx } from '@hanzo/ui' import { cn } from './utils'; // --------------------------------------------------------------------------- // Skeleton // --------------------------------------------------------------------------- export interface SkeletonProps extends Omit, 'color'> { /** A skeleton with nothing said about it is loading; that is what one is for. */ readonly loading?: boolean; /** When true, the Skeleton wraps its single child element instead of adding a wrapper div. */ readonly asChild?: boolean; } // A skeleton reserves space. It does not move. // // It used to shimmer via a per-element `background-position` animation, which // repaints the element's whole box every frame. Under a 4x CPU throttle that // costs 33.6fps at 1460 concurrent skeletons and 13.8fps at 3000. Rewriting it // as a per-element `transform` sheen — the textbook "animate transform only" // fix — measured WORSE: 23.0fps and 5.4fps, because it also adds a layer and a // clip per element. Holding still costs 120fps at every n. // // The "work is in flight" signal is below: one animated element // for the whole page instead of one per skeleton. Full numbers and reasoning // live next to the CSS, in section 6c of @luxfi/ui/tokens.css. const SKELETON_CLASSES = 'lux-skeleton rounded-sm'; export const Skeleton = React.forwardRef( function Skeleton(props, ref) { const { loading = true, asChild, className, children, style, ...rest } = props; if (!loading) { if (asChild && React.isValidElement(children)) return children; return ( { children } ); } return (
{ children }
); }, ); // --------------------------------------------------------------------------- // SkeletonCircle // --------------------------------------------------------------------------- export interface SkeletonCircleProps extends React.HTMLAttributes { readonly size?: string | number; readonly loading?: boolean; } export const SkeletonCircle = React.forwardRef( function SkeletonCircle(props, ref) { const { size = 40, loading = true, className, ...rest } = props; const dimension = typeof size === 'number' ? `${ size }px` : size; return ( ); }, ); // --------------------------------------------------------------------------- // SkeletonText // --------------------------------------------------------------------------- export interface SkeletonTextProps extends React.HTMLAttributes { readonly noOfLines?: number; readonly loading?: boolean; } export const SkeletonText = React.forwardRef( function SkeletonText(props, ref) { const { noOfLines = 3, loading = true, className, ...rest } = props; return ( { Array.from({ length: noOfLines }).map((_, index) => ( )) } ); }, ); // --------------------------------------------------------------------------- // LoadingBar // --------------------------------------------------------------------------- /** * The page-level "work is in flight" signal — one animated element, however * many skeletons are on screen. Mount it while any request is outstanding. * * It is `position: fixed` and 2px tall, so it is out of flow: it cannot shift * layout (CLS 0) and it cannot tint content. Under prefers-reduced-motion the * sweep stops and the track stays lit, so the state is still legible. * * This is what replaces a bare spinner. A spinner says "something is * happening somewhere"; a skeleton plus this bar says "here is the shape of * what is coming, and it is still coming". */ export function LoadingBar({ loading = true }: { readonly loading?: boolean }) { if (!loading) return null; return ; }