add __self to styled

This commit is contained in:
tristan 2023-12-31 19:23:26 +00:00
parent a7c1a1c7de
commit ccc6b53b91
3 changed files with 30 additions and 18 deletions

View file

@ -27,16 +27,21 @@ in
lang = "en";
class = [];
})
(s.styled "link" "a" {
__self = self: attrs: child:
if builtins.isString attrs then
self {href = attrs;} child else
self attrs child;
})
(s.styled "stylesheet" "link" {
rel = "stylesheet";
class = [];
__self = self: attrs:
if builtins.isString attrs then
self {href = attrs;} "" else
self attrs "";
})
// {
h = v: child:
html.tag "h${toString v}" {} child;
link = href: child:
html.tag "a" {inherit href;} child;
stylesheet = path:
html.tag "link" {
rel = "stylesheet";
href = path;
} "";
}

View file

@ -37,15 +37,22 @@ let
+ id;
in {
styled = name: tag: cprops: let
custom = cprops.__child or (child: child);
__child = cprops.__child or (child: child);
self = props:
html.tag tag (props // {class = [name];} // cprops // {style = "";});
__self = (cprops.__self or (self: props:
if builtins.isAttrs props
then (child: ( self props (__child child) ))
else if builtins.isString props || builtins.isList props
then (self {} (__child props))
else throw "Call element with attributes and child."
)) self;
in
{
${name} = props:
if builtins.isAttrs props
then (child: (html.tag tag (props // {class = [name];} // cprops // {style = "";}) (custom child)))
else if builtins.isString props || builtins.isList props
then (html.tag tag ({class = [name];} // cprops // {style = "";}) (custom props))
else throw "Call element with attributes and child.";
${name} = __self;
style = mkStyle (mkIdentifier name tag cprops) ( cprops.style or {} );
}

View file

@ -45,7 +45,7 @@ in
asString = true;
})
(it "makes an a tag" {
expected = html.tag "a" {href = "https://example.com";} "example";
expected = html.tag "a" {class = ["link"]; href = "https://example.com";} "example";
actual = link "https://example.com" "example";
asString = true;
})
@ -54,7 +54,7 @@ in
href = "/style";
rel = "stylesheet";
} "";
actual = stylesheet "/style";
actual = stylesheet {href = "/style"; };
asString = true;
})
(it "makes a list" {