Project Name | Stars | Downloads | Repos Using This | Packages Using This | Most Recent Commit | Total Releases | Latest Release | Open Issues | License | Language |
---|---|---|---|---|---|---|---|---|---|---|
Javascript State Machine | 7,679 | 553 | 95 | 2 years ago | 9 | July 12, 2018 | 68 | mit | JavaScript | |
A javascript finite state machine library | ||||||||||
Stateless | 4,862 | 183 | 42 | 4 days ago | 146 | February 17, 2021 | 69 | other | C# | |
A simple library for creating state machines in C# code | ||||||||||
Aasm | 4,773 | 2,378 | 130 | 20 days ago | 98 | September 17, 2022 | 161 | mit | Ruby | |
AASM - State machines for Ruby classes (plain Ruby, ActiveRecord, Mongoid, NoBrainer, Dynamoid) | ||||||||||
Squirrel | 1,954 | 22 | 3 | 7 months ago | 26 | August 23, 2022 | 62 | other | Java | |
squirrel-foundation is a State Machine library, which provided a lightweight, easy use, type safe and programmable state machine implementation for Java. | ||||||||||
Statemachine | 1,548 | a year ago | 1 | June 20, 2019 | 26 | other | Kotlin | |||
A Kotlin and Swift DSL for finite state machine | ||||||||||
Microstates | 1,314 | 9 | 9 | a year ago | 88 | September 08, 2020 | 56 | mit | JavaScript | |
Composable state primitives for JavaScript | ||||||||||
React Automata | 1,293 | 13 | 4 | 4 years ago | 27 | August 27, 2018 | 14 | mit | JavaScript | |
A state machine abstraction for React | ||||||||||
Finite | 1,228 | 69 | 12 | a year ago | 20 | April 08, 2022 | 61 | mit | PHP | |
A Simple PHP Finite State Machine | ||||||||||
Sml | 926 | 14 days ago | 151 | bsl-1.0 | C++ | |||||
SML: C++14 State Machine Library | ||||||||||
Stateless4j | 795 | 47 | 3 | 7 months ago | 8 | October 30, 2014 | 7 | apache-2.0 | Java | |
Lightweight Java 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.
# with npm
npm install react-transition-state
# with Yarn
yarn add react-transition-state
import { useTransition } from 'react-transition-state';
/* or import useTransition from 'react-transition-state'; */
function Example() {
const [state, toggle] = useTransition({ timeout: 750, preEnter: true });
return (
<div>
<button onClick={() => toggle()}>toggle</button>
<div className={`example ${state.status}`}>React transition state</div>
</div>
);
}
export default Example;
.example {
transition: all 0.75s;
}
.example.preEnter,
.example.exiting {
opacity: 0;
transform: scale(0.5);
}
.example.exited {
display: none;
}
import styled from 'styled-components';
import { useTransition } from 'react-transition-state';
const Box = styled.div`
transition: all 500ms;
${({ status }) =>
(status === 'preEnter' || status === 'exiting') &&
`
opacity: 0;
transform: scale(0.9);
`}
`;
function StyledExample() {
const [{ status, isMounted }, toggle] = useTransition({
timeout: 500,
mountOnEnter: true,
unmountOnExit: true,
preEnter: true
});
return (
<div>
{!isMounted && <button onClick={() => toggle(true)}>Show Message</button>}
{isMounted && (
<Box status={status}>
<p>This message is being transitioned in and out of the DOM.</p>
<button onClick={() => toggle(false)}>Close</button>
</Box>
)}
</div>
);
}
export default StyledExample;
You can toggle on transition with the useEffect
hook.
useEffect(() => {
toggle(true);
}, [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.
useTransition
Hookfunction useTransition(
options?: TransitionOptions
): [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 useTransition
Hook returns a tuple of values in the following order:
{
status: 'preEnter' |
'entering' |
'entered' |
'preExit' |
'exiting' |
'exited' |
'unmounted';
isMounted: boolean;
isEnter: boolean;
isResolved: boolean;
}
(toEnter?: boolean) => void
() => void
onAnimationEnd
or onTransitionEnd
event.useTransitionMap
HookIt's similar to the useTransition
Hook except that it manages multiple states in a Map structure instead of a single state.
It accepts all options as useTransition
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:
interface TransitionMapResult<K> {
stateMap: ReadonlyMap<K, TransitionState>;
toggle: (key: K, toEnter?: boolean) => void;
toggleAll: (toEnter?: boolean) => void;
endTransition: (key: K) => void;
setItem: (key: K, options?: TransitionItemOptions) => void;
deleteItem: (key: K) => boolean;
}
setItem
and deleteItem
are used to add and remove items from the state map.
MIT Licensed.