1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
use yew::prelude::*;
use super::Container;
use crate::util::Dimension;
/// # A singular dropdown item, child of [NavDropdown]
/// Used as a child of [NavDropdown] to create a dropdown menu.
///
/// See [NavDropdownItemProps] for a listing of properties.
pub struct NavDropdownItem { }
/// # Properties for [NavDropdown]
#[derive(Properties, Clone, PartialEq, Eq)]
pub struct NavDropdownItemProps {
/// Item text
#[prop_or_default]
pub text: AttrValue,
/// Link for the item
#[prop_or_default]
pub url: AttrValue,
}
impl Component for NavDropdownItem {
type Message = ();
type Properties = NavDropdownItemProps;
fn create(_ctx: &Context<Self>) -> Self {
Self {}
}
fn view(&self, ctx: &Context<Self>) -> Html {
let props = ctx.props();
html! {
<li>
<a class="dropdown-item" href={props.url.clone()}>{props.text.clone()}</a>
</li>
}
}
}
/// A dropdown menu, child of [NavBar]. See [NavDropdownProps] for a listing of properties.
#[derive(Clone, PartialEq, Eq)]
pub struct NavDropdown { }
/// Properties for [NavDropdown]
#[derive(Properties, Clone, PartialEq)]
pub struct NavDropdownProps {
#[prop_or_default]
pub children: Children,
/// the id of the link with the dropdown-toggle class, referenced by aria-labelledby
#[prop_or_default]
pub id: AttrValue,
/// If true, menu is expanded (ie visible)
#[prop_or_default]
pub expanded: bool,
/// the text of the link with the dropdown-toggle class
#[prop_or_default]
pub text: AttrValue,
/// Top level path is the currently active one
#[prop_or_default]
pub active: bool
}
impl Component for NavDropdown {
type Message = ();
type Properties = NavDropdownProps;
fn create(_ctx: &Context<Self>) -> Self {
Self { }
}
fn view(&self, ctx: &Context<Self>) -> Html {
let props = ctx.props();
let expanded = String::from(match props.expanded {
true => "true",
false => "false"
});
let mut dropdown_toggle_classes = Classes::new();
dropdown_toggle_classes.push(String::from("nav-link"));
dropdown_toggle_classes.push(String::from("dropdown-toggle"));
if props.active {
dropdown_toggle_classes.push(String::from("active"));
}
html! {
<li class="nav-item dropdown">
<a class={dropdown_toggle_classes} href="#" id={props.id.clone()} role="button" data-bs-toggle="dropdown" aria-expanded={expanded}>
{props.text.clone()}
</a>
<ul class="dropdown-menu" aria-labelledby={props.id.clone()}>
{ for props.children.iter() }
</ul>
</li>
}
}
}
/// # Item of a [NavBar]
/// This typically contains text inside a link
///
/// Refer to [NavItemProperties] for a listing of properties
pub struct NavItem { }
/// Properties for NavItem
#[derive(Properties, Clone, PartialEq)]
pub struct NavItemProperties {
/// If provided, text is inside a link
#[prop_or_default]
pub url: Option<AttrValue>,
/// Link is the currently active one
#[prop_or_default]
pub active: bool,
/// Link is disabled
#[prop_or_default]
pub disabled: bool,
/// Text of the item, ignored if dropdown is Some
#[prop_or_default]
pub text: AttrValue,
/// required for dropdowns
#[prop_or_default]
pub id: AttrValue,
/// dropdown items
#[prop_or_default]
pub children: Children
}
impl Component for NavItem {
type Message = ();
type Properties = NavItemProperties;
fn create(_ctx: &Context<Self>) -> Self {
Self {}
}
fn view(&self, ctx: &Context<Self>) -> Html {
let props = ctx.props();
match &props.children.is_empty() {
true => {
let mut classes = Classes::new();
classes.push(String::from("nav-link"));
if props.active {
classes.push(String::from("active"));
}
if props.disabled {
classes.push(String::from("disabled"));
}
match props.disabled {
true => {
html! {
<li class="nav-item">
<a class={classes} tabindex="-1" aria-disabled="true" href={props.url.clone()}>
{props.text.clone()}
</a>
</li>
}
},
false => {
html! {
<li class="nav-item">
<a class={classes} href={props.url.clone()}>
{props.text.clone()}
</a>
</li>
}
}
}
},
false => {
html! {
<NavDropdown text={props.text.clone()} id={props.id.clone()} active={props.active}>
{ for props.children.iter() }
</NavDropdown>
}
}
}
}
}
/// # Brand type for a [NavBar]
///
/// This can contain a text, icon, image or combined (text and image)
#[derive(Clone, PartialEq, Eq)]
pub enum BrandType {
/// Text with optional link
BrandSimple {
text: AttrValue, url: Option<AttrValue> },
/// a brand icon is a bootstrap icon, requiring bootstrap-icons to be imported;
/// see [crate::util::include_cdn_icons]
BrandIcon { icon: AttrValue, text: AttrValue, url: Option<AttrValue> },
/// Image with optional dimensions, link and descriptive text
BrandImage {
/// browser-accessible url to the brand image
image_url: AttrValue,
/// descriptive text for screen reader users
alt: AttrValue,
dimension: Option<Dimension>
},
/// Combined image and text with URL
BrandCombined {
text: AttrValue,
/// hyperlink destination for brand text
url: Option<AttrValue>,
/// browser-accessible url to the brand image
image_url: AttrValue,
/// descriptive text for screen reader users
alt: AttrValue,
dimension: Option<Dimension>
}
}
/// # Navbar component, parent of [NavItem], [NavDropdown], and [NavDropdownItem]
/// The navbar is a responsive horizontal menu bar that can contain links, dropdowns, and text.
/// We have broken up this component into several sub-components to make it easier to use: [NavItem], [NavDropdown], and [NavDropdownItem].
/// The brand property is set using the [BrandType] enum.
///
/// See [NavBarProps] for more information on properties supported by this component.
/// # Example
/// ```rust
/// use yew::prelude::*;
/// use yew_bootstrap::component::{BrandType, NavBar, NavDropdownItem, NavItem};
///
/// fn test() -> Html {
/// let brand = BrandType::BrandSimple {
/// text: AttrValue::from("Yew Bootstrap"),
/// url: Some(AttrValue::from("https://yew.rs"))
/// };
/// html!{
/// <NavBar nav_id={"test-nav"} class="navbar-expand-lg navbar-light bg-light" brand={brand}>
/// <NavItem text="Home" url={AttrValue::from("/")} />
/// <NavItem text="more">
/// <NavDropdownItem text="dropdown item 1" url={AttrValue::from("/dropdown1")} />
/// </NavItem>
/// </NavBar>
/// }
/// }
/// ```
pub struct NavBar { }
/// Properties for [NavBar]
#[derive(Properties, Clone, PartialEq)]
pub struct NavBarProps {
#[prop_or_default]
pub children: Children,
/// CSS class
#[prop_or_default]
pub class: AttrValue,
/// the id of the div that contains the nav-items
#[prop_or_default]
pub nav_id: AttrValue,
/// Navbar is expanded. Used to notify assitive technologies via aria-expanded
#[prop_or_default]
pub expanded: bool,
/// Brand type, see [BrandType]
#[prop_or_default]
pub brand: Option<BrandType>,
/// Callback when brand is clicked
#[prop_or_default]
pub brand_callback: Callback<MouseEvent>
}
impl Component for NavBar {
type Message = ();
type Properties = NavBarProps;
fn create(_ctx: &Context<Self>) -> Self {
Self {}
}
fn view(&self, ctx: &Context<Self>) -> Html {
let props = ctx.props();
let expanded = String::from(match &props.expanded {
true => {
"true"
},
false => {
"false"
}
});
let mut classes = Classes::new();
classes.push("navbar");
classes.push(props.class.to_string());
let brand = match &props.brand {
None => html!{},
Some(b) => {
match b {
BrandType::BrandSimple{text, url} => {
let url = match url {
Some(u) => u.clone(),
None => AttrValue::from("#")
};
html!{
<a class="navbar-brand" href={url} onclick={props.brand_callback.clone()}>
{text.clone()}
</a>
}
},
BrandType::BrandIcon { text, icon, url } => {
let url = match url {
Some(u) => u.clone(),
None => AttrValue::from("#")
};
html! {
<a class="navbar-brand" href={url} onclick={props.brand_callback.clone()}>
<i class={format!("bi-{}", icon)}></i>
{text.clone()}
</a>
}
}
BrandType::BrandImage { image_url, alt, dimension } => {
match dimension {
None => {
html! {
<a class="navbar-brand" href={"#"} onclick={props.brand_callback.clone()}>
<img src={image_url.clone()} alt={alt.clone()} class="d-inline-block align-text-top" />
</a>
}
}
Some(Dimension{width, height}) => {
html! {
<a class="navbar-brand" href={"#"} onclick={props.brand_callback.clone()}>
<img src={image_url.clone()} alt={alt.clone()} width={width.clone()} height={height.clone()} class="d-inline-block align-text-top" />
</a>
}
}
}
}
BrandType::BrandCombined { text, url, image_url, alt, dimension } => {
let url = match url {
Some(u) => u.clone(),
None => AttrValue::from("#")
};
match dimension {
None => {
html! {
<a class="navbar-brand" href={url} onclick={props.brand_callback.clone()}>
<img src={image_url.clone()} alt={alt.clone()} class="d-inline-block align-text-top" />
{text.clone()}
</a>
}
},
Some(Dimension{width, height}) => {
html! {
<a class="navbar-brand" href={url} onclick={props.brand_callback.clone()}>
<img src={image_url.clone()} alt={alt.clone()} width={width.clone()} height={height.clone()} class="d-inline-block align-text-top" />
{text.clone()}
</a>
}
}
}
}
}
}
};
html! {
<nav class={classes}>
<Container fluid=true>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target={format!("#{}", props.nav_id.clone())} aria-controls={props.nav_id.clone()} aria-expanded={expanded} aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
{brand}
<div class="collapse navbar-collapse" id={props.nav_id.clone()}>
<ul class="navbar-nav">
{ for props.children.clone() }
</ul>
</div>
</Container>
</nav>
}
}
}