package ppx_css

  1. Overview
  2. Docs
A ppx that takes in css strings and produces a module for accessing the unique names defined within

Install

Dune Dependency

Authors

Maintainers

Sources

v0.17.0.tar.gz
sha256=e60e31aadd820b131f7f05827f369164564400bd4fd51c199d066abf7e68753f

Description

A ppx that takes in css strings and produces a module for accessing the unique names defined within.

Published: 26 May 2024

README

PPX CSS

PPX CSS is a PPX for dealing with CSS from within OCaml 🐫. It it designed to be used within apps made with Bonsai 🌳, Incr_dom, or anything that uses the virtual_dom library. It makes your CSS safe, composable, and convenient.

"Styled Components" Syntax

css can be embedded within an OCaml expression. Spiritually similar to styled components. This will expand into a Vdom.Attr.t:

Vdom.Node.div ~attrs:[ [%css {|
  background-color: tomato;
  min-width: 2rem;
  min-height: 2rem;
|}] ] []

It also has the same interpolation syntax as ppx_string:

let f (color : string) =
  Vdom.Node.div ~attrs:[ [%css {|
    border: 0.3rem solid %{color};
  |}] ] []

like ppx_string, you can specify a module, it will call the module's to_string_css function:

let f (color : Css_gen.Color.t) =
  Vdom.Node.div ~attrs:[ [%css {|
    border: 0.3rem solid %{color#Css_gen.Color};
  |}] ] []

You can also use Nested CSS:

[%css {|
  background-color: tomato;
  &:hover {
    background-color: red;
  }
|}]

"Stylesheet" Syntax

The recommended way of using ppx_css is via the styled_component syntax. However, in some situations, you do need to use @media/@keyframes other things that are not expressible in the styled_component syntax/not expressible with nested CSS. In such scenarios, css also has a "stylesheet" syntax available:

module Styles = [%css stylesheet {|
 .card:hover {
    background-color: tomato;
 }

 :root {
    background-color: tomato;
  }
|}]

It will generate Styles.card : Vdom.Attr.t that you can then attach to style your app. PPX CSS will additionally do the following things behind the scenes:

  • It will hash your identifiers. Normal CSS is prone to name clashes. PPX_CSS adds a unique hash to your classnames (e.g. "card_hash_12345") to be resilient against CSS naming clases. You do not need to add unique prefixes while using ppx_css.

  • It will register your CSS. You do not need to serve/bundle your own CSS files. Under the hood, ppx_css registers your styles using the CSSStylesheet API.

You can opt-out of hashing behavior/customize it with the following options. The syntax for sending options is: [%css stylesheet {|...css string...|} ~option_name:OPTION_VALUE]

  • ~dont_hash:string list: Will not hash the identifiers it seems Will not hash the identifiers it sees. (e.g. [%css stylesheet {||} ~dont_hash:["card"]])

  • ~dont_hash_prefixes:string list: Will not hash identifiers that match that prefix. (e.g. [%css stylesheet {||} ~dont_hash_prefixes:[--]] will stop hashing all css variables.)

  • ~rewrite:(string * string) list: Let's you change what each identifier is rewritten to. (e.g. [%css stylesheet {||} ~rewrite:["foo", "bar"]] rewrites "foo" into "bar")

The stylesheet syntax also generates a For_referencing module that includes - Styles.For_referencing.card : string - the post-hashed name.

If you use CSS Variables, it will also generate a Variables module that has two functions: set: ?css_variable1:string -> ?css_variable2:string -> unit -> Vdom.Attr.t and set_all : css_variable1:string -> css_variable2:string -> Vdom.Attr.t that let you set the post-hashed variables.

Dependencies (19)

  1. sedlex >= "2.6"
  2. ppxlib >= "0.28.0"
  3. menhirLib
  4. js_of_ocaml-ppx >= "5.1.1" & < "5.7.0"
  5. js_of_ocaml >= "5.1.1" & < "5.7.0"
  6. dune >= "3.11.0"
  7. virtual_dom >= "v0.17" & < "v0.18"
  8. stdio >= "v0.17" & < "v0.18"
  9. ppx_string >= "v0.17" & < "v0.18"
  10. ppx_sexp_conv >= "v0.17" & < "v0.18"
  11. ppx_let >= "v0.17" & < "v0.18"
  12. ppx_jane >= "v0.17" & < "v0.18"
  13. ppx_here >= "v0.17" & < "v0.18"
  14. core_unix >= "v0.17" & < "v0.18"
  15. core_kernel >= "v0.17" & < "v0.18"
  16. core >= "v0.17" & < "v0.18"
  17. async_unix >= "v0.17" & < "v0.18"
  18. async >= "v0.17" & < "v0.18"
  19. ocaml >= "5.1.0"

Dev Dependencies

None

Used by (2)

  1. bonsai >= "v0.17.0"
  2. virtual_dom_toplayer

Conflicts

None

OCaml

Innovation. Community. Security.