From ec30b659dfd337f5edf512ea78032fe18251b704 Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Thu, 8 Oct 2026 13:52:25 +0200 Subject: refactor html module --- src/z/App/root.zig | 86 +++++++++++++++++++++++++++++------------------------- 1 file changed, 47 insertions(+), 39 deletions(-) (limited to 'src/z/App/root.zig') diff --git a/src/z/App/root.zig b/src/z/App/root.zig index 42ef26f..8fce591 100644 --- a/src/z/App/root.zig +++ b/src/z/App/root.zig @@ -1,13 +1,14 @@ const builtin = @import("builtin"); const std = @import("std"); -const h = @import("../html.zig"); +const Element = @import("../root.zig").html.Element; +const Document = @import("../root.zig").html.Document; const Component = @import("../root.zig").Component; pub const Options = struct { name: []const u8, globals: []const Global, components: []const Component, - body: h.Element, + body: Element, theme: []const u8, pub const Global = struct { @@ -49,44 +50,51 @@ pub fn App(comptime options: Options) type { } }; } else { + const components: []const Element = (comp: { + var cs: []const Element = &.{}; + + for (options.components) |component| { + cs = cs ++ component.toElement(); + } + + break :comp cs; + }); + return struct { - html: []const u8 = h.Element.native(.html, .{}, .children(&.{ - .native(.head, .{}, .children(&.{ - .native(.title, .{}, .content(options.name)), - .native(.meta, .{ - .name = "viewport", - .content = "width=device-width, initial-scale=1.0", - }, .void), - .native(.style, .{}, .content(options.theme)), - })), - .native(.body, .{}, .children(&.{ - .native(.script, .{}, .content(@embedFile("object-store.js"))), - .native(.script, .{}, .content(reg: { - var register_code: []const u8 = ""; - - for (options.globals, 1..) |field, id| { - register_code = register_code ++ std.fmt.comptimePrint( - "ObjectStore.registerObject({}, {s});", - .{ id, field.name }, - ); - } - - break :reg register_code; - })), - .native(.script, .{}, .content(@embedFile("main.js"))), - .transparent(.children(comp: { - var cs: []const h.Element = &.{}; - - for (options.components) |component| { - const c: []const h.Element = &.{component.toElement()}; - cs = cs ++ c; - } - - break :comp cs; - })), - options.body, - })), - })).toDocument(), + const node: Document = .{ + .root = .node("html", .{}, &.{ + .node("head", .{}, &.{ + .node("title", .{}, &.{.text(options.name)}), + .void("meta", .{ + .name = "viewport", + .content = "width=device-width, initial-scale=1.0", + }), + .node("style", .{}, &.{.text(options.theme)}), + }), + .node( + "body", + .{}, + &[_]Element{ + .node("script", .{}, &.{.text(@embedFile("object-store.js"))}), + .node("script", .{}, &.{.text(reg: { + var register_code: []const u8 = ""; + + for (options.globals, 1..) |field, id| { + register_code = register_code ++ std.fmt.comptimePrint( + "ObjectStore.registerObject({}, {s});", + .{ id, field.name }, + ); + } + + break :reg register_code; + })}), + .node("script", .{}, &.{.text(@embedFile("main.js"))}), + } ++ components ++ &[1]Element{options.body}, + ), + }), + }; + + html: []const u8 = std.fmt.comptimePrint("{f}", .{node}), pub const init: @This() = .{}; }; -- cgit v1.2.3-70-g09d2