noredink-ui/styleguide-app/Debug/Control/View.elm

125 lines
4.0 KiB
Elm
Raw Normal View History

2022-04-19 04:57:22 +03:00
module Debug.Control.View exposing (codeFromList, codeFromListWithHardcoded, codeFromListWithIndentLevel, view)
2022-03-04 22:21:34 +03:00
import Css exposing (..)
import Css.Media exposing (withMedia)
2022-03-04 22:21:34 +03:00
import Debug.Control as Control exposing (Control)
import EllieLink
import Example
2022-03-04 22:21:34 +03:00
import Html.Styled exposing (..)
import Html.Styled.Attributes exposing (css)
import Nri.Ui.Heading.V2 as Heading
import Nri.Ui.MediaQuery.V1 exposing (mobile)
2022-03-25 21:39:05 +03:00
import Nri.Ui.Text.V6 as Text
2022-03-04 22:21:34 +03:00
{-| -}
view :
{ ellieLinkConfig : EllieLink.Config
, name : String
, version : Int
, update : Control a -> msg
2022-03-04 22:21:34 +03:00
, settings : Control a
, mainType : String
2022-04-13 03:32:46 +03:00
, extraImports : List String
2022-03-04 22:21:34 +03:00
, toExampleCode : a -> List { sectionName : String, code : String }
}
-> Html msg
view config =
let
value =
Control.currentValue config.settings
ellieLink =
EllieLink.view config.ellieLinkConfig
2022-03-04 22:21:34 +03:00
in
div
[ css
[ displayFlex
, Css.flexWrap Css.wrap
, Css.property "gap" "10px"
, withMedia [ mobile ] [ flexDirection column, alignItems stretch ]
]
]
[ viewSection "Settings" <|
[ fromUnstyled (Control.view config.update config.settings) ]
, viewExampleCode ellieLink config (config.toExampleCode value)
2022-03-04 22:21:34 +03:00
]
viewExampleCode :
(EllieLink.SectionExample -> Html msg)
2022-04-13 03:32:46 +03:00
-> { component | name : String, version : Int, mainType : String, extraImports : List String }
-> List { sectionName : String, code : String }
-> Html msg
viewExampleCode ellieLink component values =
2022-03-25 21:22:20 +03:00
viewSection "Code Sample" <|
2022-03-25 21:39:05 +03:00
Text.smallBodyGray
[ Text.plaintext "😎 Configure the \"Settings\" on this page to update the code sample, then paste it into your editor!"
]
:: List.concatMap
(\example ->
[ details
[]
[ summary []
[ Heading.h3
2022-03-25 21:39:05 +03:00
[ Heading.css [ Css.display Css.inline ]
2022-04-14 23:38:33 +03:00
, Heading.style Heading.Small
2022-03-25 21:39:05 +03:00
]
[ text example.sectionName ]
]
, ellieLink
{ fullModuleName = Example.fullName component
, name = component.name
, sectionName = example.sectionName
, mainType = component.mainType
2022-04-13 03:32:46 +03:00
, extraImports = component.extraImports
, code = example.code
}
2022-03-25 21:39:05 +03:00
, code
[ css
[ display block
, whiteSpace preWrap
, Css.marginTop (px 8)
]
]
2022-03-25 21:39:05 +03:00
[ text example.code ]
]
2022-03-04 22:21:34 +03:00
]
2022-03-25 21:39:05 +03:00
)
values
viewSection : String -> List (Html msg) -> Html msg
viewSection name children =
section [ css [ flex (int 1) ] ]
2022-04-14 23:38:33 +03:00
(Heading.h2 [ Heading.style Heading.Subhead ] [ text name ]
:: children
2022-03-04 22:21:34 +03:00
)
2022-04-19 04:57:22 +03:00
codeFromListWithHardcoded : List String -> List ( String, a ) -> String
codeFromListWithHardcoded hardcodes elements =
List.map (\v -> ( v, () )) hardcodes
++ List.map (Tuple.mapSecond (always ())) elements
|> codeFromList
2022-03-04 22:21:34 +03:00
codeFromList : List ( String, a ) -> String
codeFromList =
codeFromListWithIndentLevel 1
codeFromListWithIndentLevel : Int -> List ( String, a ) -> String
codeFromListWithIndentLevel indent list =
let
indents =
String.repeat indent "\t"
in
"\n"
++ indents
++ "[ "
++ String.join ("\n" ++ indents ++ ", ") (List.map Tuple.first list)
++ "\n"
++ indents
++ "] "