Coder Social home page Coder Social logo

react-skeletor's Introduction

npm license Travis npm

React Skeletor

React-skeletor gif

Display a skeleton preview of the application's content before the data get loaded.

  • Inject dummy data into the provider
  • Define your loading status with the provider
  • Wrap leaf component with createSkeletorElement and define the style of the component when it is pending. The component will do all the magic for you, it will turn on / off the pending design for you.

Basic usage

  1. Install via npm
npm install @trainline/react-skeletor
  1. Wrap the component (often a container) with the createSkeletonProvider high order component. This adds the loading status and style into the context and inject fake data in the components subtree.
// UserDetailPage.jsx

import { createSkeletonProvider } from '@trainline/react-skeletor';

const UserDetailPage = ({ user }) => (
  <div>
    ...
    <NameCard user={user} />
    ...
  </div>
)

export default createSkeletonProvider(
  // Dummy data with a similar shape to the component's data
  {
    user: {
      firstName: '_____',
      lastName: '________'
    }
  },
  // Predicate that returns true if component is in a loading state
  ({ user }) => user === undefined,
  // Define the placeholder styling for the children elements,
  () => ({
    color: 'grey',
    backgroundColor: 'grey'
  })
)(UserDetailPage);
  1. Use a skeleton element to toggle between the placehoder design and the real content depending on the loading status in the context.
// NameCard.jsx

import { createSkeletonElement } from '@trainline/react-skeletor';

const H1 = createSkeletonElement('h1');
const H2 = createSkeletonElement('h2');

const NameCard = ({ firstName, lastName }) => (
  <div>
    <H1 style={style}>{ firstName }</H1>
    <H2 style={style}>{ lastName }</H2>
  </div>
)

export default NameCard;

Contribute

Before opening any Pull Request please post an issue explaining the problem so that the team can evaluate if the Pull Request is relevant.

Learn more on medium

react-skeletor's People

Contributors

alex3165 avatar denkristoffer avatar jamiecopeland avatar marcolanaro avatar sloria avatar thetrainconductor avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-skeletor's Issues

Add demo on codesandbox.io?

Is it possible to link to a demo from the Readme?
Will help to understand the value proposition and usage.
Thanks!

Connector API to access isPending

I'm starting to use react-skeletor, and I'm missing something in the API: a connector to access the isPending and have my own logic of rendering, for example a component to render an avatar:

import { connectSkeleton } from '@trainline/react-skeletor';

@connectSkeleton(({ isPending }) => ({
    isPending
})
class Avatar extends React.Component {
    render() {
         const { isPending } = this.props;

         return isPending ? <div></div> : <a href="...">...</a>
    }
}

Currently, I can directly use contextTypes and get the skeleton context, but this is a not recommended API by React.

At some point, it may requires a subscribing schema in the context instead of storing the values directly.

Will you be willing to accept a connectSkeleton API ? (I can make a PR)

createSkeletonElement for images switch onLoad

Is it possible to make it so that when you create a skeleton element for an image tag, that it waits for the onLoad event to occur before switching? Or for there to be a manual keep on skeleton state as a prop?

Basically I'd like to be able to have my component wait for the image source to be passed down from a fetch, then wait until the onLoad event has occurred and then switch to show the image. Otherwise, there is a flash.

What happen when a framework like material-ui is used?

Hi,

I have been playing with your react-skeletor, it's a great solution btw, but what happen when there is not complete domain of the elements of the dom like in material-ui that has useful component? Can we still be able to use react-skeletor? How?

Thank you very much!

Any examples using with Redux?

As above, any examples where the HOC (createSkeletonProvider) is wrapped in a connect() perhaps?

My component will dispatch during componentWillMount if the data is not present, which doesn't seem compatible with dummyData method.

Skeleton elements don't have displayName property

Testing libraries, such as enzyme rely on the displayName property to be able successfully find a component after rendering see example documentation here.

Currently react-skeletor does not set this property making it difficult to use with testing libraries such as enzyme.

We can use a similar technique used by styled-components to set the displayName

@alex3165 I have tested this out and I'm happy to put this in a PR.
I also work for Trainline :)

Add optional stylesheet

It would be convenient to ship this package with an optional stylesheet for styling a basic skeleton screen.

import { createSkeletonProvider } from 'react-skeletor';
import 'react-skeletor/css/index.css';

export default createSkeletonProvider(
   dummyData,
   (props) => props.loading,
    'react-skeletor',
)(App);
/*  react-skeletor/css/index.css */
@keyframes progress {
    0% {
        background-position: -200px 0;
    }
    100% {
        background-position: calc(200px + 100%) 0;
    }
}
.react-skeletor {
    animation: progress 1.2s ease-in-out infinite;
    color: rgba(0, 0, 0, 0.0);
    background-color: #eee;
    background-image: linear-gradient(90deg, #eee, #f5f5f5, #eee);
    background-repeat: no-repeat;
    border-radius: 4px;
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.