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