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