@@ -7,59 +7,65 @@ module Halogen.HTML.CSS
77
88import Prelude
99
10- import Data.Array (mapMaybe )
11- import Data.Either (Either (), either )
12- import Data.List (toUnfoldable , fromFoldable )
10+ import CSS.Property (Key , Value )
11+ import CSS.Render (render , renderedSheet , collect )
12+ import CSS.Stylesheet (CSS , Rule (..), runS )
13+
14+ import Data.Array (mapMaybe , concatMap , singleton )
15+ import Data.Either (Either )
16+ import Data.Foldable (foldMap )
1317import Data.Maybe (Maybe (..), fromMaybe )
14- import Data.String ( joinWith )
18+ import Data.Newtype ( class Newtype )
1519import Data.StrMap as SM
20+ import Data.String (joinWith )
1621import Data.Tuple (Tuple (..))
1722
18- import CSS.Property (Key (), Value ())
19- import CSS.Render (render , renderedSheet , collect )
20- import CSS.Stylesheet (CSS (), Rule (..), runS )
21-
22- import Halogen.HTML as H
23- import Halogen.HTML.Core (HTML (), Prop (), class IsProp , prop , propName , attrName )
23+ import Halogen.HTML as HH
24+ import Halogen.HTML.Core (HTML , Prop , class IsProp , prop , propName , attrName )
25+ import Halogen.HTML.Elements as HE
2426import Halogen.HTML.Properties as P
2527
2628-- | A newtype for CSS styles
2729newtype Styles = Styles (SM.StrMap String )
2830
29- -- | Unpack CSS styles
30- runStyles :: Styles -> SM.StrMap String
31- runStyles (Styles m) = m
31+ derive instance newtypeStyles ∷ Newtype Styles _
3232
33- instance stylesIsProp :: IsProp Styles where
34- toPropString _ _ (Styles m) = joinWith " ; " $ (\(Tuple key value) -> key <> " : " <> value) <$> toUnfoldable (SM .toList m)
33+ instance stylesIsProp ∷ IsProp Styles where
34+ toPropString _ _ (Styles m) =
35+ joinWith " ; " $ SM .foldMap (\key value → [key <> " : " <> value]) m
3536
3637-- | Render a set of rules as an inline style.
3738-- |
3839-- | For example:
3940-- |
4041-- | ```purescript
41- -- | H .div [ CSS.style do color red
42+ -- | HH .div [ CSS.style do color red
4243-- | display block ]
4344-- | [ ... ]
4445-- | ```
45- style :: forall i . CSS -> Prop i
46- style = prop (propName " style" ) (Just $ attrName " style" ) <<< Styles <<< rules <<< runS
46+ style ∷ ∀ i . CSS → Prop i
47+ style =
48+ prop (propName " style" ) (Just $ attrName " style" )
49+ <<< Styles
50+ <<< rules
51+ <<< runS
4752 where
48- rules :: Array Rule -> SM.StrMap String
49- rules rs = SM .fromList ( fromFoldable properties)
53+ rules ∷ Array Rule → SM.StrMap String
54+ rules rs = SM .fromFoldable properties
5055 where
51- properties :: Array (Tuple String String )
56+ properties ∷ Array (Tuple String String )
5257 properties = mapMaybe property rs >>= collect >>> rights
5358
54- property :: Rule -> Maybe (Tuple (Key Unit ) Value )
59+ property ∷ Rule → Maybe (Tuple (Key Unit ) Value )
5560 property (Property k v) = Just (Tuple k v)
5661 property _ = Nothing
5762
58- rights :: forall a b . Array (Either a b ) -> Array b
59- rights = mapMaybe (either (const Nothing ) Just )
63+ rights ∷ ∀ a b . Array (Either a b ) → Array b
64+ rights = concatMap $ foldMap singleton
6065
6166-- | Render a set of rules as a `style` element.
62- stylesheet :: forall p i . CSS -> HTML p i
63- stylesheet css = H .style [ P .type_ " text/css" ] [ H .text content ]
67+ stylesheet ∷ ∀ p i . CSS → HTML p i
68+ stylesheet css =
69+ HE .style [ P .type_ " text/css" ] [ HH .text content ]
6470 where
6571 content = fromMaybe " " $ renderedSheet $ render css
0 commit comments