Skip to content

Commit 9923ae4

Browse files
wegrygiraud
authored andcommitted
Swap emotion in for glamor
1 parent ee4e46b commit 9923ae4

File tree

4 files changed

+48
-50
lines changed

4 files changed

+48
-50
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
statically typed DSL for writing css in reason.
44

5-
Bs-css is a statically typed interface to [Glamor](https://github.com/threepointone/glamor)
5+
Bs-css is a statically typed interface to [Emotion](https://github.com/emotion-js/emotion)
66

77
## Installation
88

@@ -18,10 +18,10 @@ In your `bsconfig.json`, include `"bs-css"` in the `bs-dependencies`.
1818
module Styles = {
1919
/*Open the Css module, so we can access the style properties below without prefixing them with Css.*/
2020
open Css;
21-
21+
2222
let card = style([
2323
display(flexBox),
24-
flexDirection(column),
24+
flexDirection(column),
2525
alignItems(stretch),
2626
backgroundColor(white),
2727
boxShadow(~y=px(3), ~blur=px(5), rgba(0, 0, 0, 0.3)),
@@ -100,6 +100,6 @@ Its on its way!
100100
until then you can check out [css.rei](./src/Css.rei).
101101

102102
## Thanks
103-
Thanks to [glamor](https://github.com/threepointone/glamor) which is doing all the heavi lifting.
103+
Thanks to [emotion](https://github.com/emotion-js/emotion) which is doing all the heavy lifting.
104104
Thanks to [bs-glamor](https://github.com/poeschko/bs-glamor) which this repo was forked from.
105105
Thanks to [elm-css](https://github.com/rtfeldman/elm-css) for dsl design inspiration.

example/test.re

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ let arialNarrow =
1010
~fontFamily="Arial FontFace Test",
1111
~src=[localUrl("Arial Narrow")],
1212
~fontStyle=normal,
13+
~fontWeight=500,
1314
(),
1415
)
1516
);

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
22
"name": "bs-css",
3-
"version": "7.5.0",
4-
"description": "BuckleScript bindings for glamor",
3+
"version": "8.0.0",
4+
"description": "BuckleScript bindings for emotion",
55
"scripts": {
66
"build": "bsb -make-world",
77
"webpack": "webpack -w",
88
"start": "bsb -make-world -w"
99
},
1010
"keywords": [
11-
"glamor",
11+
"emotion",
1212
"bucklescript",
1313
"reason",
1414
"css"
@@ -20,7 +20,7 @@
2020
"url": "git+https://github.com/SentiaAnalytics/bs-css.git"
2121
},
2222
"dependencies": {
23-
"glamor": "^2.0.0"
23+
"emotion": "^9.2.12"
2424
},
2525
"devDependencies": {
2626
"bs-platform": "^4.0.4",

src/Css.re

+39-42
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,17 @@
11
include Css_Colors;
22

3-
module Glamor = {
4-
type css;
5-
type fontFace;
6-
[@bs.send] external className: css => string = "toString";
7-
[@bs.module "glamor"] external _make: Js.Json.t => css = "css";
8-
[@bs.scope "css"] [@bs.module "glamor"]
9-
external makeGlobal: (string, Js.Json.t) => unit = "global";
10-
[@bs.scope "css"] [@bs.module "glamor"]
11-
external makeInsert: string => unit = "insert";
12-
[@bs.scope "css"] [@bs.module "glamor"]
3+
module Emotion = {
4+
type css = string;
5+
[@bs.module "emotion"] external _make: Js.Json.t => css = "css";
6+
[@bs.module "emotion"]
7+
external injectGlobal: Js.Json.t => unit = "";
8+
[@bs.module "emotion"]
9+
external rawInjectGlobal: string => unit = "injectGlobal";
10+
[@bs.module "emotion"]
1311
external makeKeyFrames: Js.Dict.t(Js.Json.t) => string = "keyframes";
14-
[@bs.scope "css"] [@bs.module "glamor"]
15-
external makeFontFace: fontFace => string = "fontFace";
16-
[@bs.obj]
17-
external fontFace:
18-
(
19-
~fontFamily: string,
20-
~src: string,
21-
~fontStyle: string=?,
22-
~fontWeight: int=?
23-
) =>
24-
fontFace =
25-
"";
26-
let merge: list(css) => css = [%bs.raw
27-
{|
28-
function (styles) {
29-
const glamor = require('glamor');
30-
return glamor.css.apply(glamor, styles)
31-
}
32-
|}
33-
];
12+
[@bs.module "emotion"] [@bs.splice]
13+
external merge: array(css) => css = "cx";
14+
let merge: list(css) => css = classes => classes->Array.of_list->merge;
3415
let rec makeDict = ruleset => {
3516
let toJs = rule =>
3617
switch (rule) {
@@ -310,21 +291,23 @@ type selector = [ | `selector(string, list(rule))];
310291
let empty = [];
311292

312293
let merge = List.concat;
313-
let global = (selector, rules: list(rule)) =>
314-
Glamor.makeGlobal(selector, Glamor.makeDict(rules));
315-
let insertRule = css => Glamor.makeInsert(css);
294+
let global = (selector, rules: list(rule)) => {
295+
Emotion.injectGlobal([(selector, Emotion.makeDict(rules))]->Js.Dict.fromList->Js.Json.object_);
296+
}
297+
298+
let insertRule = raw => Emotion.rawInjectGlobal(raw);
316299

317300
type animation = string;
318301

319302
let keyframes = frames => {
320303
let addStop = (dict, (stop, rules)) => {
321-
Js.Dict.set(dict, string_of_int(stop) ++ "%", Glamor.makeDict(rules));
304+
Js.Dict.set(dict, string_of_int(stop) ++ "%", Emotion.makeDict(rules));
322305
dict;
323306
};
324-
Glamor.makeKeyFrames @@ List.fold_left(addStop, Js.Dict.empty(), frames);
307+
Emotion.makeKeyFrames @@ List.fold_left(addStop, Js.Dict.empty(), frames);
325308
};
326309

327-
let style = rules => rules |> Glamor.make |> Glamor.className;
310+
let style = rules => rules |> Emotion.make;
328311

329312
let d = (property, value) => `declaration((property, value));
330313

@@ -914,7 +897,7 @@ let overflowY = x => d("overflowY", string_of_overflow(x));
914897

915898
let zIndex = x => d("zIndex", string_of_int(x));
916899

917-
let contentRule = x => d("content", x);
900+
let contentRule = x => d("content", {j|"$x"|j});
918901

919902
let columnCount = x =>
920903
d(
@@ -1256,6 +1239,7 @@ let outlineOffset = x => d("outlineOffset", string_of_length(x));
12561239
* Text
12571240
*/
12581241

1242+
[@bs.deriving jsConverter]
12591243
type fontStyle = [ | `normal | `italic | `oblique];
12601244
let fontStyleToJs =
12611245
fun
@@ -1281,6 +1265,7 @@ let fontVariant = x =>
12811265
);
12821266

12831267
let fontStyle = x => d("fontStyle", fontStyleToJs(x));
1268+
let fontWeight = x => d("fontWeight", string_of_int(x));
12841269

12851270
let fontFace = (~fontFamily, ~src, ~fontStyle=?, ~fontWeight=?, ()) => {
12861271
let fontStyle =
@@ -1293,12 +1278,24 @@ let fontFace = (~fontFamily, ~src, ~fontStyle=?, ~fontWeight=?, ()) => {
12931278
| `url(value) => {j|url("$(value)")|j},
12941279
)
12951280
|> String.concat(", ");
1296-
Glamor.(
1297-
makeFontFace(fontFace(~fontFamily, ~src, ~fontStyle?, ~fontWeight?))
1298-
);
1299-
};
13001281

1301-
let fontWeight = x => d("fontWeight", string_of_int(x));
1282+
let fontStyle =
1283+
Belt.Option.mapWithDefault(fontStyle, "", s => "font-style: " ++ s);
1284+
let fontWeight =
1285+
Belt.Option.mapWithDefault(fontWeight, "", w =>
1286+
"font-weight: " ++ string_of_int(w)
1287+
);
1288+
let asString = {j|@font-face {
1289+
font-family: $fontFamily;
1290+
src: $src;
1291+
$(fontStyle);
1292+
$(fontWeight);
1293+
}|j};
1294+
1295+
Emotion.rawInjectGlobal(asString);
1296+
1297+
fontFamily;
1298+
};
13021299

13031300
let lineHeight = x =>
13041301
d(

0 commit comments

Comments
 (0)