ygdrassil
v2025.12.7
Published
**Ygdrassil** is a very basic state machine implementation for React. Each state is declared in JSX and only the active state's children are rendered. The active state and context variables are synced the URL hash/query so app state can be bookmarked.
Readme
Ygdrassil
Ygdrassil is a very basic state machine implementation for React. Each state is declared in JSX and only the active state's children are rendered. The active state and context variables are synced the URL hash/query so app state can be bookmarked.
Features
StateMachineprovider that manages the current state.- prop
nameprop for identifying the machine; shows up in the URL asyg-<name>=yadayada - optional prop
initialname of the initial state to render- w/o
initialno state is rendered until the user navigates to one
- w/o
- optional prop
classNameto wrap the children in a<div>with those classes
- prop
Statecomponent for individual states- optional prop
onEnter~ fx run when entering the state - optional prop
onExit~ fx run when exiting the state - optional prop
transitionprop for listing allowed transitions from the state- if no
transitionprop is provided it will transition to any other state
- if no
- optional prop
useStateMachinehook for reading or changing the current state.queryReact-state object that mirrors the values in the URL query-stringgotoStatefunction for changing the current statecurrentStatestring of the current state's namecloseunloads the current state-machine and URL paramsisfunction to check if the current state matches a given nameavailableTransitionsarray of allowed transitions from the current statesetQueryfunction for updating the query string & React-state object- first argument is an object with key-value pairs to set
- second argument is a boolean: true means it will replace the entire string with the equivalent of the given object
registerStatefunction for programmatically registering a state, takes these arguments:name~ name of the state to registertransition~ array of allowed transitions from the stateonEnter~ function to run when entering the stateonExit~ function to run when exiting the state
unregisterStatefunction for removing a state, takes thenameof the state to removeparamstring with is used to identify this state-machine in the URL hash- if the state-machine is named
demothe URL will containyg-demo=one - the value of that param is the name of the active state
- if the state-machine is named
StateButtonconvenience component for state navigation- prop
tofor the name of the state to navigate to - prop
classNameadds those classes to the button- if a StateButton's
tomatches the current state it will have anactiveclass
- if a StateButton's
- optional
onClickruns before the state change - children are rendered if given, otherwise the button will render the
tostring in the button
- prop
StateLinkis just likeStateButtonbut renders an<a>tag instead of a<button>ExternalButtonis likeStateButtonbut can be used outside of aStateMachinecontext.- you must give it the
machineprop with the name of the target state-machine - optional
onClickruns before the state change
- you must give it the
ExternalLinkis likeExternalButtonbut renders an<a>tag instead of a<button>
Demo / Example / Test
This repository includes a small demo app. You can view it online here: https://fingerskier.github.io/ygdrassil/ Or view it offline:
git clone https://github.com/fingerskier/ygdrassil.git
cd ygdrassil
npm install
npm run dev