@charmland/gem 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (121) hide show
  1. package/LICENSE +177 -0
  2. package/NOTICE +13 -0
  3. package/README.md +24 -2
  4. package/assets/fonts/Hypography-Italic.woff2 +0 -0
  5. package/assets/fonts/Hypography-Regular.woff2 +0 -0
  6. package/assets/fonts/PPFraktionMono-Variable.ttf +0 -0
  7. package/assets/fonts/PPFraktionMonoItalic-Variable.ttf +0 -0
  8. package/assets/fonts/PPFraktionSans-Variable.ttf +0 -0
  9. package/assets/fonts/PPFraktionSansItalic-Variable.ttf +0 -0
  10. package/bin/gem-elm.js +3 -0
  11. package/dist/browser/boot.d.ts +10 -0
  12. package/dist/browser/boot.js +14 -0
  13. package/dist/browser/color-scheme.d.ts +9 -0
  14. package/dist/browser/color-scheme.js +76 -0
  15. package/dist/browser/ports.d.ts +37 -0
  16. package/dist/browser/ports.js +90 -0
  17. package/dist/browser/router.d.ts +5 -0
  18. package/dist/browser/router.js +35 -0
  19. package/dist/browser/runtime.d.ts +12 -0
  20. package/dist/browser/runtime.js +22 -0
  21. package/dist/browser/tabs.d.ts +9 -0
  22. package/dist/browser/tabs.js +69 -0
  23. package/dist/default/theme.css +4664 -0
  24. package/dist/default/theme.js +13815 -0
  25. package/dist/node/build.d.ts +16 -0
  26. package/dist/node/build.js +48 -0
  27. package/dist/node/cli.d.ts +2 -0
  28. package/dist/node/cli.js +21 -0
  29. package/elm.json +28 -0
  30. package/package.json +86 -4
  31. package/src/app/Gem/App.elm +1268 -0
  32. package/src/app/Gem/CharmSticker.elm +35 -0
  33. package/src/app/Gem/Components.elm +292 -0
  34. package/src/app/Gem/Main.elm +17 -0
  35. package/src/app/Gem/Ports.elm +32 -0
  36. package/src/app/Gem/Shell.elm +957 -0
  37. package/src/browser/theme.d.ts +1 -0
  38. package/src/core/Gem/Node.elm +219 -0
  39. package/src/core/Gem/Page.elm +560 -0
  40. package/src/core/Gem/Render.elm +510 -0
  41. package/src/core/Gem/Slots.elm +11 -0
  42. package/src/styles/docs.scss +1083 -0
  43. package/src/styles/fonts.scss +51 -0
  44. package/src/styles/gem.scss +362 -0
  45. package/src/styles/theme.scss +8 -0
  46. package/src/styles/ui/burger.scss +272 -0
  47. package/src/styles/ui/button.scss +350 -0
  48. package/src/styles/ui/charmtone.scss +70 -0
  49. package/src/styles/ui/controls-extra.scss +316 -0
  50. package/src/styles/ui/controls.scss +110 -0
  51. package/src/styles/ui/display.scss +359 -0
  52. package/src/styles/ui/dropdown.scss +341 -0
  53. package/src/styles/ui/form.scss +314 -0
  54. package/src/styles/ui/menu.scss +200 -0
  55. package/src/styles/ui/meter.scss +124 -0
  56. package/src/styles/ui/modal.scss +545 -0
  57. package/src/styles/ui/oauth.scss +50 -0
  58. package/src/styles/ui/page-nav.scss +63 -0
  59. package/src/styles/ui/reset.scss +111 -0
  60. package/src/styles/ui/scrollbar.scss +39 -0
  61. package/src/styles/ui/search.scss +164 -0
  62. package/src/styles/ui/sidebar.scss +229 -0
  63. package/src/styles/ui/squircle.scss +60 -0
  64. package/src/styles/ui/table.scss +97 -0
  65. package/src/styles/ui/theme.scss +132 -0
  66. package/src/styles/ui/toast.scss +587 -0
  67. package/src/styles/ui/toc.scss +128 -0
  68. package/src/styles/ui/typography.scss +21 -0
  69. package/src/styles/ui/ui.scss +33 -0
  70. package/src/ui/Ui/Badge.elm +51 -0
  71. package/src/ui/Ui/Banner.elm +97 -0
  72. package/src/ui/Ui/Burger.elm +259 -0
  73. package/src/ui/Ui/Button.elm +261 -0
  74. package/src/ui/Ui/Card.elm +32 -0
  75. package/src/ui/Ui/Cards.elm +15 -0
  76. package/src/ui/Ui/ChoiceGroup.elm +62 -0
  77. package/src/ui/Ui/CopyButton.elm +139 -0
  78. package/src/ui/Ui/Divider.elm +16 -0
  79. package/src/ui/Ui/Dropdown.elm +600 -0
  80. package/src/ui/Ui/Empty.elm +41 -0
  81. package/src/ui/Ui/ErrorBubble.elm +34 -0
  82. package/src/ui/Ui/Field.elm +45 -0
  83. package/src/ui/Ui/FormStatus.elm +32 -0
  84. package/src/ui/Ui/Icon.elm +381 -0
  85. package/src/ui/Ui/InlineForm.elm +51 -0
  86. package/src/ui/Ui/Input.elm +113 -0
  87. package/src/ui/Ui/KeyValue.elm +25 -0
  88. package/src/ui/Ui/ListRow.elm +67 -0
  89. package/src/ui/Ui/Loader.elm +32 -0
  90. package/src/ui/Ui/Logo/Charm.elm +21 -0
  91. package/src/ui/Ui/Logo/CharmMark.elm +32 -0
  92. package/src/ui/Ui/Logo/CharmSticker.elm +35 -0
  93. package/src/ui/Ui/Logo/Discord.elm +26 -0
  94. package/src/ui/Ui/Logo/GitHub.elm +21 -0
  95. package/src/ui/Ui/Logo/Google.elm +21 -0
  96. package/src/ui/Ui/Logo/Hexy.elm +29 -0
  97. package/src/ui/Ui/Logo/Hyper.elm +42 -0
  98. package/src/ui/Ui/Logo/Integration.elm +117 -0
  99. package/src/ui/Ui/Logo/Prism.elm +35 -0
  100. package/src/ui/Ui/Logos.elm +115 -0
  101. package/src/ui/Ui/Menu.elm +348 -0
  102. package/src/ui/Ui/Meter.elm +132 -0
  103. package/src/ui/Ui/Modal.elm +247 -0
  104. package/src/ui/Ui/Motion.elm +32 -0
  105. package/src/ui/Ui/NumberBox.elm +86 -0
  106. package/src/ui/Ui/OAuthButton.elm +39 -0
  107. package/src/ui/Ui/PageNav.elm +65 -0
  108. package/src/ui/Ui/Pager.elm +49 -0
  109. package/src/ui/Ui/Search.elm +396 -0
  110. package/src/ui/Ui/SearchBox.elm +78 -0
  111. package/src/ui/Ui/Section.elm +21 -0
  112. package/src/ui/Ui/Sidebar.elm +239 -0
  113. package/src/ui/Ui/Stat.elm +29 -0
  114. package/src/ui/Ui/Switch.elm +34 -0
  115. package/src/ui/Ui/Table.elm +89 -0
  116. package/src/ui/Ui/Tabs.elm +280 -0
  117. package/src/ui/Ui/ToC.elm +120 -0
  118. package/src/ui/Ui/Toast.elm +1145 -0
  119. package/src/ui/Ui/Toolbar.elm +31 -0
  120. package/src/ui/Ui/TwoStepConfirm.elm +159 -0
  121. package/src/ui/Ui/Typography.elm +33 -0
@@ -0,0 +1,1145 @@
1
+ module Ui.Toast exposing
2
+ ( Card(..)
3
+ , Config
4
+ , Duration(..)
5
+ , Kind(..)
6
+ , Model
7
+ , Msg(..)
8
+ , Position(..)
9
+ , Settings
10
+ , hasCards
11
+ , init
12
+ , presets
13
+ , setDuration
14
+ , subscriptions
15
+ , update
16
+ , viewRegion
17
+ )
18
+
19
+ {-| The toast: severity icon and title in the pill, description open
20
+ beneath it, cards stacked as a hover/tap-fanned deck that flings out
21
+ on drag. The host keeps a `Model`, maps a `Msg`, raises with `Push`.
22
+ An action lingers one and a half times longer but still dismisses;
23
+ `PushFor` pins a duration (`Sticky`) for the rare card that must not leave.
24
+ -}
25
+
26
+ import Browser.Dom
27
+ import Browser.Events
28
+ import Html exposing (Html, button, div, p, span, text)
29
+ import Html.Attributes exposing (attribute, class, classList)
30
+ import Html.Events
31
+ import Html.Keyed
32
+ import Json.Decode as Decode
33
+ import Ui.Motion as Motion
34
+ import Process
35
+ import Spring
36
+ import Ui.Icon as Icon
37
+ import Task
38
+ import Time
39
+
40
+
41
+
42
+ -- Public types
43
+
44
+
45
+ type Kind
46
+ = Default
47
+ | Success
48
+ | Info
49
+ | Warn
50
+ | Error
51
+
52
+
53
+ type Duration
54
+ = Ms Int
55
+ | Sticky
56
+
57
+
58
+ type alias Config =
59
+ { kind : Kind
60
+ , title : String
61
+ , description : Maybe String
62
+ , action : Maybe String
63
+ }
64
+
65
+
66
+ {-| Docks name the region corner.
67
+ -}
68
+ type Position
69
+ = TopLeft
70
+ | TopCenter
71
+ | TopRight
72
+ | BottomLeft
73
+ | BottomCenter
74
+ | BottomRight
75
+
76
+
77
+ type alias Settings =
78
+ { position : Position
79
+ , card : Card
80
+ , duration : Duration
81
+ }
82
+
83
+
84
+ {-| The deck's card colors. Neutral is the house look; Custom swaps the
85
+ background and foreground for the one region.
86
+ -}
87
+ type Card
88
+ = Neutral
89
+ | Custom String String
90
+
91
+
92
+
93
+ -- Model
94
+
95
+
96
+ {-| version replays the header morph on a patch.
97
+ -}
98
+ type alias Item =
99
+ { id : Int
100
+ , version : Int
101
+ , config : Config
102
+ , leaving : Bool
103
+ , remaining : Maybe Int
104
+ , x : Spring.Spring
105
+ , y : Spring.Spring
106
+ }
107
+
108
+
109
+ type alias Drag =
110
+ { id : Int
111
+ , startX : Float
112
+ , startY : Float
113
+ , dx : Float
114
+ , dy : Float
115
+ , axis : Maybe Axis
116
+ }
117
+
118
+
119
+ type Axis
120
+ = AxisX
121
+ | AxisY
122
+
123
+
124
+ type alias Model =
125
+ { items : List Item
126
+ , nextId : Int
127
+ , drag : Maybe Drag
128
+ , fanned : Bool
129
+ , duration : Duration
130
+ , stackHeight : Float
131
+ , hovering : Bool
132
+ , focusDepth : Int
133
+ , documentHidden : Bool
134
+ }
135
+
136
+
137
+ {-| The region's duration comes from the settings; `setDuration`
138
+ changes it live.
139
+ -}
140
+ init : Settings -> Model
141
+ init settings =
142
+ { items = []
143
+ , nextId = 0
144
+ , drag = Nothing
145
+ , fanned = False
146
+ , duration = settings.duration
147
+ , stackHeight = 0
148
+ , hovering = False
149
+ , focusDepth = 0
150
+ , documentHidden = False
151
+ }
152
+
153
+
154
+
155
+ -- Msg
156
+
157
+
158
+ type Msg
159
+ = Push Config
160
+ | PushFor Duration Config
161
+ | PatchOldest Config
162
+ | PatchIf (Config -> Bool) Config (Maybe Int)
163
+ | Action Int
164
+ | Dismiss Int
165
+ | DismissIf (Config -> Bool)
166
+ | Clear
167
+ | Expire Int
168
+ | Hover Bool
169
+ | FocusChanged Bool
170
+ | VisibilityChanged Browser.Events.Visibility
171
+ | Tick
172
+ | Measure
173
+ | StackMeasured Int (Result Browser.Dom.Error Browser.Dom.Element)
174
+ | Frame Float
175
+ | DragStart Int Float Float
176
+ | DragMove Float Float
177
+ | DragEnd
178
+
179
+
180
+
181
+ -- Update
182
+
183
+
184
+ update : Msg -> Model -> ( Model, Cmd Msg )
185
+ update msg model =
186
+ case msg of
187
+ Push config ->
188
+ push Nothing config model
189
+
190
+ PushFor duration config ->
191
+ push (Just duration) config model
192
+
193
+ PatchOldest config ->
194
+ let
195
+ patch item =
196
+ if not item.leaving then
197
+ { item
198
+ | config = config
199
+ , version = item.version + 1
200
+ }
201
+
202
+ else
203
+ item
204
+ in
205
+ case model.items of
206
+ oldest :: rest ->
207
+ let
208
+ next =
209
+ { model | items = patch oldest :: rest }
210
+ in
211
+ ( next, measureStack next )
212
+
213
+ [] ->
214
+ ( model, Cmd.none )
215
+
216
+ PatchIf matches config dismissAfter ->
217
+ case newestMatch matches model.items of
218
+ Just item ->
219
+ let
220
+ patched =
221
+ { item
222
+ | config = config
223
+ , version = item.version + 1
224
+ , remaining =
225
+ case dismissAfter of
226
+ Just ms ->
227
+ Just ms
228
+
229
+ Nothing ->
230
+ item.remaining
231
+ }
232
+
233
+ next =
234
+ { model
235
+ | items =
236
+ List.map
237
+ (\t ->
238
+ if t.id == patched.id then
239
+ patched
240
+
241
+ else
242
+ t
243
+ )
244
+ model.items
245
+ }
246
+ in
247
+ ( next, measureStack next )
248
+
249
+ Nothing ->
250
+ ( model, Cmd.none )
251
+
252
+ DismissIf matches ->
253
+ let
254
+ ids =
255
+ model.items
256
+ |> List.filter (\item -> not item.leaving && matches item.config)
257
+ |> List.map .id
258
+ in
259
+ ( List.foldl (\id m -> dismiss m id) model ids
260
+ , Cmd.batch (List.map (\id -> expireIn 220 (Expire id)) ids)
261
+ )
262
+
263
+ Action _ ->
264
+ -- The deck reports the press; the host owns what it means.
265
+ ( model, Cmd.none )
266
+
267
+ Dismiss id ->
268
+ ( dismiss model id, expireIn 220 (Expire id) )
269
+
270
+ Clear ->
271
+ ( { model
272
+ | items =
273
+ List.map
274
+ (\t -> { t | leaving = True, remaining = Nothing })
275
+ model.items
276
+ }
277
+ , Cmd.batch (List.map (.id >> (\id -> expireIn 220 (Expire id))) model.items)
278
+ )
279
+
280
+ Expire id ->
281
+ let
282
+ items =
283
+ List.filter (\t -> t.id /= id) model.items
284
+
285
+ next =
286
+ { model
287
+ | items = items
288
+ , stackHeight =
289
+ if List.isEmpty items then
290
+ 0
291
+
292
+ else
293
+ model.stackHeight
294
+ , hovering =
295
+ if List.isEmpty items then
296
+ False
297
+
298
+ else
299
+ model.hovering
300
+ , focusDepth =
301
+ if List.isEmpty items then
302
+ 0
303
+
304
+ else
305
+ model.focusDepth
306
+ }
307
+ in
308
+ ( next
309
+ , Cmd.batch
310
+ [ measureStack next
311
+ , -- The new front card is still scaling up from its
312
+ -- stack position; measure again once it settles.
313
+ Process.sleep 260 |> Task.perform (\_ -> Measure)
314
+ ]
315
+ )
316
+
317
+ Hover hovering ->
318
+ ( { model | hovering = hovering }, Cmd.none )
319
+
320
+ FocusChanged within ->
321
+ ( { model
322
+ | focusDepth =
323
+ max 0
324
+ (model.focusDepth
325
+ + (if within then
326
+ 1
327
+
328
+ else
329
+ -1
330
+ )
331
+ )
332
+ }
333
+ , Cmd.none
334
+ )
335
+
336
+ VisibilityChanged visibility ->
337
+ ( { model | documentHidden = visibility == Browser.Events.Hidden }, Cmd.none )
338
+
339
+ Tick ->
340
+ if timersPaused model then
341
+ ( model, Cmd.none )
342
+
343
+ else
344
+ let
345
+ ( items, expired ) =
346
+ tickItems tickMs model.items
347
+ in
348
+ ( { model | items = items }
349
+ , Cmd.batch (List.map (\id -> expireIn 220 (Expire id)) expired)
350
+ )
351
+
352
+ Measure ->
353
+ ( model, measureStack model )
354
+
355
+ StackMeasured id result ->
356
+ case ( frontItem model, result ) of
357
+ ( Just front, Ok box ) ->
358
+ if front.id == id then
359
+ ( { model | stackHeight = box.element.height }, Cmd.none )
360
+
361
+ else
362
+ ( model, Cmd.none )
363
+
364
+ _ ->
365
+ ( model, Cmd.none )
366
+
367
+ Frame dt ->
368
+ let
369
+ draggedId =
370
+ Maybe.map .id model.drag
371
+
372
+ step t =
373
+ if Just t.id == draggedId then
374
+ t
375
+
376
+ else
377
+ { t
378
+ | x = Motion.step dragSpring dt t.x
379
+ , y = Motion.step dragSpring dt t.y
380
+ }
381
+ in
382
+ ( { model | items = List.map step model.items }, Cmd.none )
383
+
384
+ DragStart id clientX clientY ->
385
+ ( { model
386
+ | drag =
387
+ Just
388
+ { id = id
389
+ , startX = clientX
390
+ , startY = clientY
391
+ , dx = 0
392
+ , dy = 0
393
+ , axis = Nothing
394
+ }
395
+ , items =
396
+ List.map
397
+ (\t ->
398
+ if t.id == id then
399
+ { t
400
+ | leaving = False
401
+ , x = Spring.create { value = Spring.value t.x, target = 0 }
402
+ , y = Spring.create { value = Spring.value t.y, target = 0 }
403
+ }
404
+
405
+ else
406
+ t
407
+ )
408
+ model.items
409
+ }
410
+ , Cmd.none
411
+ )
412
+
413
+ DragMove clientX clientY ->
414
+ case model.drag of
415
+ Nothing ->
416
+ ( model, Cmd.none )
417
+
418
+ Just drag ->
419
+ let
420
+ rawDx =
421
+ clientX - drag.startX
422
+
423
+ rawDy =
424
+ clientY - drag.startY
425
+
426
+ -- Lane lock: the dominant axis of the first
427
+ -- movement past the dead zone owns the drag.
428
+ axis =
429
+ case drag.axis of
430
+ Just locked ->
431
+ Just locked
432
+
433
+ Nothing ->
434
+ if max (abs rawDx) (abs rawDy) < 8 then
435
+ Nothing
436
+
437
+ else if abs rawDx >= abs rawDy then
438
+ Just AxisX
439
+
440
+ else
441
+ Just AxisY
442
+
443
+ ( dx, dy ) =
444
+ case axis of
445
+ Just AxisX ->
446
+ ( rawDx, 0 )
447
+
448
+ Just AxisY ->
449
+ ( 0, rawDy )
450
+
451
+ Nothing ->
452
+ ( 0, 0 )
453
+ in
454
+ ( { model
455
+ | drag = Just { drag | dx = dx, dy = dy, axis = axis }
456
+ , items =
457
+ List.map
458
+ (\t ->
459
+ if t.id == drag.id then
460
+ { t
461
+ | x = Spring.create { value = dx, target = 0 }
462
+ , y = Spring.create { value = dy, target = 0 }
463
+ }
464
+
465
+ else
466
+ t
467
+ )
468
+ model.items
469
+ }
470
+ , Cmd.none
471
+ )
472
+
473
+ DragEnd ->
474
+ case model.drag of
475
+ Nothing ->
476
+ ( model, Cmd.none )
477
+
478
+ Just drag ->
479
+ if abs drag.dx > 80 || abs drag.dy > 80 then
480
+ -- Fling out along the locked lane.
481
+ let
482
+ ( exitX, exitY ) =
483
+ if drag.dy == 0 then
484
+ ( sign drag.dx * 260, 0 )
485
+
486
+ else
487
+ ( 0, sign drag.dy * 260 )
488
+ in
489
+ ( { model | drag = Nothing }
490
+ |> (\m -> dismissWith m drag.id ( exitX, exitY ))
491
+ , expireIn 450 (Expire drag.id)
492
+ )
493
+
494
+ else if drag.axis == Nothing then
495
+ -- A tap, not a drag: fan the deck out, which is
496
+ -- how touch shows the hidden cards (no hover).
497
+ ( { model | drag = Nothing, fanned = not model.fanned }
498
+ , Cmd.none
499
+ )
500
+
501
+ else
502
+ ( { model | drag = Nothing }, Cmd.none )
503
+
504
+
505
+
506
+ -- Queue internals
507
+
508
+
509
+ {-| A card with an action stays reachable longer without becoming
510
+ permanent: it lingers for one and a half times the region duration.
511
+ `PushFor` pins a duration instead, including `Sticky` when a card
512
+ genuinely must stay.
513
+ -}
514
+ push : Maybe Duration -> Config -> Model -> ( Model, Cmd Msg )
515
+ push pinned config model =
516
+ let
517
+ id =
518
+ model.nextId
519
+
520
+ duration =
521
+ case pinned of
522
+ Just override ->
523
+ override
524
+
525
+ Nothing ->
526
+ if config.action == Nothing then
527
+ model.duration
528
+
529
+ else
530
+ linger model.duration
531
+
532
+ item =
533
+ { id = id
534
+ , version = 0
535
+ , leaving = False
536
+ , remaining = remainingFor duration
537
+ , x = restSpring
538
+ , y = restSpring
539
+ , config = config
540
+ }
541
+
542
+ next =
543
+ { model
544
+ | items = pushItem model.items item
545
+ , nextId = id + 1
546
+ }
547
+ in
548
+ ( next, measureStack next )
549
+
550
+
551
+ remainingFor : Duration -> Maybe Int
552
+ remainingFor duration =
553
+ case duration of
554
+ Ms ms ->
555
+ Just ms
556
+
557
+ Sticky ->
558
+ Nothing
559
+
560
+
561
+ linger : Duration -> Duration
562
+ linger duration =
563
+ case duration of
564
+ Ms ms ->
565
+ Ms (ms * 3 // 2)
566
+
567
+ Sticky ->
568
+ Sticky
569
+
570
+
571
+ {-| Capped at eight so a click burst stays finite.
572
+ -}
573
+ pushItem : List Item -> Item -> List Item
574
+ pushItem items item =
575
+ List.take 7 items ++ [ item ]
576
+
577
+
578
+ tickMs : Int
579
+ tickMs =
580
+ 100
581
+
582
+
583
+ {-| Hovering, keyboard focus, a hidden tab, and an active swipe all hold
584
+ the countdown so a card never leaves from under the reader.
585
+ -}
586
+ timersPaused : Model -> Bool
587
+ timersPaused model =
588
+ model.hovering
589
+ || (model.focusDepth > 0)
590
+ || model.documentHidden
591
+ || (model.drag /= Nothing)
592
+
593
+
594
+ {-| The card the deck shows in front (the newest, even while it exits),
595
+ the one measured for the collapsed stack's pill line.
596
+ -}
597
+ frontItem : Model -> Maybe Item
598
+ frontItem model =
599
+ model.items
600
+ |> List.reverse
601
+ |> List.head
602
+
603
+
604
+ {-| Measures the front card's pill: the one-line height every card
605
+ shares while the deck is collapsed.
606
+ -}
607
+ measureStack : Model -> Cmd Msg
608
+ measureStack model =
609
+ case frontItem model of
610
+ Just item ->
611
+ if item.leaving then
612
+ -- Its exit animation scales the box; wait for Expire to
613
+ -- measure the card that takes its place.
614
+ Cmd.none
615
+
616
+ else
617
+ Browser.Dom.getElement ("toast-pill-" ++ String.fromInt item.id)
618
+ |> Task.attempt (StackMeasured item.id)
619
+
620
+ Nothing ->
621
+ Cmd.none
622
+
623
+
624
+ tickItems : Int -> List Item -> ( List Item, List Int )
625
+ tickItems delta items =
626
+ let
627
+ step item ( kept, expired ) =
628
+ case item.remaining of
629
+ Just remaining ->
630
+ if remaining <= delta then
631
+ ( { item | remaining = Nothing, leaving = True } :: kept, item.id :: expired )
632
+
633
+ else
634
+ ( { item | remaining = Just (remaining - delta) } :: kept, expired )
635
+
636
+ Nothing ->
637
+ ( item :: kept, expired )
638
+ in
639
+ List.foldl step ( [], [] ) items
640
+ |> Tuple.mapFirst List.reverse
641
+
642
+
643
+ dismiss : Model -> Int -> Model
644
+ dismiss model id =
645
+ { model
646
+ | items =
647
+ List.map
648
+ (\t ->
649
+ if t.id == id then
650
+ { t | leaving = True, remaining = Nothing }
651
+
652
+ else
653
+ t
654
+ )
655
+ model.items
656
+ }
657
+
658
+
659
+ {-| The newest non-leaving match, what a patch targets.
660
+ -}
661
+ newestMatch : (Config -> Bool) -> List Item -> Maybe Item
662
+ newestMatch matches items =
663
+ items
664
+ |> List.filter (\item -> not item.leaving && matches item.config)
665
+ |> List.reverse
666
+ |> List.head
667
+
668
+
669
+ dismissWith : Model -> Int -> ( Float, Float ) -> Model
670
+ dismissWith model id ( dx, dy ) =
671
+ { model
672
+ | items =
673
+ List.map
674
+ (\t ->
675
+ if t.id == id then
676
+ { t
677
+ | leaving = True
678
+ , remaining = Nothing
679
+ , x = Spring.create { value = Spring.value t.x, target = dx }
680
+ , y = Spring.create { value = Spring.value t.y, target = dy }
681
+ }
682
+
683
+ else
684
+ t
685
+ )
686
+ model.items
687
+ }
688
+
689
+
690
+ sign : Float -> Float
691
+ sign x =
692
+ if x < 0 then
693
+ -1
694
+
695
+ else
696
+ 1
697
+
698
+
699
+ expireIn : Int -> Msg -> Cmd Msg
700
+ expireIn ms msg =
701
+ Process.sleep (toFloat ms)
702
+ |> Task.perform (\_ -> msg)
703
+
704
+
705
+ {-| Changes the region's dismiss duration on a live model.
706
+ -}
707
+ setDuration : Duration -> Model -> Model
708
+ setDuration duration model =
709
+ { model | duration = duration }
710
+
711
+
712
+ {-| A little overshoot on release.
713
+ -}
714
+ dragSpring : Spring.Config
715
+ dragSpring =
716
+ Spring.config { tension = 220, friction = 17 }
717
+ |> Spring.setEquilibriumThreshold 0.01
718
+
719
+
720
+ restSpring : Spring.Spring
721
+ restSpring =
722
+ Spring.create { value = 0, target = 0 }
723
+
724
+
725
+
726
+ -- Presets
727
+
728
+
729
+ {-| Demo copy; a real caller writes its own Config.
730
+ -}
731
+ presets : Kind -> Config
732
+ presets kind =
733
+ case kind of
734
+ Default ->
735
+ { kind = Default
736
+ , title = "Saved."
737
+ , description = Just "A plain confirmation."
738
+ , action = Nothing
739
+ }
740
+
741
+ Success ->
742
+ { kind = Success
743
+ , title = "Saved."
744
+ , description = Just "The API key can be pasted anywhere."
745
+ , action = Nothing
746
+ }
747
+
748
+ Info ->
749
+ { kind = Info
750
+ , title = "Heads up."
751
+ , description = Just "Two devices connected this week."
752
+ , action = Nothing
753
+ }
754
+
755
+ Warn ->
756
+ { kind = Warn
757
+ , title = "Careful."
758
+ , description = Just "Auto top-up disabled itself after three failures."
759
+ , action = Nothing
760
+ }
761
+
762
+ Error ->
763
+ { kind = Error
764
+ , title = "Something broke."
765
+ , description = Just "The card on file was declined."
766
+ , action = Nothing
767
+ }
768
+
769
+
770
+ {-| Whether the deck holds a card that's still showing.
771
+ -}
772
+ hasCards : Model -> Bool
773
+ hasCards model =
774
+ List.any (\item -> not item.leaving) model.items
775
+
776
+
777
+
778
+ -- Subscriptions
779
+
780
+
781
+ subscriptions : Model -> Sub Msg
782
+ subscriptions model =
783
+ Sub.batch
784
+ [ if model.drag /= Nothing then
785
+ Sub.batch
786
+ [ Browser.Events.onMouseMove
787
+ (Decode.map2 DragMove
788
+ (Decode.field "clientX" Decode.float)
789
+ (Decode.field "clientY" Decode.float)
790
+ )
791
+ , Browser.Events.onMouseUp (Decode.succeed DragEnd)
792
+ ]
793
+
794
+ else
795
+ Sub.none
796
+ , let
797
+ offset t =
798
+ not (Spring.equilibrium t.x) || not (Spring.equilibrium t.y)
799
+ in
800
+ if List.any offset model.items then
801
+ Browser.Events.onAnimationFrameDelta Frame
802
+
803
+ else
804
+ Sub.none
805
+ , if List.isEmpty model.items then
806
+ Sub.none
807
+
808
+ else
809
+ Browser.Events.onResize (\_ _ -> Measure)
810
+ , Browser.Events.onVisibilityChange VisibilityChanged
811
+ , if List.any (\item -> item.remaining /= Nothing) model.items && not (timersPaused model) then
812
+ Time.every (toFloat tickMs) (\_ -> Tick)
813
+
814
+ else
815
+ Sub.none
816
+ ]
817
+
818
+
819
+
820
+ -- View
821
+
822
+
823
+ viewRegion : Settings -> Model -> Html Msg
824
+ viewRegion settings model =
825
+ let
826
+ cardStyle =
827
+ case settings.card of
828
+ Neutral ->
829
+ ""
830
+
831
+ Custom background foreground ->
832
+ "--card: "
833
+ ++ background
834
+ ++ "; --card-fg: "
835
+ ++ foreground
836
+
837
+ stackStyle =
838
+ if model.stackHeight > 0 then
839
+ (if String.isEmpty cardStyle then
840
+ ""
841
+
842
+ else
843
+ "; "
844
+ )
845
+ ++ "--stack-h: "
846
+ ++ String.fromFloat model.stackHeight
847
+ ++ "px"
848
+
849
+ else
850
+ cardStyle
851
+
852
+ styleAttrs =
853
+ if String.isEmpty stackStyle then
854
+ []
855
+
856
+ else
857
+ [ attribute "style" stackStyle ]
858
+
859
+ halfClass =
860
+ case settings.position of
861
+ TopLeft ->
862
+ "pos-top"
863
+
864
+ TopCenter ->
865
+ "pos-top"
866
+
867
+ TopRight ->
868
+ "pos-top"
869
+
870
+ BottomLeft ->
871
+ "pos-bottom"
872
+
873
+ BottomCenter ->
874
+ "pos-bottom"
875
+
876
+ BottomRight ->
877
+ "pos-bottom"
878
+ in
879
+ Html.Keyed.node "div"
880
+ (styleAttrs
881
+ ++ [ classList
882
+ [ ( "toast-region", True )
883
+ , ( halfClass, True )
884
+ , ( "pos-" ++ dockClass settings.position, True )
885
+ , ( "fanned", model.fanned )
886
+ , ( "hovered", model.hovering )
887
+ ]
888
+ , attribute "role" "region"
889
+ , attribute "aria-label" "Notifications"
890
+ , attribute "aria-live" "polite"
891
+ , attribute "aria-atomic" "false"
892
+ , attribute "aria-relevant" "additions text"
893
+ , attribute "tabindex" "-1"
894
+ , Html.Events.on "pointerenter"
895
+ (Decode.field "pointerType" Decode.string
896
+ |> Decode.map (\pointerType -> Hover (pointerType /= "touch"))
897
+ )
898
+ , Html.Events.on "pointerleave" (Decode.succeed (Hover False))
899
+ , Html.Events.on "focusin" (Decode.succeed (FocusChanged True))
900
+ , Html.Events.on "focusout" (Decode.succeed (FocusChanged False))
901
+ , Html.Events.on "animationend" layoutSettled
902
+ ]
903
+ )
904
+ (let
905
+ total =
906
+ List.length model.items
907
+ in
908
+ List.reverse model.items
909
+ |> List.indexedMap
910
+ (\index item ->
911
+ ( String.fromInt item.id
912
+ , viewItem model (index == 0) (total - 1 - index) item
913
+ )
914
+ )
915
+ )
916
+
917
+
918
+ viewItem : Model -> Bool -> Int -> Item -> Html Msg
919
+ viewItem model isFront behind item =
920
+ let
921
+ dragging =
922
+ case model.drag of
923
+ Just drag ->
924
+ drag.id == item.id
925
+
926
+ Nothing ->
927
+ False
928
+
929
+ dx =
930
+ Spring.value item.x
931
+
932
+ dy =
933
+ Spring.value item.y
934
+
935
+ moving =
936
+ dragging
937
+ || not (Spring.equilibrium item.x)
938
+ || not (Spring.equilibrium item.y)
939
+ in
940
+ div
941
+ [ classList
942
+ [ ( "toast-item", True )
943
+ , ( "dragging", dragging )
944
+ , ( "moving", moving )
945
+ , ( "leaving", item.leaving )
946
+ , ( "front", behind > 0 )
947
+ ]
948
+ , Html.Events.on "mousedown"
949
+ (Decode.map2 (DragStart item.id)
950
+ (Decode.field "clientX" Decode.float)
951
+ (Decode.field "clientY" Decode.float)
952
+ )
953
+
954
+ -- Touch events retarget to the touchstart element.
955
+ , Html.Events.on "touchstart"
956
+ (touchPosition (DragStart item.id))
957
+ , Html.Events.on "touchmove"
958
+ (touchPosition DragMove)
959
+ , Html.Events.on "touchend" (Decode.succeed DragEnd)
960
+ , Html.Events.on "touchcancel" (Decode.succeed DragEnd)
961
+ , Html.Attributes.style "transform"
962
+ ("translate(" ++ String.fromFloat dx ++ "px, " ++ String.fromFloat dy ++ "px)")
963
+ ]
964
+ [ view item.id item.config item.version (Dismiss item.id) (Action item.id)
965
+ , if isFront then
966
+ span
967
+ (classList
968
+ [ ( "toast-count", True )
969
+ , ( "visible", behind > 0 )
970
+ ]
971
+ :: (if behind > 0 then
972
+ []
973
+
974
+ else
975
+ [ attribute "aria-hidden" "true" ]
976
+ )
977
+ )
978
+ [ text ("+" ++ String.fromInt (max 1 behind)) ]
979
+
980
+ else
981
+ text ""
982
+ ]
983
+
984
+
985
+ {-| Only the mobile title's unwrap changes the pill's own height; the
986
+ description is clipped while the deck is stacked and never feeds the
987
+ measurement.
988
+ -}
989
+ layoutSettled : Decode.Decoder Msg
990
+ layoutSettled =
991
+ Decode.field "animationName" Decode.string
992
+ |> Decode.andThen
993
+ (\name ->
994
+ if name == "toast-unwrap" then
995
+ Decode.succeed Measure
996
+
997
+ else
998
+ Decode.fail "not a layout animation"
999
+ )
1000
+
1001
+
1002
+ view : Int -> Config -> Int -> msg -> msg -> Html msg
1003
+ view toastId config version close action =
1004
+ div
1005
+ [ Html.Attributes.id ("toast-card-" ++ String.fromInt toastId)
1006
+ , classList
1007
+ [ ( "toast", True )
1008
+ , ( "is-success", config.kind == Success )
1009
+ , ( "is-info", config.kind == Info )
1010
+ , ( "is-warn", config.kind == Warn )
1011
+ , ( "is-error", config.kind == Error )
1012
+ , ( "has-description", config.description /= Nothing )
1013
+ ]
1014
+ ]
1015
+ [ div
1016
+ [ Html.Attributes.id ("toast-pill-" ++ String.fromInt toastId)
1017
+ , class "toast-pill"
1018
+ ]
1019
+ [ Html.Keyed.node "div"
1020
+ [ class "toast-pill-inner" ]
1021
+ [ ( kindKey config.kind ++ "-" ++ String.fromInt version
1022
+ , div
1023
+ [ classList
1024
+ [ ( "toast-pill-row", True )
1025
+ , ( "morph", version > 0 )
1026
+ ]
1027
+ ]
1028
+ [ if config.kind == Default then
1029
+ text ""
1030
+
1031
+ else
1032
+ span [ class "toast-icon" ] [ icon config.kind ]
1033
+ , span [ class "toast-title" ] [ text config.title ]
1034
+ , button
1035
+ [ class "toast-close"
1036
+ , attribute "type" "button"
1037
+ , attribute "aria-label" "Dismiss"
1038
+ , Html.Events.onClick close
1039
+ , Html.Events.stopPropagationOn "mousedown"
1040
+ (Decode.succeed ( close, True ))
1041
+ , Html.Events.stopPropagationOn "touchstart"
1042
+ (Decode.succeed ( close, True ))
1043
+ ]
1044
+ [ Icon.view Icon.Close ]
1045
+ ]
1046
+ )
1047
+ ]
1048
+ ]
1049
+ , case ( config.description, config.action ) of
1050
+ ( Nothing, Nothing ) ->
1051
+ text ""
1052
+
1053
+ _ ->
1054
+ div [ class "toast-desc" ]
1055
+ (List.filterMap identity
1056
+ [ Maybe.map (\description -> p [] [ text description ]) config.description
1057
+ , Maybe.map (viewAction action) config.action
1058
+ ]
1059
+ )
1060
+ ]
1061
+
1062
+
1063
+ viewAction : msg -> String -> Html msg
1064
+ viewAction action label =
1065
+ button
1066
+ [ class "toast-action secondary compact"
1067
+ , attribute "type" "button"
1068
+ , Html.Events.onClick action
1069
+ , Html.Events.stopPropagationOn "mousedown"
1070
+ (Decode.succeed ( action, True ))
1071
+ , Html.Events.stopPropagationOn "touchstart"
1072
+ (Decode.succeed ( action, True ))
1073
+ ]
1074
+ [ text label ]
1075
+
1076
+
1077
+ kindKey : Kind -> String
1078
+ kindKey kind =
1079
+ case kind of
1080
+ Default ->
1081
+ "default"
1082
+
1083
+ Success ->
1084
+ "success"
1085
+
1086
+ Info ->
1087
+ "info"
1088
+
1089
+ Warn ->
1090
+ "warn"
1091
+
1092
+ Error ->
1093
+ "error"
1094
+
1095
+
1096
+ {-| The first changed touch's viewport position.
1097
+ -}
1098
+ touchPosition : (Float -> Float -> msg) -> Decode.Decoder msg
1099
+ touchPosition toMsg =
1100
+ Decode.map2 toMsg
1101
+ (Decode.at [ "changedTouches", "0" ] (Decode.field "clientX" Decode.float))
1102
+ (Decode.at [ "changedTouches", "0" ] (Decode.field "clientY" Decode.float))
1103
+
1104
+
1105
+ {-| Class slug for the region's dock.
1106
+ -}
1107
+ dockClass : Position -> String
1108
+ dockClass position =
1109
+ case position of
1110
+ TopLeft ->
1111
+ "top-left"
1112
+
1113
+ TopCenter ->
1114
+ "top-center"
1115
+
1116
+ TopRight ->
1117
+ "top-right"
1118
+
1119
+ BottomLeft ->
1120
+ "bottom-left"
1121
+
1122
+ BottomCenter ->
1123
+ "bottom-center"
1124
+
1125
+ BottomRight ->
1126
+ "bottom-right"
1127
+
1128
+
1129
+ icon : Kind -> Html msg
1130
+ icon kind =
1131
+ case kind of
1132
+ Default ->
1133
+ text ""
1134
+
1135
+ Success ->
1136
+ Icon.view Icon.Check
1137
+
1138
+ Info ->
1139
+ Icon.view Icon.ToastInfo
1140
+
1141
+ Warn ->
1142
+ Icon.view Icon.ToastWarn
1143
+
1144
+ Error ->
1145
+ Icon.view Icon.Close