Skip to content

kevronosx/state-designer

 
 

Repository files navigation

State Designer

⚠️ This project is not yet fully documented. The best way to explore the project is through its tutorials package.

State Designer is a JavaScript and TypeScript library for managing the state of a user interface. It prioritizes the design experience, making it easy to experiment with ideas, iterate on solutions, and communicate the final result.

Learn more at state-designer.com.

Features

  • Create your state using a flexible, declarative syntax.
  • Use collections to reuse code and clarify make your state.
  • Create both global and local states.

Packages

Starters

Usage

Using State Designer involves three steps:

  1. Create a state with a configuration object.
  2. Subscribe to the state's updates.
  3. Send events to the state.

Your exact usage will depend on your framework:

Example

Note: This example uses the React package.

Edit little-snowflake-rmu8q

import React from "react"
import { useStateDesigner } from "@state-designer/react"

function App() {
  const { data, send, can, whenIn } = useStateDesigner({
    data: { count: 1 },
    initial: "inactive",
    states: {
      inactive: {
        on: { TOGGLED: { to: "active" } },
      },
      active: {
        on: {
          TOGGLED: { to: "inactive" },
          CLICKED_PLUS: { if: "belowMax", do: "increment" },
          CLICKED_MINUS: "decrement",
        },
      },
    },
    actions: {
      increment(d) {
        d.count++
      },
      decrement(d) {
        d.count--
      },
    },
    conditions: {
      belowMax(d) {
        return d.count < 10
      },
    },
  })

  return (
    <div className="App">
      <h2>{data.count}</h2>
      <button
        disabled={!can("CLICKED_MINUS")}
        onClick={() => send("CLICKED_MINUS")}
      >
        -1
      </button>
      <button
        disabled={!can("CLICKED_PLUS")}
        onClick={() => send("CLICKED_PLUS")}
      >
        +1
      </button>
      <button onClick={() => send("TOGGLED")}>
        {whenIn({
          active: "Turn Off",
          inactive: "Turn On",
        })}
      </button>
    </div>
  )
}

export default App

Inspiration

State Designer is heavily inspired by xstate. Note that, unlike xstate, State Designer does not adhere to the scxml spec.

Author

License

MIT

About

A state management tool designed to be designed.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 66.8%
  • JavaScript 31.7%
  • Other 1.5%