@@ -7,60 +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 ( fromList , toList )
10+ import Data.Array (mapMaybe , concatMap , singleton )
11+ import Data.Either (Either )
12+ import Data.Foldable ( foldMap )
1313import Data.Maybe (Maybe (..), fromMaybe )
14+ import Data.Newtype (class Newtype )
1415import Data.String (joinWith )
1516import Data.Tuple (Tuple (..))
16- import qualified Data.StrMap as SM
17+ import Data.StrMap as SM
1718
18- import Css .Property (Key () , Value () )
19- import Css .Render (render , renderedSheet , collect )
20- import Css .Stylesheet (Css () , Rule (..), runS )
19+ import CSS .Property (Key , Value )
20+ import CSS .Render (render , renderedSheet , collect )
21+ import CSS .Stylesheet (CSS , Rule (..), runS )
2122
22- import Halogen.HTML.Core (HTML () , Prop (), IsProp , prop , propName , attrName )
23- import qualified Halogen.HTML as H
24- import qualified Halogen.HTML.Elements as H
25- import qualified Halogen.HTML.Properties as P
23+ import Halogen.HTML.Core (HTML , Prop , class IsProp , prop , propName , attrName )
24+ import Halogen.HTML as HH
25+ import Halogen.HTML.Elements as HE
26+ import Halogen.HTML.Properties as P
2627
2728-- | A newtype for CSS styles
2829newtype Styles = Styles (SM.StrMap String )
2930
30- -- | Unpack CSS styles
31- runStyles :: Styles -> SM.StrMap String
32- runStyles (Styles m) = m
31+ derive instance newtypeStyles ∷ Newtype Styles _
3332
34- instance stylesIsProp :: IsProp Styles where
35- toPropString _ _ (Styles m) = joinWith " ; " $ (\(Tuple key value) -> key <> " : " <> value) <$> fromList (SM .toList m)
33+ instance stylesIsProp ∷ IsProp Styles where
34+ toPropString _ _ (Styles m) =
35+ joinWith " ; " $ SM .foldMap (\key value → [key <> " : " <> value]) m
3636
3737-- | Render a set of rules as an inline style.
3838-- |
3939-- | For example:
4040-- |
4141-- | ```purescript
42- -- | H .div [ Css .style do color red
42+ -- | HH .div [ CSS .style do color red
4343-- | display block ]
4444-- | [ ... ]
4545-- | ```
46- style :: forall i . Css -> Prop i
47- 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
4852 where
49- rules :: Array Rule -> SM.StrMap String
50- rules rs = SM .fromList (toList properties)
53+ rules ∷ Array Rule → SM.StrMap String
54+ rules rs = SM .fromFoldable properties
5155 where
52- properties :: Array (Tuple String String )
56+ properties ∷ Array (Tuple String String )
5357 properties = mapMaybe property rs >>= collect >>> rights
5458
55- property :: Rule -> Maybe (Tuple (Key Unit ) Value )
59+ property ∷ Rule → Maybe (Tuple (Key Unit ) Value )
5660 property (Property k v) = Just (Tuple k v)
5761 property _ = Nothing
5862
59- rights :: forall a b . Array (Either a b ) -> Array b
60- rights = mapMaybe (either (const Nothing ) Just )
63+ rights ∷ ∀ a b . Array (Either a b ) → Array b
64+ rights = concatMap $ foldMap singleton
6165
6266-- | Render a set of rules as a `style` element.
63- stylesheet :: forall p i . Css -> HTML p i
64- 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 ]
6570 where
6671 content = fromMaybe " " $ renderedSheet $ render css
0 commit comments