media queries in styled components

This commit is contained in:
Tristan 2024-01-04 22:37:43 +00:00
parent 4f5bedbc45
commit 89b29b2e46
4 changed files with 117 additions and 29 deletions

View file

@ -35,11 +35,11 @@
test = tix.run [ test = tix.run [
# ./testing/md.test.nix # ./testing/md.test.nix
#./testing/html.test.nix ./testing/html.test.nix
#./testing/elems.test.nix ./testing/elems.test.nix
./testing/builder.test.nix
./testing/site.test.nix ./testing/site.test.nix
#./testing/style.test.nix ./testing/style.test.nix
#./testing/builder.test.nix
]; ];
}; };
}; };

View file

@ -1,15 +1,20 @@
let rec {
styleToString = identifier: styles: styleToString = identifier: styles:
if !builtins.isAttrs styles if !builtins.isAttrs styles
then "" then ""
else if styles == {} else if styles == {}
then "" then ""
else else assert builtins.isString identifier; ''
assert builtins.isString identifier; ''
${identifier} { ${identifier} {
${ ${
toString (builtins.attrValues toString (builtins.attrValues
(builtins.mapAttrs (key: value: "${key}: ${value};") styles)) (builtins.mapAttrs (
key: value:
if builtins.isString value
then "${key}: ${value};"
else styleToString key value
)
styles))
} }
} }
''; '';
@ -20,8 +25,9 @@ let
else if element.attrs ? __id else if element.attrs ? __id
then then
({ ({
${element.attrs.__id} = element.attrs.style or {}; ${element.attrs.__id} = removeMediaQuery element.attrs.style or {};
} }
// getMediaQuery element.attrs.__id element.attrs.style or {}
// ( // (
if element.attrs ? __extends if element.attrs ? __extends
then { then {
@ -32,6 +38,12 @@ let
)) ))
else {}; else {};
removeMediaQuery = style: builtins.removeAttrs style ["__mediaQueries"];
getMediaQuery = id: style:
builtins.foldl' (a: b: a // b) {} (builtins.attrValues (builtins.mapAttrs
(name: value: {"@media ${name}" = {${id} = value;};})
(style."__mediaQueries" or {})));
getStyles = element: getStyles = element:
(getStyle element) (getStyle element)
// ( // (
@ -59,9 +71,10 @@ let
stylesToString = styles: stylesToString = styles:
builtins.concatStringsSep "" builtins.concatStringsSep ""
(builtins.attrValues (builtins.mapAttrs styleToString styles)); (
in { builtins.sort (a: b: builtins.substring 0 1 b == "@")
inherit getStyle getStyles stylesToString; (builtins.attrValues (builtins.mapAttrs styleToString styles))
);
component = tag: class: props: tag (mkProps tag class props); component = tag: class: props: tag (mkProps tag class props);
} }

View file

@ -1,17 +1,28 @@
{nixite}: let {nixite}:
markup = { with nixite.elems; let
"index.html" = with nixite.elems; let
blue = nixite.style.component span "blue" { blue = nixite.style.component span "blue" {
style = {color = "blue";}; style = {
color = "blue";
__mediaQueries = {
"(max-width: 500px)" = {
color = "green";
};
};
};
}; };
underblue = nixite.style.component blue "under" { underblue = nixite.style.component blue "under" {
style = {text-decoration = "underline";}; style = {text-decoration = "underline";};
}; };
in ( readme = nixite.site.link {
content = nixite.md.mdToPage ../README.md;
name = "readme";
};
markup = {
"index.html" = (
nixite.html.document { nixite.html.document {
head = [(title "Nixite")]; head = [(title "Nixite")];
body = main [ body = main [
(a "/" "Readme") (a {href = readme;} "Readme")
(a "/blog" "blog") (a "/blog" "blog")
(List {} ["item 1" "item 2" "item 3"]) (List {} ["item 1" "item 2" "item 3"])
(p [ (p [

View file

@ -39,6 +39,41 @@ in [
}; };
actual = style.getStyle (para ""); actual = style.getStyle (para "");
})) }))
(it "separates media queries" (let
s = {
foo = "bar";
__mediaQueries = {
"(max-width: 500px)" = {
foo = "nar";
};
"(prefers-color-scheme: dark)" = {
foo = "blar";
};
};
};
para = style.component elems.p "para" {
style = s;
};
in {
expected = {
"p" = {};
"p.para" = {
foo = "bar";
};
"@media (max-width: 500px)" = {
"p.para" = {
foo = "nar";
};
};
"@media (prefers-color-scheme: dark)" = {
"p.para" = {
foo = "blar";
};
};
};
actual = style.getStyle (para "");
}))
] ]
) )
( (
@ -157,6 +192,35 @@ in [
''; '';
actual = style.stylesToString s; actual = style.stylesToString s;
})) }))
(it "converts nested styles to string" (let
s = {
"p" = {};
"p.para" = {foo = "bar";};
"a.link" = {this = "that";};
"@media blah" = {
"a.link" = {
this = "not that";
};
};
};
in {
expected = ''
a.link {
this: that;
}
p.para {
foo: bar;
}
@media blah {
a.link {
this: not that;
}
}
'';
actual = style.stylesToString s;
}))
] ]
) )
] ]