Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tighter React.JS integration, composable classes, modular animations. #20

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions example/src/Chain.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import Haste hiding (fromString)
import Haste.JSON
import Lens.Family2 hiding (view)
import React
import React.Anim
import React.Anim.Class


-- model
Expand Down Expand Up @@ -71,9 +73,8 @@ derive t | t < 1 =
)
derive t = (finalWidth, finalHeight)

view :: ChainState -> Chain React'
view status = div_ [ class_ "chain-container" ] $ do
animState <- getAnimationState
view :: ChainState -> Double -> Chain ReactA'
view status animState = div_ [ class_ "chain-container" ] $ do

let numStatus = if status == Open then 1 else 0
t = animState + numStatus
Expand All @@ -92,6 +93,6 @@ view status = div_ [ class_ "chain-container" ] $ do
]
""

chainClass :: IO (Chain ReactClass)
chainClass :: IO (Chain ReactClassA')
chainClass =
createClass view transition initialClassState initialAnimationState []
13 changes: 7 additions & 6 deletions example/src/Circles.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import Lens.Family2
import Haste hiding (fromString)
import Haste.JSON
import React
import React.Anim
import React.Anim.Class


-- model
Expand Down Expand Up @@ -93,15 +95,15 @@ fillorange = Color 245 175 51
fill_' = fill_ . fromString . show


circ :: Circ -> Color -> Circles React'
circ :: Circ -> Color -> Circles ReactA'
circ c = circ' True (const (Just (SingleFlash c))) (coord c)


circ' :: Bool
-> (MouseEvent -> Maybe Transition)
-> (Double, Double)
-> Color
-> Circles React'
-> Circles ReactA'
circ' clickable handler (x, y) color =
let lst = [ cx_ x
, cy_ y
Expand All @@ -115,9 +117,8 @@ circ' clickable handler (x, y) color =
in circle_ (if clickable then lst' else lst)


mainView :: CircState -> Circles React'
mainView (CircState c _) = div_ $ do
AnimState c1 c2 c3 c4 trans <- getAnimationState
mainView :: CircState -> AnimState -> Circles ReactA'
mainView (CircState c _) (AnimState c1 c2 c3 c4 trans) = div_ $ do

svg_ [ width_ 600
, height_ 600
Expand All @@ -130,6 +131,6 @@ mainView (CircState c _) = div_ $ do
circ' False (const Nothing) (coord c `animSub` trans) fillblue


circlesClass :: IO (Circles ReactClass)
circlesClass :: IO (Circles ReactClassA')
circlesClass =
createClass mainView transition initialState initialAnimationState [RepeatingFlash]
14 changes: 8 additions & 6 deletions example/src/Easing.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import Prelude hiding (lookup)
import Haste hiding (fromString)
import Haste.JSON hiding ((!))
import React hiding (repeat)
import React.Anim
import React.Anim.Class

import Lens.Family2 hiding (view)

-- model
Expand Down Expand Up @@ -84,16 +87,15 @@ transition Toggle (Easings Open easings) =

-- view

buttonBox :: Ease React'
buttonBox :: Ease ReactA'
buttonBox = div_ [ class_ "button-box" ] $
button_ [ class_ "btn btn--m btn--gray-border"
, onClick (const (Just Toggle))
]
"toggle easing"

view :: EasingState -> Ease React'
view (Easings direction easings) = div_ $ do
EasingMap runningEasings <- getAnimationState
view :: EasingState -> AnimState -> Ease ReactA'
view (Easings direction easings) (EasingMap runningEasings) = div_ $ do
let t = if direction == Closed then 0 else 1

buttonBox
Expand Down Expand Up @@ -126,7 +128,7 @@ safeShow x =
in if take 2 shown == "--" then drop 2 shown else shown

-- Trying to replicate http://www.objc.io/issue-12/view-layer-synergy.html
subView :: Double -> Easing -> Ease React'
subView :: Double -> Easing -> Ease ReactA'
subView t easing = svg_ [ width_ 100
, height_ 100
, viewBox_ "0 0 100 100"
Expand Down Expand Up @@ -177,6 +179,6 @@ subView t easing = svg_ [ width_ 100
]


easingClass :: IO (Ease ReactClass)
easingClass :: IO (Ease ReactClassA')
easingClass =
createClass view transition initialClassState initialAnimationState []
12 changes: 7 additions & 5 deletions example/src/Simple.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Simple (simpleClass) where
import Haste
import Haste.JSON
import React
import React.Class

-- model

Expand All @@ -15,23 +16,24 @@ data SimpleState = SimpleState
, fighter2 :: JSString
, typing :: JSString -- what the user's currently typing
}
type Simple a = a SimpleState Transition ()
type Simple a = a SimpleState Transition

initialState = SimpleState "little mac!" "pit" ""

-- update

transition :: Transition -> SimpleState -> (SimpleState, [AnimConfig Transition ()])
transition (Typing str) state = (state{typing=str}, [])
transition :: Transition -> SimpleState -> SimpleState
transition (Typing str) state = state{typing=str}
transition Enter SimpleState{fighter1, typing} =
(SimpleState typing fighter1 "", [])
SimpleState typing fighter1 ""

-- view

view :: SimpleState -> Simple React'
view (SimpleState fighter1 fighter2 typing) = div_ $ do
div_ $ do
"send a new competitor into the ring: "
div_ [] $ text_ typing
input_
[ value_ typing

Expand All @@ -52,4 +54,4 @@ view (SimpleState fighter1 fighter2 typing) = div_ $ do
text_ fighter2

simpleClass :: IO (Simple ReactClass)
simpleClass = createClass view transition initialState () []
simpleClass = createClass view transition initialState []
10 changes: 6 additions & 4 deletions example/src/Slide.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import Haste
import Haste.JSON
import Lens.Family2 hiding (view)
import React
import React.Anim
import React.Anim.Class


-- model
Expand Down Expand Up @@ -42,9 +44,9 @@ transition Toggle Closed = (Open, [ slide (-paneWidth) ])

-- view

view :: SlideState -> Slide React'
view slid = div_ [ class_ "slider-container" ] $ do
animWidth <- getAnimationState
view :: SlideState -> Double -> Slide ReactA'
view slid animWidth = div_ [ class_ "slider-container" ] $ do

let inherentWidth = case slid of
Open -> paneWidth
Closed -> 0
Expand All @@ -55,6 +57,6 @@ view slid = div_ [ class_ "slider-container" ] $ do
]
""

slideClass :: IO (Slide ReactClass)
slideClass :: IO (Slide ReactClassA')
slideClass =
createClass view transition initialClassState initialAnimationState []
31 changes: 28 additions & 3 deletions lib/stubs.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ function js_id(a) {return a;}
// custom
function js_React_DOM_leaf(name, a) { return React.DOM[name](a); }
function js_React_DOM_parent(name, a, c) { return React.DOM[name](a, c); }
function js_React_DOM_class(klass) { return React.createElement(klass, null); }

function js_parseChangeEvent(raw) {
// wrap the string in two constructors - Ptr and JSString
Expand Down Expand Up @@ -320,9 +321,17 @@ function js_raf(cb) {
});
}

function js_createClass(render, setState) {
function js_createClass(render, getInitialState, _) {
return React.createClass({
render: render
render: function() {
// render :: a -> b -> IO ForeignNode
// need either
// - something like runIO
// - render to not run in the IO monad
// - React to use continuation style passing
return B(A(render, [[0, this], [0, this.state.hs], 0]))[1];
},
getInitialState: function() { return {hs: B(A(getInitialState, [[0, this], 0]))} }
});
}

Expand All @@ -331,9 +340,25 @@ function js_bezier(x0, y0, x1, y1, x) {
}

function js_render(e, r){
React.render(e, r);
React.render(React.createElement(e, null), r);
}

function js_cancelRaf(id) {
window.cancelAnimationFrame(id);
}

function js_getState(inst) {
return inst.state.hs;
}

function js_setState(inst, state) {
inst.replaceState({hs: state});
}

function js_overState(inst, func) {
inst.replaceState({hs: B(A(func, [[0, inst.state.hs], 0]))[1]});
}

function js_performance_now() {
return window.performance.now();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note to self: think about polyfilling

}
5 changes: 2 additions & 3 deletions react-haskell.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,11 @@ source-repository head
location: https://github.com/joelburget/react-haskell.git

library
exposed-modules: React
exposed-modules: React, React.Class, React.Anim, React.Anim.Class
other-modules:
React.Anim,
React.Attrs,
React.Class,
React.Elements,
React.ElemTypes,
React.Events,
React.Imports,
React.Interpret,
Expand Down
19 changes: 7 additions & 12 deletions src/React.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,15 @@ module React
( module X

-- React.Anim
, Color(..)
, getAnimationState
, Animatable(..) -- XXX

-- React.Class
, ReactClass()
, createClass
--, Color(..)
--, getAnimationState
--, Animatable(..) -- XXX

-- React.Local
, locally
, GeneralizeSignal(..)

-- React.Render
, cancelRender
, render

-- React.Types
Expand All @@ -31,8 +26,8 @@ module React
, React'
, Pure
, RenderHandle(..)
, AnimConfig(..)
, Easing(..)
--, AnimConfig(..)
--, Easing(..)
, EventProperties(..)
, ModifierKeys(..)
, MouseEvent(..)
Expand All @@ -58,13 +53,13 @@ module React
-- store elem in monad
-- escaping / dangerouslySetInnerHTML

import React.Anim
import React.Class
-- import React.Imports
-- import React.Interpret
import React.Local
import React.Render
import React.Types
import React.Render
import React.ElemTypes

import React.Attrs as X
import React.Elements as X
Expand Down
Loading