All Projects → KadoBOT → react-gizmo

KadoBOT / react-gizmo

Licence: other
🦎 React Gizmo - UI Finite State Machine for React

Programming Languages

javascript
184084 projects - #8 most used programming language
CSS
56736 projects

Projects that are alternatives of or similar to react-gizmo

xstate-catalogue
Professionally designed, interactive state machines
Stars: ✭ 616 (+1479.49%)
Mutual labels:  state-machine, xstate
Stately.js
Stately.js is a JavaScript based finite-state machine (FSM) engine for Node.js and the browser.
Stars: ✭ 785 (+1912.82%)
Mutual labels:  state-machine, transition
Aasm
AASM - State machines for Ruby classes (plain Ruby, ActiveRecord, Mongoid, NoBrainer, Dynamoid)
Stars: ✭ 4,474 (+11371.79%)
Mutual labels:  state-machine, transition
xstate-cpp-generator
C++ State Machine generator for Xstate
Stars: ✭ 33 (-15.38%)
Mutual labels:  state-machine, xstate
annihilate
js action game
Stars: ✭ 49 (+25.64%)
Mutual labels:  state-machine, xstate
Laravel Stager
Laravel Stager State Machine, Its purpose is to add state machine functionality to models
Stars: ✭ 16 (-58.97%)
Mutual labels:  state-machine, transition
xstate-viz
Visualizer for XState machines
Stars: ✭ 274 (+602.56%)
Mutual labels:  state-machine, xstate
FiniteStateMachine
This project is a finite state machine designed to be used in games.
Stars: ✭ 45 (+15.38%)
Mutual labels:  state-machine, transition
react-transition-state
Zero dependency React transition state machine.
Stars: ✭ 239 (+512.82%)
Mutual labels:  state-machine, transition
xstate-react-router
XState connector to React Router.
Stars: ✭ 23 (-41.03%)
Mutual labels:  state-machine, xstate
use-secret-code
Custom hook for adding cheat codes to your React app.
Stars: ✭ 16 (-58.97%)
Mutual labels:  state-machine, xstate
state-machine-demo
A React state machine demo using xstate
Stars: ✭ 18 (-53.85%)
Mutual labels:  state-machine, xstate
state machines-graphviz
Graphviz module for state machines
Stars: ✭ 27 (-30.77%)
Mutual labels:  state-machine
typed-machine
A strict Finite State Machine, written in TS
Stars: ✭ 21 (-46.15%)
Mutual labels:  state-machine
qm
QM model-based design tool and code generator based on UML state machines
Stars: ✭ 54 (+38.46%)
Mutual labels:  state-machine
SPStorkController
Now playing controller from Apple Music, Mail & Podcasts Apple's apps.
Stars: ✭ 2,515 (+6348.72%)
Mutual labels:  transition
AniX
🐿 Super easy and lightweight(<3kb) JavaScript animation library
Stars: ✭ 253 (+548.72%)
Mutual labels:  transition
ember-fsm
[Maintenance Mode] A promise-aware finite state machine implementation for Ember
Stars: ✭ 37 (-5.13%)
Mutual labels:  state-machine
react-transition-box
React component for easily transitioning your container size based on children 🎁
Stars: ✭ 13 (-66.67%)
Mutual labels:  transition
temporal-electronic-signature
Electronic signature demonstration built with Temporal and XState
Stars: ✭ 36 (-7.69%)
Mutual labels:  xstate

React Gizmo

UI Finite State Machine for React

Gizmo

React Gizmo is a cute State Machine that doesn't become a monster if you throw water on it

Quick Start

Installation

yarn add react-gizmo

Usage

PS: react-gizmo uses new React Context API. React 16.3 is needed for this library to work.

import { Machine, State } from "react-gizmo";

const state = {
	initialState: { text: "Next" },
	flow: {
		initial: "start",
		states: {
			start: { on: { NEXT: "end" } },
			end: { on: { PREV: "start" } }
		}
	}
};

const MachineApp = () => (
	<Machine log state={state}>
		<App />
	</Machine>
);

ReactDOM.render(<MachineApp />, document.getElementById("root"));

App.js

class App extends Component {
	render() {
		return (
			<React.Fragment>
				<State on="start">
					<ButtonStart />
				</State>
				<State on="end">
					<ButtonEnd />
				</State>
			</React.Fragment>
		);
	}
}

ButtonStart.js

class ButtonNext extends Component {
	handleOnClick = () => {
		this.props.transition("NEXT", {
			off: "start",
			setState: { text: "Prev" }
		});
	};

	render() {
		return <button onClick={this.handleOnClick}>{this.props.text}</button>;
	}
}

ButtonPrev.js

class ButtonPrev extends Component {
	handleOnClick = () => {
		this.props.transition("PREV", {
			off: "end",
			setState: { text: "Next" }
		});
	};

	render() {
		return <button onClick={this.handleOnClick}>{props.text}</button>;
	}
}

API

<Machine />

The <Machine /> wraps your App and is responsible for passing down the props to the <State />. The <Machine /> should have only one children, similar to react-router. The initialState and flow are passed to the machine through the state prop.

Prop Type Description
graph bool Display realtime statechart visualization
log bool If true logs state transitions
state object The object containing the state machine and initial State: { initialState: any, flow: statechart }

<State />

The <State /> represents the individual state of your flow. on prop is what glues the state to a specific flow state, and the children prop returns a function with the machine props. It's recommended to use class based component for the children of the State so it can be referenced by the Machine.

...
states: {
  start: { on: {  NEXT: 'end' }},
  end: { on: { PREV: 'start' }}
}
...
<State on="start">
  <PageOne />
</State>
<State on="end">
  <PageTwo />
</State>
Prop Type Description
on string Component that will be turned 'on' when flow transitions to a state with same name

props.transition(state[,options])

As the name suggests, this function is responsible for transitioning your app from one state to another. The first argument is the value of the state to be transitioned, and the second argument can receive a bunch of options. Like off to hide other non-actives <State /> components, setState to update your state/initialState, draftState which temporarily stores your changes until it's published and condition where you can pass xState Guards

Option Type Description
off oneOfType(string, arrayOf(string)) Component(s) that will be turned 'off' when transition is called
setState object Mutates initialState keys with passed values
draftState object Like setState, but changes take effect only after being published. Newer draftStates will replace unpublished ones.
condition any Check xState Contitional Transitions (Guards)
props.transition("NEXT", {
	off: "end",
	setState: {
		text: "Will be updated"
	},
	draftState: {
		text: "Will update again, but only after publish"
	},
	condition: { shouldTransition: this.text.length < 99 }
});

props.publish()

Publishes unpublished state aka. draftState. Draft is merged into State and then draft is cleaned. This is usefull when you are not sure if you want to update your state, I.E if a user clicks a cancel button during an API request.

props.transition("NEXT", { draftState: { text: "Hello World" } });
console.log(this.props.text); // ''
props.publish();
console.log(this.props.text); // 'Hello World'

props[state]

Your initialState/state, can be accessed directly via props.YOUR_STATE_KEY.

console.log(this.props.text); // Hello

Todo

  • Connect state to Redux DevTools
  • Examples
  • Better integration with other State Managers like Redux and Mobx ie.
  • Tests

Thanks

David the creator of xstate who made this library possible and Michele for inspiring me with react-automata. Even if you like react-gizmo I recommend you to give them a try. Also, a big thanks to Ryan Florence for giving a great talk about State Machine.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].