aboutsummaryrefslogtreecommitdiff
path: root/src/app/components/navbutton/root.zig
blob: 6a403cb7648667f270fdce44e5f6c28da482d9ff (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
46
47
48
49
50
51
52
const std = @import("std");
const app = @import("root").app;
const zilch = @import("zilch");
const object = zilch.Object.bindings;

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;

    var shadow_root = self.element.get(&env, "shadowRoot");

    var button = env.call(object.call, .{shadow_root, "getElementById", "button"});

    var class_list = env.call(object.get, .{button, "classList"});
    self.button.classList = try class_list.await(&env, .object);

    self.shadow_root = try shadow_root.await(&env, .object);
    _ = (try button.await(&env, .object)).free(&env);
}

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"});
}