Gathering detailed insights and metrics for react-transition-state
Gathering detailed insights and metrics for react-transition-state
Gathering detailed insights and metrics for react-transition-state
Gathering detailed insights and metrics for react-transition-state
react-transition-group
A react component toolset for managing animations
d3-transition
Animated transitions for D3 selections.
@types/react-transition-group
TypeScript definitions for react-transition-group
@uirouter/core
UI-Router Core: Framework agnostic, State-based routing for JavaScript Single Page Apps
Zero dependency React transition state machine
npm install react-transition-state
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
387 Stars
375 Commits
13 Forks
1 Watching
13 Branches
3 Contributors
Updated on 24 Nov 2024
Minified
Minified + Gzipped
JavaScript (89.68%)
CSS (5.54%)
HTML (3.71%)
Shell (1.07%)
Cumulative downloads
Total Downloads
Last day
-1.4%
37,000
Compared to previous day
Last week
2.9%
189,088
Compared to previous week
Last month
11%
749,435
Compared to previous month
Last year
131.3%
6,896,509
Compared to previous year
22
Zero dependency React transition state machine
Inspired by the React Transition Group, this tiny library helps you easily perform animations/transitions of your React component in a fully controlled manner, using a Hook API.
🤔 Not convinced? See a comparison with React Transition Group
The initialEntered
and mountOnEnter
props are omitted from the diagram to keep it less convoluted. Please read more details at the API section.
1# with npm 2npm install react-transition-state 3 4# with Yarn 5yarn add react-transition-state
1import { useTransitionState } from 'react-transition-state'; 2 3function Example() { 4 const [state, toggle] = useTransitionState({ timeout: 750, preEnter: true }); 5 return ( 6 <div> 7 <button onClick={() => toggle()}>toggle</button> 8 <div className={`example ${state.status}`}>React transition state</div> 9 </div> 10 ); 11} 12 13export default Example;
1.example { 2 transition: all 0.75s; 3} 4 5.example.preEnter, 6.example.exiting { 7 opacity: 0; 8 transform: scale(0.5); 9} 10 11.example.exited { 12 display: none; 13}
1import styled from 'styled-components'; 2import { useTransitionState } from 'react-transition-state'; 3 4const Box = styled.div` 5 transition: all 500ms; 6 7 ${({ $status }) => 8 ($status === 'preEnter' || $status === 'exiting') && 9 ` 10 opacity: 0; 11 transform: scale(0.9); 12 `} 13`; 14 15function StyledExample() { 16 const [{ status, isMounted }, toggle] = useTransitionState({ 17 timeout: 500, 18 mountOnEnter: true, 19 unmountOnExit: true, 20 preEnter: true 21 }); 22 23 return ( 24 <div> 25 {!isMounted && <button onClick={() => toggle(true)}>Show Message</button>} 26 {isMounted && ( 27 <Box $status={status}> 28 <p>This message is being transitioned in and out of the DOM.</p> 29 <button onClick={() => toggle(false)}>Close</button> 30 </Box> 31 )} 32 </div> 33 ); 34} 35 36export default StyledExample;
You can create switch transition effects using one of the provided hooks,
useTransitionState
if the number of elements participating in the switch transition is static.useTransitionMap
if the number of elements participating in the switch transition is dynamic and only known at runtime.You can toggle on transition with the useEffect
hook.
1useEffect(() => { 2 toggle(true); 3}, [toggle]);
React Transition Group | This library | |
---|---|---|
Use derived state | Yes – use an in prop to trigger changes in a derived transition state | No – there is only a single state which is triggered by a toggle function |
Controlled | No – Transition state is managed internally. Resort to callback events to read the internal state. | Yes – Transition state is lifted up into the consuming component. You have direct access to the transition state. |
DOM updates | Imperative – commit changes into DOM imperatively to update classes | Declarative – you declare what the classes look like and DOM updates are taken care of by ReactDOM |
Render something in response to state updates | Resort to side effects – rendering based on state update events | Pure – rendering based on transition state |
Working with styled-components | Your code looks like – &.box-exit-active { opacity: 0; } &.box-enter-active { opacity: 1; } | Your code looks like – opacity: ${({ state }) => (state === 'exiting' ? '0' : '1')}; It's the way how you normally use the styled-components |
Bundle size | ✅ | |
Dependency count | ✅ |
This CodeSandbox example demonstrates how the same transition can be implemented in a simpler, more declarative, and controllable manner than React Transition Group.
useTransitionState
Hook1function useTransitionState( 2 options?: TransitionOptions 3): [TransitionState, (toEnter?: boolean) => void, () => void];
Name | Type | Default | Description |
---|---|---|---|
enter | boolean | true | Enable or disable enter phase transitions |
exit | boolean | true | Enable or disable exit phase transitions |
preEnter | boolean | Add a 'preEnter' state immediately before 'entering', which is necessary to change DOM elements from unmounted or display: none with CSS transition (not necessary for CSS animation). | |
preExit | boolean | Add a 'preExit' state immediately before 'exiting' | |
initialEntered | boolean | Beginning from 'entered' state | |
mountOnEnter | boolean | State will be 'unmounted' until hit enter phase for the first time. It allows you to create lazily mounted component. | |
unmountOnExit | boolean | State will become 'unmounted' after 'exiting' finishes. It allows you to transition component out of DOM. | |
timeout | number | { enter?: number; exit?: number; } | Set timeout in ms for transitions; you can set a single value or different values for enter and exit transitions. | |
onStateChange | (event: { current: TransitionState }) => void | Event fired when state has changed. Prefer to read state from the hook function return value directly unless you want to perform some side effects in response to state changes. Note: create an event handler with useCallback if you need to keep toggle or endTransition function's identity stable across re-renders. |
The useTransitionState
Hook returns a tuple of values in the following order:
1{ 2 status: 'preEnter' | 3 'entering' | 4 'entered' | 5 'preExit' | 6 'exiting' | 7 'exited' | 8 'unmounted'; 9 isMounted: boolean; 10 isEnter: boolean; 11 isResolved: boolean; 12}
(toEnter?: boolean) => void
() => void
onAnimationEnd
or onTransitionEnd
event.useTransitionMap
HookIt's similar to the useTransitionState
Hook except that it manages multiple states in a Map structure instead of a single state.
It accepts all options as useTransitionState
and the following ones:
Name | Type | Default | Description |
---|---|---|---|
allowMultiple | boolean | Allow multiple items to be in the enter phase at the same time. |
The Hook returns an object of shape:
1interface TransitionMapResult<K> { 2 stateMap: ReadonlyMap<K, TransitionState>; 3 toggle: (key: K, toEnter?: boolean) => void; 4 toggleAll: (toEnter?: boolean) => void; 5 endTransition: (key: K) => void; 6 setItem: (key: K, options?: TransitionItemOptions) => void; 7 deleteItem: (key: K) => boolean; 8}
setItem
and deleteItem
are used to add and remove items from the state map.
MIT Licensed.
No vulnerabilities found.
Reason
21 commit(s) and 2 issue activity found in the last 90 days -- score normalized to 10
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 1/5 approved changesets -- score normalized to 2
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
13 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More