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

144 lines
3.5 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 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
2021-10-27 20:42:23 +03:00
import Nri.Ui.Text.V6 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 =
2020-09-09 21:43:10 +03:00
{ name = "Loading"
, version = 1
2020-04-03 04:13:29 +03:00
, categories = [ Pages ]
, keyboardSupport = []
2020-04-03 04:13:29 +03:00
, state = init
, update = update
, subscriptions = subscriptions
2021-11-05 21:19:08 +03:00
, preview = []
2020-04-03 04:13:29 +03:00
, 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
2021-10-27 21:54:14 +03:00
, Text.caption [ Text.plaintext "By default, the spinningPencil is white. Showing as blue for visibility." ]
2020-04-03 04:35:55 +03:00
, 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) ]
]