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

174 lines
4.8 KiB
Elm
Raw Normal View History

2022-05-20 21:57:47 +03:00
module Examples.BreadCrumbs exposing (example, State, Msg)
{-|
@docs example, State, Msg
-}
2022-05-24 19:52:46 +03:00
import Accessibility.Styled exposing (..)
2022-05-20 21:57:47 +03:00
import Category exposing (Category(..))
2022-05-24 21:09:05 +03:00
import CommonControls
2022-05-24 19:52:46 +03:00
import Css
2022-05-24 20:27:15 +03:00
import Debug.Control as Control exposing (Control)
import Debug.Control.Extra as ControlExtra
import Debug.Control.View as ControlView
2022-05-20 21:57:47 +03:00
import Example exposing (Example)
2022-05-24 21:17:15 +03:00
import Html.Styled.Attributes exposing (css, href)
2022-05-24 20:27:15 +03:00
import Nri.Ui.BreadCrumbs.V1 as BreadCrumbs exposing (BreadCrumb, BreadCrumbs)
2022-05-24 19:52:46 +03:00
import Nri.Ui.Colors.V1 as Colors
import Nri.Ui.Fonts.V1 as Fonts
import Nri.Ui.Svg.V1 as Svg
import Nri.Ui.Text.V6 as Text
2022-05-24 19:52:46 +03:00
import Nri.Ui.UiIcon.V1 as UiIcon
2022-05-20 21:57:47 +03:00
{-| -}
type alias State =
2022-05-24 20:27:15 +03:00
Control Settings
2022-05-20 21:57:47 +03:00
2022-05-24 20:27:15 +03:00
moduleName : String
moduleName =
"BreadCrumbs"
version : Int
version =
1
2022-05-20 21:57:47 +03:00
{-| -}
example : Example State Msg
example =
2022-05-24 20:27:15 +03:00
{ name = moduleName
, version = version
2022-05-20 21:57:47 +03:00
, categories = [ Layout ]
, keyboardSupport = []
2022-05-24 20:27:15 +03:00
, state = init
, update = update
2022-05-20 21:57:47 +03:00
, subscriptions = \_ -> Sub.none
2022-05-24 19:52:46 +03:00
, preview =
[ previewContainer [ previewText "🏠 Home" ]
, previewContainer [ previewText "🏠 Home", previewArrowRight, previewText "🟠 Category " ]
, previewContainer [ previewText "🏠", previewArrowRight, previewText "🟠", previewArrowRight, previewText "🟣 Sub-Category " ]
]
2022-05-20 21:57:47 +03:00
, view =
2022-05-24 20:27:15 +03:00
\ellieLinkConfig state ->
[ ControlView.view
{ ellieLinkConfig = ellieLinkConfig
, name = moduleName
, version = version
, update = UpdateControl
, settings = state
, mainType = "RootHtml.Html msg"
, extraImports = []
, toExampleCode =
\a ->
-- TODO: implement
-- List { sectionName : String, code : String }
[]
}
, BreadCrumbs.view
2022-05-24 21:17:15 +03:00
{ aTagAttributes = \route -> [ href route ]
2022-05-24 20:27:15 +03:00
, isCurrentRoute = \_ -> False
}
(Control.currentValue state).breadCrumbs
]
2022-05-20 21:57:47 +03:00
}
2022-05-24 19:52:46 +03:00
previewContainer : List (Html msg) -> Html msg
previewContainer =
span
[ css
[ Css.displayFlex
, Css.alignItems Css.center
, Fonts.baseFont
, Css.fontSize (Css.px 10)
, Css.fontWeight (Css.int 600)
, Css.color Colors.navy
]
]
previewText : String -> Html msg
previewText name =
span [ css [ Css.margin (Css.px 2) ] ] [ text name ]
previewArrowRight : Html msg
previewArrowRight =
UiIcon.arrowRight
|> Svg.withColor Colors.gray75
|> Svg.withHeight (Css.px 10)
|> Svg.withWidth (Css.px 8)
|> Svg.withCss [ Css.flexShrink Css.zero ]
|> Svg.toHtml
2022-05-24 20:27:15 +03:00
{-| -}
type Msg
= UpdateControl (Control Settings)
update : Msg -> State -> ( State, Cmd Msg )
update msg state =
case msg of
UpdateControl control ->
( control, Cmd.none )
type alias Settings =
2022-05-24 21:17:15 +03:00
{ breadCrumbs : BreadCrumbs String
2022-05-24 20:27:15 +03:00
}
init : Control Settings
init =
2022-05-24 22:14:30 +03:00
Control.map Settings controlBreadCrumbs
2022-05-24 20:27:15 +03:00
2022-05-24 21:17:15 +03:00
controlBreadCrumbs : Control (BreadCrumbs String)
2022-05-24 20:27:15 +03:00
controlBreadCrumbs =
2022-05-24 22:14:30 +03:00
Control.map (\f -> f Nothing) (controlBreadCrumbs_ 1)
2022-05-24 20:27:15 +03:00
2022-05-24 22:14:30 +03:00
controlBreadCrumbs_ : Int -> Control (Maybe (BreadCrumbs String) -> BreadCrumbs String)
controlBreadCrumbs_ index =
2022-05-24 21:09:05 +03:00
Control.record
2022-05-24 22:14:30 +03:00
(\icon iconStyle text after maybeBase ->
let
breadCrumb =
{ icon = icon
, iconStyle = iconStyle
, text = text
, id = "breadcrumb-id-" ++ String.fromInt index
, route = "/breadcrumb=" ++ String.fromInt index
}
newBase =
case maybeBase of
Just base ->
BreadCrumbs.after base breadCrumb
Nothing ->
BreadCrumbs.init breadCrumb
in
Maybe.map (\f -> f (Just newBase)) after |> Maybe.withDefault newBase
2022-05-24 21:09:05 +03:00
)
2022-05-24 20:50:11 +03:00
|> Control.field "icon" (Control.maybe False (Control.map Tuple.second CommonControls.uiIcon))
2022-05-24 21:09:05 +03:00
|> Control.field "iconStyle"
(Control.choice
[ ( "Default", Control.value BreadCrumbs.Default )
, ( "Circled", Control.value BreadCrumbs.Circled )
]
)
2022-05-24 22:14:30 +03:00
|> Control.field "text" (Control.string ("Category " ++ String.fromInt index))
|> Control.field ("category " ++ String.fromInt (index + 1))
(Control.maybe False
(Control.lazy
(\() -> controlBreadCrumbs_ (index + 1))
)
)