fc_dioxus/components/
divider.rs1use crate::{
4 core::{
5 assets,
6 class::{Bem, ClassList},
7 state::Orientation,
8 },
9 fc_modifier,
10 theme::use_stylesheets,
11};
12use dioxus::core::AttributeValue;
13use dioxus::prelude::*;
14
15const BEM: Bem = Bem::new("fc-divider");
16
17fc_modifier! {
18 pub enum DividerInset for BEM {
20 None => None,
22 Inset => "inset",
24 Middle => "middle",
26 }
27}
28
29fc_modifier! {
30 enum DividerAxis for BEM {
32 Horizontal => None,
34 Vertical => "vertical",
36 }
37}
38
39#[derive(Props, Clone, PartialEq)]
41pub struct DividerProps {
42 #[props(default)]
44 pub orientation: Orientation,
45 #[props(default)]
47 pub inset: DividerInset,
48 #[props(default)]
50 pub class: Option<String>,
51 #[props(extends = GlobalAttributes)]
53 pub attributes: Vec<Attribute>,
54}
55
56#[component]
58pub fn Divider(props: DividerProps) -> Element {
59 let axis = if props.orientation == Orientation::Vertical {
60 DividerAxis::Vertical
61 } else {
62 DividerAxis::Horizontal
63 };
64 let mut classes = ClassList::new(BEM.block())
65 .with_modifier(BEM, props.inset)
66 .with_modifier(BEM, axis);
67 for attribute in &props.attributes {
68 if attribute.name == "class" {
69 if let AttributeValue::Text(value) = &attribute.value {
70 classes = classes.with(value);
71 }
72 }
73 }
74 let classes = classes.extend(props.class.as_deref()).to_string();
75 let mut attributes: Vec<_> = props
76 .attributes
77 .into_iter()
78 .filter(|attr| {
79 !matches!(
80 attr.name,
81 "class" | "role" | "aria-orientation" | "tabindex"
82 )
83 })
84 .collect();
85 attributes.push(Attribute::new("class", classes, None, false));
86 if props.orientation == Orientation::Vertical {
87 attributes.push(Attribute::new("role", "separator", None, false));
88 attributes.push(Attribute::new("aria-orientation", "vertical", None, false));
89 }
90 rsx! {
91 if use_stylesheets() { document::Stylesheet { href: assets::COMPONENTS_DIVIDER_CSS } }
92 if props.orientation == Orientation::Vertical {
93 div { ..attributes }
94 } else {
95 hr { ..attributes }
96 }
97 }
98}