0.77.1 • Published 13 days ago

@zag-js/core v0.77.1

Weekly downloads
-
License
MIT
Repository
github
Last release
13 days ago

@zag-js/core

This package contains a minimal implementation of XState FSM for finite state machines with addition of extra features we need for our components.

Features

  • Finite states (non-nested)
  • Initial state
  • Transitions (object or strings)
  • Context
  • Entry actions
  • Exit actions
  • Delayed timeout actions (basically setTimeout)
  • Delayed interval actions (basically setInterval)
  • Transition actions
  • Boolean guard helpers (and, or, not)
  • Basic spawn helpers
  • Activities (for state nodes)

To better understand the state machines, we strongly recommend going though the xstate docs and videos. It'll give you the foundations you need.

Quick start

Installation

npm i @zag-js/core
# or
yarn add @zag-js/core

Usage (machine):

import { createMachine } from "@zag-js/core"

const toggleMachine = createMachine({
  id: "toggle",
  initial: "inactive",
  states: {
    inactive: { on: { TOGGLE: "active" } },
    active: { on: { TOGGLE: "inactive" } },
  },
})

toggleMachine.start()
console.log(toggleMachine.state.value) // => "inactive"

toggleMachine.send("TOGGLE")
console.log(toggleMachine.state.value) // => "active"

toggleMachine.send("TOGGLE")
console.log(toggleMachine.state.value) // => "inactive"

Usage (service):

import { createMachine } from "@zag-js/core"

const toggleMachine = createMachine({...})
toggleMachine.start()

toggleService.subscribe((state) => {
  console.log(state.value)
})

toggleService.send("TOGGLE")
toggleService.send("TOGGLE")
toggleService.stop()

API

createMachine(config, options)

Creates a new finite state machine from the config.

ArgumentTypeDescription
configobject (see below)The config object for creating the machine.
optionsobject (see below)The optional options object.

Returns:

A Machine, which provides:

  • machine.initialState: the machine's resolved initial state
  • machine.start(): the function to start the machine in the specified initial state.
  • machine.stop(): the function to stop the machine completely. It also cleans up all scheduled actions and timeouts.
  • machine.transition(state, event): a transition function that returns the next state given the current state and event. It also performs any delayed, entry or exit side-effects.
  • machine.send(event): a transition function instructs the machine to execute a transition based on the event.
  • machine.onTransition(fn): a function that gets called when the machine transition function instructs the machine to execute a transition based on the event.
  • machine.onChange(fn): a function that gets called when the machine's context value changes.
  • machine.state: the state object that describes the machine at a specific point in time. It contains the following properties:
    • value: the current state value
    • previousValue: the previous state value
    • event: the event that triggered the transition to the current state
    • nextEvents: the list of events the machine can respond to at its current state
    • tags: the tags associated with this state
    • done: whehter the machine that reached its final state
    • context: the current context value
    • matches(...): a function used to test whether the current state matches one or more state values

The machine config has this schema:

Machine config

  • id (string) - an identifier for the type of machine this is. Useful for debugging.
  • context (object) - the extended state data that represents quantitative data (string, number, objects, etc) that can be modified in the machine.
  • initial (string) - the key of the initial state.
  • states (object) - an object mapping state names (keys) to states
  • on (object) - an global mapping of event types to transitions. If specified, this event will called if the state node doesn't handle the emitted event.

State config

  • on (object) - an object mapping event types (keys) to transitions

Transition config

String syntax:

  • (string) - the state name to transition to.
    • Same as { target: stateName }

Object syntax:

  • target (string) - the state name to transition to.
  • actions (Action | Action[]) - the action(s) to execute when this transition is taken.
  • guard (Guard) - the condition (predicate function) to test. If it returns true, the transition will be taken.

Machine options

  • actions? (object) - a lookup object for your string actions.
  • guards? (object) - a lookup object for your string guards specified as guard in the machine.
  • activities? (object) - a lookup object for your string activities.
  • delays? (object) - a lookup object for your string delays used in after and every config.

Action config

The action function to execute while transitioning from one state to another. It takes the following arguments:

  • context (any) - the machine's current context.
  • event (object) - the event that caused the action to be executed.
0.77.1

13 days ago

0.77.0

24 days ago

0.76.0

28 days ago

0.75.0

1 month ago

0.74.0

1 month ago

0.74.2

1 month ago

0.74.1

1 month ago

0.72.0

2 months ago

0.73.1

2 months ago

0.73.0

2 months ago

0.68.1

2 months ago

0.68.0

2 months ago

0.66.1

3 months ago

0.66.0

3 months ago

0.70.0

2 months ago

0.67.0

2 months ago

0.69.0

2 months ago

0.71.0

2 months ago

0.65.1

3 months ago

0.65.0

4 months ago

0.64.0

4 months ago

0.63.0

4 months ago

0.62.1

4 months ago

0.62.0

4 months ago

0.59.0

5 months ago

0.57.0

5 months ago

0.61.1

4 months ago

0.61.0

5 months ago

0.56.1

5 months ago

0.56.0

6 months ago

0.58.3

5 months ago

0.58.1

5 months ago

0.58.2

5 months ago

0.58.0

5 months ago

0.60.0

5 months ago

0.55.0

6 months ago

0.53.0

6 months ago

0.54.0

6 months ago

0.52.0

6 months ago

0.51.2

6 months ago

0.51.0

6 months ago

0.51.1

6 months ago

0.50.0

6 months ago

0.49.0

7 months ago

0.48.0

7 months ago

0.47.0

7 months ago

0.46.0

7 months ago

0.45.0

8 months ago

0.44.0

8 months ago

0.43.0

8 months ago

0.41.0

8 months ago

0.42.0

8 months ago

0.40.0

8 months ago

0.39.0

8 months ago

0.38.1

9 months ago

0.38.0

9 months ago

0.37.3

9 months ago

0.37.2

9 months ago

0.37.1

9 months ago

0.37.0

9 months ago

0.36.3

9 months ago

0.36.2

9 months ago

0.36.1

9 months ago

0.36.0

9 months ago

0.35.0

10 months ago

0.34.0

10 months ago

0.33.2

10 months ago

0.33.1

10 months ago

0.33.0

10 months ago

0.32.1

10 months ago

0.32.0

11 months ago

0.20.0

1 year ago

0.21.0

1 year ago

0.10.3

1 year ago

0.10.4

1 year ago

0.10.5

1 year ago

0.22.0

1 year ago

0.30.0

1 year ago

0.31.1

12 months ago

0.31.0

12 months ago

0.13.0

1 year ago

0.25.0

1 year ago

0.14.0

1 year ago

0.26.0

1 year ago

0.11.0

1 year ago

0.11.1

1 year ago

0.11.2

1 year ago

0.23.0

1 year ago

0.12.0

1 year ago

0.24.0

1 year ago

0.17.0

1 year ago

0.29.0

1 year ago

0.18.0

1 year ago

0.15.0

1 year ago

0.27.1

1 year ago

0.27.0

1 year ago

0.16.0

1 year ago

0.28.1

1 year ago

0.28.0

1 year ago

0.19.0

1 year ago

0.19.1

1 year ago

0.10.1

1 year ago

0.10.2

1 year ago

0.10.0

1 year ago

0.9.2

2 years ago

0.9.1

2 years ago

0.7.0

2 years ago

0.5.0

2 years ago

0.2.12

2 years ago

0.2.10

2 years ago

0.2.9

2 years ago

0.2.8

2 years ago

0.2.7

2 years ago

0.2.6

2 years ago

0.2.5

2 years ago

0.2.4

2 years ago

0.2.3

2 years ago

0.2.2

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.12

2 years ago

0.1.11

2 years ago

0.1.10

2 years ago

0.1.9

2 years ago

0.1.8

2 years ago

0.1.7

2 years ago

0.1.6

2 years ago

0.1.5

2 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago