Skip to main content

yew_nav_link/components/
text.rs

1//! # `NavText`
2//!
3//! Static text label component for navigation items.
4//! Renders a `<span>` with the `nav-text` class.
5//!
6//! # Example
7//!
8//! ```rust
9//! use yew::prelude::*;
10//! use yew_nav_link::{NavItem, NavLink, NavList, NavText};
11//! use yew_router::prelude::*;
12//!
13//! # #[derive(Clone, PartialEq, Routable)]
14//! # enum Route {
15//! #     #[at("/")]
16//! #     Home,
17//! # }
18//! #[component]
19//! fn Nav() -> Html {
20//!     html! {
21//!         <NavList>
22//!             <NavItem><NavLink<Route> to={Route::Home}>{ "Home" }</NavLink<Route>></NavItem>
23//!             <NavText text="v0.6.0" />
24//!         </NavList>
25//!     }
26//! }
27//! ```
28//!
29//! # CSS Classes
30//!
31//! | Class | Condition |
32//! |-------|-----------|
33//! | `nav-text` | Always applied |
34//!
35//! # Props
36//!
37//! | Prop | Type | Default | Description |
38//! |------|------|---------|-------------|
39//! | `text` | `&'static str` | — | Static text content (required) |
40//! | `classes` | `Classes` | — | Additional CSS classes |
41
42use yew::prelude::*;
43
44/// Properties for the [`NavText`] component.
45///
46/// | Prop | Type | Default | Description |
47/// |------|------|---------|-------------|
48/// | `text` | `&'static str` | — | Static text content (required) |
49/// | `classes` | `Classes` | — | Additional CSS classes |
50#[derive(Properties, Clone, PartialEq, Eq, Debug)]
51pub struct NavTextProps {
52    /// Additional CSS classes applied to the text element.
53    #[prop_or_default]
54    pub classes: Classes,
55
56    /// Static text content to display.
57    pub text: &'static str
58}
59
60/// Text label component for navigation items.
61///
62/// Renders a `<span>` with the `nav-text` class.
63#[function_component]
64pub fn NavText(props: &NavTextProps) -> Html {
65    let mut classes = props.classes.clone();
66    classes.push("nav-text");
67
68    html! {
69        <span {classes}>
70            { props.text }
71        </span>
72    }
73}
74
75#[cfg(test)]
76mod tests {
77    use super::*;
78
79    #[test]
80    fn nav_text_props() {
81        let props = NavTextProps {
82            classes: Classes::default(),
83            text:    "Hello"
84        };
85
86        assert_eq!(props.text, "Hello");
87    }
88
89    #[test]
90    fn nav_text_clone() {
91        let props1 = NavTextProps {
92            classes: Classes::from("custom"),
93            text:    "Text"
94        };
95
96        let props2 = props1.clone();
97        assert_eq!(props1.text, props2.text);
98    }
99}