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

144 lines
3.6 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
-}
2020-06-19 23:41:28 +03:00
import AtomicDesignType exposing (AtomicDesignType(..))
2020-04-03 04:13:29 +03:00
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 KeyboardSupport exposing (Direction(..), Key(..))
2020-04-03 04:13:29 +03:00
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:35:55 +03:00
, showSpinners : Bool
2020-04-03 04:13:29 +03:00
}
init : State
init =
{ showLoadingFadeIn = False
, showLoading = False
2020-04-03 04:35:55 +03:00
, showSpinners = False
2020-04-03 04:13:29 +03:00
}
{-| -}
type Msg
= ShowLoadingFadeIn
| ShowLoading
2020-04-03 04:35:55 +03:00
| ShowSpinners
2020-04-03 04:21:04 +03:00
| 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:35:55 +03:00
ShowSpinners ->
( { model | showSpinners = True }
2020-04-03 04:21:04 +03:00
, Cmd.none
)
Close ->
2020-04-03 04:13:29 +03:00
( { model
| showLoadingFadeIn = False
, showLoading = False
2020-04-03 04:35:55 +03:00
, showSpinners = False
2020-04-03 04:13:29 +03:00
}
, Cmd.none
)
subscriptions : State -> Sub Msg
2020-04-03 04:35:55 +03:00
subscriptions { showLoadingFadeIn, showLoading, showSpinners } =
if showLoadingFadeIn || showLoading || showSpinners then
2020-04-03 04:21:04 +03:00
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 ]
2020-06-20 00:16:10 +03:00
, atomicDesignType = Page
, keyboardSupport = []
2020-04-03 04:13:29 +03:00
, state = init
, update = update
, subscriptions = subscriptions
, view =
2020-04-03 04:35:55 +03:00
\{ showLoadingFadeIn, showLoading, showSpinners } ->
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
2020-04-03 04:35:55 +03:00
, if showSpinners then
2020-04-03 04:21:04 +03:00
Html.div []
2020-04-03 04:35:55 +03:00
[ Loading.spinningPencil
2020-04-03 04:21:04 +03:00
|> Svg.withColor Colors.blue
|> Svg.toHtml
2020-04-03 04:35:55 +03:00
, Text.caption [ Html.text "By default, the spinningPencil is white. Showing as blue for visibility." ]
, Loading.spinningDots
|> Svg.toHtml
2020-04-03 04:13:29 +03:00
]
2020-04-03 04:21:04 +03:00
else
2020-04-03 04:35:55 +03:00
button "Loading.spinningPencil, Loading.spinningDots" ShowSpinners 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) ]
]