From 91d9b6992ac0ebee13704a3faefa8fd41a00075e Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Mon, 26 Oct 2026 09:23:55 +0100 Subject: zilch: dynamic element creation --- src/app/components/gallery/index.css | 3 +++ src/app/components/gallery/index.html | 1 + src/app/components/gallery/root.zig | 38 +++++++++++++++++++++++++++++++++++ src/app/components/icons/root.zig | 4 ++-- src/app/components/image/index.css | 10 +++------ src/app/components/image/root.zig | 14 +++++++++++-- src/app/components/navbutton/root.zig | 4 ++-- src/app/components/root.zig | 9 +++++++-- 8 files changed, 68 insertions(+), 15 deletions(-) create mode 100644 src/app/components/gallery/index.css create mode 100644 src/app/components/gallery/index.html create mode 100644 src/app/components/gallery/root.zig (limited to 'src/app/components') diff --git a/src/app/components/gallery/index.css b/src/app/components/gallery/index.css new file mode 100644 index 0000000..c6a5817 --- /dev/null +++ b/src/app/components/gallery/index.css @@ -0,0 +1,3 @@ +:host { + background: gray; +} diff --git a/src/app/components/gallery/index.html b/src/app/components/gallery/index.html new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/app/components/gallery/index.html @@ -0,0 +1 @@ + diff --git a/src/app/components/gallery/root.zig b/src/app/components/gallery/root.zig new file mode 100644 index 0000000..7a06d5f --- /dev/null +++ b/src/app/components/gallery/root.zig @@ -0,0 +1,38 @@ +const std = @import("std"); +const zilch = @import("zilch"); +const Object = zilch.Object; +const components = @import("../root.zig"); +const Image = components.Image; + +const app = @import("root").app; + +pub const component: zilch.Component = .build(.{ + .name = "gallery", + .html = .parse(@embedFile("index.html")), + .css = @embedFile("index.css"), + .link = @This(), +}); + +const Self = @This(); + +element: *Object, +image: *Image, +body: *Object, + +pub fn init(self: *Self, element: *Object) !void { + var env: zilch.Env = .default; + defer env.deinit(); + + self.element = element; + + self.image = try std.heap.wasm_allocator.create(Image); + var el = Image.component.new(self.image, &env); + try self.image.init(try el.await(&env)); + + var body = self.element.get(&env, "shadowRoot"); + + var append_call = Object.call(body, &env, "append", .{self.image.element}); + append_call.ignore(&env); + + self.body = try body.await(&env, *Object); +} diff --git a/src/app/components/icons/root.zig b/src/app/components/icons/root.zig index be814d8..0328472 100644 --- a/src/app/components/icons/root.zig +++ b/src/app/components/icons/root.zig @@ -7,11 +7,11 @@ pub const icons: []const zilch.Component = &.{ }; pub fn icon(name: []const u8) zilch.Component { - return .{ + return .build(.{ .namespace = "icon", .name = name, .html = .parse(@embedFile(name ++ ".svg")), .css = @embedFile("index.css"), .link = struct {}, - }; + }); } diff --git a/src/app/components/image/index.css b/src/app/components/image/index.css index 86ba44e..034a509 100644 --- a/src/app/components/image/index.css +++ b/src/app/components/image/index.css @@ -1,12 +1,8 @@ -:host { - --src: attr(src) -} - .image { - width: 100%; - padding-bottom: 100%; + width: calc(100% / var(--gallery-splits)); + padding-bottom: calc(100% / var(--gallery-splits)); height: 0; - background-image: url("" ++ var(--src)); + background: red; background-size: cover; background-position: center; } diff --git a/src/app/components/image/root.zig b/src/app/components/image/root.zig index 971713d..c9aeef4 100644 --- a/src/app/components/image/root.zig +++ b/src/app/components/image/root.zig @@ -1,8 +1,18 @@ const zilch = @import("zilch"); -pub const component: zilch.Component = .{ +pub const component: zilch.Component = .build(.{ .name = "image", .html = .parse(@embedFile("index.html")), .css = @embedFile("index.css"), .link = @This(), -}; +}); + +const Self = @This(); + +element: *zilch.Object, + +pub fn init(self: *Self, element: *zilch.Object) !void { + self.element = element; +} + +pub const connect = component.connect; diff --git a/src/app/components/navbutton/root.zig b/src/app/components/navbutton/root.zig index 2f26116..a512d1d 100644 --- a/src/app/components/navbutton/root.zig +++ b/src/app/components/navbutton/root.zig @@ -6,12 +6,12 @@ const Promise = zilch.Promise; const Self = @This(); -pub const component: zilch.Component = .{ +pub const component: zilch.Component = .build(.{ .name = "navbutton", .html = .parse(@embedFile("index.html")), .css = @embedFile("index.css"), .link = @This(), -}; +}); element: *zilch.Object, shadow_root: *zilch.Object, diff --git a/src/app/components/root.zig b/src/app/components/root.zig index f51e3c1..999a052 100644 --- a/src/app/components/root.zig +++ b/src/app/components/root.zig @@ -2,7 +2,12 @@ const zilch = @import("zilch"); const icons = @import("icons/root.zig").icons; +pub const NavButton = @import("navbutton/root.zig"); +pub const Image = @import("image/root.zig"); +pub const Gallery = @import("gallery/root.zig"); + pub const components = &[_]zilch.Component{ - @import("navbutton/root.zig").component, - @import("image/root.zig").component, + NavButton.component, + Image.component, + Gallery.component, } ++ icons; -- cgit v1.2.3-70-g09d2