Skip to content

Commit

Permalink
Merge branch 'main' into start-by-rendering-css
Browse files Browse the repository at this point in the history
  • Loading branch information
zakybilfagih committed Jan 15, 2025
2 parents 009181e + f0c162d commit d6c15c0
Show file tree
Hide file tree
Showing 37 changed files with 1,176 additions and 344 deletions.
45 changes: 27 additions & 18 deletions packages/ppx/src/Css_to_runtime.re
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module CSS = {
/* This is the public API of the CSS module */
let ident = (~loc, name) =>
{txt: Ldot(Lident("CSS"), name), loc} |> Builder.pexp_ident(~loc);
let selector = (~loc) => ident(~loc, "selector");
let selectorMany = (~loc) => ident(~loc, "selectorMany");
let media = (~loc) => ident(~loc, "media");
let global = (~loc) => ident(~loc, "global");
let label = (~loc) => ident(~loc, "label");
Expand Down Expand Up @@ -182,12 +182,15 @@ and render_container_query = (~loc, at_rule: at_rule) => {
)
| Ok(conditions) =>
let query =
String_interpolation.transform(
~attrs,
~delimiter,
~loc=at_rule.loc,
"@container " ++ conditions,
);
[
String_interpolation.transform(
~attrs,
~delimiter,
~loc=at_rule.loc,
"@container " ++ conditions,
),
]
|> Builder.pexp_array(~loc=at_rule.loc);

let rules =
switch (at_rule.block) {
Expand All @@ -202,7 +205,7 @@ and render_container_query = (~loc, at_rule: at_rule) => {

Helper.Exp.apply(
~loc=at_rule.loc,
CSS.selector(~loc=at_rule.loc),
CSS.selectorMany(~loc=at_rule.loc),
[(Nolabel, query), (Nolabel, rules)],
);
};
Expand Down Expand Up @@ -352,7 +355,11 @@ and render_selector = (~loc, selector: selector) => {
":"
++ name
++ "("
++ (render_selectors(~loc, payload) |> String.trim)
++ (
render_selectors(~loc, payload)
|> String.concat(", ")
|> String.trim
)
++ ")";
}
and render_pseudo_selector =
Expand Down Expand Up @@ -405,8 +412,7 @@ and render_selector = (~loc, selector: selector) => {
}
and render_selectors = (~loc, selectors) => {
selectors
|> List.map(((selector, _loc)) => render_selector(~loc, selector))
|> String.concat(", ");
|> List.map(((selector, _loc)) => render_selector(~loc, selector));
}
and render_style_rule = (~loc, rule: style_rule) => {
let (prelude, prelude_loc) = rule.prelude;
Expand All @@ -423,16 +429,19 @@ and render_style_rule = (~loc, rule: style_rule) => {
let selector_name =
prelude
|> render_selectors(~loc=selector_location)
|> String.trim
|> String_interpolation.transform(
~attrs,
~delimiter,
~loc=selector_location,
);
|> List.map(String.trim)
|> List.map(
String_interpolation.transform(
~attrs,
~delimiter,
~loc=selector_location,
),
)
|> Builder.pexp_array(~loc=selector_location);

Helper.Exp.apply(
~loc=selector_location,
CSS.selector(~loc=selector_location),
CSS.selectorMany(~loc=selector_location),
[(Nolabel, selector_name), (Nolabel, selector_expr)],
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ If this test fail means that the module is not in sync with the ppx
CSS.style([|
CSS.label("selectors"),
CSS.color(CSS.white),
CSS.selector(
{js|&:hover|js},
CSS.selectorMany(
[|{js|&:hover|js}|],
[|[%ocaml.error "Unknown property 'colorx'"]|],
),
|]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ If this test fail means that the module is not in sync with the ppx
CSS.style([|
CSS.label("selectors"),
CSS.color(CSS.white),
CSS.selector(
{js|&:hover|js},
CSS.selectorMany(
[|{js|&:hover|js}|],
[|[%ocaml.error "Property 'color' has an invalid value: 'cositas'"]|],
),
|]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ If this test fail means that the module is not in sync with the ppx
let _className =
CSS.style([|
CSS.label("_className"),
CSS.selector({js|.lolaso :nth-child(2n)|js}, [|CSS.color(CSS.red)|]),
CSS.selectorMany(
[|{js|.lolaso :nth-child(2n)|js}|],
[|CSS.color(CSS.red)|],
),
|]);

let _className = [%ocaml.error "Parse error while reading token '2'"];
20 changes: 11 additions & 9 deletions packages/ppx/test/css-support/selectors.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,27 @@ If this test fail means that the module is not in sync with the ppx
CSS.style([|
CSS.label("_chart"),
CSS.userSelect(`none),
CSS.selector(
{js|.recharts-cartesian-grid-horizontal|js},
CSS.selectorMany(
[|{js|.recharts-cartesian-grid-horizontal|js}|],
[|
CSS.selector(
{js|line|js},
CSS.selectorMany(
[|{js|line|js}|],
[|
CSS.selector(
{js|:nth-last-child(1), :nth-last-child(2)|js},
CSS.selectorMany(
[|{js|:nth-last-child(1)|js}, {js|:nth-last-child(2)|js}|],
[|CSS.SVG.strokeOpacity(`num(0.))|],
),
|],
),
|],
),
CSS.selector(
{js|.recharts-scatter .recharts-scatter-symbol .recharts-symbols|js},
CSS.selectorMany(
[|
{js|.recharts-scatter .recharts-scatter-symbol .recharts-symbols|js},
|],
[|
CSS.opacity(0.8),
CSS.selector({js|:hover|js}, [|CSS.opacity(1.)|]),
CSS.selectorMany([|{js|:hover|js}|], [|CSS.opacity(1.)|]),
|],
),
|]);
Expand Down
28 changes: 15 additions & 13 deletions packages/ppx/test/native/At_rule_test.re
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ let container_query_tests = [
[%expr [%cx "@container (min-width: 150px) {}"]],
[%expr
CSS.style([|
CSS.selector({js|@container (min-width: 150px)|js}, [||]),
CSS.selectorMany([|{js|@container (min-width: 150px)|js}|], [||]),
|])
],
),
Expand All @@ -261,7 +261,7 @@ let container_query_tests = [
[%expr [%cx "@container (max-width: 1000px) {}"]],
[%expr
CSS.style([|
CSS.selector({js|@container (max-width: 1000px)|js}, [||]),
CSS.selectorMany([|{js|@container (max-width: 1000px)|js}|], [||]),
|])
],
),
Expand All @@ -270,23 +270,25 @@ let container_query_tests = [
[%expr [%cx "@container name (width >= 150px) {}"]],
[%expr
CSS.style([|
CSS.selector({js|@container name (width >= 150px)|js}, [||]),
CSS.selectorMany([|{js|@container name (width >= 150px)|js}|], [||]),
|])
],
),
(
"(height >= 150px)",
[%expr [%cx "@container (height >= 150px) {}"]],
[%expr
CSS.style([|CSS.selector({js|@container (height >= 150px)|js}, [||])|])
CSS.style([|
CSS.selectorMany([|{js|@container (height >= 150px)|js}|], [||]),
|])
],
),
(
"(inline-size >= 150px)",
[%expr [%cx "@container (inline-size >= 150px) {}"]],
[%expr
CSS.style([|
CSS.selector({js|@container (inline-size >= 150px)|js}, [||]),
CSS.selectorMany([|{js|@container (inline-size >= 150px)|js}|], [||]),
|])
],
),
Expand All @@ -295,7 +297,7 @@ let container_query_tests = [
[%expr [%cx "@container (block-size >= 150px) {}"]],
[%expr
CSS.style([|
CSS.selector({js|@container (block-size >= 150px)|js}, [||]),
CSS.selectorMany([|{js|@container (block-size >= 150px)|js}|], [||]),
|])
],
),
Expand All @@ -304,7 +306,7 @@ let container_query_tests = [
[%expr [%cx "@container (aspect-ratio: 1 / 1) {}"]],
[%expr
CSS.style([|
CSS.selector({js|@container (aspect-ratio: 1 / 1)|js}, [||]),
CSS.selectorMany([|{js|@container (aspect-ratio: 1 / 1)|js}|], [||]),
|])
],
),
Expand All @@ -313,7 +315,7 @@ let container_query_tests = [
[%expr [%cx "@container (orientation: portrait) {}"]],
[%expr
CSS.style([|
CSS.selector({js|@container (orientation: portrait)|js}, [||]),
CSS.selectorMany([|{js|@container (orientation: portrait)|js}|], [||]),
|])
],
),
Expand All @@ -322,8 +324,8 @@ let container_query_tests = [
[%expr [%cx "@container (width >= 150px) and (orientation: portrait) {}"]],
[%expr
CSS.style([|
CSS.selector(
{js|@container (width >= 150px) and (orientation: portrait)|js},
CSS.selectorMany(
[|{js|@container (width >= 150px) and (orientation: portrait)|js}|],
[||],
),
|])
Expand All @@ -334,7 +336,7 @@ let container_query_tests = [
[%expr [%cx "@container name not (width < 150px) {}"]],
[%expr
CSS.style([|
CSS.selector({js|@container name not (width < 150px)|js}, [||]),
CSS.selectorMany([|{js|@container name not (width < 150px)|js}|], [||]),
|])
],
),
Expand All @@ -343,8 +345,8 @@ let container_query_tests = [
[%expr [%cx "@container (width >= 150px) or (orientation: portrait) {}"]],
[%expr
CSS.style([|
CSS.selector(
{js|@container (width >= 150px) or (orientation: portrait)|js},
CSS.selectorMany(
[|{js|@container (width >= 150px) or (orientation: portrait)|js}|],
[||],
),
|])
Expand Down
Loading

0 comments on commit d6c15c0

Please sign in to comment.