2018-09-26 17:02:10 +03:00
|
|
|
module Main exposing (init, main)
|
2018-02-13 00:32:38 +03:00
|
|
|
|
2021-11-05 21:26:43 +03:00
|
|
|
import Accessibility.Styled as Html exposing (Html)
|
2020-03-24 03:25:24 +03:00
|
|
|
import Browser exposing (Document, UrlRequest(..))
|
2020-03-24 22:26:41 +03:00
|
|
|
import Browser.Dom
|
2018-12-05 01:36:15 +03:00
|
|
|
import Browser.Navigation exposing (Key)
|
2020-03-24 03:35:08 +03:00
|
|
|
import Category
|
2020-03-24 03:25:24 +03:00
|
|
|
import Css exposing (..)
|
2021-10-05 01:40:18 +03:00
|
|
|
import Css.Media exposing (withMedia)
|
2020-04-01 02:09:09 +03:00
|
|
|
import Dict exposing (Dict)
|
|
|
|
import Example exposing (Example)
|
|
|
|
import Examples
|
2020-03-24 03:25:24 +03:00
|
|
|
import Html.Attributes
|
|
|
|
import Html.Styled.Attributes as Attributes exposing (..)
|
2020-03-24 22:26:41 +03:00
|
|
|
import Html.Styled.Events as Events
|
2021-11-05 21:26:43 +03:00
|
|
|
import Nri.Ui.ClickableText.V3 as ClickableText
|
2020-03-24 03:25:24 +03:00
|
|
|
import Nri.Ui.Colors.V1 as Colors
|
2020-11-06 21:51:33 +03:00
|
|
|
import Nri.Ui.CssVendorPrefix.V1 as VendorPrefixed
|
2020-03-24 03:25:24 +03:00
|
|
|
import Nri.Ui.Fonts.V1 as Fonts
|
|
|
|
import Nri.Ui.Heading.V2 as Heading
|
2021-10-05 01:40:18 +03:00
|
|
|
import Nri.Ui.MediaQuery.V1 exposing (mobile, notMobile)
|
2021-11-06 01:16:54 +03:00
|
|
|
import Nri.Ui.Page.V3 as Page
|
2018-02-13 00:32:38 +03:00
|
|
|
import Routes as Routes exposing (Route(..))
|
2020-06-19 23:52:02 +03:00
|
|
|
import Sort.Set as Set exposing (Set)
|
2020-03-24 22:26:41 +03:00
|
|
|
import Task
|
2018-12-05 01:36:15 +03:00
|
|
|
import Url exposing (Url)
|
2018-02-13 00:32:38 +03:00
|
|
|
|
|
|
|
|
2018-12-05 01:36:15 +03:00
|
|
|
main : Program () Model Msg
|
2018-02-13 00:32:38 +03:00
|
|
|
main =
|
2018-12-05 01:36:15 +03:00
|
|
|
Browser.application
|
2018-02-13 00:32:38 +03:00
|
|
|
{ init = init
|
|
|
|
, update = update
|
|
|
|
, subscriptions = subscriptions
|
2018-12-05 01:36:15 +03:00
|
|
|
, view = view
|
|
|
|
, onUrlRequest = OnUrlRequest
|
|
|
|
, onUrlChange = OnUrlChange
|
2018-02-13 00:32:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-24 03:25:24 +03:00
|
|
|
type alias Model =
|
|
|
|
{ -- Global UI
|
|
|
|
route : Route
|
2021-11-06 01:04:14 +03:00
|
|
|
, previousRoute : Maybe Route
|
2020-04-01 02:09:09 +03:00
|
|
|
, moduleStates : Dict String (Example Examples.State Examples.Msg)
|
2020-03-24 03:25:24 +03:00
|
|
|
, navigationKey : Key
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-05 01:36:15 +03:00
|
|
|
init : () -> Url -> Key -> ( Model, Cmd Msg )
|
2018-12-05 21:56:04 +03:00
|
|
|
init () url key =
|
2018-12-05 01:36:15 +03:00
|
|
|
( { route = Routes.fromLocation url
|
2021-11-06 01:04:14 +03:00
|
|
|
, previousRoute = Nothing
|
2020-04-01 02:09:09 +03:00
|
|
|
, moduleStates =
|
|
|
|
Dict.fromList
|
|
|
|
(List.map (\example -> ( example.name, example )) Examples.all)
|
2018-12-05 21:56:04 +03:00
|
|
|
, navigationKey = key
|
2018-02-13 00:32:38 +03:00
|
|
|
}
|
|
|
|
, Cmd.none
|
|
|
|
)
|
2020-03-24 03:25:24 +03:00
|
|
|
|
|
|
|
|
|
|
|
type Msg
|
2020-04-01 02:09:09 +03:00
|
|
|
= UpdateModuleStates String Examples.Msg
|
2020-03-24 03:25:24 +03:00
|
|
|
| OnUrlRequest Browser.UrlRequest
|
|
|
|
| OnUrlChange Url
|
2021-11-06 01:04:14 +03:00
|
|
|
| ChangeRoute Route
|
2020-03-24 22:26:41 +03:00
|
|
|
| SkipToMainContent
|
2020-03-24 03:25:24 +03:00
|
|
|
| NoOp
|
|
|
|
|
|
|
|
|
|
|
|
update : Msg -> Model -> ( Model, Cmd Msg )
|
|
|
|
update action model =
|
|
|
|
case action of
|
2020-04-01 02:09:09 +03:00
|
|
|
UpdateModuleStates key exampleMsg ->
|
|
|
|
case Dict.get key model.moduleStates of
|
|
|
|
Just example ->
|
|
|
|
example.update exampleMsg example.state
|
|
|
|
|> Tuple.mapFirst
|
|
|
|
(\newState ->
|
|
|
|
{ model
|
|
|
|
| moduleStates =
|
|
|
|
Dict.insert key
|
|
|
|
{ example | state = newState }
|
|
|
|
model.moduleStates
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|> Tuple.mapSecond (Cmd.map (UpdateModuleStates key))
|
|
|
|
|
|
|
|
Nothing ->
|
|
|
|
( model, Cmd.none )
|
2020-03-24 03:25:24 +03:00
|
|
|
|
|
|
|
OnUrlRequest request ->
|
|
|
|
case request of
|
|
|
|
Internal loc ->
|
|
|
|
( model, Browser.Navigation.pushUrl model.navigationKey (Url.toString loc) )
|
|
|
|
|
|
|
|
External loc ->
|
|
|
|
( model, Browser.Navigation.load loc )
|
|
|
|
|
|
|
|
OnUrlChange route ->
|
2021-11-06 01:04:14 +03:00
|
|
|
( { model
|
|
|
|
| route = Routes.fromLocation route
|
|
|
|
, previousRoute = Just model.route
|
|
|
|
}
|
|
|
|
, Cmd.none
|
|
|
|
)
|
2020-03-24 03:25:24 +03:00
|
|
|
|
2021-11-06 01:04:14 +03:00
|
|
|
ChangeRoute route ->
|
2021-11-06 01:16:54 +03:00
|
|
|
( model
|
|
|
|
, Browser.Navigation.pushUrl model.navigationKey
|
|
|
|
(Routes.toString route)
|
|
|
|
)
|
2021-11-05 21:13:28 +03:00
|
|
|
|
2020-03-24 22:26:41 +03:00
|
|
|
SkipToMainContent ->
|
|
|
|
( model
|
|
|
|
, Task.attempt (\_ -> NoOp) (Browser.Dom.focus "maincontent")
|
|
|
|
)
|
|
|
|
|
2020-03-24 03:25:24 +03:00
|
|
|
NoOp ->
|
|
|
|
( model, Cmd.none )
|
|
|
|
|
|
|
|
|
|
|
|
subscriptions : Model -> Sub Msg
|
|
|
|
subscriptions model =
|
2020-04-01 02:09:09 +03:00
|
|
|
Dict.values model.moduleStates
|
|
|
|
|> List.map (\example -> Sub.map (UpdateModuleStates example.name) (example.subscriptions example.state))
|
|
|
|
|> Sub.batch
|
2020-03-24 03:25:24 +03:00
|
|
|
|
|
|
|
|
|
|
|
view : Model -> Document Msg
|
|
|
|
view model =
|
|
|
|
{ title = "Style Guide"
|
|
|
|
, body = [ view_ model |> Html.toUnstyled ]
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
view_ : Model -> Html Msg
|
|
|
|
view_ model =
|
2020-06-19 23:52:02 +03:00
|
|
|
let
|
|
|
|
examples filterBy =
|
2021-05-28 04:23:07 +03:00
|
|
|
List.filter (\m -> filterBy m) (Dict.values model.moduleStates)
|
2021-11-06 01:16:54 +03:00
|
|
|
in
|
|
|
|
case model.route of
|
|
|
|
Routes.Doodad doodad ->
|
|
|
|
case List.head (examples (\m -> m.name == doodad)) of
|
|
|
|
Just example ->
|
|
|
|
Html.main_ []
|
|
|
|
[ Example.view model.previousRoute example
|
|
|
|
|> Html.map (UpdateModuleStates example.name)
|
|
|
|
]
|
2020-09-02 22:39:35 +03:00
|
|
|
|
2021-11-06 01:16:54 +03:00
|
|
|
Nothing ->
|
|
|
|
Page.notFound
|
|
|
|
{ link = ChangeRoute Routes.All
|
|
|
|
, recoveryText = Page.ReturnTo "Component Library"
|
|
|
|
}
|
|
|
|
|
|
|
|
Routes.Category category ->
|
|
|
|
withSideNav model.route
|
|
|
|
[ mainContentHeader (Category.forDisplay category)
|
|
|
|
, examples
|
|
|
|
(\doodad ->
|
|
|
|
Set.memberOf
|
|
|
|
(Set.fromList Category.sorter doodad.categories)
|
|
|
|
category
|
|
|
|
)
|
|
|
|
|> viewPreviews (Category.forId category)
|
2020-09-02 22:39:35 +03:00
|
|
|
]
|
2021-11-06 01:16:54 +03:00
|
|
|
|
|
|
|
Routes.All ->
|
|
|
|
withSideNav model.route
|
|
|
|
[ mainContentHeader "All"
|
|
|
|
, viewPreviews "all" (examples (\_ -> True))
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
withSideNav : Route -> List (Html Msg) -> Html Msg
|
|
|
|
withSideNav currentRoute content =
|
2020-06-20 00:12:53 +03:00
|
|
|
Html.div
|
|
|
|
[ css
|
|
|
|
[ displayFlex
|
2021-10-05 01:40:18 +03:00
|
|
|
, withMedia [ mobile ] [ flexDirection column, alignItems stretch ]
|
2020-06-20 00:12:53 +03:00
|
|
|
, alignItems flexStart
|
|
|
|
]
|
2020-03-24 03:25:24 +03:00
|
|
|
]
|
2021-11-06 01:16:54 +03:00
|
|
|
[ navigation currentRoute
|
2021-11-05 21:26:43 +03:00
|
|
|
, Html.main_
|
|
|
|
[ css
|
|
|
|
[ flexGrow (int 1)
|
|
|
|
, margin2 (px 40) zero
|
|
|
|
, Css.minHeight (Css.vh 100)
|
|
|
|
]
|
|
|
|
]
|
2021-11-06 01:16:54 +03:00
|
|
|
content
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
mainContentHeader : String -> Html msg
|
|
|
|
mainContentHeader heading =
|
|
|
|
Heading.h1
|
|
|
|
[ Heading.customAttr (id "maincontent")
|
|
|
|
, Heading.customAttr (tabindex -1)
|
|
|
|
, Heading.css [ marginBottom (px 30) ]
|
2020-03-24 03:25:24 +03:00
|
|
|
]
|
2021-11-06 01:16:54 +03:00
|
|
|
[ Html.text heading ]
|
2020-03-24 03:25:24 +03:00
|
|
|
|
|
|
|
|
2021-11-05 21:13:28 +03:00
|
|
|
viewPreviews : String -> List (Example state msg) -> Html Msg
|
2021-11-05 21:05:08 +03:00
|
|
|
viewPreviews containerId examples =
|
|
|
|
examples
|
2021-11-06 01:04:14 +03:00
|
|
|
|> List.map (\example -> Example.preview ChangeRoute example)
|
2021-11-05 21:05:08 +03:00
|
|
|
|> Html.div
|
|
|
|
[ id containerId
|
|
|
|
, css
|
|
|
|
[ Css.displayFlex
|
|
|
|
, Css.flexWrap Css.wrap
|
|
|
|
, Css.property "gap" "10px"
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2021-05-28 04:23:07 +03:00
|
|
|
navigation : Route -> Html Msg
|
|
|
|
navigation route =
|
2020-03-24 03:25:24 +03:00
|
|
|
let
|
|
|
|
isActive category =
|
|
|
|
case route of
|
|
|
|
Routes.Category routeCategory ->
|
|
|
|
category == routeCategory
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
False
|
|
|
|
|
2020-03-24 22:26:41 +03:00
|
|
|
link active hash displayName =
|
2021-11-05 21:26:43 +03:00
|
|
|
ClickableText.link displayName
|
|
|
|
[ ClickableText.small
|
|
|
|
, ClickableText.css
|
|
|
|
[ Css.color Colors.navy
|
|
|
|
, Css.display Css.block
|
|
|
|
, Css.padding (Css.px 8)
|
|
|
|
, Css.borderRadius (Css.px 8)
|
2020-06-20 00:12:53 +03:00
|
|
|
, if active then
|
2021-11-05 21:26:43 +03:00
|
|
|
Css.backgroundColor Colors.glacier
|
2020-03-24 03:25:24 +03:00
|
|
|
|
2020-06-20 00:12:53 +03:00
|
|
|
else
|
2021-11-05 21:26:43 +03:00
|
|
|
Css.batch []
|
2020-06-20 00:12:53 +03:00
|
|
|
]
|
2021-11-05 21:26:43 +03:00
|
|
|
, ClickableText.href hash
|
2020-03-24 03:25:24 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
navLink category =
|
2020-03-24 22:26:41 +03:00
|
|
|
link (isActive category)
|
2021-11-06 01:04:14 +03:00
|
|
|
(Routes.toString (Routes.Category category))
|
2020-03-24 03:33:42 +03:00
|
|
|
(Category.forDisplay category)
|
2020-03-24 03:25:24 +03:00
|
|
|
|
|
|
|
toNavLi element =
|
|
|
|
Html.li
|
|
|
|
[ css
|
2021-11-05 21:26:43 +03:00
|
|
|
[ margin zero
|
2020-03-24 03:25:24 +03:00
|
|
|
, listStyle none
|
|
|
|
, textDecoration none
|
|
|
|
]
|
|
|
|
]
|
|
|
|
[ element ]
|
|
|
|
in
|
2020-06-20 00:12:53 +03:00
|
|
|
Html.nav
|
|
|
|
[ css
|
2021-10-05 01:40:18 +03:00
|
|
|
[ backgroundColor Colors.gray96
|
|
|
|
, withMedia [ notMobile ]
|
|
|
|
[ VendorPrefixed.value "position" "sticky"
|
|
|
|
, top (px 55)
|
|
|
|
, flexShrink zero
|
|
|
|
, borderRadius (px 8)
|
|
|
|
, marginRight (px 40)
|
|
|
|
, padding (px 20)
|
|
|
|
, flexBasis (px 200)
|
|
|
|
]
|
2020-06-20 00:12:53 +03:00
|
|
|
]
|
|
|
|
, attribute "aria-label" "Main Navigation"
|
2020-03-24 03:25:24 +03:00
|
|
|
]
|
2020-06-20 00:12:53 +03:00
|
|
|
[ Html.button
|
|
|
|
[ css
|
|
|
|
[ backgroundColor transparent
|
|
|
|
, borderStyle none
|
|
|
|
, textDecoration none
|
|
|
|
, color Colors.azure
|
|
|
|
, Fonts.baseFont
|
|
|
|
, Css.marginBottom (px 20)
|
2020-09-02 22:53:27 +03:00
|
|
|
, Css.pseudoClass "not(:focus)"
|
|
|
|
[ Css.property "clip" "rect(1px, 1px, 1px, 1px)"
|
|
|
|
, Css.position Css.absolute
|
|
|
|
, Css.height (Css.px 1)
|
|
|
|
, Css.width (Css.px 1)
|
|
|
|
, Css.overflow Css.hidden
|
|
|
|
, Css.margin (Css.px -1)
|
|
|
|
, Css.padding Css.zero
|
|
|
|
, Css.border Css.zero
|
|
|
|
]
|
2020-06-20 00:12:53 +03:00
|
|
|
]
|
|
|
|
, Events.onClick SkipToMainContent
|
|
|
|
, id "skip"
|
2020-03-24 22:26:41 +03:00
|
|
|
]
|
|
|
|
[ Html.text "Skip to main content" ]
|
2020-03-24 23:05:30 +03:00
|
|
|
, (link (route == Routes.All) "#/" "All"
|
2020-03-24 03:35:08 +03:00
|
|
|
:: List.map navLink Category.all
|
2020-03-24 03:25:24 +03:00
|
|
|
)
|
|
|
|
|> List.map toNavLi
|
2020-06-20 00:12:53 +03:00
|
|
|
|> Html.ul
|
2021-11-05 21:26:43 +03:00
|
|
|
[ css [ margin zero, padding zero ]
|
2020-06-20 00:12:53 +03:00
|
|
|
, id "categories"
|
2020-03-24 03:25:24 +03:00
|
|
|
]
|
|
|
|
]
|