-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add mouse effects and draggable example
draggable implementation heavily inspired by http://elm-lang.org/examples/drag
- Loading branch information
Showing
5 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Sub } from '../spindle'; | ||
|
||
const ns = Symbol('MOUSE'); | ||
|
||
|
||
const windowEvent = (ev, extract) => | ||
Sub(ns, { | ||
key: ev, | ||
start: change => { | ||
const handler = e => | ||
change(handler, extract(e)); | ||
window.addEventListener(ev, handler); | ||
return handler; | ||
}, | ||
stop: handler => | ||
window.removeEventListener(ev, handler), | ||
}); | ||
|
||
|
||
export const ups = windowEvent('mouseup', () => | ||
undefined); | ||
|
||
export const downs = windowEvent('mousedown', () => | ||
undefined); | ||
|
||
export const moves = windowEvent('mousemove', e => | ||
({ x: e.pageX, y: e.pageY })); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import React from 'react'; | ||
import Spindle, { Update } from '../../spindle'; | ||
import { Union, Maybe } from 'results'; | ||
import { Record } from 'immutable'; | ||
import { moves, ups } from '../../effects/mouse'; | ||
|
||
|
||
const SIZE = 100; | ||
|
||
|
||
const Action = Union({ | ||
Grab: null, | ||
Move: null, | ||
Release: null, | ||
}); | ||
|
||
|
||
const Model = Record({ | ||
position: { x: SIZE / 1.5, y: 240 }, | ||
drag: Maybe.None(), | ||
}); | ||
|
||
|
||
const Drag = Record({ | ||
start: { x: 0, y: 0 }, | ||
current: { x: 0, y: 0 }, | ||
}); | ||
|
||
|
||
const init = () => | ||
Update({ model: Model() }); | ||
|
||
|
||
const update = (action, model) => Action.match(action, { | ||
Grab: e => | ||
Update({ | ||
model: model.set('drag', Maybe.Some(Drag({ | ||
start: { x: e.pageX, y: e.pageY }, | ||
current: { x: e.pageX, y: e.pageY }, | ||
})) ), | ||
}), | ||
|
||
Move: newPos => | ||
Update({ | ||
model: model.update('drag', d => | ||
d.andThen(({ start }) => | ||
({ start, current: newPos }))), | ||
}), | ||
|
||
Release: () => | ||
Update({ model: Model({ | ||
position: getPosition(model), | ||
drag: Maybe.None(), | ||
}) }), | ||
}); | ||
|
||
|
||
const subscriptions = model => | ||
model.drag.isSome() | ||
? [ [moves, Action.Move] | ||
, [ups, Action.Release] | ||
] | ||
: []; | ||
|
||
|
||
const getPosition = model => | ||
Maybe.match(model.drag, { | ||
None: () => | ||
model.position, | ||
Some: drag => ({ | ||
x: model.position.x + drag.current.x - drag.start.x, | ||
y: model.position.y + drag.current.y - drag.start.y, | ||
}), | ||
}); | ||
|
||
|
||
const view = (model, dispatch) => { | ||
const { x, y } = getPosition(model); | ||
return ( | ||
<div | ||
onMouseDown={dispatch.Grab} | ||
style={{ | ||
alignItems: 'center', | ||
background: model.drag.isSome() ? '#086' : '#068', | ||
borderRadius: 9, | ||
color: '#fff', | ||
cursor: 'move', | ||
display: 'flex', | ||
height: SIZE, | ||
justifyContent: 'center', | ||
left: x - SIZE / 2, | ||
position: 'absolute', | ||
top: y - SIZE / 2, | ||
width: SIZE, | ||
}}> | ||
drag me | ||
</div> | ||
); | ||
}; | ||
|
||
|
||
export default Spindle('Draggable', | ||
{ Action, init, update, subscriptions, view }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import Draggable from './Draggable'; | ||
|
||
|
||
ReactDOM.render(<Draggable />, document.getElementById('app')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters