Coder Social home page Coder Social logo

daydreamer-riri / transition-hooks Goto Github PK

View Code? Open in Web Editor NEW
11.0 2.0 0.0 477 KB

An extremely light-weight react transition animation hook which is simpler and easier to use than react-transition-group

Home Page: https://transition-hooks.netlify.app/

License: MIT License

TypeScript 97.40% JavaScript 1.59% HTML 1.01%
animation hooks react react-native react-transition-group transition transition-animation

transition-hooks's Introduction

transition-hooks

NPM version bundle JSDocs License

Extremely light-weight react transition hooks which is simpler and easier to use than react-transition-group.

Features

  • State-driven, supports react-dom and react-native
  • Hooks style, easy to use
  • Tiny: ~1KB each hook and no dependencies
  • Support view transition for list transition

Documentation

Awesome documentation station is under construction!

Usage

This is only a brief introduction to use, more usage please visit the documentation.

useTransition

import { useState } from 'react'
import { useTransition } from 'transition-hooks'

function Demo() {
  const [show, setShow] = useState(false)
  const { status, shouldMount } = useTransition(show)

  return shouldMount
    ? (
      <div
        style={{
          transition: 'opacity 0.3s',
          opacity: (status === 'entering' || status === 'entered')
            ? 1
            : 0,
        }}
      >
        Hello Word
      </div>
      )
    : null
}

useSwitchTransition

import { useState } from 'react'
import { useSwitchTransition } from 'transition-hooks'

function Demo() {
  const [count, setCount] = useState(0)
  const { transition } = useSwitchTransition(count, { mode: 'default' })

  return (
    <div>
      <button onClick={() => setCount(count + 1)}>add</button>
      {transition((count, { simpleStatus }) => (
        <p style={{
          transition: '.3s',
          opacity: simpleStatus === 'enter' ? 1 : 0,
          transform: {
            from: 'translateX(-100%)',
            enter: 'translateX(0%)',
            exit: 'translateX(100%)',
          }[simpleStatus]
        }}
        >{count}
        </p>
      ))}
    </div>
  )
}

useListTransition

import { useState } from 'react'
import { useListTransition } from 'transition-hooks'

function Demo() {
  const [list, setList] = useState([0, 1, 2])
  const { transitionList } = useListTransition(list)

  return (
    <div>
      <ul>
        {transitionList((item, { simpleStatus }) => {
          return (
            <li
              style={{
                opacity: simpleStatus === 'enter' ? 1 : 0,
                transform: simpleStatus === 'enter' ? 'translateX(0)' : 'translateX(20px)',
                transition: 'all 300ms',
              }}
            >
              - {item}
            </li>
          )
        })}
      </ul>
    </div>
  )
}

StatusState

interface StatusState {
  status: 'entered' | 'from' | 'entering' | 'exiting' | 'exited'
  simpleStatus: 'from' | 'enter' | 'exit'
  shouldMount: boolean
  isEnter: boolean
  notExit: boolean
  isResolved: boolean
}

See the documentation for more usage.

Credits

All credit goes to iamyoki for initiating transition-hook(the project is not currently active and does not support react18).

License

MIT License © 2023 Riri

transition-hooks's People

Contributors

daydreamer-riri avatar

Stargazers

Colin Peyrat avatar cnwns avatar  avatar Zhazha_JiaYiZhen avatar 裴宇欣 avatar sean avatar 严文彬 avatar 柴桑文远 avatar RedTeai_zhang avatar Carl Chen avatar  avatar

Watchers

 avatar  avatar

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.