Skip to main content

pax_std/core/
link.rs

1#![allow(unused)]
2use crate::Rectangle;
3use pax_engine::api::cursor::CursorStyle;
4use pax_engine::api::{Click, MouseOut, MouseOver, Property};
5use pax_engine::api::{Event, NavigationTarget};
6use pax_engine::*;
7use pax_runtime::api::NodeContext;
8use pax_runtime::{
9    bind_content_measurement_effect, resolve_axis_autosize, sync_content_autosize_with_axes,
10    ContentMeasurementGeometry,
11};
12
13#[pax]
14#[engine_import_path("pax_engine")]
15#[inlined(
16    <Rectangle id=_link_raycast_ignore fill=TRANSPARENT/>
17    for i in 0..self._projected_children {
18        slot(i)
19    }
20
21    @settings {
22        @mouse_over: self.mouse_over
23        @mouse_out: self.mouse_out
24        @mount: on_mount
25        @click: on_click
26    }
27
28)]
29/// Navigates to a URL when its slotted content is clicked or tapped.
30///
31/// `Link` remains router-agnostic: it writes a URL, while `Router` and `Route`
32/// declaratively read the current location. On web targets, same-origin
33/// `target=Current` navigation can be serviced through client-side history
34/// updates instead of a full document reload.
35pub struct Link {
36    /// Destination URL.
37    pub url: Property<String>,
38    /// Whether to open the URL in the current or a new browsing context.
39    pub target: Property<Target>,
40    /// Automatically sizes the link wrapper to its slotted content when possible.
41    pub autosize: Property<bool>,
42    /// Optional override for whether autosize manages the `x` axis.
43    pub autosize_x: Property<Option<bool>>,
44    /// Optional override for whether autosize manages the `y` axis.
45    pub autosize_y: Property<Option<bool>>,
46    // Number of slotted children to render.
47    pub _projected_children: Property<usize>,
48}
49
50/// Navigation target for `Link`.
51#[pax]
52#[engine_import_path("pax_engine")]
53pub enum Target {
54    /// Navigate in the current window or tab.
55    #[default]
56    Current,
57    /// Navigate in a new window or tab.
58    New,
59}
60
61impl From<Target> for NavigationTarget {
62    fn from(value: Target) -> Self {
63        match value {
64            Target::Current => NavigationTarget::Current,
65            Target::New => NavigationTarget::New,
66        }
67    }
68}
69
70impl Link {
71    // Binds slot count and reactive autosize behavior for the generated inline template.
72    pub fn on_mount(&mut self, ctx: &NodeContext) {
73        let s = ctx.projected_children_count.clone();
74        let deps = [s.untyped()];
75        self._projected_children
76            .replace_with(Property::computed(move || s.get(), &deps));
77        let Some(expanded_node) = ctx.expanded_node.upgrade() else {
78            return;
79        };
80        let autosize = self.autosize.clone();
81        let autosize_x = self.autosize_x.clone();
82        let autosize_y = self.autosize_y.clone();
83        let deps = [
84            autosize.untyped(),
85            autosize_x.untyped(),
86            autosize_y.untyped(),
87        ];
88        bind_content_measurement_effect(
89            &expanded_node,
90            ctx,
91            "link autosize",
92            ContentMeasurementGeometry::Placed,
93            &deps,
94            move |node, node_ctx| {
95                sync_content_autosize_with_axes(
96                    node,
97                    node_ctx,
98                    resolve_axis_autosize(autosize.get(), autosize_x.get(), true),
99                    resolve_axis_autosize(autosize.get(), autosize_y.get(), true),
100                );
101            },
102        );
103    }
104
105    // Dispatches navigation through the active runtime context.
106    pub fn on_click(&mut self, ctx: &NodeContext, _event: Event<Click>) {
107        ctx.navigate_to(&self.url.get(), self.target.get().into());
108    }
109
110    // Uses a pointer cursor while hovering the link.
111    pub fn mouse_over(&mut self, ctx: &NodeContext, _event: Event<MouseOver>) {
112        ctx.set_cursor(CursorStyle::Pointer);
113    }
114
115    // Restores the default cursor after hover.
116    pub fn mouse_out(&mut self, ctx: &NodeContext, _event: Event<MouseOut>) {
117        ctx.set_cursor(CursorStyle::Auto);
118    }
119}