From 008f090e1de2773d501a8210bd54acc7d977ebf7 Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Thu, 8 Oct 2026 20:05:11 +0200 Subject: Component: add namespace option --- src/app/components/hello/index.css | 12 ----------- src/app/components/hello/index.html | 3 --- src/app/components/hello/root.zig | 35 --------------------------------- src/app/components/navbutton/index.css | 9 +++++++++ src/app/components/navbutton/index.html | 2 ++ src/app/components/navbutton/root.zig | 16 +++++++++++++++ src/app/components/root.zig | 5 +++++ src/app/index.html | 1 + src/app/root.zig | 8 ++++---- src/app/theme.css | 5 +---- src/z/Component.zig | 5 +++-- 11 files changed, 41 insertions(+), 60 deletions(-) delete mode 100644 src/app/components/hello/index.css delete mode 100644 src/app/components/hello/index.html delete mode 100644 src/app/components/hello/root.zig create mode 100644 src/app/components/navbutton/index.css create mode 100644 src/app/components/navbutton/index.html create mode 100644 src/app/components/navbutton/root.zig create mode 100644 src/app/components/root.zig create mode 100644 src/app/index.html (limited to 'src') diff --git a/src/app/components/hello/index.css b/src/app/components/hello/index.css deleted file mode 100644 index 7b316cb..0000000 --- a/src/app/components/hello/index.css +++ /dev/null @@ -1,12 +0,0 @@ -div { - font-family: 'sans-serif'; - cursor: pointer; - user-select: none; - background: var(--background); - color: var(--color); - display: block; - width: fit-content; - padding: 10px 10px; - border-radius: 4px; - margin: 4px; -} diff --git a/src/app/components/hello/index.html b/src/app/components/hello/index.html deleted file mode 100644 index 6d19662..0000000 --- a/src/app/components/hello/index.html +++ /dev/null @@ -1,3 +0,0 @@ -
- click me! -
diff --git a/src/app/components/hello/root.zig b/src/app/components/hello/root.zig deleted file mode 100644 index 6dafd98..0000000 --- a/src/app/components/hello/root.zig +++ /dev/null @@ -1,35 +0,0 @@ -const std = @import("std"); -const z = @import("z"); - -const Self = @This(); - -pub const component: z.Component = .{ - .name = "hello", - .html = .parse(@embedFile("index.html")), - .css = @embedFile("index.css"), - .link = @This(), -}; - -element: *z.Object, -counter: usize, - -pub fn init(self: *Self, element: *z.Object) void { - self.element = element; - self.counter = 0; -} - -pub fn clicked(self: *Self, event: *z.Object) !void { - var env: z.Env = .init(std.heap.wasm_allocator); - defer env.deinit(); - - self.counter += 1; - - const x = event.get(&env, "x"); - const y = event.get(&env, "y"); - - std.log.info("clicked {} times at ({}, {})", .{ - self.counter, - try x.await(&env, .uint), - try y.await(&env, .uint), - }); -} diff --git a/src/app/components/navbutton/index.css b/src/app/components/navbutton/index.css new file mode 100644 index 0000000..bf1b4ee --- /dev/null +++ b/src/app/components/navbutton/index.css @@ -0,0 +1,9 @@ +#button { + position: absolute; + bottom: 10px; + right: 10px; + width: 50px; + height: 50px; + background: var(--primary); + border-radius: 100%; +} diff --git a/src/app/components/navbutton/index.html b/src/app/components/navbutton/index.html new file mode 100644 index 0000000..ab23160 --- /dev/null +++ b/src/app/components/navbutton/index.html @@ -0,0 +1,2 @@ +
+
diff --git a/src/app/components/navbutton/root.zig b/src/app/components/navbutton/root.zig new file mode 100644 index 0000000..0040c9d --- /dev/null +++ b/src/app/components/navbutton/root.zig @@ -0,0 +1,16 @@ +const z = @import("z"); + +const Self = @This(); + +pub const component: z.Component = .{ + .name = "navbutton", + .html = .parse(@embedFile("index.html")), + .css = @embedFile("index.css"), + .link = @This(), +}; + +element: *z.Object, + +pub fn init(self: *Self, element: *z.Object) void { + self.element = element; +} diff --git a/src/app/components/root.zig b/src/app/components/root.zig new file mode 100644 index 0000000..32ed436 --- /dev/null +++ b/src/app/components/root.zig @@ -0,0 +1,5 @@ +const z = @import("z"); + +pub const components: []const z.Component = &.{ + @import("navbutton/root.zig").component, +}; diff --git a/src/app/index.html b/src/app/index.html new file mode 100644 index 0000000..18c0c6b --- /dev/null +++ b/src/app/index.html @@ -0,0 +1 @@ + diff --git a/src/app/root.zig b/src/app/root.zig index 8ed7f43..2b3554c 100644 --- a/src/app/root.zig +++ b/src/app/root.zig @@ -7,16 +7,16 @@ pub const std_options: std.Options = .{ .logFn = z.log.default, }; +const components = @import("components/root.zig").components; + pub const app: z.App(.{ .name = "Memora", .globals = &.{ z.Object.global(.window), z.Object.global(.Object), }, - .components = &.{ - @import("components/hello/root.zig").component, - }, - .body = .node("app-hello", .{}, &.{}), + .components = components, + .body = .parse(@embedFile("index.html")), .theme = @embedFile("theme.css"), }) = .init; diff --git a/src/app/theme.css b/src/app/theme.css index de933dc..4471dbe 100644 --- a/src/app/theme.css +++ b/src/app/theme.css @@ -2,6 +2,7 @@ :root { --background: #282828; --color: #ebdbb2; + --primary: #726eff; } body { @@ -11,7 +12,3 @@ body { margin: 0; overflow: hidden; } - -div { - background: var(--background); -} diff --git a/src/z/Component.zig b/src/z/Component.zig index 70d6e1d..408a76b 100644 --- a/src/z/Component.zig +++ b/src/z/Component.zig @@ -6,6 +6,7 @@ const Object = @import("Object.zig"); const Self = @This(); +namespace: []const u8 = "app", name: []const u8, html: Fragment, css: []const u8, @@ -172,7 +173,7 @@ pub fn toElement(self: *const Self) []const Element { .{}, &.{.text( std.fmt.comptimePrint( - \\customElements.define("app-{s}", class extends HTMLElement {{ + \\customElements.define("{s}-{s}", class extends HTMLElement {{ \\ constructor() {{ \\ super(); \\ this.attachShadow({{ mode: 'open' }}); @@ -189,7 +190,7 @@ pub fn toElement(self: *const Self) []const Element { \\ }}); \\ }} \\}}) - , .{ self.name, self.name, self.name }), + , .{ self.namespace, self.name, self.name, self.name }), )}, ), }; -- cgit v1.2.3-70-g09d2