aboutsummaryrefslogtreecommitdiff
path: root/src/app/components/navbutton/root.zig
blob: b76af435a917c6f649b33b4befc62a2892ee59ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const std = @import("std");
const zs = @import("zeroscript");

const Self = @This();

pub const component: zs.Component = .{
    .name = "navbutton",
    .html = .parse(@embedFile("index.html")),
    .css = @embedFile("index.css"),
    .link = @This(),
};

element: *zs.Object,
shadow_root: *zs.Object,
button: struct {
    classList: *zs.Object,
},

pub fn init(self: *Self, element: *zs.Object) !void {
    var env: zs.Env = .default;
    defer env.deinit();

    self.element = element;
    self.shadow_root = try self.element.get(&env, "shadowRoot").await(&env, .object);
    const button = try self.shadow_root.call(&env, "getElementById", .{"button"}).await(&env, .object);
    defer _ = button.free(&env);
    self.button.classList = try button.get(&env, "classList").await(&env, .object);
}

pub fn deinit(self: *Self) !void {
    var env: zs.Env = .default;
    defer env.deinit();

    _ = self.element.free(&env);
    _ = self.shadow_root.free(&env);
    _ = self.button.classList.free(&env);
}

pub fn open(self: *Self) !void {
    var env: zs.Env = .default;
    defer env.deinit();

    _ = self.button.classList.call(&env, "toggle", .{"open"});
}