⚪ SVG-Powered component to easily create skeleton loadings.

Overview

react-content-loader

Example's react-content-loader

SVG-Powered component to easily create placeholder loadings (like Facebook's cards loading).

Features

  • ⚙️  Customizable: Feel free to change the colors, speed, sizes and even RTL;
  • 👌  Plug and play: with many presets to use, see the examples;
  • ✏️  DIY: use the create-content-loader to create your own custom loaders easily;
  • 📱 React Native support: same API, as same powerful features;
  • ⚛️ Really lightweight: less than 2kB and 0 dependencies for web version;

Index

Getting Started

npm i react-content-loader --save
yarn add react-content-loader

For React Native

npm i react-content-loader react-native-svg --save
yarn add react-content-loader react-native-svg

CDN from JSDELIVR

Usage

There are two ways to use it:

1. Presets, see the examples:

import ContentLoader, { Facebook } from 'react-content-loader'

const MyLoader = () => <ContentLoader />
const MyFacebookLoader = () => <Facebook />

2. Custom mode, see the online tool

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>
)

Still not clear? Take a look at this working example at codesandbox.io Or try the components editable demo hands-on and install it from bit.dev

Native

react-content-loader can be used with React Native in the same way as web version with the same import:

1. Presets, see the examples:

import ContentLoader, { Facebook } from 'react-content-loader/native'

const MyLoader = () => <ContentLoader />
const MyFacebookLoader = () => <Facebook />

2. Custom mode

To create custom loaders there is an important difference: as React Native doesn't have any native module for SVG components, it's necessary to import the shapes from react-native-svg or use the named export Rect and Circle from react-content-loader import:

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>
)

Options

Prop name and type
Environment Description
animate?: boolean
Defaults to true
React DOM
React Native
Opt-out of animations with false
title?: string
Defaults to Loading interface...
React DOM only It's used to describe what element it is. 
Use '' (empty string) to remove.
baseUrl?: string
Defaults to an empty string
React DOM only Required if you're using <base url="/" /> document <head/>
This prop is common used as: 
<ContentLoader baseUrl={window.location.pathname} /> which will fill the SVG attribute with the relative path. Related #93.
speed?: number
Defaults to 1.2
React DOM
React Native
Animation speed in seconds.
interval?: number
Defaults to 0.25
React DOM
React Native
Interval of time between runs of the animation, 
as a fraction of the animation speed.
viewBox?: string
Defaults to undefined
React DOM
React Native
Use viewBox props to set a custom viewBox value,
for more information about how to use it,
read the article How to Scale SVG.
gradientRatio?: number
Defaults to 1.2
React DOM only Width of the animated gradient as a fraction of the view box width.
rtl?: boolean
Defaults to false
React DOM
React Native
Content right-to-left.
backgroundColor?: string
Defaults to #f5f6f7
React DOM
React Native
Used as background of animation.
foregroundColor?: string
Defaults to #eee
React DOM
React Native
Used as the foreground of animation.
backgroundOpacity?: number
Defaults to 1
React DOM only Background opacity (0 = transparent, 1 = opaque)
used to solve an issue in Safari
foregroundOpacity?: number
Defaults to 1
React DOM only Animation opacity (0 = transparent, 1 = opaque)
used to solve an issue in Safari
style?: React.CSSProperties
Defaults to {}
React DOM only
uniqueKey?: string
Defaults to random unique id
React DOM only Use the same value of prop key, 
that will solve inconsistency on the SSR, see more here.

See all options live

Examples

Facebook Style
import { Facebook } from 'react-content-loader'

const MyFacebookLoader = () => <Facebook />

Facebook Style

Instagram Style
import { Instagram } from 'react-content-loader'

const MyInstagramLoader = () => <Instagram />

Instagram Style

Code Style
import { Code } from 'react-content-loader'

const MyCodeLoader = () => <Code />

Code Style

List Style
import { List } from 'react-content-loader'

const MyListLoader = () => <List />

List Style

Bullet list Style
import { BulletList } from 'react-content-loader'

const MyBulletListLoader = () => <BulletList />

Bullet list Style

Custom Style

For the custom mode, use the online tool.

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>
)

Custom

Troubleshooting

Responsive - Mobile version

In order to avoid unexpected behavior, the package doesn't have opinioned settings. So if it needs to be responsive, have in mind that the output of package is a regular SVG, so it just needs the same attributes to become a regular SVG responsive, which means:

import { Code } from 'react-content-loader'

const MyCodeLoader = () => (
  <Code
    width={100}
    height={100}
    viewBox="0 0 100 100"
    style={{ width: '100%' }}
  />
)

Server-side rendering (SSR) - Match snapshot

As the main component generates random values to match the id of the SVG element with background style, it can encounter unexpected errors and unmatching warning on render, once the random value of id will be generated twice, in case of SSR: server and client; or in case of snapshot test: on the first match and re-running the test.

To fix it, set the prop uniqueKey, then the id will not be random anymore:

import { Facebook } from 'react-content-loader'

const MyFacebookLoader = () => <Facebook uniqueKey="my-random-value" />

Alpha is not working: Safari / iOS

When using rgba as a backgroundColor or foregroundColor value, Safari does not respect the alpha channel, meaning that the color will be opaque. To prevent this, instead of using a rgba value for backgroundColor/foregroundColor, use the rgb equivalent and move the alpha channel value to the backgroundOpacity/foregroundOpacity props.

{/* Opaque color in Safari and iOS */}
<ContentLoader
  backgroundColor="rgba(0,0,0,0.06)"
  foregroundColor="rgba(0,0,0,0.12)">


{/_ Semi-transparent color in Safari and iOS _/}
<ContentLoader
    backgroundColor="rgb(0,0,0)"
    foregroundColor="rgb(0,0,0)"
    backgroundOpacity={0.06}
    foregroundOpacity={0.12}>

Black box in Safari / iOS (again)

Using the base tag on a page that contains SVG elements fails to render and it looks like a black box. Just remove the base-href tag from the <head /> and issue solved.

black box

See: #93 / 109

Browser supports SVG-Animate

Old browser doesn't support animation in SVG (compatibility list), and if your project must support IE for examples, here's a couple of ways to make sure that browser supports SVG Animate:

  • window.SVGAnimateElement
  • document.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#SVG-Animation", "1.1")
  • Or even use https://modernizr.com/

Similar packages


Development

Fork the repo then clone it

$ git clone [email protected]:YourUsername/react-content-loader.git && cd react-content-loader

$ npm i: Install the dependencies;

$ npm run build: Build to production;

$ npm run dev: Run the docz to see your changes;

$ npm run test: Run all tests: type checking, unit tests on web and native;

$ yarn test:watch: Watch unit tests;

$ yarn tsc: Typescript checking;

$ yarn tsc:watch: Typescript checking with watching;

Commit messages

Commit messages should follow the commit message convention so, changelogs could be generated automatically by that. Commit messages are validated automatically upon commit. If you aren't familiar with the commit message convention, you can use yarn commit (or npm run commit) instead of git commit, which provides an interactive CLI for generating proper commit messages.

License

MIT

Comments
  • Not working in Safari.

    Not working in Safari.

    What did you do?

    My Loader Component

    const Loader = props => (
      <ContentLoader
        preserveAspectRatio="none"
        style={{width: '100%' }}
      >
        <rect x="0" y="0" rx="5" ry="5" width="100%" height={300} /> 
      </ContentLoader>
    )
    

    What did you expect to happen?

    Expect to work for all browsers

    What happened actually?

    Working fine on Chrome and FireFox but giving black layout for safari. image

    Which versions of react-content-loader, and which browser are affected by this issue?

    "react-content-loader": "^3.1.1" "react": "^15.4.2" Browsers: Safari

    bug Solved Needs doc released 
    opened by Ekluv 28
  • When unmounting component with ContentLoader a LOOP Warning is thown

    When unmounting component with ContentLoader a LOOP Warning is thown

    Im using ContentLoader inside a LoadingListItem Component. When a flag is changed I unmount this component to ListItem BUT when this happens a WARNING is thrown , this warning is consuming the memory.

    Warning: Can only update a mounted or mounting component. This usually means you called setState, replaceState, or forceUpdate on an unmounted component. This is a no-op.

    Please check the code for the ContentLoader component.

    help wanted feature released 
    opened by beickon 19
  • Text error

    Text error

    What did you do?

    Please include the actual source code causing the issue. import { Facebook } from 'react-content-loader'; // then in render method return <Facebook />;

    What did you expect to happen?

    Please mention the expected behaviour. See the facebook loader

    What happened actually?

    Invariant Violation: Text string must be rendered within a component, error located at title in Svg

    Which versions of react-content-loader, and which browser are affected by this issue?

    Please also mention the version of react. Everything is the latest

    bug waiting answer released 
    opened by virtualLast 17
  • Option to remove view box

    Option to remove view box

    This is not a bug, but a question/discussion

    I'm not sure if I'm using svg's properly but for having responsive Skeletons, I thought of disabling viewBox. And I did so by passing unexpected values to width/height. Then by skeleton loader was responsive in the way I want (more with in desktop, same height for rectangles)

    1- Do you think that's correct approach for this kind of svgs? 2- If you answered yes/maybe, can we add an option to remove view box?

    feature 
    opened by dogancana 14
  • SSR matching of fill url

    SSR matching of fill url

    Using react-content-loader with SSR (in this case with Next.js) I'm getting the warning:

    Warning: Prop `style` did not match. Server: "fill:url(#yu80c54k1c8)" Client: "fill:url(#zhqtwhb70bm)"
    

    Any workaround?

    Thanks!

    opened by pdrbrnd 10
  • Run local build w/ RN

    Run local build w/ RN

    I'm trying to work on a fork of this library but unfortunately I can't seem to get it to build in way that my RN project likes it. My steps:

    cd react-content-loader
    npm i
    npm run build
    cd ../rn-app
    npm link ../react-content-loader
    

    It seems like the version that gets pulled from npm is structured in a totally different way than what gets built. I'm seeing folders named "stylized" that I don't get when doing npm run build

    opened by ds8k 9
  • View config not found for name rect

    View config not found for name rect

    What did I do?

    I write this code and I expected to run properly this is my code :

    
    const MyLoader = props => (
      <ContentLoader
        height={130}
        width={400}
        speed={2}
        primaryColor="#f3f3f3"
        secondaryColor="#ecebeb"
        {...props}
      >
        <rect x="19" y="45" rx="3" ry="3" width="245" height="5" /> 
        <circle cx="333.5" cy="78.76" r="49.5" /> 
        <rect x="19" y="75" rx="3" ry="3" width="245" height="5" /> 
        <rect x="19" y="105" rx="3" ry="3" width="245" height="5" />
      </ContentLoader>
    )
    
    render() {
      return(
          <MyLoader/>
      )
    }
    

    And i got this error

    Invariant Violation: Invariant Violation: Invariant Violation: Invariant Violation: View config not found fot name rect

    I used the last version of this library

    Solved released 
    opened by mahdidavoodi7 9
  • Add support for top-down gradient animation

    Add support for top-down gradient animation

    Adding gradientDirection prop which translates into a rotate transformation applied to the linearGradient SVG element. Providing the option to animate either from left to right or top to bottom.

    fix #255

    Summary

    This is related to #255. I was browsing for Hactoberfest tagged issues and this one seemed quite interesting. Plus, I love what you've created here, so I'm glad to contribute.

    Related Issue #255

    Any Breaking Changes

    No breaking changes :)

    Checklist

    • [X] Are all the test cases passing?
    • [X] If any new feature has been added, then are the test cases updated/added?
    • [X] Has the documentation been updated for the proposed change, if required?

    Demo

    Kapture 2021-10-14 at 23 15 26

    released 
    opened by moQuez 8
  • RTL switch not working

    RTL switch not working

    I flipped the rtl flag on the component - seems that the navigation has changed, but the content stayed the same. Do I need to make my own RTL component? I thought it would switch around automatically. I went to http://danilowoz.com/create-content-loader/ and switching RTL flag also does nothing. Thanks

    feature released 
    opened by adamgajzlerowicz 8
  • Support for setting width and height attributes directly on the SVG element (not viewBox)

    Support for setting width and height attributes directly on the SVG element (not viewBox)

    I checked the source and it doesn't seem possible to set the width and height of the SVG element; it's only possible to set the viewBox width and height. According to this stackoverflow answer (and the testing I've done myself), this is the only way to have an SVG element that has a constant height but stretches in width.

    How about adding extra options for elementWidth and elementHeight that will set the width and height attribute directly on the SVG element?

    I hope it's alright I didn't fill in the issue template. I felt this was easier to explain with words.

    opened by heralden 8
  • Animation is not working in React Native with react-native-svg >= 13.0.0

    Animation is not working in React Native with react-native-svg >= 13.0.0

    Animation is not working in React Native since react-native-svg 13.0.0

    What did you do?

    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>
    )
    

    What did you expect to happen?

    Loader should animate with backgroundColor and foregroundColor colors.

    What happened actually?

    No animation.

    Which versions of react-content-loader, and which browser are affected by this issue?

    react-content-loader 6.2.0 react-native-svg 13.2.0 react-native 0.70.1

    opened by alexanderoskin 7
  • SVG: Support for defaultProps will be removed from function components

    SVG: Support for defaultProps will be removed from function components

    react-content-loader 6.0.2 react 18.2.0

    receiving this

    Warning: SVG: Support for defaultProps will be removed from function components in a future major release. Use JavaScript default parameters instead

    opened by ahmedyounes 1
  • Rect fill color doesn't work

    Rect fill color doesn't work

    What did you do?

    fill color doesn't work

    code:

    <ContentLoader
                speed={2}
                width={'100%'}
                height={148}
                backgroundColor="#f3f3f3"
                foregroundColor="#ecebeb"
            >
                <Rect
                    x="275"
                    y="100"
                    rx="18"
                    ry="18"
                    width="120"
                    height="36"
                    fill="#FFE9EC"
                />
                <Rect
                    x="148"
                    y="112"
                    rx="4"
                    ry="4"
                    width="80"
                    height="24"
                    fill="#FFE9EC"
                />
                <Rect
                    x="151"
                    y="73"
                    rx="3"
                    ry="3"
                    width="182"
                    height="15"
                    fill="#F5F3F3"
                />
                <Rect
                    x="151"
                    y="53"
                    rx="3"
                    ry="3"
                    width="182"
                    height="15"
                    fill="#F5F3F3"
                />
                <Rect
                    x="151"
                    y="12"
                    rx="3"
                    ry="3"
                    width="244"
                    height="17"
                    fill="#F5F3F3"
                />
                <Rect
                    x="19"
                    y="12"
                    rx="4"
                    ry="4"
                    width="124"
                    height="124"
                    fill="#F5F3F3"
                />
                <Rect
                    x="151"
                    y="31"
                    rx="3"
                    ry="3"
                    width="244"
                    height="17"
                    fill="#F5F3F3"
                />
            </ContentLoader>
    

    What did you expect to happen?

    wan't fill color work well

    opened by ybyqzxl 1
  • Using backspace in the editor deletes element even when editing values outside of the canvas area

    Using backspace in the editor deletes element even when editing values outside of the canvas area

    What did you do?

    When using the editor, the backspace button always deletes the element. Even if you're using the backspace in the form to edit the width, height, etc., the element will still get deleted. This is frustrating as there is also no undo button.

    What did you expect to happen?

    If you're editing the values in the form outside of the canvas area using backspace should not delete the element.

    What happened actually?

    The element is deleted when using backspace no matter what you're doing.

    Which versions of react-content-loader, and which browser are affected by this issue?

    The latest, and Chrome.

    If the cursor is outside of the canvas area, like to edit the width or height of the element, using backspace should not also delete the element. Or having an undo button would be highly beneficial.

    opened by alan-nousot 0
  • confused viewBox in react-native

    confused viewBox in react-native

    What did you do?

    Please include the actual source code causing the issue.

    import React from 'react'
    import ContentLoader, { Rect } from 'react-content-loader/native'
    
    const MyLoader = (props) => (
      <ContentLoader
        speed={2}
        width="100%"
        height="100%"
        viewBox="0 0 100 60"
        backgroundColor="#f3f3f3"
        style={{ backgroundColor: 'red' }}
        foregroundColor="#ecebeb"
        // {...props}
      >
        <Rect x="0" y="0" rx="2" ry="2" width="40" height="60" />
        <Rect x="45" y="0" rx="2" ry="2" width="40" height="60" />
      </ContentLoader>
    )
    
    export default MyLoader
    
    // use
    <View style={{ height: 75, width: '100%' }}>
      <MyCodeLoader />
    </View>
    

    What did you expect to happen?

    Please mention the expected behaviour. image

    What happened actually?

    image

    Which versions of react-content-loader, and which browser are affected by this issue?

    Please also mention the version of react.

    opened by GreatAuk 2
Releases(v6.2.0)
Owner
Danilo Woznica
Design-driven developer focused on crafting products in ReactJS. Developer at @codesandbox
Danilo Woznica
This is a starter file with base SvelteKit skeleton configurations with Open Props, Jit Props, and PostCSS configured.

create-svelte Everything you need to build a Svelte project, powered by create-svelte; Creating a project If you're seeing this, you've probably alrea

Brittney Postma 4 Jul 22, 2022
A react component available on npm to easily link to your project on github and is made using React, TypeScript and styled-components.

fork-me-corner fork-me-corner is a react component available on npm to easily link to your project on github and is made using React, TypeScript and s

Victor Dantas 9 Jun 30, 2022
Create Sonnat App - Set up a modern web app which is powered by Sonnat.

Create Sonnat App - Set up a modern web app which is powered by Sonnat.

Sonnat 2 Jan 31, 2022
A small CLI tool to create a semantic release and git-cliff powered Changelog

cliff-jumper A small CLI tool to create a semantic release and git-cliff powered Changelog Description When managing a collection of projects you ofte

Favware 15 Sep 22, 2022
svg react icons of popular icon packs

React Icons Include popular icons in your React projects easily with react-icons, which utilizes ES6 imports that allows you to include only the icons

null 9k Jan 1, 2023
Vue-hero-icons - A set of free MIT-licensed high-quality SVG icons, sourced from @tailwindlabs/heroicons, as Vue 2 functional components.

vue-hero-icons For Vue3, install the official package @heroicons/vue A set of free MIT-licensed high-quality SVG icons, sourced from @tailwindlabs/her

Mathieu Schimmerling 97 Dec 16, 2022
🎉 Sensoro Design SVG Icons

Sensoro Design Icons 语义化矢量图形库,提供了描述图标的抽象节点来满足对各种框架的适配。 ✨ 特性 ?? 内置了丰富的图标资源 ?? 支持对 React、Vue、React Native、Angular 各种框架的适配 ?? 使用 TypeScript 开发,提供完整的类型定义文

Sensoro Design Team 3 Dec 15, 2022
The JavaScript and API powered WordPress.com

Calypso Calypso is the new WordPress.com front-end – a beautiful redesign of the WordPress dashboard using a single-page web application, powered by t

Automattic 12.2k Dec 30, 2022
A Workspace Web App powered by React and Node Js.

zc_main This is the Zuri Chat frontend built with ExpressJS (Backend) and React (Frontend) Getting Started. Make sure you have nodejs installed by run

Zuri Chat 189 Dec 14, 2022
Chat with an AI that's powered by GPT-j. Talk with it, set parameters, ask questions, and twist words

AI Chat - Open Source | Powered by GPT-j with 6 billion neurons Chat with an AI that's powered by GPT-j. Talk with it, set parameters, ask questions,

Romelianism 3 Dec 29, 2022
FastStore powered by Next.js

A starter powered by FastStore and NextJS Kickoff your store with this boilerplate. This starter ships with the main FastStore configuration files you

VTEX Sites 26 Dec 27, 2022
An application that has a frontend (user interface) that allows you to create, read, update or delete (CRUD) products using an API in which you can also create, read, update or delete products.

CRUD app with React and Firebase 9 An application that has a frontend (user interface) that allows you to create, read, update or delete (CRUD) produc

Júlio Bem 3 Sep 28, 2021
An easily internationalizable, mobile-friendly datepicker library for the web

react-dates An easily internationalizable, accessible, mobile-friendly datepicker library for the web. Live Playground For examples of the datepicker

Airbnb 12k Dec 30, 2022
react-dialog is a custom react hook to use a dialog easily

react-dialog react-dialog is a custom react hook to use a dialog easily. Documentation To use react-dialog follow the documentation. useDialog Returns

Levy Mateus Macedo 2 Mar 29, 2022
A lightweight package to easily track window size in React.js

useWindowSizes - a custom React hook A lightweight package to easily track window width & height in React.js Comes in handy for responsize design and

Harry Fox 3 Feb 3, 2022
A lightweight (1.7 kB) package to easily track mouse position in React.js

useMousePosition - a custom React hook A lightweight (1.7 kB) package to easily track mouse position in React.js Install npm install react-use-mouse-p

Harry Fox 17 Dec 1, 2022
🧱 Easily extend native three.js materials with modular and composable shader units and effects

three-extended-material Easily extend native three.js materials with modular and composable shader units and effects. Usage npm install three-extended

Leonardo Cavaletti 26 Dec 2, 2022
A PhotoShot Plugin to get material you colors easily.

material-you 可以快速获取符合 material-you 配色的 PhotoShop 插件。 参考自 Material Theme Builder 安装 初次安装 从 release 中下载压缩包并解压。打开 Photoshop ,运行 文件-脚本-浏览 ,选择安装包中的 安装脚本.js

周财发 4 Jun 17, 2022
A custom react hook to use a dialogs easily.

dialog-hook The dialog-hook is a custom react hook to use a dialog easily. First of all it is necessary to install the package in your project some co

Levy Mateus Macedo 2 Mar 29, 2022