Socket
Book a DemoInstallSign in
Socket

@rot1024/use-transition

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@rot1024/use-transition

React Hooks for transition animation like Transition component in react-transition-group

latest
Source
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

@rot1024/use-transition

React Hooks for transition animation like Transition component in react-transition-group, but improves smooth transition

Transition component in react-transition-group, and use-transition are very nice. But they are incompatible with styled-components or Emotion, particularly with mountOnEnter or unmountOnExit.

In addition, react-transition-group and use-transition have incorrect transition duration when the transition direction is changed during the transition.

This library improves their problems. Following image is comparision of the libraries.

comparison

Usage

npm install --dev @rot1024/use-transition
# or
yarn add @rot1024/use-transition
import { useState } from "react";
import { styled } from "styled-components";
import useTransition from "@rot1024/use-transition";

const StyledDiv = styled.div`
  transition: ${({ state }) => state === "entering" || state === "exiting" ? "all 1s ease" : ""};
  opacity: ${({ state }) => state === "entering" || state === "entered" ? 1 : 0};
`;

const Component = () => {
  const [isActive, setActive] = useState(false);
  const state = useTransition(isActive, 1000, {
    mountOnEnter: true,
    unmountOnExit: true
  });
  // state is "entering", "entered", "exiting", "exited", or "unmounted"

  const handleClick = useCallback(() => {
    setActive(active => !active);
  }, [isActive, setActive])

  return (
    <div>
      <button onClick={handleClick}>Toggle</button>
      {state === "unmounted" ? null : <StyledDiv state={state}>Transition</StyledDiv>}
    </div>
  );
};

License

MIT License

FAQs

Package last updated on 04 Jul 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts