Skip to content

Instantly share code, notes, and snippets.

@sesteva
Last active February 10, 2020 16:00
Show Gist options
  • Save sesteva/cac6c89460b34a3ea2373da3db856b02 to your computer and use it in GitHub Desktop.
Save sesteva/cac6c89460b34a3ea2373da3db856b02 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const buttonMachine = Machine(
{
id: "submitButtonWithTransitions",
initial: "disabled",
states: {
active: {
on: {
HOVER: "hovered",
PRESS: "pressed",
CLICK: "clicked",
DISABLE: "disabled",
FOCUS: "focused"
}
},
disabled: {
on: {
ENABLE: "active"
}
},
hovered: {
on: {
HOVEROFF: "active",
PRESS: "pressed"
}
},
pressed: {
on: {
"": "clicked"
}
},
clicked: {
invoke: {
id: "asyncFn",
src: "asyncFn",
onDone: "success",
onError: "retry"
}
},
focused: {
on: {
PRESS: "pressed",
BLUR: "active"
}
},
success: {
type: 'final'
},
retry: {
on: {
HOVER: "hovered",
PRESS: "pressed",
CLICK: "clicked",
DISABLE: "disabled",
FOCUS: "focused"
}
}
}
},
{
services: {
asyncFn: (ctx, evt) => new Promise((resolve, reject) => setTimeout(() => (Math.random() < 0.5 ? reject() : resolve()), 1000))
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment