skeleton.tsx
Overview
skeleton.tsx provides reusable React functional components that render skeleton UI placeholders. These placeholders simulate loading content with animated, stylized shapes, enhancing perceived performance and user experience during data fetching or asynchronous operations. It defines a base Skeleton component and two composed skeleton components: ParagraphSkeleton and CardSkeleton, which represent common UI patterns while loading.
The components rely on utility styling (presumably Tailwind CSS classes) and a helper function cn (classNames) to compose CSS class strings dynamically.
Components and Functions
1. Skeleton
Description
A generic skeleton loader component rendering a div with an animated pulsing effect and rounded corners. It acts as the base visual unit for skeleton screens.
Signature
function Skeleton({
className,
...props
}: React.HTMLAttributes<HTMLDivElement>): JSX.Element
Parameters
className(optional): A string for additional CSS classes to customize the skeleton’s appearance (e.g., size, shape)....props: Any other valid HTML attributes for adivelement (e.g.,style,id, event handlers).
Returns
A
divelement styled with animation and background color to simulate a loading placeholder.
Usage Example
<Skeleton className="h-6 w-40 rounded-sm" />
This renders a small rectangular skeleton bar.
2. ParagraphSkeleton
Description
A composed skeleton component mimicking a paragraph or profile preview layout, typically used to indicate a loading user profile or text snippet. It consists of a circular skeleton representing an avatar and two horizontal skeleton bars representing text lines.
Signature
function ParagraphSkeleton(): JSX.Element
Parameters
None.
Returns
A React fragment containing a flex container with:
A circular
Skeletonof size 48x48 pixels (h-12 w-12 rounded-full).Two stacked rectangular
Skeletonbars of differing widths (250px and 200px).
Usage Example
<ParagraphSkeleton />
This inserts a paragraph-like skeleton placeholder in the UI.
3. CardSkeleton
Description
A composed skeleton component representing a card UI loading placeholder. It features a large rectangular image skeleton at the top and two text line skeletons below.
Signature
function CardSkeleton(): JSX.Element
Parameters
None.
Returns
A vertical flex container with:
A large rectangular
Skeleton(125x250 pixels, rounded corners).Two stacked rectangular
Skeletonbars beneath it (250px and 200px wide).
Usage Example
<CardSkeleton />
This renders a card-like loading skeleton with an image placeholder and text lines.
Implementation Details
cnutility: This helper function (imported from@/lib/utils) merges static and dynamic CSS classes into a single string. It allows conditional and multiple class names to be combined cleanly.Animation and style: Components use Tailwind CSS classes like
animate-pulsefor smooth pulsing animation,rounded-md/rounded-fullfor shape, andbg-mutedfor a muted background color to create the skeleton effect.Props forwarding: The
Skeletoncomponent forwards all other props (...props) to the rendereddiv, enabling customization and event handling.
Interaction with Other Parts of the Application
cnUtility: Relies on a utility function for class name composition, indicating integration with a shared utility library or common codebase.Styling Framework: Uses Tailwind CSS classes, implying the application is styled with Tailwind or a similar utility-first CSS framework.
Loading States: These skeleton components are intended to be placed in UI locations where asynchronous data loading occurs, replacing or preceding actual content components.
Composable UI:
ParagraphSkeletonandCardSkeletoncompose the baseSkeletoncomponent, promoting reusable and maintainable UI patterns.
Visual Diagram
componentDiagram
direction LR
Skeleton <|-- ParagraphSkeleton : uses
Skeleton <|-- CardSkeleton : uses
Skeleton: +className: string (optional)
Skeleton: +props: HTMLDivElement attributes
Skeleton: +Returns a pulsing div placeholder
ParagraphSkeleton: +Returns avatar + two lines skeleton
CardSkeleton: +Returns image + two lines skeleton
Summary
skeleton.tsx is a focused utility module providing skeleton loaders for React applications using Tailwind CSS. It encapsulates a base animated skeleton component and two common UI skeleton patterns (ParagraphSkeleton and CardSkeleton). These help improve UX by visually indicating loading states with consistent, reusable components that integrate seamlessly with the app's styling and utility libraries.