react-content-loader

repository·master·Indexed 12 days ago

https://github.com/danilowoz/react-content-loader

An SVG-powered component library for creating placeholder loading animations (skeleton screens) in React and React Native applications. Version 6.2.1 provides a ContentLoader component for custom SVG shapes and pre-built presets such as Facebook, Instagram, Code, List, and BulletList. Supports customization of animation speed, colors, and opacity, with specific implementations for web and native environments.

Tokens
4K
Snippets
17
Records
21
Agent score
96%

What's inside react-content-loader

  1. Use react-content-loader in React Native

    master

    For React Native, import from react-content-loader/native.

    Important: Because React Native does not have native SVG support, you must use the named exports Rect and Circle (or other shapes) provided by react-content-loader/native to ensure compatibility.

    import ContentLoader, { Rect, Circle } from 'react-content-loader/native'
    
    const MyLoader = () => (
      <ContentLoader viewBox="0 0 380 70">
        <Circle cx="30" cy="30" r="30" />
        <Rect x="80" y="17" rx="4" ry="4" width="300" height="13" />
        <Rect x="80" y="40" rx="3" ry="3" width="250" height="10" />
      </ContentLoader>
    )
  2. Create a custom loader with ContentLoader

    master

    To create a custom skeleton loader, use the ContentLoader component. You can define the overall dimensions, speed, and colors, and then nest standard SVG shapes (like <rect />) as children to define the skeleton structure.

    It is recommended to use the online tool to generate the SVG shape code for your custom design.

    Key props for ContentLoader:

    • height: Height of the SVG container.
    • speed: Animation speed.
    • backgroundColor: Color of the background.
    • foregroundColor: Color of the animated foreground.
    • viewBox: The SVG viewBox string.

    Note: Only SVG shapes should be used as children.

    const MyLoader = () => (
      <ContentLoader
        height={140}
        speed={1}
        backgroundColor={'#333'}
        foregroundColor={'#999'}
        viewBox="0 0 380 70"
      >
        {/* Only SVG shapes */}
        <rect x="0" y="0" rx="5" ry="5" width="70" height="70" />
        <rect x="80" y="17" rx="4" ry="4" width="300" height="13" />
        <rect x="80" y="40" rx="3" ry="3" width="250" height="10" />
      </ContentLoader>
    )
  3. Fix SSR hydration or snapshot mismatch errors

    master

    Because the component generates random IDs to link the SVG element with its background style, Server-Side Rendering (SSR) or snapshot testing can cause mismatches between the server-generated ID and the client-generated ID.

    To resolve this, provide a stable string to the uniqueKey prop.

    import { Facebook } from 'react-content-loader'
    
    const MyFacebookLoader = () => <Facebook uniqueKey="my-random-value" />
  4. Make loaders responsive

    master

    The output of react-content-loader is a standard SVG. To make it responsive (e.g., for mobile), treat it like any other SVG by providing a viewBox and using CSS to control the width (e.g., width: 100%).

    import { Code } from 'react-content-loader'
    
    const MyCodeLoader = () => (
      <Code
        width={100}
        height={100}
        viewBox="0 0 100 100"
        style={{ width: '100%' }}
      />
    )
  5. Fix alpha channel issues in Safari/iOS

    master

    Safari and iOS do not respect the alpha channel when using rgba values for backgroundColor or foregroundColor. To achieve transparency in these browsers, use rgb values for the colors and set the opacity using the backgroundOpacity and foregroundOpacity props instead.

    {/* Instead of rgba, use rgb + opacity props for Safari/iOS compatibility */}
    <ContentLoader
      backgroundColor="rgb(0,0,0)"
      foregroundColor="rgb(0,0,0)"
      backgroundOpacity={0.06}
      foregroundOpacity={0.12}>
  6. Use pre-built loader components

    master

    The package provides several ready-to-use components that mimic common UI patterns. You can import and use them directly in your React application.

    Available pre-built components:

    • Facebook
    • Instagram
    • Code
    • List
    • BulletList
    import { Facebook } from 'react-content-loader'
    
    const MyFacebookLoader = () => <Facebook />
  7. Create custom loaders (Web)

    master

    In custom mode, you use the ContentLoader component as an SVG wrapper. Inside it, you can place standard SVG shapes to define your loading pattern.

    const MyLoader = () => (
      <ContentLoader viewBox="0 0 380 70">
        {/* Only SVG shapes */}
        <rect x="0" y="0" rx="5" ry="5" width="70" height="70" />
        <rect x="80" y="17" rx="4" ry="4" width="300" height="13" />
        <rect x="80" y="40" rx="3" ry="3" width="250" height="10" />
      </ContentLoader>
    )
  8. Use react-content-loader presets (Web)

    master

    You can use pre-built loader presets (like Facebook) by importing them directly from react-content-loader.

    import ContentLoader, { Facebook } from 'react-content-loader'
    
    const MyLoader = () => <ContentLoader />
    const MyFacebookLoader = () => <Facebook />
  9. Configure ContentLoader options

    master

    The ContentLoader component accepts several props to customize the animation and appearance.

    PropTypeEnvironmentDescription
    animatebooleanWeb & NativeDefaults to true. Set to false to disable animations.
    speednumberWeb & NativeAnimation speed in seconds. Defaults to 1.2.
    viewBoxstringWeb & NativeSets the custom viewBox value.
    rtlbooleanWeb & NativeEnables right-to-left content. Defaults to false.
    backgroundColorstringWeb & NativeBackground color of the animation. Defaults to #f5f6f7.
    foregroundColorstringWeb & NativeForeground color of the animation. Defaults to #eee.
    backgroundOpacitynumberWeb & NativeBackground opacity (0 to 1). Defaults to 1.
    foregroundOpacitynumberWeb & NativeAnimation opacity (0 to 1). Defaults to 1.
    beforeMaskJSX.ElementWeb & NativeDefine custom shapes to render before the content.
    titlestringWeb onlyAccessibility title. Defaults to Loading.... Use '' to remove.
    baseUrlstringWeb onlyUsed if your document has a <base url="/" />.
    gradientRationumberWeb onlyWidth of the animated gradient as a fraction of the viewBox width.
    styleReact.CSSPropertiesWeb onlyCustom CSS styles.
    uniqueKeystringWeb onlyUse the same value as the component key to solve SSR inconsistencies.