wealth/packages/island/src/atoms/skeleton/Skeleton.tsx

55 lines
1.4 KiB
TypeScript

import { Element } from '../../primitives/element';
import { cn } from '../../tools/cn';
import { FlexBox } from '../flex-box';
import { LAST_LINE_WIDTH } from './constants';
import type { SkeletonProps } from './types';
import { skeletonVariants } from './variants';
/** One placeholder block, hidden from screen readers. Group blocks in a SkeletonRegion. */
export function Skeleton({
shape = 'text',
width,
height,
lines = 1,
className,
style,
...props
}: SkeletonProps) {
if (shape === 'text' && lines > 1) {
// The wrapper takes the caller's class and width; each line keeps the plain text block.
return (
<FlexBox
tag="span"
direction="flex-col"
aria-hidden
className={cn('gap-2.5', className)}
style={{ width, ...style }}
{...props}
>
{Array.from({ length: lines }, (_, line) => (
<Element
tag="span"
// Static placeholders that never reorder, so the index is a stable key.
key={line}
className={skeletonVariants({ shape })}
style={{
height,
width: line === lines - 1 ? LAST_LINE_WIDTH : '100%',
}}
/>
))}
</FlexBox>
);
}
return (
<Element
tag="span"
aria-hidden
className={cn(skeletonVariants({ shape }), className)}
style={{ width, height, ...style }}
{...props}
/>
);
}