From 4e86e3e18e9b43e442a35361ac82b30dd6c0103a Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Wed, 7 Oct 2026 00:09:34 +0200 Subject: wasm implement components --- src/app/components/hello/index.css | 0 src/app/components/hello/index.html | 0 src/app/components/hello/root.zig | 17 +++++++++++++ src/app/root.zig | 48 ++++--------------------------------- 4 files changed, 22 insertions(+), 43 deletions(-) create mode 100644 src/app/components/hello/index.css create mode 100644 src/app/components/hello/index.html create mode 100644 src/app/components/hello/root.zig (limited to 'src/app') diff --git a/src/app/components/hello/index.css b/src/app/components/hello/index.css new file mode 100644 index 0000000..e69de29 diff --git a/src/app/components/hello/index.html b/src/app/components/hello/index.html new file mode 100644 index 0000000..e69de29 diff --git a/src/app/components/hello/root.zig b/src/app/components/hello/root.zig new file mode 100644 index 0000000..6f68550 --- /dev/null +++ b/src/app/components/hello/root.zig @@ -0,0 +1,17 @@ +const std = @import("std"); +const z = @import("z"); + +pub const component: z.Component = .{ + .name = "hello", + .html = .native( + .div, + .{ .onclick = "@clicked" }, + .content("click me"), + ), + .css = @embedFile("index.css"), + .link = @This(), +}; + +pub fn clicked() void { + z.log.js.debug("hi"); +} diff --git a/src/app/root.zig b/src/app/root.zig index 6de9216..f587cfb 100644 --- a/src/app/root.zig +++ b/src/app/root.zig @@ -13,50 +13,12 @@ pub const app: z.App(.{ z.Object.global(.window), z.Object.global(.Object), }, + .components = &.{ + @import("components/hello/root.zig").component, + }, + .body = .native(.@"app-hello", .{}, .content("")), }) = .init; -fn run_wrapped() !void { - var env: z.Env = .init(std.heap.wasm_allocator); - defer env.deinit(); - - // Set and delete properties - _ = app.globals.window.set(&env, "my_prop", 45); - _ = app.globals.window.delete(&env, "my_prop"); - - // Get properties - const console = try app.globals.window.get(&env, "console").await(&env, .object); - const location = try app.globals.window.get(&env, "location").await(&env, .object); - - // Create new object - const obj = app.globals.Object.new(&env); - - // Get string, needs to be freed. - const hostname = try location.get(&env, "hostname").await(&env, .string); - defer std.heap.wasm_allocator.free(hostname); - - // Call method of an object - try console.call( - &env, - .log, - .{ - "hello, wasm!", - try obj.await(&env), - hostname, - }, - ).await(&env, .undefined); - - // Unlink Object from global store. - _ = env.free(console); -} - -fn run() callconv(.c) void { - run_wrapped() catch |e| { - std.log.err("{}", .{e}); - }; -} - comptime { - if (builtin.target.cpu.arch.isWasm()) { - @export(&run, .{ .name = "run" }); - } + _ = app; } -- cgit v1.2.3-70-g09d2