noredink-ui/styleguide-app/Examples/Loading.elm

138 lines
3.3 KiB
Elm
Raw Normal View History

2020-04-03 04:13:29 +03:00
module Examples.Loading exposing (example, State, Msg)
{-|
@docs example, State, Msg
-}
import Browser.Events
import Category exposing (Category(..))
import Css
import Css.Global exposing (Snippet, adjacentSiblings, children, class, descendants, each, everything, media, selector, withClass)
import Example exposing (Example)
import Html.Styled as Html exposing (Html)
import Html.Styled.Events as Events
import Json.Decode
import Nri.Ui.Button.V10 as Button
2020-04-03 04:21:04 +03:00
import Nri.Ui.Colors.V1 as Colors
2020-04-03 04:13:29 +03:00
import Nri.Ui.Heading.V2 as Heading
import Nri.Ui.Loading.V1 as Loading
2020-04-03 04:21:04 +03:00
import Nri.Ui.Svg.V1 as Svg
import Nri.Ui.Text.V4 as Text
2020-04-03 04:13:29 +03:00
{-| -}
type alias State =
{ showLoadingFadeIn : Bool
, showLoading : Bool
2020-04-03 04:21:04 +03:00
, showSpinner : Bool
2020-04-03 04:13:29 +03:00
}
init : State
init =
{ showLoadingFadeIn = False
, showLoading = False
2020-04-03 04:21:04 +03:00
, showSpinner = False
2020-04-03 04:13:29 +03:00
}
{-| -}
type Msg
= ShowLoadingFadeIn
| ShowLoading
2020-04-03 04:21:04 +03:00
| ShowSpinner
| Close
2020-04-03 04:13:29 +03:00
update : Msg -> State -> ( State, Cmd Msg )
update msg model =
case msg of
ShowLoadingFadeIn ->
( { model | showLoadingFadeIn = True }
, Cmd.none
)
ShowLoading ->
( { model | showLoading = True }
, Cmd.none
)
2020-04-03 04:21:04 +03:00
ShowSpinner ->
( { model | showSpinner = True }
, Cmd.none
)
Close ->
2020-04-03 04:13:29 +03:00
( { model
| showLoadingFadeIn = False
, showLoading = False
2020-04-03 04:21:04 +03:00
, showSpinner = False
2020-04-03 04:13:29 +03:00
}
, Cmd.none
)
subscriptions : State -> Sub Msg
2020-04-03 04:21:04 +03:00
subscriptions { showLoadingFadeIn, showLoading, showSpinner } =
if showLoadingFadeIn || showLoading || showSpinner then
Browser.Events.onClick (Json.Decode.succeed Close)
2020-04-03 04:13:29 +03:00
else
Sub.none
{-| -}
example : Example State Msg
example =
{ name = "Nri.Ui.Loading.V1"
, categories = [ Pages ]
, state = init
, update = update
, subscriptions = subscriptions
, view =
2020-04-03 04:21:04 +03:00
\{ showLoadingFadeIn, showLoading, showSpinner } ->
2020-04-03 04:13:29 +03:00
[ if showLoading then
Loading.page
else
Html.text ""
2020-04-03 04:21:04 +03:00
, button "Loading.page" ShowLoading showLoadingFadeIn
2020-04-03 04:13:29 +03:00
, if showLoadingFadeIn then
Loading.fadeInPage
else
Html.text ""
2020-04-03 04:21:04 +03:00
, button "Loading.fadeInPage" ShowLoadingFadeIn showLoadingFadeIn
, if showSpinner then
Html.div []
[ Loading.spinner
|> Svg.withColor Colors.blue
|> Svg.toHtml
, Text.caption [ Html.text "By default, the spinner is white. Showing as blue for visibility." ]
2020-04-03 04:13:29 +03:00
]
2020-04-03 04:21:04 +03:00
else
button "Loading.spinner" ShowSpinner showLoadingFadeIn
2020-04-03 04:13:29 +03:00
]
}
2020-04-03 04:21:04 +03:00
button : String -> Msg -> Bool -> Html Msg
button name do disabled =
Button.button name
[ Button.custom
[ Events.stopPropagationOn "click"
(Json.Decode.map (\m -> ( m, True ))
(Json.Decode.succeed do)
)
]
, if disabled then
Button.disabled
else
Button.secondary
, Button.css [ Css.marginRight (Css.px 20) ]
]