Skip to content

Commit b6fbbf7

Browse files
author
Daniel Harvey
committed
Opening salvo of regretful turds
0 parents  commit b6fbbf7

File tree

11 files changed

+335
-0
lines changed

11 files changed

+335
-0
lines changed

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/bower_components/
2+
/node_modules/
3+
/.pulp-cache/
4+
/output/
5+
/generated-docs/
6+
/.psc-package/
7+
/.psc*
8+
/.purs*
9+
/.psa*
10+
/.spago

packages.dhall

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
let mkPackage =
2+
https://raw.githubusercontent.com/purescript/package-sets/psc-0.13.2-20190725/src/mkPackage.dhall sha256:0b197efa1d397ace6eb46b243ff2d73a3da5638d8d0ac8473e8e4a8fc528cf57
3+
4+
let upstream =
5+
https://raw.githubusercontent.com/purescript/package-sets/psc-0.13.2-20190725/src/packages.dhall sha256:60cc03d2c3a99a0e5eeebb16a22aac219fa76fe6a1686e8c2bd7a11872527ea3
6+
7+
let overrides = {=}
8+
9+
let additions =
10+
{ cssom =
11+
mkPackage
12+
[ "effect", "console" ]
13+
"https://github.com/danieljharvey/purescript-cssom.git"
14+
"v0.0.2"
15+
, styling =
16+
mkPackage
17+
[ "console"
18+
, "debug"
19+
, "effect"
20+
, "foreign"
21+
, "generics-rep"
22+
, "ordered-collections"
23+
, "prelude"
24+
, "psci-support"
25+
, "refs"
26+
, "test-unit"
27+
, "unordered-collections"
28+
, "cssom"
29+
]
30+
"../purs-cssom"
31+
"v0.0.1"
32+
}
33+
34+
35+
in upstream // overrides // additions

spago.dhall

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{-
2+
Welcome to a Spago project!
3+
You can edit this file as you like.
4+
-}
5+
{ name =
6+
"my-project"
7+
, dependencies =
8+
[ "effect", "console", "psci-support", "react", "cssom", "styling" ]
9+
, packages =
10+
./packages.dhall
11+
}

src/Internal/Consumer.purs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module React.Stylesheet.Internal.Consumer where
2+
3+
import Prelude (($))
4+
import Effect.Unsafe (unsafePerformEffect)
5+
6+
import Data.Maybe (Maybe(..))
7+
import Data.Symbol (class IsSymbol)
8+
9+
import React as React
10+
import PursUI (CSSRuleSet, PursUI, addStyle)
11+
12+
import React.Stylesheet.Internal.Types
13+
14+
-- | Internal function that creates a Purescript React class which allows
15+
-- | consumption of the Radox store via React Context
16+
styleConsumer
17+
:: forall props label localState
18+
. IsSymbol label
19+
=> React.Context (Maybe (PursUI label))
20+
-> props
21+
-> localState
22+
-> CSSRuleSet props
23+
-> StyleRenderMethod props localState
24+
-> React.ReactElement
25+
styleConsumer context props localState cssRules renderer = do
26+
let render stylesheet
27+
= let classNames = case stylesheet of
28+
Just stylesheet'
29+
-> unsafePerformEffect $ addStyle stylesheet' cssRules props
30+
_ -> []
31+
in renderer { props: props
32+
, localState: localState
33+
, classNames: classNames
34+
}
35+
React.createLeafElement context.consumer
36+
{ children: render }
37+
38+

src/Internal/Provider.purs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module React.Stylesheet.Internal.Provider where
2+
3+
import Prelude (bind, pure, ($))
4+
import Data.Maybe (Maybe(..))
5+
import Data.Symbol (class IsSymbol)
6+
import React as React
7+
import PursUI
8+
9+
-- | Internal function for creating a Purescript React class that providers the
10+
-- | Radox store through React Context
11+
styleProvider
12+
:: forall label
13+
. IsSymbol label
14+
=> React.Context (Maybe (PursUI label))
15+
-> React.ReactClass { children :: Array React.ReactElement }
16+
styleProvider context
17+
= React.pureComponent "Provider" component
18+
where
19+
component this = do
20+
(stylesheet :: PursUI label) <- createBlankStyleSheet
21+
pure $ { state : { }
22+
, render : render' this stylesheet
23+
}
24+
25+
render' this stylesheet = do
26+
props <- React.getProps this
27+
state <- React.getState this
28+
pure $
29+
React.createElement
30+
context.provider
31+
{ value: Just stylesheet
32+
}
33+
props.children
34+
35+
36+

src/Internal/StyleContext.purs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module React.Stylesheet.Internal.StyleContext where
2+
3+
import Data.Maybe (Maybe(..))
4+
import Data.Symbol (class IsSymbol, SProxy)
5+
import React as React
6+
import PursUI (PursUI)
7+
import React.Stylesheet.Internal.Types
8+
import React.Stylesheet.Internal.Provider (styleProvider)
9+
import React.Stylesheet.Internal.Consumer (styleConsumer)
10+
11+
-- | Takes a CombinedReducer and a default state, and returns both a root-level
12+
-- | provider and connect component for using the Radox store
13+
createStyleContext
14+
:: forall label
15+
. IsSymbol label
16+
=> SProxy label
17+
-> StyleContext label
18+
createStyleContext label =
19+
let myContext = React.createContext (Nothing :: Maybe (PursUI label))
20+
in { provider : styleProvider myContext
21+
, consumer : styleConsumer myContext
22+
}
23+
24+

src/Internal/Types.purs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module React.Stylesheet.Internal.Types where
2+
3+
import React as React
4+
import PursUI
5+
6+
type StyleRenderMethod props localState
7+
= (
8+
{ props :: props
9+
, localState :: localState
10+
, classNames :: Array CSSSelector
11+
} -> React.ReactElement
12+
)
13+
14+
type StyleContext (label :: Symbol)
15+
= { provider :: React.ReactClass { children :: Array React.ReactElement }
16+
, consumer :: (forall props localState
17+
. props
18+
-> localState
19+
-> CSSRuleSet props
20+
-> StyleRenderMethod props localState
21+
-> React.ReactElement)
22+
}
23+
24+

src/Main.purs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module Main where
2+
3+
import Prelude
4+
5+
import Effect (Effect)
6+
import Effect.Console (log)
7+
8+
main :: Effect Unit
9+
main = do
10+
log "🍝"

src/StyleProvider.purs

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
module StyleProvider where
2+
3+
import Prelude (bind, pure, ($))
4+
import Effect.Unsafe (unsafePerformEffect)
5+
6+
import Data.Maybe (Maybe(..))
7+
import Data.Symbol (class IsSymbol, SProxy(..))
8+
9+
import React as React
10+
import PursUI
11+
12+
styleContext :: StyleContext "horse"
13+
styleContext = createStyleContext (SProxy :: SProxy "horse")
14+
15+
type StyleRenderMethod props localState
16+
= (
17+
{ props :: props
18+
, localState :: localState
19+
, classNames :: Array CSSSelector
20+
} -> React.ReactElement
21+
)
22+
23+
type StyleContext (label :: Symbol)
24+
= { provider :: React.ReactClass { children :: Array React.ReactElement }
25+
, consumer :: (forall props localState
26+
. props
27+
-> localState
28+
-> CSSRuleSet props
29+
-> StyleRenderMethod props localState
30+
-> React.ReactElement)
31+
}
32+
33+
-- | Takes a CombinedReducer and a default state, and returns both a root-level
34+
-- | provider and connect component for using the Radox store
35+
createStyleContext
36+
:: forall label
37+
. IsSymbol label
38+
=> SProxy label
39+
-> StyleContext label
40+
createStyleContext label =
41+
let myContext = React.createContext (Nothing :: Maybe (PursUI label))
42+
in { provider : styleProvider myContext
43+
, consumer : styleConsumer myContext
44+
}
45+
46+
-- | Internal function for creating a Purescript React class that providers the
47+
-- | Radox store through React Context
48+
styleProvider
49+
:: forall label
50+
. IsSymbol label
51+
=> React.Context (Maybe (PursUI label))
52+
-> React.ReactClass { children :: Array React.ReactElement }
53+
styleProvider context
54+
= React.pureComponent "Provider" component
55+
where
56+
component this = do
57+
(stylesheet :: PursUI label) <- createBlankStyleSheet
58+
pure $ { state : { }
59+
, render : render' this stylesheet
60+
}
61+
62+
render' this stylesheet = do
63+
props <- React.getProps this
64+
state <- React.getState this
65+
pure $
66+
React.createElement
67+
context.provider
68+
{ value: Just stylesheet
69+
}
70+
props.children
71+
72+
73+
-- | Internal function that creates a Purescript React class which allows
74+
-- | consumption of the Radox store via React Context
75+
styleConsumer
76+
:: forall props label localState
77+
. IsSymbol label
78+
=> React.Context (Maybe (PursUI label))
79+
-> props
80+
-> localState
81+
-> CSSRuleSet props
82+
-> StyleRenderMethod props localState
83+
-> React.ReactElement
84+
styleConsumer context props localState cssRules renderer = do
85+
let render stylesheet
86+
= let classNames = case stylesheet of
87+
Just stylesheet'
88+
-> unsafePerformEffect $ addStyle stylesheet' cssRules props
89+
_ -> []
90+
in renderer { props: props
91+
, localState: localState
92+
, classNames: classNames
93+
}
94+
React.createLeafElement context.consumer
95+
{ children: render }
96+
97+

src/Styled/Div.purs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
module App.Components.Styled.Div (div) where
2+
3+
import Prelude (map, ($))
4+
import Data.Array (intercalate)
5+
import React.DOM.Props as Props
6+
import React as React
7+
import React.DOM as RDom
8+
9+
import StyleProvider
10+
11+
import PursUI (CSSRuleSet, CSSSelector(..))
12+
13+
construct
14+
:: forall props
15+
. (Array Props.Props -> Array React.ReactElement -> React.ReactElement)
16+
-> CSSRuleSet props
17+
-> props
18+
-> Array React.ReactElement
19+
-> React.ReactElement
20+
construct element cssRule props children
21+
= styleContext.consumer props {} cssRule renderDiv
22+
where
23+
renderDiv { classNames }
24+
= RDom.div [ Props.className (toClassNames classNames) ]
25+
children
26+
27+
div
28+
:: forall props
29+
. CSSRuleSet props
30+
-> props
31+
-> Array React.ReactElement
32+
-> React.ReactElement
33+
div = construct RDom.div
34+
35+
toClassNames :: Array CSSSelector -> String
36+
toClassNames as
37+
= intercalate " " $ map unwrap as
38+
where
39+
unwrap (CSSClassSelector s) = s

0 commit comments

Comments
 (0)