nixite/nixite/style.nix

60 lines
1.5 KiB
Nix

let
styleToString = identifier: styles:
if !builtins.isAttrs styles then
""
else if styles == { } then
""
else
assert builtins.isString identifier; ''
${identifier} {
${
toString (builtins.attrValues
(builtins.mapAttrs (key: value: "${key}: ${value};") styles))
}
}
'';
getStyle = element:
if builtins.isAttrs element && element.attrs ? __id then
({
${element.attrs.__id} = element.attrs.style or { };
} // (if element.attrs ? __extends then {
${element.attrs.__extends.attrs.__id or element.attrs.__extends.tag} =
element.attrs.__extends.attrs.style or { };
} else
{ }))
else
{ };
getStyles = element:
(getStyle element) // (if builtins.isList element then
getStylesFromList element
else if element ? child then
getStyles element.child
else
{ }) // {
__toString = stylesToString;
};
getStylesFromList = elements:
builtins.zipAttrsWith (name: value: builtins.elemAt value 0)
(map getStyles elements);
mkProps = tag: class: props:
props // {
__id = "${tag.tag}.${class}";
__extends = tag;
class = (tag.attrs.class or [ ]) ++ [ class ] ++ (props.class or [ ]);
};
stylesToString = styles:
builtins.concatStringsSep ""
(builtins.attrValues (builtins.mapAttrs (styleToString) styles));
in {
inherit getStyle getStyles stylesToString;
component = tag: class: props: tag (mkProps tag class props);
}