xstate-vue

Simple xstate and vue3 integration
3,217
Install
npm install xstate-vue
Latest Version:1.0.1
Vue:3
License:ISC
Last Updated:Dec 24, 2020
Links: npm
Maintainer: asmbatha

xstate-vue

Simple xstate and vue3 integration

Quick Start

npm i xstate xstate-vue

Usage Example

Dark mode toggling machine

<template>
    <div :class="{
        light: theme.state.matches('light'),
        dark: theme.state.matches('dark')
    }">
        <label class="toggle">
            Dark mode <input type="checkbox" @change="theme.send('TOGGLE')">
        </label>
    </div>
</template>

<script>
import { useMachine } from 'xstate-vue'
import { Machine } from 'xstate'

const themeMachine = Machine({
    id: 'theme',
    initial: 'light',
    states: {
        light: {
            on: { TOGGLE: 'dark' }
        },
        dark: {
            on: { TOGGLE: 'light' }
        }
    }
})

export default {
    data: () => ({
        theme: useMachine({ machine: themeMachine })
    })
}
</script>

<style lang="scss">
body {
    margin: 0;
    padding: 0;
}
#app {
    &, .light, .dark {
        display: block;
        min-height: 100vh;
    }

    .light {
        color: #2c3e50;
    }
    .dark {
        background: #2c3e50;
        color: #ffffff;
    }
}

.toggle {
    position: fixed;
    right: 0;
    top: 50px;
    padding: 8px 8px 8px 16px;
    background: #888888;
    border-radius: 8px 0 0 8px;
    display: flex;
    justify-content: center;
    align-items: center;
    color: #ffffff;
    cursor: pointer;
}
</style>

Related Packages

pinia-xstate

Put your xstate state machines into a global pinia store.

26,194
markdown-to-jsx

A very fast and versatile markdown toolchain. AST, React, React Native, SolidJS,...

214,042,213 2,394
@scalar/use-toasts

display toasts in Vue

24,371,992 16,329
@scalar/use-codemirror

CodeMirror for Vue

21,770,238 16,329

Version History

Version Released Vue License
1.0.1 ^3.0.0 ISC
1.0.0 ^3.0.0 ISC