Skip to content

Instantly share code, notes, and snippets.

@davidkpiano
Last active March 6, 2020 23:27
Show Gist options
  • Save davidkpiano/b437f46bbe19c39068f86a05bb3b8286 to your computer and use it in GitHub Desktop.
Save davidkpiano/b437f46bbe19c39068f86a05bb3b8286 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
Machine({
initial: 'idle',
context: {
dx: 0,
dy: 0,
pointerx: 0,
pointery: 0
},
states: {
'idle': {
id: 'idle',
entry: assign({
dx: 0,
dy: 0,
pointerx: 0,
pointery: 0
}),
on: {
// when this event happens...
'mousedown': {
// go here...
target: 'dragging',
// and do this...
actions: assign({
pointerx: (context, event) => event.clientX,
pointery: (context, event) => event.clientY
})
}
}
},
'dragging': {
initial: 'outside',
states: {
outside: { // dragging.outside
on: {
'mouseenter': 'inside',
'mouseup': {
target: '#idle'
}
}
},
inside: { // dragging.inside
on: {
'mouseleave': 'outside',
'mouseup': {
target: '#dropped'
}
}
}
},
on: {
'mousemove': {
internal: true,
actions: assign({
dx: (context, event) => event.clientX - context.pointerx,
dy: (context, event) => event.clientY - context.pointery,
})
}
}
},
'dropped': {
id: 'dropped',
after: {
1000: 'idle'
}
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment