@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,239 @@
1
+ module Ui.Sidebar exposing (Item, Section(..), Target(..), view)
2
+
3
+ {-| The navigation rail: the fixed sidebar the dashboards hang
4
+ everything from. Items carry the house hover sweep (a charple wash
5
+ wiping in from the left) and the active item wears the blush edge.
6
+
7
+ Build it from sections — a bare list for a flat rail, labeled groups
8
+ for the longer navigations. Labeled groups collapse when the host
9
+ wires `onToggleGroup` (see `view`). `footer` pins to the rail's
10
+ bottom (the account control's slot); `narrow` collapses to an
11
+ icon-only rail.
12
+
13
+ -}
14
+
15
+ import Html exposing (Attribute, Html, div, nav, span, text, ul)
16
+ import Html.Attributes
17
+ import Html.Events
18
+ import Ui.Icon
19
+
20
+
21
+ {-| Where an item leads: an href (routed by the host's link
22
+ interception) or a bare click message (modal entries, actions).
23
+ -}
24
+ type Target msg
25
+ = Href String
26
+ | Click msg
27
+
28
+
29
+ {-| One nav item. `icon` rides before the label (or replaces it in the
30
+ narrow rail); `active` wears the blush edge.
31
+ -}
32
+ type alias Item msg =
33
+ { label : String
34
+ , icon : Maybe (Html msg)
35
+ , target : Target msg
36
+ , active : Bool
37
+ }
38
+
39
+
40
+ {-| A group of items. `Section` labels are the small quiet headings
41
+ between groups; use `Unlabeled` for the flat rail. `Group` is a labeled
42
+ group with its own child sections, for categories that contain
43
+ categories: `key` is the collapse identity, `label` what the reader sees.
44
+ -}
45
+ type Section msg
46
+ = Unlabeled (List (Item msg))
47
+ | Labeled String (List (Item msg))
48
+ | Group { key : String, label : String } (List (Item msg)) (List (Section msg))
49
+
50
+
51
+ itemNode : Item msg -> Html msg
52
+ itemNode item =
53
+ let
54
+ targetAttrs =
55
+ case item.target of
56
+ Href href ->
57
+ [ Html.Attributes.href href ]
58
+
59
+ Click msg ->
60
+ [ Html.Attributes.href "#"
61
+ , Html.Events.onClick msg
62
+ ]
63
+
64
+ content =
65
+ case item.icon of
66
+ Just icon ->
67
+ [ span [ Html.Attributes.class "icon" ] [ icon ]
68
+ , span [ Html.Attributes.class "label" ] [ Html.text item.label ]
69
+ ]
70
+
71
+ Nothing ->
72
+ [ span [ Html.Attributes.class "label" ] [ Html.text item.label ] ]
73
+ in
74
+ Html.li
75
+ [ Html.Attributes.classList [ ( "active", item.active ) ] ]
76
+ [ Html.a
77
+ ([ Html.Attributes.attribute "aria-current"
78
+ (if item.active then
79
+ "page"
80
+
81
+ else
82
+ "false"
83
+ )
84
+ ]
85
+ ++ targetAttrs
86
+ )
87
+ (content
88
+ ++ [ -- The sweep overlay: the same content again, in
89
+ -- salt ink on the charple fill the mask wipes in.
90
+ Html.span
91
+ [ Html.Attributes.class "sweep"
92
+ , Html.Attributes.attribute "aria-hidden" "true"
93
+ ]
94
+ content
95
+ ]
96
+ )
97
+ ]
98
+
99
+
100
+ sectionNode : Maybe (String -> msg) -> List String -> Section msg -> List (Html msg)
101
+ sectionNode onToggleGroup collapsed section =
102
+ case section of
103
+ Unlabeled items ->
104
+ [ Html.ul [] (List.map itemNode items) ]
105
+
106
+ Labeled label items ->
107
+ let
108
+ isCollapsed =
109
+ List.member label collapsed
110
+ in
111
+ [ Html.ul []
112
+ (headerNode onToggleGroup label label isCollapsed (List.any .active items)
113
+ ++ (if isCollapsed then
114
+ []
115
+
116
+ else
117
+ List.map itemNode items
118
+ )
119
+ )
120
+ ]
121
+
122
+ Group group items children ->
123
+ let
124
+ isCollapsed =
125
+ List.member group.key collapsed
126
+
127
+ containsActive =
128
+ List.any .active items || List.any sectionHasActive children
129
+ in
130
+ [ Html.ul []
131
+ (headerNode onToggleGroup group.key group.label isCollapsed containsActive
132
+ ++ (if isCollapsed then
133
+ []
134
+
135
+ else
136
+ List.map itemNode items
137
+ ++ List.concatMap (sectionNode onToggleGroup collapsed) children
138
+ )
139
+ )
140
+ ]
141
+
142
+
143
+ {-| The group header: a toggle when the host wires one, a plain label
144
+ otherwise. Collapsed groups holding the active page keep the label lit.
145
+ -}
146
+ headerNode : Maybe (String -> msg) -> String -> String -> Bool -> Bool -> List (Html msg)
147
+ headerNode onToggleGroup key label isCollapsed containsActive =
148
+ case onToggleGroup of
149
+ Just onToggle ->
150
+ [ Html.li [ Html.Attributes.class "section" ]
151
+ [ Html.button
152
+ [ Html.Attributes.class "section-toggle"
153
+ , Html.Attributes.classList
154
+ [ ( "collapsed", isCollapsed )
155
+ , ( "active", isCollapsed && containsActive )
156
+ ]
157
+ , Html.Attributes.attribute
158
+ "aria-expanded"
159
+ (if isCollapsed then
160
+ "false"
161
+
162
+ else
163
+ "true"
164
+ )
165
+ , Html.Events.onClick (onToggle key)
166
+ ]
167
+ [ span
168
+ [ Html.Attributes.class "section-label" ]
169
+ [ Html.text label ]
170
+ , span
171
+ [ Html.Attributes.class "chevron" ]
172
+ [ Ui.Icon.view Ui.Icon.Chevron ]
173
+ ]
174
+ ]
175
+ ]
176
+
177
+ Nothing ->
178
+ [ Html.li [ Html.Attributes.class "section" ]
179
+ [ span
180
+ [ Html.Attributes.class "section-label" ]
181
+ [ Html.text label ]
182
+ ]
183
+ ]
184
+
185
+
186
+ {-| True when the active page sits anywhere inside the section, so a
187
+ collapsed parent still shows its header lit.
188
+ -}
189
+ sectionHasActive : Section msg -> Bool
190
+ sectionHasActive section =
191
+ case section of
192
+ Unlabeled items ->
193
+ List.any .active items
194
+
195
+ Labeled _ items ->
196
+ List.any .active items
197
+
198
+ Group _ items children ->
199
+ List.any .active items || List.any sectionHasActive children
200
+
201
+
202
+ {-| Render the rail. `narrow` collapses it to an icon-only strip;
203
+ `footer` pins to the rail's bottom (the account control's slot).
204
+
205
+ Labeled groups become collapsible when `onToggleGroup` carries a
206
+ message: the header turns into a toggle button with a chevron, the
207
+ labels in `collapsed` hide their items, and a collapsed group whose
208
+ items include the active page keeps a lit header so the current
209
+ location never disappears silently.
210
+
211
+ -}
212
+ view :
213
+ { sections : List (Section msg)
214
+ , narrow : Bool
215
+ , footer : Maybe (Html msg)
216
+ , collapsed : List String
217
+ , onToggleGroup : Maybe (String -> msg)
218
+ , attributes : List (Attribute msg)
219
+ }
220
+ -> Html msg
221
+ view config =
222
+ nav
223
+ ([ Html.Attributes.classList
224
+ [ ( "sidebar", True )
225
+ , ( "narrow", config.narrow )
226
+ ]
227
+ ]
228
+ ++ config.attributes
229
+ )
230
+ (div [ Html.Attributes.class "sidebar-items" ]
231
+ (List.concatMap (sectionNode config.onToggleGroup config.collapsed) config.sections)
232
+ :: (case config.footer of
233
+ Just footer ->
234
+ [ div [ Html.Attributes.class "sidebar-footer" ] [ footer ] ]
235
+
236
+ Nothing ->
237
+ []
238
+ )
239
+ )
@@ -0,0 +1,29 @@
1
+ module Ui.Stat exposing (view)
2
+
3
+ {-| The big-number readout: a figure with a caption riding it, used for
4
+ counts ("6 members"), amounts ("$42.00"), and plan prices.
5
+ -}
6
+
7
+ import Html exposing (Attribute, Html, div, span, text)
8
+ import Html.Attributes
9
+
10
+
11
+ {-| Render a stat: `value` is the large figure, `caption` the small
12
+ label beside or under it.
13
+ -}
14
+ view :
15
+ { value : String
16
+ , caption : Maybe String
17
+ , attributes : List (Attribute msg)
18
+ }
19
+ -> Html msg
20
+ view config =
21
+ div ([ Html.Attributes.class "count" ] ++ config.attributes)
22
+ [ span [ Html.Attributes.class "figure" ] [ text config.value ]
23
+ , case config.caption of
24
+ Just caption ->
25
+ span [ Html.Attributes.class "muted" ] [ text caption ]
26
+
27
+ Nothing ->
28
+ Html.text ""
29
+ ]
@@ -0,0 +1,34 @@
1
+ module Ui.Switch exposing (view)
2
+
3
+ {-| The on/off switch: a checkbox restyled as a sliding thumb in a
4
+ recessed track, charple when on. Carried over from the billing page's
5
+ auto top-up.
6
+ -}
7
+
8
+ import Html exposing (Attribute, Html, input)
9
+ import Html.Attributes exposing (type_)
10
+ import Html.Events
11
+
12
+
13
+ {-| Render a switch. It is a real checkbox underneath, so wrapping
14
+ labels keep native behavior; pass extra attributes (ids, label `for`)
15
+ as needed.
16
+ -}
17
+ view :
18
+ { checked : Bool
19
+ , onToggle : Bool -> msg
20
+ , disabled : Bool
21
+ , attributes : List (Attribute msg)
22
+ }
23
+ -> Html msg
24
+ view config =
25
+ input
26
+ ([ type_ "checkbox"
27
+ , Html.Attributes.class "switch"
28
+ , Html.Attributes.checked config.checked
29
+ , Html.Attributes.disabled config.disabled
30
+ , Html.Events.onCheck config.onToggle
31
+ ]
32
+ ++ config.attributes
33
+ )
34
+ []
@@ -0,0 +1,89 @@
1
+ module Ui.Table exposing (CellKind(..), Column, view)
2
+
3
+ {-| The table shell: a hairline-framed wrap, header band on
4
+ the recessed fill, hover rows, rule-separated body cells. Typed
5
+ columns turn rows into cells; the cell kinds only pick alignment and
6
+ typography (date cells go quiet and tabular, numeric cells
7
+ right-align, action cells hold compact buttons).
8
+ -}
9
+
10
+ import Html exposing (Attribute, Html, div, table, tbody, td, text, th, thead, tr)
11
+ import Html.Attributes
12
+
13
+
14
+ {-| How a column renders. `Plain` is the default left-aligned text
15
+ cell; the other kinds add the shell's specialized treatments.
16
+ -}
17
+ type CellKind
18
+ = Plain
19
+ | Numeric
20
+ | Date
21
+ | Status
22
+
23
+
24
+ cellClass : CellKind -> Maybe String
25
+ cellClass kind =
26
+ case kind of
27
+ Plain ->
28
+ Nothing
29
+
30
+ Numeric ->
31
+ Just "num"
32
+
33
+ Date ->
34
+ Just "date-cell"
35
+
36
+ Status ->
37
+ Just "status-cell"
38
+
39
+
40
+ {-| One column: its header, how it renders a row, and its kind.
41
+ -}
42
+ type alias Column row msg =
43
+ { header : String
44
+ , cell : row -> Html msg
45
+ , kind : CellKind
46
+ }
47
+
48
+
49
+ {-| Render a table inside the framed wrap.
50
+ -}
51
+ view :
52
+ { columns : List (Column row msg)
53
+ , rows : List row
54
+ , foot : Maybe (List (Html msg))
55
+ , attributes : List (Attribute msg)
56
+ }
57
+ -> Html msg
58
+ view config =
59
+ let
60
+ headerCell column =
61
+ th [] [ text column.header ]
62
+
63
+ bodyCell row column =
64
+ td
65
+ (column.kind
66
+ |> cellClass
67
+ |> Maybe.map (\cls -> [ Html.Attributes.class cls ])
68
+ |> Maybe.withDefault []
69
+ )
70
+ [ column.cell row ]
71
+ in
72
+ div ([ Html.Attributes.class "table-wrap" ] ++ config.attributes)
73
+ [ table []
74
+ [ thead []
75
+ [ tr [] (List.map headerCell config.columns) ]
76
+ , tbody []
77
+ (List.map
78
+ (\row -> tr [] (List.map (bodyCell row) config.columns))
79
+ config.rows
80
+ )
81
+ , case config.foot of
82
+ Just cells ->
83
+ Html.tfoot []
84
+ [ tr [] (List.map (\cell -> td [] [ cell ]) cells) ]
85
+
86
+ Nothing ->
87
+ Html.text ""
88
+ ]
89
+ ]
@@ -0,0 +1,280 @@
1
+ module Ui.Tabs exposing
2
+ ( Choice
3
+ , Model
4
+ , Msg(..)
5
+ , Size(..)
6
+ , init
7
+ , subscriptions
8
+ , update
9
+ , view
10
+ )
11
+
12
+ {-| The segmented tab switcher, with the sliding thumb: a raised,
13
+ rounded pill that glides from the outgoing tab to the selected one.
14
+
15
+ The component owns the active value and measures the tab buttons to
16
+ place the thumb, so widths can stay natural (labels of different
17
+ lengths). Until the first measurement lands the thumb is hidden and
18
+ the active button paints its own background — the pre-slide look.
19
+
20
+ type Msg
21
+ = TabsMsg Ui.Tabs.Msg
22
+
23
+ model =
24
+ { tabs = Ui.Tabs.init "demo-tabs" "keys" }
25
+
26
+ Ui.Tabs.view
27
+ { choices =
28
+ [ { label = "Keys", icon = Nothing, value = "keys" }
29
+ , { label = "Setup Guide", icon = Nothing, value = "setup" }
30
+ ]
31
+ , size = Ui.Tabs.Regular
32
+ , attributes = []
33
+ }
34
+ model.tabs
35
+
36
+ Read the selected value from `model.tabs.active`. The component
37
+ measures itself on the frame after mount and again whenever the
38
+ viewport resizes; re-measure on any other container resize by sending
39
+ `Measure`.
40
+
41
+ A switcher that measures as zero-size (`display: none`, as the header
42
+ copies are on a phone) is not measured: it keeps the fallback fill,
43
+ stops the frame retry so it does not spin while hidden, and picks the
44
+ thumb up on the next resize that shows it.
45
+
46
+ -}
47
+
48
+ import Browser.Dom
49
+ import Browser.Events
50
+ import Html exposing (Attribute, Html, button, div)
51
+ import Html.Attributes
52
+ import Html.Events
53
+ import Task
54
+
55
+
56
+
57
+ -- MODEL
58
+
59
+
60
+ {-| The switcher's state: the selected value, its index, the
61
+ measured thumb position (x relative to the track, w the active
62
+ button's width), and whether the last measurement found the switcher
63
+ hidden (zero-size), which parks the frame retry until a resize.
64
+ -}
65
+ type alias Model value =
66
+ { id : String
67
+ , active : value
68
+ , activeIndex : Int
69
+ , thumb : Maybe { x : Float, w : Float }
70
+ , hidden : Bool
71
+ }
72
+
73
+
74
+ {-| Create the model. The id must be unique per page instance — the
75
+ measurement tasks target elements by it. `activeIndex` is the
76
+ position of `active` in the choices list (the component can't see the
77
+ choices, so the host tells it where the selection starts).
78
+ -}
79
+ init : String -> Int -> value -> Model value
80
+ init id activeIndex active =
81
+ { id = id
82
+ , active = active
83
+ , activeIndex = activeIndex
84
+ , thumb = Nothing
85
+ , hidden = False
86
+ }
87
+
88
+
89
+
90
+ -- MSG
91
+
92
+
93
+ {-| Internal messages; the host maps them wholesale. `Measure`
94
+ re-runs the thumb measurement (host's call on container resize).
95
+ -}
96
+ type Msg value
97
+ = Select Int value
98
+ | Measured Int (Result Browser.Dom.Error ( Float, Float ))
99
+ | Measure
100
+
101
+
102
+
103
+ -- UPDATE
104
+
105
+
106
+ {-| While the thumb is unmeasured AND the view is mounted, listen to
107
+ animation frames and retry the measurement, so the thumb is in place
108
+ before the first interaction and the very first click already slides.
109
+ Pass `mounted = False` for switchers that are not on the rendered
110
+ page — otherwise the frame retry would spin on elements that don't
111
+ exist. Once measured, or once found hidden, the frame retry goes
112
+ quiet; a mounted switcher still re-measures on every viewport resize,
113
+ which is what brings a hidden one back (and keeps a visible one's
114
+ thumb on its button when the layout reflows).
115
+ -}
116
+ subscriptions : Bool -> Model value -> Sub (Msg value)
117
+ subscriptions mounted model =
118
+ if not mounted then
119
+ Sub.none
120
+
121
+ else if model.thumb == Nothing && not model.hidden then
122
+ Sub.batch
123
+ [ Browser.Events.onAnimationFrame (always Measure)
124
+ , Browser.Events.onResize (\_ _ -> Measure)
125
+ ]
126
+
127
+ else
128
+ Browser.Events.onResize (\_ _ -> Measure)
129
+
130
+
131
+ {-| Advance the selection and re-measure the thumb.
132
+ -}
133
+ update : Msg value -> Model value -> ( Model value, Cmd (Msg value) )
134
+ update msg model =
135
+ case msg of
136
+ Select index value ->
137
+ ( { model | active = value, activeIndex = index }
138
+ , Task.attempt (Measured index) (measure model.id index)
139
+ )
140
+
141
+ Measured index (Ok ( x, w )) ->
142
+ if index /= model.activeIndex then
143
+ ( model, Cmd.none )
144
+
145
+ else if w <= 0 then
146
+ -- A hidden switcher measures 0×0. Keeping that as the
147
+ -- thumb would mark it measured with nothing to paint,
148
+ -- leaving the active label in salt on the bare track.
149
+ ( { model | thumb = Nothing, hidden = True }, Cmd.none )
150
+
151
+ else
152
+ ( { model | thumb = Just { x = x, w = w }, hidden = False }, Cmd.none )
153
+
154
+ Measured _ (Err _) ->
155
+ ( model, Cmd.none )
156
+
157
+ Measure ->
158
+ ( model
159
+ , Task.attempt (Measured model.activeIndex) (measure model.id model.activeIndex)
160
+ )
161
+
162
+
163
+ {-| Measure the track and the active button; resolve to the button's
164
+ x relative to the track and its width.
165
+ -}
166
+ measure : String -> Int -> Task.Task Browser.Dom.Error ( Float, Float )
167
+ measure id index =
168
+ let
169
+ buttonId =
170
+ id ++ "-" ++ String.fromInt index
171
+ in
172
+ Task.map2
173
+ (\track button ->
174
+ ( button.element.x - track.element.x
175
+ , button.element.width
176
+ )
177
+ )
178
+ (Browser.Dom.getElement id)
179
+ (Browser.Dom.getElement buttonId)
180
+
181
+
182
+
183
+ -- VIEW
184
+
185
+
186
+ {-| One selectable segment: a text label, an optional leading icon, and
187
+ a value the parent owns. Icon-only tabs pass an empty label. Icons are
188
+ `Html Never` — glyphs carry no messages of their own.
189
+ -}
190
+ type alias Choice value =
191
+ { label : String
192
+ , icon : Maybe (Html Never)
193
+ , value : value
194
+ }
195
+
196
+
197
+ {-| The control height. `Regular` matches toolbar controls; `Compact`
198
+ is the dense switcher used next to compact controls and tables;
199
+ `Underline` drops the pill for a bare text row whose selection is a
200
+ two-pixel underline — the style card groups and page-level section
201
+ switchers use.
202
+ -}
203
+ type Size
204
+ = Regular
205
+ | Compact
206
+ | Underline
207
+
208
+
209
+ {-| Render the switcher. The active value comes from the model; the
210
+ thumb slides between the measured positions.
211
+ -}
212
+ view :
213
+ { choices : List (Choice value)
214
+ , size : Size
215
+ , attributes : List (Attribute (Msg value))
216
+ }
217
+ -> Model value
218
+ -> Html (Msg value)
219
+ view config model =
220
+ let
221
+ sizeClass =
222
+ case config.size of
223
+ Regular ->
224
+ []
225
+
226
+ Compact ->
227
+ [ ( "compact", True ) ]
228
+
229
+ Underline ->
230
+ [ ( "underline", True ) ]
231
+
232
+ thumb =
233
+ case model.thumb of
234
+ Just { x, w } ->
235
+ [ div
236
+ [ Html.Attributes.class "thumb"
237
+ , Html.Attributes.style "transform" ("translateX(" ++ String.fromFloat x ++ "px)")
238
+ , Html.Attributes.style "width" (String.fromFloat w ++ "px")
239
+ ]
240
+ []
241
+ ]
242
+
243
+ Nothing ->
244
+ []
245
+
246
+ button_ index choice =
247
+ button
248
+ [ Html.Attributes.classList [ ( "active", choice.value == model.active ) ]
249
+ , Html.Attributes.id (model.id ++ "-" ++ String.fromInt index)
250
+ , Html.Attributes.type_ "button"
251
+ , Html.Attributes.attribute "role" "tab"
252
+ , Html.Attributes.attribute "aria-selected"
253
+ (if choice.value == model.active then
254
+ "true"
255
+
256
+ else
257
+ "false"
258
+ )
259
+ , Html.Events.onClick (Select index choice.value)
260
+ ]
261
+ (case choice.icon of
262
+ Just icon ->
263
+ [ Html.map never icon ]
264
+
265
+ Nothing ->
266
+ [ Html.text choice.label ]
267
+ )
268
+ in
269
+ div
270
+ ([ Html.Attributes.classList
271
+ (( "tabs", True )
272
+ :: ( "measured", model.thumb /= Nothing )
273
+ :: sizeClass
274
+ )
275
+ , Html.Attributes.id model.id
276
+ , Html.Attributes.attribute "role" "tablist"
277
+ ]
278
+ ++ config.attributes
279
+ )
280
+ (thumb ++ List.indexedMap button_ config.choices)