0.0.29 • Published 3 years ago

@invisionag/react-skeletons v0.0.29

Weekly downloads
6
License
MIT
Repository
github
Last release
3 years ago

ivx npm David

react-skeletons

Summary

react-skeletons consists of two components:

  • A hook to easily extend your components with loading animations
  • A prestyled skeleton component to use in your application

useSkeleton hook

The hook can be used in components that control loading state as well as in components that display loading state. It automatically communicates loading state down the render tree via contexts. You tell the hook during initialization whether you want to set a new loading condition or just register a loading layout and then wrap the components return value in the hooks response (see Example)

Skeleton component

The component is straightforward. It's a div element with a skeleton animation. It is built with styled components and extendable.

Props:

  • width: Allows to set the css value for width directly as a prop
  • height: Allows to set the css value for height directly as a prop

Example

Encapsulated in a single component

import React from 'react';
import { useSkeleton, Skeleton } from '@invisionag/react-skeletons';

const LoadableComponent = () => {
  const [data, setData] = React.useState();
  const [isLoading, setIsLoading] = React.useState(true);

  const { withSkeleton } = useSkeleton({
    isLoading,
    Skeleton: () => <Skeleton width="200px" height="24px" />,
  });

  React.useEffect(() => {
    fetch('example.com').then((data) => {
      setData(data);
      setIsLoading(false);
    });
  }, []);

  return withSkeleton(<p>{data}</p>);
};

Spread over several components

You can use the hook to control a loading state in a controlling component high up in the tree and then use the hook again in your "dumb" components just to register a skeleton layout. They will use the loading state in their closest parent.

import React from 'react';
import { useSkeleton, Skeleton } from '@invisionag/react-skeletons';

const App = () => {
  const [data, setData] = React.useState();
  const [isLoading, setIsLoading] = React.useState(true);

  const { withSkeleton } = useSkeleton({
    isLoading,
  });

  React.useEffect(() => {
    fetch('example.com').then((data) => {
      setData(data);
      setIsLoading(false);
    });
  }, []);

  return withSkeleton(
    <div>
      <Text>{data}</Text>
      <Button>Save</Button>
    </div>,
  );
};

const Text = ({ children, ...rest }) => {
  const { withSkeleton } = useSkeleton({
    Skeleton: () => <Skeleton width="200px" height="24px" />,
  });

  return withSkeleton(<p {...rest}>{children}</p>);
};

const Button = ({ children, ...rest }) => {
  const { withSkeleton } = useSkeleton({
    Skeleton: () => <Skeleton width="120px" height="30px" />,
  });

  return withSkeleton(<button {...rest}>{children}</button>);
};
0.0.29

3 years ago

0.0.28

3 years ago

0.0.23

3 years ago

0.0.22

3 years ago

0.0.21

3 years ago

0.0.20

3 years ago

0.0.19

4 years ago

0.0.18

4 years ago

0.0.17

4 years ago

0.0.15

4 years ago

0.0.16

4 years ago

0.0.14

4 years ago

0.0.13

4 years ago

0.0.12

4 years ago

0.0.11

4 years ago

0.0.10

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.4

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago