zasuiv0.1
GitHub
Menú

Dashboard

Chart Placeholder

Silueta estática de un gráfico mientras su llamada sigue en vuelo; tiene la forma y el alto del gráfico final para que no salte el layout.

Variantes

Instalación

Con la CLI de zasui (ver Introducción): instala el componente con sus dependencias y queda listo para usar.

pnpm dlx @medialityc/zasui add chart-placeholder
Ver el código
import { cn } from '@/lib/utils';

export type ChartPlaceholderVariant = 'line' | 'area' | 'bar' | 'donut' | 'list';

interface ChartPlaceholderProps {
	/** Forma del gráfico que se está esperando. */
	variant: ChartPlaceholderVariant;
	/** Alto en px; por defecto el de los gráficos del dashboard (280). */
	height?: number;
	className?: string;
}

/* Alturas fijas (en %) de las barras: siempre las mismas para que la silueta
   sea estática y no parezca un dato real. */
const BAR_HEIGHTS = [45, 70, 55, 85, 40, 65, 75, 50, 60, 35];

const SHAPE = 'text-gray-200 dark:text-gray-700';

function AxisFrame({ children }: { children: React.ReactNode }) {
	return (
		<div className='relative h-full w-full'>
			{/* Rejilla horizontal, como la de ApexCharts. */}
			<div className='absolute inset-0 flex flex-col justify-between'>
				{Array.from({ length: 5 }).map((_, i) => (
					<div
						key={i}
						className='border-border border-t border-dashed'
					/>
				))}
			</div>
			{children}
		</div>
	);
}

function LinePlaceholder({ filled }: { filled: boolean }) {
	return (
		<AxisFrame>
			<svg
				viewBox='0 0 300 100'
				preserveAspectRatio='none'
				className={cn('absolute inset-0 h-full w-full', SHAPE)}
				aria-hidden='true'
			>
				{filled ? (
					<path
						d='M0,70 C40,55 60,80 100,60 C140,40 170,65 200,45 C230,28 260,40 300,25 L300,100 L0,100 Z'
						fill='currentColor'
						opacity={0.5}
					/>
				) : null}
				<path
					d='M0,70 C40,55 60,80 100,60 C140,40 170,65 200,45 C230,28 260,40 300,25'
					fill='none'
					stroke='currentColor'
					strokeWidth={3}
					vectorEffect='non-scaling-stroke'
				/>
				<path
					d='M0,85 C40,78 70,88 100,75 C140,62 170,80 200,68 C230,58 260,66 300,55'
					fill='none'
					stroke='currentColor'
					strokeWidth={3}
					vectorEffect='non-scaling-stroke'
				/>
			</svg>
		</AxisFrame>
	);
}

function BarPlaceholder() {
	return (
		<AxisFrame>
			<div className='absolute inset-0 flex items-end justify-around gap-2 px-2'>
				{BAR_HEIGHTS.map((h, i) => (
					<div
						key={i}
						className='w-full max-w-8 rounded-t-md bg-gray-200 dark:bg-gray-700'
						style={{ height: `${h}%` }}
					/>
				))}
			</div>
		</AxisFrame>
	);
}

function DonutPlaceholder() {
	return (
		<div className='flex h-full flex-col items-center justify-center gap-4'>
			<svg
				viewBox='0 0 100 100'
				className={cn('h-3/4 max-h-48 w-auto', SHAPE)}
				aria-hidden='true'
			>
				<circle
					cx='50'
					cy='50'
					r='38'
					fill='none'
					stroke='currentColor'
					strokeWidth={16}
				/>
			</svg>
			<div className='flex flex-wrap justify-center gap-3'>
				{Array.from({ length: 3 }).map((_, i) => (
					<div
						key={i}
						className='h-3 w-16 rounded bg-gray-200 dark:bg-gray-700'
					/>
				))}
			</div>
		</div>
	);
}

function ListPlaceholder() {
	return (
		<div className='flex h-full flex-col gap-2 overflow-hidden'>
			{Array.from({ length: 5 }).map((_, i) => (
				<div
					key={i}
					className='flex items-center justify-between gap-3 rounded-lg border border-gray-100 p-3 dark:border-gray-800'
				>
					<div className='flex min-w-0 items-center gap-3'>
						<div className='h-8 w-8 shrink-0 rounded-full bg-gray-200 dark:bg-gray-700' />
						<div className='min-w-0 space-y-1.5'>
							<div className='h-3 w-32 max-w-full rounded bg-gray-200 dark:bg-gray-700' />
							<div className='h-2.5 w-20 max-w-full rounded bg-gray-200 dark:bg-gray-700' />
						</div>
					</div>
					<div className='h-3 w-16 shrink-0 rounded bg-gray-200 dark:bg-gray-700' />
				</div>
			))}
		</div>
	);
}

/**
 * Gráfico estático y sin datos que ocupa el sitio de un panel del dashboard
 * mientras su llamada sigue en vuelo. Cada panel hace streaming por su cuenta,
 * así que la silueta tiene la forma y el alto del gráfico final: al llegar los
 * datos no salta el layout.
 */
export function ChartPlaceholder({
	variant,
	height = 280,
	className,
}: ChartPlaceholderProps) {
	return (
		<div
			className={cn('w-full animate-pulse', className)}
			style={{ height }}
			aria-busy='true'
			aria-live='polite'
		>
			<span className='sr-only'>Cargando gráfico…</span>
			{variant === 'line' || variant === 'area' ? (
				<LinePlaceholder filled={variant === 'area'} />
			) : variant === 'bar' ? (
				<BarPlaceholder />
			) : variant === 'donut' ? (
				<DonutPlaceholder />
			) : (
				<ListPlaceholder />
			)}
		</div>
	);
}