-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathExample.purs
167 lines (146 loc) · 4.63 KB
/
Example.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
module Lumi.Components.Example
( exampleStyleToggle
, example
, exampleCode
) where
import Prelude
import Color (cssStringHSLA)
import Data.Array (index)
import Data.Maybe (Maybe(..), fromMaybe)
import Data.String (Pattern(..), split, toLower)
import Effect.Uncurried (runEffectFn1)
import Lumi.Components.Color (colors)
import Lumi.Components.Column (column)
import Lumi.Components.Tab (TabId(..), TabKey(..), tabs, urlParts)
import Lumi.Components.Text (body, text)
import Lumi.Components.Utility.ReactRouter (RouterProps, withRouter)
import React.Basic.Classic (Component, JSX, createComponent, element, empty, toReactComponent)
import React.Basic.DOM (css, mergeStyles)
import React.Basic.DOM as R
import Web.HTML.History (URL(..))
type Lang = String
data ExampleTabs = Demo | Boundaries
derive instance eqExampleTabs :: Eq ExampleTabs
renderTabLabel :: ExampleTabs -> String
renderTabLabel = case _ of
Demo -> "Demo"
Boundaries -> "Boundaries"
parseTabLabel :: String -> Maybe ExampleTabs
parseTabLabel = case _ of
"Demo" -> Just Demo
"Boundaries" -> Just Boundaries
_ -> Nothing
exampleStyleToggle :: JSX
exampleStyleToggle = (\c -> element c { children: empty }) (withRouter $ toReactComponent identity component { render: render <<< _.props })
where
render props =
tabs
{ currentLocation: getCurrentLocation props
, navigate: Just \url ->
let
parts = urlParts url
newUrl = parts.path <> parts.query <> parts.hash.path <> parts.hash.query
newUrlNoHash = fromMaybe "" $ flip index 1 $ split (Pattern "#") newUrl
in
runEffectFn1 props.history.push $ URL $ newUrlNoHash
, queryKey: TabKey "example"
, selectedTabStyle: css { boxShadow: "none" }
, style: css { boxShadow: "none" }
, tabStyle: css {}
, tabs:
[ mkTab Demo
, mkTab Boundaries
]
, useHash: true
}
mkTab tab =
{ content: \_ -> empty
, id: TabId $ toLower $ renderTabLabel tab
, label: renderTabLabel tab
, count: Nothing
, testId: Nothing
}
component :: Component (RouterProps ( children :: JSX ))
component = createComponent "Example"
example :: JSX -> JSX
example = (\c children -> element c { children }) (withRouter $ toReactComponent identity component { render: render <<< _.props })
where
render props =
tabs
{ currentLocation: getCurrentLocation props
, navigate: Nothing
, queryKey: TabKey "example"
, selectedTabStyle: css {}
, style: css { height: 0, boxShadow: "none" }
, tabStyle: css { display: "none" }
, tabs:
[ mkTab Demo props.children
, mkTab Boundaries props.children
]
, useHash: true
}
mkTab tab children =
{ content: \_ -> tabContent tab children
, id: TabId $ toLower $ renderTabLabel tab
, label: renderTabLabel tab
, count: Nothing
, testId: Nothing
}
tabContent tab children =
outerRow tab
[ innerRow tab
case tab of
Demo -> children
Boundaries -> children
]
outerRow tab children =
column
{ children
, style:
case tab of
Demo -> outerRowStyle
Boundaries -> mergeStyles
[ outerRowStyle
, outerRowStyle_Boundaries
]
}
outerRowStyle = css
{ justifyContent: "center"
, boxSizing: "border-box"
, marginBottom: "32px"
}
outerRowStyle_Boundaries = css
{ backgroundColor: "#f7f6f4"
}
innerRow tab child =
column
{ style: mergeStyles
[ innerRowStyle
, case tab of
Boundaries -> innerRowStyle_Boundaries
_ -> css {}
]
, children: [ child ]
}
innerRowStyle = css
{ margin: 20
, background: cssStringHSLA colors.white
, border: "1px solid rgba(0, 0, 255, 0)"
, alignItems: "flex-start"
}
innerRowStyle_Boundaries = css
{ border: "1px solid rgba(0, 0, 255, 0.6)"
, boxShadow: "0 0 40px rgba(0, 0, 255, 0.4)"
}
getCurrentLocation
:: forall props
. { location :: { pathname :: String
, search :: String
, hash :: String
}
| props
}
-> URL
getCurrentLocation props = URL $ "#" <> props.location.pathname <> props.location.search <> props.location.hash
exampleCode :: String -> JSX
exampleCode code = text body { children = [ R.pre_ [ R.code_ [ R.text code ] ] ] }