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
use pax_engine::api::Property;
use pax_engine::api::{Click, Event, NavigationTarget};
use pax_engine::*;
use pax_runtime::api::NodeContext;

#[pax]
#[engine_import_path("pax_engine")]
#[inlined(
    for i in 0..self._slot_children {
        slot(i)
    }

    @settings {
        @mount: on_mount
        @click: on_click
    }

)]
pub struct Link {
    pub url: Property<String>,
    pub target: Property<Target>,
    pub _slot_children: Property<usize>,
}

#[pax]
#[engine_import_path("pax_engine")]
pub enum Target {
    #[default]
    Current,
    New,
}

impl From<Target> for NavigationTarget {
    fn from(value: Target) -> Self {
        match value {
            Target::Current => NavigationTarget::Current,
            Target::New => NavigationTarget::New,
        }
    }
}

impl Link {
    pub fn on_mount(&mut self, ctx: &NodeContext) {
        let s = ctx.slot_children_count.clone();
        let deps = [s.untyped()];
        self._slot_children
            .replace_with(Property::computed(move || s.get(), &deps));
    }
    pub fn on_click(&mut self, ctx: &NodeContext, _event: Event<Click>) {
        ctx.navigate_to(&self.url.get(), self.target.get().into());
    }
}