-
-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CSS Extraction at build-time #433
Comments
The problem remaining to solve is the static interpolation on let small = `px 10 in
let classname = [%cx {| margin: $(small); |}]; How can we access Possible solutionsSolution AChange let%cx classname = {
let lola = `px 10 in
{| font-size: $(lola) |}
}; let classname = {
let _ = Css.global_var "lola" (`px 10); in
"r4nd0mCl4sSN4M3"
}; :root {
--lola: 10px;
}
.r4nd0mCl4sSN4M3 {
font-size: var(--lola);
} Problems
module StyleVars = struct
let lola = `px 10
end
let%cx classname = {
let lola = StyleVars.lola
{| font-size: $(lola) |}
}; Solution BCreate a new css extension let%css_var small = `px 10 in
(* Will be transformed into a side-effect + the value:
let small = Css.publish_global "-small" `px 10; `px 10 in *)
let classname = [%cx {| margin: var(--small); |}] :root {
--small: 10px;
}
.lola {
margin: var(--small);
} Problems
Solution C
module Css = struct
type cx = { static: string; dynamic: unit => ReactDOM.Style.t }
let merge cxa cxb =
{ static: cxa.static ++ " " ++ cxb.static; dynamic: () => ReactDOM.Style.make(~unsafe=cxa.dynamic() ++ cxb.dynamic(), ()) }
end
let small = `px 10 in
let classname: CSS.cx = [%cx {| margin: $(small); |} { small }]
(* .lola {
margin: var(--lola-small);
} *)
let classname: CSS.cx = { static: "lola", dynamic: Css.variable("small", small) }; Problems
let foo: string = [%cx {| margin: $(small); |}];
let bar: string = [%cx {| margin: $(small); |}];
let className = foo ++ " " ++ bar
let className = Css.merge foo bar Solution DSimilar to B, but with a CSS-like approach with Example 1[%root
let lola = `px 10
] :root {
--lola: 10px;
} let classname = [%cx {| font-size: $(lola) |}]; .lola {
font-size: var(--lola);
} Example 2[%root
module StyleVars = struct
let lola = `px 10
end
] :root {
--lola: 10px;
} let classname = [%cx {| font-size: $(StyleVars.lola) |}]; .lola {
font-size: var(--lola);
} |
We can remove the runtime completely and go for static styles. Generate CSS files in the _build folder (for Melange/native) and in-source in ReScript.
Design
styled.div
cx
Ideal plan
Some JavaScript implementations
https://github.com/krasimir/cssx/tree/master/packages/cssx
https://www.smashingmagazine.com/2016/04/finally-css-javascript-meet-cssx/
https://github.com/emotion-js/emotion/blob/v9.2.12/packages/create-emotion/src/index.js
https://github.com/thysultan/stylis.js/tree/master/src
https://www.youtube.com/watch?v=-Idub5K7K6Q&t=2556s
withCSSVar
is a simple helper to access/modify css variables in your codeThe text was updated successfully, but these errors were encountered: