Install react-loading-skeleton
masterInstall the package using npm or yarn:
yarn add react-loading-skeleton
npm install react-loading-skeletonrepository·master·Indexed 26 days ago
https://github.com/dvtng/react-loading-skeletonA React library for creating animated loading skeletons that adapt to an application's styles and typography. Version 3.5.0 provides the Skeleton component for rendering single or multiple loading lines, and the SkeletonTheme component for applying consistent baseColor, highlightColor, and dimensions across a component hierarchy.
Install the package using npm or yarn:
yarn add react-loading-skeleton
npm install react-loading-skeletonSkeleton with a specific height is slightly larger than expected, it is due to CSS line-height. To ensure the container matches the skeleton height exactly, set the container's line-height to 1.If a Skeleton has 0 width inside a parent with display: flex, it is because the skeleton has no intrinsic width. Fix this by applying flex: 1 to the container using the containerClassName prop.
<div style={{ display: 'flex' }}>
<Skeleton containerClassName="flex-1" />
</div>To customize the gradient of the highlight animation, use the customHighlightBackground prop. Note that when using this prop, baseColor and highlightColor props are ignored, but you can still reference their CSS variables (var(--base-color) and var(--highlight-color)).
<Skeleton customHighlightBackground="linear-gradient(90deg, var(--base-color) 40%, var(--highlight-color) 50%, var(--base-color) 60%)" />Import Skeleton and its required CSS to render loading states. You can render a single line or multiple lines using the count prop.
import Skeleton from 'react-loading-skeleton'
import 'react-loading-skeleton/dist/skeleton.css'
<Skeleton /> // Simple, single-line loading skeleton
<Skeleton count={5} /> // Five-line loading skeletonUse the SkeletonTheme component to wrap a section of your application. This will apply the specified baseColor and highlightColor to all Skeleton components within that React hierarchy.
import Skeleton, { SkeletonTheme } from 'react-loading-skeleton';
return (
<SkeletonTheme baseColor="#202020" highlightColor="#444">
<p>
<Skeleton count={3} />
</p>
</SkeletonTheme>
);The Skeleton component accepts several props to control its appearance and behavior.
Skeleton-only props:
count (number, default: 1): The number of lines to render. Decimal values (e.g., 3.5) render partial lines.wrapper (React.FunctionComponent): A custom component to wrap individual skeleton elements.circle (boolean, default: false): Sets border-radius to 50%.className (string): Custom class name for individual skeleton elements (added alongside react-loading-skeleton).containerClassName (string): Custom class name for the <span> wrapping the elements.containerTestId (string): Adds a data-testid attribute to the container.style (React.CSSProperties): Escape hatch for advanced styling (props like width take priority).Shared props (Skeleton and SkeletonTheme):
baseColor (string, default: #ebebeb): Background color.highlightColor (string, default: #f5f5f5): Animation highlight color.width (string | number, default: 100%): Width of the skeleton.height (string | number, default: font size): Height of each line.borderRadius (string | number, default: 0.25rem): Border radius.inline (boolean, default: false): If true, no <br /> is inserted after skeletons.duration (number, default: 1.5): Animation length in seconds.direction ('ltr' | 'rtl', default: 'ltr'): Animation direction.enableAnimation (boolean, default: true): Toggles the animation.customHighlightBackground (string): Overrides the background-image for the highlight element.SkeletonTheme component to apply specific styling options to all Skeleton components within that tree. This uses a provider pattern to pass SkeletonStyleProps down via context. Any Skeleton component rendered as a descendant of SkeletonTheme will inherit these styles instead of using the default theme.SkeletonTheme and SkeletonStyleProps to allow for advanced customization of the skeleton's appearance and styling via context or props.The Skeleton component inherits styling properties from SkeletonStyleProps. These properties can be passed directly to the Skeleton component or provided via a SkeletonTheme.
| Prop | Type | Description |
|---|---|---|
baseColor | string | The background color of the skeleton. |
highlightColor | string | The color of the animation highlight. |
width | string | number | The width of the skeleton. |
height | string | number | The height of the skeleton. |
borderRadius | string | number | The border radius of the skeleton. |
direction | 'ltr' | 'rtl' | The direction of the animation. 'rtl' reverses the animation direction. |
duration | number | The duration of the animation in seconds. |
enableAnimation | boolean | Whether to enable the animation. If false, the highlight pseudo-element is hidden. |
customHighlightBackground | string | A custom background value for the highlight effect. |
inline | boolean | If true, skeletons are rendered without <br /> tags between them. |
The Skeleton component accepts the following props to control its appearance and behavior:
| Prop | Type | Description |
|---|---|---|
count | number | Number of skeleton elements to render. Supports decimals for fractional widths. |
wrapper | React.FunctionComponent<PropsWithChildren<unknown>> | A component that wraps each individual skeleton element. |
className | string | Additional CSS class applied to the skeleton <span> elements. |
containerClassName | string | CSS class applied to the outer container <span>. |
containerTestId | string | data-testid applied to the outer container <span>. |
circle | boolean | If true, sets borderRadius to 50%. |
style | CSSProperties | Standard React inline styles applied to the skeleton elements. |
...originalPropsStyleOptions | SkeletonStyleProps | Inherited props from SkeletonStyleProps (see below). |
The Skeleton component is used to display loading placeholders. It can render a single skeleton or multiple skeletons based on the count prop. It supports theming via SkeletonThemeContext, where component props take priority over context values, and the style prop has the lowest priority.
Key features:
count prop to render multiple lines. If count is a decimal (e.g., 3.5), the last skeleton will be rendered with a fractional width.circle prop to render a circular placeholder.wrapper prop to wrap each skeleton element in a custom component.inline prop (from SkeletonStyleProps) to prevent the component from adding <br /> tags between skeletons, allowing them to sit side-by-side.