noredink-ui/styleguide-app/Example.elm

159 lines
4.8 KiB
Elm
Raw Normal View History

module Example exposing (Example, view, wrapMsg, wrapState)
2020-03-31 23:20:03 +03:00
import Category exposing (Category)
2020-03-31 23:33:05 +03:00
import Css exposing (..)
2020-10-02 07:08:41 +03:00
import Css.Global exposing (a, descendants)
2020-03-31 23:20:03 +03:00
import Html.Styled as Html exposing (Html)
2020-03-31 23:33:05 +03:00
import Html.Styled.Attributes as Attributes
import Html.Styled.Lazy as Lazy
import KeyboardSupport exposing (KeyboardSupport)
2020-03-31 23:33:05 +03:00
import Nri.Ui.Colors.V1 exposing (..)
2020-10-02 07:08:41 +03:00
import Nri.Ui.Fonts.V1 as Fonts
2020-06-20 00:23:17 +03:00
import Nri.Ui.Html.Attributes.V2 as AttributeExtras exposing (targetBlank)
2020-03-31 23:20:03 +03:00
type alias Example state msg =
{ name : String
2020-09-09 21:43:10 +03:00
, version : Int
2020-03-31 23:20:03 +03:00
, state : state
, update : msg -> state -> ( state, Cmd msg )
, subscriptions : state -> Sub msg
, view : state -> List (Html msg)
, categories : List Category
, keyboardSupport : List KeyboardSupport
2020-03-31 23:20:03 +03:00
}
2020-03-31 23:33:05 +03:00
wrapMsg :
(msg -> msg2)
-> (msg2 -> Maybe msg)
2020-04-01 00:39:34 +03:00
-> Example state msg
-> Example state msg2
wrapMsg wrapMsg_ unwrapMsg example =
2020-04-01 00:39:34 +03:00
{ name = example.name
2020-09-09 21:43:10 +03:00
, version = example.version
, state = example.state
2020-04-01 00:39:34 +03:00
, update =
\msg2 state ->
case unwrapMsg msg2 of
Just msg ->
2020-04-01 00:39:34 +03:00
example.update msg state
|> Tuple.mapSecond (Cmd.map wrapMsg_)
Nothing ->
( state, Cmd.none )
, subscriptions = \state -> Sub.map wrapMsg_ (example.subscriptions state)
, view = \state -> List.map (Html.map wrapMsg_) (example.view state)
, categories = example.categories
, keyboardSupport = example.keyboardSupport
}
wrapState :
(state -> state2)
-> (state2 -> Maybe state)
-> Example state msg
-> Example state2 msg
wrapState wrapState_ unwrapState example =
{ name = example.name
2020-09-09 21:43:10 +03:00
, version = example.version
, state = wrapState_ example.state
, update =
\msg state2 ->
case unwrapState state2 of
Just state ->
example.update msg state
|> Tuple.mapFirst wrapState_
2020-04-01 00:39:34 +03:00
Nothing ->
( state2, Cmd.none )
, subscriptions =
unwrapState
>> Maybe.map example.subscriptions
>> Maybe.withDefault Sub.none
, view =
unwrapState
>> Maybe.map example.view
>> Maybe.withDefault []
2020-04-01 00:39:34 +03:00
, categories = example.categories
, keyboardSupport = example.keyboardSupport
2020-04-01 00:39:34 +03:00
}
view : Example state msg -> Html msg
view =
Lazy.lazy view_
view_ : Example state msg -> Html msg
view_ example =
2020-09-09 21:43:10 +03:00
let
fullName =
"Nri.Ui." ++ example.name ++ ".V" ++ String.fromInt example.version
in
2020-03-31 23:33:05 +03:00
Html.div
[ -- this class makes the axe accessibility checking output easier to parse
String.replace "." "-" example.name
|> (++) "module-example__"
|> Attributes.class
]
[ Html.div
[ Attributes.css
[ displayFlex
, alignItems center
, justifyContent flexStart
2020-04-01 00:39:34 +03:00
, flexWrap Css.wrap
2020-10-02 07:08:41 +03:00
, Fonts.baseFont
, descendants [ Css.Global.a [ textDecoration none ] ]
2020-03-31 23:33:05 +03:00
]
]
[ Html.styled Html.h2
2020-10-02 07:08:41 +03:00
[ color navy
2020-03-31 23:33:05 +03:00
, fontSize (px 20)
, marginTop zero
, marginBottom zero
2020-10-02 07:08:41 +03:00
, Fonts.baseFont
2020-03-31 23:33:05 +03:00
]
[]
[ Html.a
[ Attributes.href ("#/doodad/" ++ example.name)
, Attributes.class "module-example__doodad-link"
, -- this data attribute is used to name the Percy screenshots
String.replace "." "-" example.name
|> Attributes.attribute "data-percy-name"
]
[ Html.text fullName ]
2020-09-09 21:43:10 +03:00
]
, String.replace "." "-" fullName
2020-03-31 23:33:05 +03:00
|> (++) "https://package.elm-lang.org/packages/NoRedInk/noredink-ui/latest/"
2020-06-20 00:23:17 +03:00
|> viewLink "Docs"
2020-09-09 21:43:10 +03:00
, String.replace "." "/" fullName
2020-06-20 00:23:17 +03:00
++ ".elm"
|> (++) "https://github.com/NoRedInk/noredink-ui/blob/master/src/"
|> viewLink "Source"
2020-03-31 23:33:05 +03:00
]
, KeyboardSupport.view example.keyboardSupport
2020-10-02 07:08:41 +03:00
, Html.div
[ Attributes.css
[ padding (px 40)
, boxShadow5 zero (px 2) (px 4) zero (rgba 0 0 0 0.25)
, border3 (px 1) solid gray92
, borderRadius (px 20)
, margin3 (px 10) zero (px 40)
]
]
(example.view example.state)
2020-03-31 23:33:05 +03:00
]
viewLink : String -> String -> Html msg
viewLink text href =
Html.a
2020-06-20 00:23:17 +03:00
([ Attributes.href href
, Attributes.css [ Css.display Css.block, marginLeft (px 20) ]
]
++ targetBlank
)
2020-03-31 23:33:05 +03:00
[ Html.text text
]