Code Monkey home page Code Monkey logo

react-fade-in's Introduction

react-fade-in

Dead-simple and opinionated component to fade in an element's children.

React Fade In

Installation

npm install react-fade-in

Usage

react-fade-in

import FadeIn from 'react-fade-in';
// ...
<FadeIn>
  <div>Element 1</div>
  <div>Element 2</div>
  <div>Element 3</div>
  <div>Element 4</div>
  <div>Element 5</div>
  <div>Element 6</div>
</FadeIn>

API

FadeIn

Animates its children, one by one.

Note: To have children animate separately, they must be first-level children of the <FadeIn> component (i.e. members of its props.children).

Props

  • delay: Default: 50. Delay between each child's animation in milliseconds.
  • transitionDuration: Default: 400. Duration of each child's animation in milliseconds.
  • className: No default. Adds a className prop to the container div.
  • childClassName: No default. Adds a className prop to each child div, allowing you to style the direct children of the FadeIn component.
  • wrapperTag: Default: "div". Override the HTML element of the wrapping div.
  • childTag: Default: "div". Override the HTML element wrapped around each child element.
  • visible: New in 2.0.0: If not undefined, the visible prop can be used to control when the fade in occurs. If set to false after the fade-in animation completes, the children will fade out one by one.
  • onComplete: New in 2.0.0: specifies a callback to be called when the animation completes.

Happy fading.

react-fade-in's People

Contributors

atnpcg avatar deveosys avatar domarp-j avatar gkaemmer avatar hackrmomo avatar themandunord 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

react-fade-in's Issues

MIT License

Please add the MIT license to your repository.

Tests are a nice thing to have :)

Hi hi
I was looking at this lib (got here through a post in Medium)
And it looks really good and simple.
The only thing that I'm missing in order to put in my production app is the testing.
I need to guarantee that the component will not break my app, or that accidentally the usage API has been changed.
The tests will make sure nothing breaks, and if the usage is changed - this will help indicate that probably a major version needs to change.
(I currently don't have the time to help with the testing implementation itself, but feel free to contact me for guidance or moral support :D)

Well done on the lib 👍

Effect not run in dynamic route

Hi, I am thank you for creating very nice package.

I installed the package and it run very good everytime I change the page. But in the dynamic route (Ex: [slug].js) the effect just run in the first time. When the [slug] is changed, the effect not run anymore.

I am using Nextjs, use getStaticProps() to get data and not use useEffect in my site.
Can you show me something to do?

Soory if my english is not very good ^^

Webpack5 produce wrong ES5 with react-fade-in version 2

Using webpack5 the produced ie11 (es5) code doesn't work


Object.defineProperty(exports, "__esModule", ({ value: true }));
const react_1 = __importStar(__webpack_require__(67294));
function FadeIn(props) {
    const [maxIsVisible, setMaxIsVisible] = react_1.useState(0);
    const transitionDuration = props.transitionDuration || 400;
    const delay = props.delay || 50;
    const WrapperTag = props.wrapperTag || "div";
    const ChildTag = props.childTag || "div";
    const visible = typeof props.visible === "undefined" ? true : props.visible;
    react_1.useEffect(() => {
        let count = react_1.default.Children.count(props.children);
        if (!visible) {
            // Animate all children out
            count = 0;
        }
        if (count == maxIsVisible) {
            // We're done updating maxVisible, notify when animation is done
            const timeout = setTimeout(() => {
                if (props.onComplete)
                    props.onComplete();
            }, transitionDuration);
            return () => clearTimeout(timeout);
        }
        // Move maxIsVisible toward count
        const increment = count > maxIsVisible ? 1 : -1;
        const timeout = setTimeout(() => {
            setMaxIsVisible(maxIsVisible + increment);
        }, delay);
        return () => clearTimeout(timeout);
    }, [
        react_1.default.Children.count(props.children),
        delay,
        maxIsVisible,
        visible,
        transitionDuration,
    ]);
    return (react_1.default.createElement(WrapperTag, { className: props.className }, react_1.default.Children.map(props.children, (child, i) => {
        return (react_1.default.createElement(ChildTag, { className: props.childClassName, style: {
                transition: `opacity ${transitionDuration}ms, transform ${transitionDuration}ms`,
                transform: maxIsVisible > i ? "none" : "translateY(20px)",
                opacity: maxIsVisible > i ? 1 : 0,
            } }, child));
    })));
}
exports.default = FadeIn;

Rewrite as functional typescript component

Hey, I rewrote this as a functional typescript component. Figured I'd pass it along if you want it, if not no worries. Feel free to close the issue.

import React from 'react';

interface Props {
  delay?: number;
  transitionDuration?: number;
  wrapperTag?: keyof JSX.IntrinsicElements;
  childTag?: keyof JSX.IntrinsicElements;
  children: React.ReactElement;
  className?: string;
  childClassName?: string;
}

const FadeIn = ({
  delay = 50,
  transitionDuration = 400,
  wrapperTag,
  childTag,
  children,
  className = null,
  childClassName = null,
}: Props): React.ReactElement => {
  const [maxIsVisible, setMaxIsVisible] = React.useState(0);
  const WrapperTag = wrapperTag || 'div';
  const ChildTag = childTag || 'div';

  React.useEffect(() => {
    const count = React.Children.count(children);
    let i = 0;
    const interval = setInterval(() => {
      i++;
      if (i > count) clearInterval(interval);

      setMaxIsVisible(i);
    }, delay);

    return () => {
      clearInterval(interval);
    };
  }, []);

  return (
    <WrapperTag className={className}>
      {React.Children.map(children, (child, i) => {
        return (
          <ChildTag
            className={childClassName}
            style={{
              transition: `opacity ${transitionDuration}ms, transform ${transitionDuration}ms`,
              transform: `translateY(${maxIsVisible > i ? 0 : 20}px)`,
              maxHeight: maxIsVisible > i ? '100%' : '0%',
              overflow: 'hidden',
              opacity: maxIsVisible > i ? 1 : 0,
            }}
          >
            {child}
          </ChildTag>
        );
      })}
    </WrapperTag>
  );
};

export default FadeIn;

Fade Out?

Any way to play it backwards and then unmount inner components?

Direction?

This is one of my favourite UI libraries :) Thank you to everyone who has contributed.

Are there any plans to add a direction prop? Would be nice to specify the direction they animation should fade-in from.

Cheers!

react v18

Hallo,

could something be done with it?

npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! npm ERR! While resolving: [email protected] npm ERR! Found: [email protected] npm ERR! node_modules/react npm ERR! react@"^18.2.0" from the root project npm ERR! npm ERR! Could not resolve dependency: npm ERR! peer react@"^16.8 || 17" from [email protected] npm ERR! node_modules/react-fade-in npm ERR! react-fade-in@"*" from the root project npm ERR! npm ERR! Fix the upstream dependency conflict, or retry npm ERR! this command with --force, or --legacy-peer-deps npm ERR! to accept an incorrect (and potentially broken) dependency resolution.

Importing into a TS project provides a module declaration warning

TS7016: Could not find a declaration file for module 'react-fade-in'. '/home/ryan/Code/llapp10/node_modules/react-fade-in/lib/index.js' implicitly has an 'any' type.   Try npm i --save-dev @types/react-fade-in if it exists or add a new declaration (.d.ts) file containing declare module 'react-fade-in';

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.