/
Modal.purs
510 lines (471 loc) · 18.7 KB
/
Modal.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
module Lumi.Components.Modal where
import Prelude
import Color (cssStringHSLA, hsla, toHSLA)
import Data.Foldable (foldMap, for_)
import Data.Maybe (Maybe(..))
import Data.Monoid (guard)
import Data.Nullable (Nullable, toMaybe, toNullable)
import Effect (Effect)
import Effect.Uncurried (EffectFn2, mkEffectFn1, runEffectFn2)
import Effect.Unsafe (unsafePerformEffect)
import JSS (JSS, jss)
import Lumi.Components.Button as Button
import Lumi.Components.Color (colors)
import Lumi.Components.Icon (IconType(..), icon_)
import Lumi.Components.Link as Link
import Lumi.Components.Size (Size(..))
import Lumi.Components.Text (sectionHeader_, subsectionHeader, text)
import Lumi.Components.ZIndex (ziModal)
import Prim.Row (class Nub, class Union)
import React.Basic.Classic (Component, JSX, ReactComponent, createComponent, element, empty, make, makeStateless, toReactComponent)
import React.Basic.DOM as R
import React.Basic.DOM.Components.GlobalEvents (windowEvent)
import React.Basic.DOM.Events (currentTarget, stopPropagation, target)
import React.Basic.Events as Events
import Record (merge)
import Web.Event.Event (Event, EventType(..))
import Web.Event.Event as E
import Web.UIEvent.KeyboardEvent (fromEvent, key)
foreign import toggleBodyClass :: EffectFn2 String Boolean Unit
type ModalLinkProps value props =
{ label :: JSX
, title :: JSX
, component :: { value :: value
, onChange :: value -> Effect Unit
| props
}
-> JSX
, actionButtonTitle :: String
, value :: value
, onChange :: value -> Effect Unit
, componentProps :: { | props }
, style :: R.CSS
}
modalLinkComponent :: forall value props. Component (ModalLinkProps value props)
modalLinkComponent = createComponent "ModalLink"
-- | This component displays a link which displays an editor component in
-- | a modal popup.
modalLink
:: forall value props
. Union
props
( value :: value
, onChange :: value -> Effect Unit
)
( value :: value
, onChange :: value -> Effect Unit
| props
)
=> Nub
( value :: value
, onChange :: value -> Effect Unit
| props
)
( value :: value
, onChange :: value -> Effect Unit
| props
)
=> ModalLinkProps value props
-> JSX
modalLink = make modalLinkComponent { initialState, didMount, render }
where
initialState =
{ modalOpen: false
, value: Nothing
}
didMount self = do
self.setState _ { value = Just self.props.value }
render self@{ props, state } =
lumiModalLink
{ style: props.style
, children:
[ Link.link Link.defaults
{ className = pure "lumi"
, navigate = pure $ self.setState _ { modalOpen = true }
, text = props.label
}
, case state.value of
Nothing -> empty
Just value_ ->
modal
{ modalOpen: state.modalOpen
, internalBorders: false
, title: props.title
, onActionButtonClick: toNullable $ Just do
self.setState _ { modalOpen = false }
props.onChange value_
, actionButtonTitle: props.actionButtonTitle
, actionButtonState: Button.Enabled
, size: Medium
, variant: ""
, closeButton: true
, onRequestClose: self.setState _ { modalOpen = false }
, children:
props.component $
merge
props.componentProps
( { value: value_
, onChange: \value -> self.setState _ { value = Just value }
} :: { value :: value
, onChange :: value -> Effect Unit
}
)
}
]
}
lumiModalLink = element (unsafePerformEffect $ R.unsafeCreateDOMComponent "lumi-modal-link")
type CommonProps rest =
{ modalOpen :: Boolean
, onActionButtonClick :: Nullable (Effect Unit)
, actionButtonTitle :: String
, actionButtonState :: Button.ButtonState
, size :: Size
, variant :: String
, children :: JSX
, internalBorders :: Boolean
, closeButton :: Boolean
, title :: JSX
| rest
}
type ModalProps = CommonProps (onRequestClose :: Effect Unit)
foreign import modalBuilder :: ReactComponent ModalPortalProps -> ReactComponent ModalProps
modal_ :: ReactComponent ModalProps
modal_ = modalBuilder modalPortal
modal :: ModalProps -> JSX
modal = element modal_
modalTitle :: String -> JSX
modalTitle s = sectionHeader_ s
type DialogProps =
{ modalOpen :: Boolean
, onRequestClose :: Effect Unit
, onActionButtonClick :: Nullable (Effect Unit)
, actionButtonTitle :: String
, size :: Size
, children :: JSX
}
dialogComponent :: Component DialogProps
dialogComponent = createComponent "Dialog"
dialog :: DialogProps -> JSX
dialog = makeStateless dialogComponent render
where
render props =
modal
{ modalOpen: props.modalOpen
, closeButton: true
, onRequestClose: props.onRequestClose
, onActionButtonClick: props.onActionButtonClick
, actionButtonTitle: props.actionButtonTitle
, actionButtonState: Button.Enabled
, size: props.size
, variant: "dialog"
, children:
text subsectionHeader
{ style = R.css { fontWeight: 400 }
, children = [ props.children ]
}
, internalBorders: false
, title: modalTitle ""
}
type ErrorModalProps =
{ modalOpen :: Boolean
, onActionButtonClick :: Nullable (Effect Unit)
, actionButtonTitle :: String
, children :: JSX
, onRequestClose :: Effect Unit
}
errorModalComponent :: Component ErrorModalProps
errorModalComponent = createComponent "Error modal"
errorModal :: ErrorModalProps -> JSX
errorModal = makeStateless errorModalComponent render
where
render props =
modal
{ modalOpen: props.modalOpen
, closeButton: true
, onRequestClose: props.onRequestClose
, onActionButtonClick: props.onActionButtonClick
, actionButtonTitle: props.actionButtonTitle
, actionButtonState: Button.Enabled
, size: Small
, variant: ""
, children: props.children
, internalBorders: false
, title: modalTitle "Error"
}
type ModalPortalProps = CommonProps (requestClose :: Effect Unit)
modalPortalComponent :: Component ModalPortalProps
modalPortalComponent = createComponent "ModalPortal"
modalPortal :: ReactComponent ModalPortalProps
modalPortal = toReactComponent identity modalPortalComponent
{ didMount: checkBodyClass
, didUpdate: \self _ -> checkBodyClass self
, willUnmount: const closeModal
, render
}
where
checkBodyClass self =
if self.props.modalOpen
then openModal
else closeModal
openModal = runEffectFn2 toggleBodyClass "react-modal-open" true
closeModal = runEffectFn2 toggleBodyClass "react-modal-open" false
render { props, state } =
if not props.modalOpen
then empty
else windowEvent
{ eventType: EventType "keydown"
, options: { capture: false, once: false, passive: false }
, handler: \e -> do
keydownEventHandler e
}
$ lumiModalContainer
{ onClick: Events.handler (Events.merge { target, currentTarget })
\{ target, currentTarget } -> do
props.requestClose
closeModal
, children:
lumiModalOverlay
{ "data-variant": props.variant
, children:
lumiModal
{ "data-size": show props.size
, onClick: Events.handler stopPropagation \_ -> pure unit
, children:
[ lumiModalHeader
{ children:
[ if props.variant == "dialog"
then empty
else lumiModalClose
{ children: icon_ Remove
, onClick: mkEffectFn1 \_ -> do
props.requestClose
closeModal
}
, props.title
]
}
, lumiModalContent
{ children: props.children
, "data-internal-borders": props.internalBorders
}
, lumiModalFooter
{ children:
[ guard props.closeButton $
Button.button Button.secondary
{ title =
case toMaybe props.onActionButtonClick of
Nothing -> "Close"
Just _ -> "Cancel"
, onPress = mkEffectFn1 \_ -> do
props.requestClose
closeModal
}
, toMaybe props.onActionButtonClick # foldMap \actionFn ->
Button.button Button.primary
{ title = props.actionButtonTitle
, buttonState = props.actionButtonState
, onPress = mkEffectFn1 \_ -> actionFn
, style = R.css { marginLeft: "12px" }
}
]
}
]
}
}
}
where
keydownEventHandler :: Event -> Effect Unit
keydownEventHandler e = do
let mKey = eventKey e
for_ mKey case _ of
"Escape" -> do
E.preventDefault e
E.stopPropagation e
props.requestClose
closeModal
_ -> pure unit
lumiModalContainer = element (unsafePerformEffect $ R.unsafeCreateDOMComponent "lumi-modal-container")
lumiModalOverlay = element (unsafePerformEffect $ R.unsafeCreateDOMComponent "lumi-modal-overlay")
lumiModal = element (unsafePerformEffect $ R.unsafeCreateDOMComponent "lumi-modal")
lumiModalClose = element (unsafePerformEffect $ R.unsafeCreateDOMComponent "lumi-modal-close")
lumiModalHeader = element (unsafePerformEffect $ R.unsafeCreateDOMComponent "lumi-modal-header")
lumiModalContent = element (unsafePerformEffect $ R.unsafeCreateDOMComponent "lumi-modal-content")
lumiModalFooter = element (unsafePerformEffect $ R.unsafeCreateDOMComponent "lumi-modal-footer")
styles :: JSS
styles = jss
{ "@global":
{ -- locks normal content scrolling while the modal is open
"body.react-modal-open":
{ overflow: "hidden"
}
-- fills screen with faded backdrop and positions the modal
, "lumi-modal-container":
{ boxSizing: "border-box"
, position: "fixed"
, top: "0"
, left: "0"
, bottom: "0"
, right: "0"
, zIndex: ziModal
, display: "flex"
, flexFlow: "column"
, alignItems: "center" -- horizontal centering, vertical centering is handled by lumi-modal-overlay > lumi-modal
, overflowY: "auto"
, background: cssStringHSLA (alpha 0.25 colors.black)
, "WebkitOverflowScrolling": "touch" -- smooth momentum scrolling (iOS)
-- invisible modal wrapper adds padding and vertical centering
, "& lumi-modal-overlay":
{ boxSizing: "border-box"
, display: "flex"
, flexFlow: "column"
, padding: "64px 0"
, "@media (max-width: 860px)": { padding: "0" }
, flex: "0 0 auto" -- expand to encompass large modals with scrolling
, minHeight: "100%" -- vertical centering of small modals
-- the modal itself
, "& lumi-modal":
{ boxSizing: "border-box"
, margin: "auto 0" -- vertical centering of small modals
, display: "flex"
, flexFlow: "column"
, position: "relative"
, padding: "24px 0"
, "&[data-size=\"small\"]":
{ width: "400px"
}
, "&[data-size=\"medium\"]":
{ width: "504px"
}
, "&[data-size=\"large\"]":
{ width: "600px"
}
, "&[data-size=\"extra-large\"]":
{ width: "848px"
}
, "&[data-size=\"extra-extra-large\"]":
{ width: "1048px"
}
, maxWidth: "calc(100vw - (24px * 2))"
, background: "rgba(255, 255, 255, 1)"
, borderRadius: "4px"
, "& lumi-modal-close":
{ boxSizing: "border-box"
, position: "absolute"
, top: "22px"
, right: "24px"
, display: "flex"
, flexFlow: "column"
, fontSize: "14px"
, color: cssStringHSLA colors.black2
, cursor: "pointer"
, "@media (max-width: 860px)":
{ "& lumi-font-icon":
{ fontSize: "16px"
, lineHeight: "24px"
}
}
}
, "& lumi-modal-header":
{ boxSizing: "border-box"
, flex: "0 0 auto"
, display: "flex"
, flexFlow: "column"
, paddingLeft: "24px"
, paddingRight: "48px" -- avoid colliding with the close "X"
, "@media (max-width: 860px)":
{ paddingLeft: "16px"
}
, padding: "0 48px 0 24px"
, "& > lumi-section-header":
{ paddingTop: "0"
, marginTop: "0"
}
, lineHeight: "1"
}
, "& lumi-modal-content":
{ boxSizing: "border-box"
, margin: "24px 0"
, flex: "1 0 auto"
, display: "flex"
, flexFlow: "column"
, paddingLeft: "24px"
, paddingRight: "24px"
, "@media (max-width: 860px)":
{ paddingLeft: "16px"
, paddingRight: "16px"
}
, "&[data-internal-borders=\"true\"]":
{ borderTop: [ "1px", "solid", cssStringHSLA colors.black4 ]
, borderBottom: [ "1px", "solid", cssStringHSLA colors.black4 ]
, paddingTop: "8px"
, paddingBottom: "8px"
}
}
, "& lumi-modal-footer":
{ boxSizing: "border-box"
, flex: "0 0 auto"
, display: "flex"
, flexFlow: "row"
, justifyContent: "flex-end"
, paddingLeft: "24px"
, paddingRight: "24px"
, "@media (max-width: 860px)":
{ paddingLeft: "16px"
, paddingRight: "16px"
, "& button.lumi":
{ fontSize: "16px"
, lineHeight: "initial"
, padding: "10px 21px 10px"
}
}
}
}
-- non-dialog modals fill the screen on mobile devices
, "&:not([data-variant=\"dialog\"])":
{ "@media (max-width: 860px)":
{ maxHeight: "100%"
, "& lumi-modal":
{ position: "fixed"
, top: "0"
, right: "0"
, bottom: "0"
, left: "0"
, height: "100%"
, width: "100vw"
, maxWidth: "100vw"
, borderRadius: "0"
, "&[data-size=\"small\"], &[data-size=\"medium\"], &[data-size=\"large\"]":
{ width: "100vw"
}
, "& lumi-modal-content":
{ flex: "1 1 auto"
, overflowY: "auto"
}
}
}
}
, "&[data-variant=\"dialog\"] lumi-modal lumi-modal-content":
{ margin: "0 0 16px"
}
-- constrain total height while making the intenal content area flexible and scrollable
, "&[data-variant=\"internal-scrolling\"]":
{ maxHeight: "100%"
, "& lumi-modal":
{ height: "100%"
}
, "& lumi-modal lumi-modal-content":
{ flex: "1 1 auto"
, overflowY: "auto"
}
}
, "&[data-variant=\"top-aligned\"] lumi-modal":
{ margin: "0"
}
}
}
}
}
where
alpha a = toHSLA >>> \{ h, s, l } -> hsla h s l a
eventKey :: Event -> Maybe String
eventKey e = map key (fromEvent e)