From ccc6b53b918ddeb20f1768be21901c24e6f7f40a Mon Sep 17 00:00:00 2001 From: tristan Date: Sun, 31 Dec 2023 19:23:26 +0000 Subject: [PATCH] add __self to styled --- nixite/elems.nix | 23 ++++++++++++++--------- nixite/style.nix | 21 ++++++++++++++------- testing/elems.test.nix | 4 ++-- 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/nixite/elems.nix b/nixite/elems.nix index b10ffdc..b49bd0c 100644 --- a/nixite/elems.nix +++ b/nixite/elems.nix @@ -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; - } ""; } diff --git a/nixite/style.nix b/nixite/style.nix index a525a73..dd06e83 100644 --- a/nixite/style.nix +++ b/nixite/style.nix @@ -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 {} ); } diff --git a/testing/elems.test.nix b/testing/elems.test.nix index 705a434..4eedac5 100644 --- a/testing/elems.test.nix +++ b/testing/elems.test.nix @@ -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" {