aboutsummaryrefslogtreecommitdiff
path: root/src/z/Component.zig
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2026-10-08 13:52:25 +0200
committerNathan Reiner <nathan@nathanreiner.xyz>2026-10-08 13:52:25 +0200
commitec30b659dfd337f5edf512ea78032fe18251b704 (patch)
tree8fe9cfd4b965c7ffbac7f190681b6ac5ac9f8ae7 /src/z/Component.zig
parent0a2f6f8cc48258fca60df2f193790b2d94a9d0f6 (diff)
refactor html module
Diffstat (limited to 'src/z/Component.zig')
-rw-r--r--src/z/Component.zig174
1 files changed, 99 insertions, 75 deletions
diff --git a/src/z/Component.zig b/src/z/Component.zig
index f3a1a99..ba328b6 100644
--- a/src/z/Component.zig
+++ b/src/z/Component.zig
@@ -1,5 +1,6 @@
const std = @import("std");
-const Element = @import("html.zig").Element;
+const Element = @import("root.zig").html.Element;
+const Attribute = @import("root.zig").html.Attribute;
const Object = @import("Object.zig");
const Self = @This();
@@ -11,24 +12,34 @@ link: type,
fn exportFromHtml(self: *const Self, element: Element) void {
comptime {
- for (element.attributes) |attr| {
- if (attr.value.len > 0 and attr.value[0] == '@') {
- const func = struct {
- pub fn func(obj: *self.link) callconv(.c) void {
- @field(self.link, attr.value[1..])(obj);
- }
- }.func;
+ switch (element) {
+ .tag => |tag| for (tag.attributes) |attr| {
+ if (attr.value.len > 0 and attr.value[0] == '@') {
+ const func = struct {
+ pub fn func(obj: *self.link) callconv(.c) void {
+ @field(self.link, attr.value[1..])(obj);
+ }
+ }.func;
- @export(
- &func,
- .{
- .name = std.fmt.comptimePrint(
- "export::{s}::{s}",
- .{ self.name, attr.value[1..] },
- ),
+ @export(
+ &func,
+ .{
+ .name = std.fmt.comptimePrint(
+ "export::{s}::{s}",
+ .{ self.name, attr.value[1..] },
+ ),
+ },
+ );
+ }
+
+ switch (tag.content) {
+ .children => |children| for (children) |child| {
+ self.exportFromHtml(child);
},
- );
- }
+ else => {},
+ }
+ },
+ else => {},
}
}
}
@@ -47,7 +58,7 @@ pub fn exportLinks(self: *const Self) void {
.{
.name = std.fmt.comptimePrint(
"export::{s}::init",
- .{ self.name },
+ .{self.name},
),
},
);
@@ -55,72 +66,85 @@ pub fn exportLinks(self: *const Self) void {
comptime self.exportFromHtml(self.html);
}
+fn linkAttributes(self: *const Self, attributes: []const Attribute) []const Attribute {
+ var linked: []const Attribute = &.{};
+
+ for (attributes) |*attr| {
+ if (attr.value.len > 0 and attr.value[0] == '@') {
+ const attribute: []const Attribute = &.{.{
+ .name = attr.name,
+ .value = std.fmt.comptimePrint(
+ "wasm.exports[\"export::{s}::{s}\"](this[\"@ptr\"])",
+ .{ self.name, attr.value[1..] },
+ ),
+ }};
+ linked = linked ++ attribute;
+ }
+ }
+ return linked;
+}
+
fn linkHtmlRecursive(self: *const Self, element: Element) Element {
comptime {
- return .{
- .tag = element.tag,
- .attributes = attrs: {
- var attributes: []const Element.Attribute = &.{};
+ return switch (element) {
+ .tag => |el| .{
+ .tag = .{
+ .name = el.name,
+ .attributes = self.linkAttributes(el.attributes),
+ .content = switch (el.content) {
+ .children => |children| ch: {
+ var elements: []const Element = &.{};
- for (element.attributes) |*attr| {
- if (attr.value.len > 0 and attr.value[0] == '@') {
- const attribute: []const Element.Attribute = &.{.{
- .name = attr.name,
- .value = std.fmt.comptimePrint(
- "wasm.exports[\"export::{s}::{s}\"](this[\"@ptr\"])",
- .{ self.name, attr.value[1..] },
- ),
- }};
- attributes = attributes ++ attribute;
- }
- }
- break :attrs attributes;
+ for (children) |child| {
+ const e: []const Element = &.{self.linkHtmlRecursive(child)};
+ elements = elements ++ e;
+ }
+
+ break :ch .{ .children = elements };
+ },
+ .void => .void,
+ },
+ },
},
- .content = element.content,
+ .string => element,
};
}
}
-pub fn toElement(self: *const Self) Element {
- return .transparent(
- .children(
+pub fn toElement(self: *const Self) []const Element {
+ return &.{
+ .node(
+ "template",
+ .{ .id = "@" ++ self.name },
&.{
- .native(
- .template,
- .{ .id = "@" ++ self.name },
- .children(&.{
- .native(.style, .{}, .content(self.css)),
- self.linkHtmlRecursive(self.html)
- }),
- ),
- .native(
- .script,
- .{},
- .content(
- std.fmt.comptimePrint(
- \\customElements.define("app-{s}", class extends HTMLElement {{
- \\ constructor() {{
- \\ super();
- \\ this.attachShadow({{ mode: 'open' }});
- \\ const template = document.getElementById("@{s}");
- \\ const node = document.importNode(template.content, true);
- \\ this.shadowRoot.appendChild(node);
- \\ this['@ptr'] = null;
- \\ }}
- \\ connectedCallback() {{
- \\ wasm_ready.then(() => {{
- \\ const id = ObjectStore.generateFreeId();
- \\ ObjectStore.registerObject(id, this);
- \\ this['@ptr'] = wasm.exports['export::{s}::init'](id)
- \\ }});
- \\ }}
- \\}})
- ,
- .{self.name, self.name, self.name}
- ),
- ),
- ),
+ .node("style", .{}, &.{.text(self.css)}),
+ comptime self.linkHtmlRecursive(self.html),
},
),
- );
+ .node(
+ "script",
+ .{},
+ &.{.text(
+ std.fmt.comptimePrint(
+ \\customElements.define("app-{s}", class extends HTMLElement {{
+ \\ constructor() {{
+ \\ super();
+ \\ this.attachShadow({{ mode: 'open' }});
+ \\ const template = document.getElementById("@{s}");
+ \\ const node = document.importNode(template.content, true);
+ \\ this.shadowRoot.appendChild(node);
+ \\ this['@ptr'] = null;
+ \\ }}
+ \\ connectedCallback() {{
+ \\ wasm_ready.then(() => {{
+ \\ const id = ObjectStore.generateFreeId();
+ \\ ObjectStore.registerObject(id, this);
+ \\ this['@ptr'] = wasm.exports['export::{s}::init'](id)
+ \\ }});
+ \\ }}
+ \\}})
+ , .{ self.name, self.name, self.name }),
+ )},
+ ),
+ };
}