aboutsummaryrefslogtreecommitdiff
path: root/src/app/components/navbutton/root.zig
blob: f2b71e638563b895e864a2e8d587eb8e359262e4 (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
45
const std = @import("std");
const app = @import("root").app;
const zilch = @import("zilch");

const Self = @This();

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

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

pub fn init(self: *Self, element: *zilch.Object) !void {
    var env: zilch.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: zilch.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: zilch.Env = .default;
    defer env.deinit();

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