Copyright | (c) Alexey Seledkov 2022 |
---|---|
License | GPL-3 |
Maintainer | qyutou@gmail.com |
Stability | experimental |
Portability | portable |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
This library provides an eDSL for creating the CSS files. It uses the Writer Monad to fill the CSS AST-like representation, which then can be rendered as a text.
Note: OverloadedStrings are necessary for comfort work
>>>
:set -XOverloadedStrings
To create any Css rule you can use the rule
function, which takes
the name of the selector as an argument.
It also can be used for creating media queries.
>>>
rule ".wrapper" (maxWidth "72rem")
.wrapper { max-width: 72rem; }
Or, if you prefer infix notation:
>>>
".wrapper" ? (maxWidth "72rem")
.wrapper { max-width: 72rem; }
Or, without any function if the type is specified:
>>>
".wrapper" (maxWidth "72rem") :: Css ()
.wrapper { max-width: 72rem; }
Rules may be nested in other rules:
>>>
rule "@media only screen and (min-width: 48rem)" (rule ".wrapper" (maxWidth "72rem"))
@media only screen and (min-width: 48rem) { .wrapper { max-width: 72rem; } }
Css Monad also has a Semigroup and Monoid instance, so the elements are juxtaposed via semigroup's append:
>>>
rule "body" (background "#000000") <> rule ".wrapper" (width "90vw" <> maxWidth "72rem")
body { background: #000000; } .wrapper { width: 90vw; max-width: 72rem; }
Same example using do-notation:
-- This example requires OverloadedStrings and BlockArguments extensions sampleStyles :: Css () sampleStyles = do "body" do background "#000000" ".wrapper" do width "90vw" maxWidth "72rem"
If some property is not provided in Properties
you can create a new with
either declaration or infix (|>):
>>>
declaration "width" "90vw"
width: 90vw;>>>
"width" |> "90vw"
width: 90vw;
Or without functions if the type is specified:
-- Requires OverloadedStrings and BlockArguments sampleStyles :: Css () sampleStyles = do "body" do "background" "#000000"
Synopsis
- data Rule
- newtype Css a = Css {}
- data Config = Config {}
- pretty :: Config
- compact :: Config
- renderRule :: Rule -> Text
- renderRuleWith :: Rule -> Config -> Text
- renderCss :: Css () -> Text
- renderCssWith :: Css () -> Config -> Text
- putCss :: Css () -> IO ()
- putCssWith :: Css () -> Config -> IO ()
- renderCssToFile :: Css () -> FilePath -> IO ()
- renderCssToFileWith :: Css () -> Config -> FilePath -> IO ()
- rule :: Text -> Css () -> Css ()
- (?) :: Text -> Css () -> Css ()
- declaration :: Text -> Text -> Css ()
- (|>) :: Text -> Text -> Css ()
- module Css.Properties
Types
AST-like CSS Rule representation
Rule !Text !Rule !Rule | Rule: selector, inner rules/declarations, next |
Declaration !Text !Text !Rule | Declaration: property, value, next |
Leaf !Text | Leaf: text This is used to allow creating the declarations without using of functions |
Empty | Empty |
Css monad - newtype wrapper around Control.Monad.Writer.Writer monad
Instances
Applicative Css Source # | |
Functor Css Source # | |
Monad Css Source # | |
MonadWriter Rule Css Source # | MonadWriter instance for the Css monad |
IsString (Css ()) Source # | IsString instance used to allow the creation of declarations NOTE: This is only for creating the Declarations, it doesn't do anything else. |
Defined in Css.Internal fromString :: String -> Css () # | |
Monoid (Css ()) Source # | Monoid instance for the Css monad |
Semigroup (Css ()) Source # | Semigroup instance for the Css monad |
Show (Css ()) Source # | Show instance for the Css monad |
Rendering
Rendering configuration
Used to render the Css Rules with pretty config
Render the Css Rules with certain configuration
Render the Css Monad with pretty config as Text
Render the Css Monad with certain config as Text
Render the Css Monad and print it to stdout
Render the CSS with certain configuration and print it to stdout
Render the Css Monad and save it to the filePath
Render the Css Monad with certain config and save it to the filepath
eDSL
Create new rule
Examples
>>>
rule "body" (background "#000000") <> rule ".wrapper" (width "90vw" <> maxWidth "72rem")
body { background: #000000; } .wrapper { width: 90vw; max-width: 72rem; }
Create new declaration
Examples
>>>
declaration "width" "90vw"
width: 90vw;
This module contains (almost) all CSS3 properties
module Css.Properties