Use Attribute pattern wrapping a function

This commit is contained in:
Tessa Kelly 2021-10-27 10:47:33 -07:00
parent f478bcc508
commit 0531a7b0df

View File

@ -55,41 +55,44 @@ import Nri.Ui.Fonts.V1 as Fonts
{-| -} {-| -}
type Attribute type Attribute
= NoBreak = Attribute (Settings -> Settings)
| Css (List Style)
type alias Settings =
{ noBreak : Bool
, styles : List Css.Style
}
defaultSettings : Settings
defaultSettings =
{ noBreak = False
, styles = []
}
{-| Text with this attribute will never wrap. {-| Text with this attribute will never wrap.
-} -}
noBreak : Attribute noBreak : Attribute
noBreak = noBreak =
NoBreak Attribute (\config -> { config | noBreak = True })
{-| Add some custom CSS to the text. If you find yourself using this a lot, {-| Add some custom CSS to the text. If you find yourself using this a lot,
please add a stricter attribute to noredink-ui! please add a stricter attribute to noredink-ui!
-} -}
css : List Style -> Attribute css : List Style -> Attribute
css = css styles =
Css Attribute (\config -> { config | styles = config.styles ++ styles })
styleForAttributes : List Attribute -> Style styleForAttributes : List Attribute -> Style
styleForAttributes attrs = styleForAttributes attrs =
let let
config : Settings
config = config =
List.foldl List.foldl (\(Attribute f) acc -> f acc)
(\attr soFar -> defaultSettings
case attr of
NoBreak ->
{ soFar | noBreak = True }
Css styles ->
{ soFar | styles = soFar.styles ++ styles }
)
{ noBreak = False
, styles = []
}
attrs attrs
in in
batch batch