gpui_router/
nav_link.rs

1use crate::use_navigate;
2use gpui::*;
3use smallvec::SmallVec;
4
5/// A navigation link that changes the route when clicked.
6pub fn nav_link() -> impl IntoElement {
7  NavLink::new().active(|style| style)
8}
9
10/// A navigation link that changes the route when clicked.
11#[derive(IntoElement)]
12pub struct NavLink {
13  base: Div,
14  children: SmallVec<[AnyElement; 1]>,
15  to: SharedString,
16  // is_active: bool,
17}
18
19impl Default for NavLink {
20  fn default() -> Self {
21    Self {
22      base: div(),
23      children: Default::default(),
24      to: Default::default(),
25    }
26  }
27}
28
29impl Styled for NavLink {
30  fn style(&mut self) -> &mut StyleRefinement {
31    self.base.style()
32  }
33}
34
35impl ParentElement for NavLink {
36  fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
37    self.children.extend(elements);
38  }
39}
40
41impl InteractiveElement for NavLink {
42  fn interactivity(&mut self) -> &mut gpui::Interactivity {
43    self.base.interactivity()
44  }
45}
46
47impl NavLink {
48  pub fn new() -> Self {
49    Default::default()
50  }
51
52  /// Sets the destination route for the navigation link.
53  pub fn to(mut self, to: impl Into<SharedString>) -> Self {
54    self.to = to.into();
55    self
56  }
57
58  /// Sets the style for the active state of the navigation link.
59  pub fn active(self, _f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
60    unimplemented!()
61  }
62}
63
64impl RenderOnce for NavLink {
65  fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
66    self
67      .base
68      .id(ElementId::from(self.to.clone()))
69      .on_click(move |_, window, cx| {
70        let mut navigate = use_navigate(cx);
71        navigate(self.to.clone());
72        window.refresh();
73      })
74      .children(self.children)
75  }
76}