{-| Module: IHP.View.Classes Description: Provides the classes view helper function Copyright: (c) digitally induced GmbH, 2020 -} module IHP.View.Classes where import IHP.Prelude -- | Helper for dynamically generating the @class=".."@ attribute. -- -- Given a list like -- -- > [("a", True), ("b", False), ("c", True)] -- -- builds a class name string for all parts where the second value is @True@. -- -- E.g. -- -- >>> classes [("a", True), ("b", False), ("c", True)] -- "a c" -- -- When setting @b@ to @True@: -- -- >>> classes [("a", True), ("b", True), ("c", True)] -- "a b c" -- -- __Example:__ -- -- >>> <div class={classes [("is-active", False)]}> -- <div class=""> -- -- >>> <div class={classes [("is-active", True)]}> -- <div class="is-active"> -- -- >>> forEach projects \project -> [hsx| -- >>> <div class={classes [("project", True), ("active", project.active)]}> -- >>> {project} -- >>> </div> -- >>> |] -- If project is active: <div class="project active">{project}</div> -- Otherwise: <div class="project">{project}</div> classes :: [(Text, Bool)] -> Text classes :: [(Text, Bool)] -> Text classes ![(Text, Bool)] classNameBoolPairs = [(Text, Bool)] classNameBoolPairs [(Text, Bool)] -> ([(Text, Bool)] -> [(Text, Bool)]) -> [(Text, Bool)] forall {t1} {t2}. t1 -> (t1 -> t2) -> t2 |> ((Text, Bool) -> Bool) -> [(Text, Bool)] -> [(Text, Bool)] forall a. (a -> Bool) -> [a] -> [a] filter (Text, Bool) -> Bool forall a b. (a, b) -> b snd [(Text, Bool)] -> ([(Text, Bool)] -> [Text]) -> [Text] forall {t1} {t2}. t1 -> (t1 -> t2) -> t2 |> ((Text, Bool) -> Text) -> [(Text, Bool)] -> [Text] forall a b. (a -> b) -> [a] -> [b] map (Text, Bool) -> Text forall a b. (a, b) -> a fst [Text] -> ([Text] -> Text) -> Text forall {t1} {t2}. t1 -> (t1 -> t2) -> t2 |> [Text] -> Text unwords {-# INLINABLE classes #-} -- | Allows `("my-class", True)` to be written as `"my-class"` -- -- Useful together with 'classes' instance IsString (Text, Bool) where fromString :: String -> (Text, Bool) fromString String string = (String -> Text forall a b. ConvertibleStrings a b => a -> b cs String string, Bool True) {-# INLINABLE fromString #-}