Skip to main content

tablo_ui/components/primitives/
separator.rs

1// SYNC: topcoat-ui-registry@0.12.1 sha256:ba4ee980966fb66d3f549f26a27a269b1ad85b5e3c39fd9ed047aadfaae5d9bf — do not hand-edit. Sync via `cargo xtask sync-topcoat-ui` (ADR-0007).
2use topcoat::{
3    Result,
4    context::Cx,
5    view::{Attributes, PromotedStr, StaticClass, View, class, component, view},
6};
7
8/// The direction a [`separator`] runs in.
9///
10/// [`Default`] is `SeparatorOrientation::Horizontal`, used when no
11/// orientation is given.
12#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
13#[allow(dead_code)]
14pub enum SeparatorOrientation {
15    /// A rule across the full width of its container.
16    #[default]
17    Horizontal,
18    /// A rule down the full height of its container.
19    Vertical,
20}
21
22impl SeparatorOrientation {
23    /// Classes that set the rule's thickness and stretch it along its orientation.
24    fn classes(self) -> StaticClass {
25        match self {
26            Self::Horizontal => class!("h-px w-full"),
27            Self::Vertical => class!("h-full w-px"),
28        }
29    }
30
31    /// The value of the `aria-orientation` attribute, or `None` for the
32    /// horizontal default assistive technology already assumes.
33    fn aria(self) -> Option<PromotedStr> {
34        match self {
35            Self::Horizontal => None,
36            Self::Vertical => Some(PromotedStr(&"vertical")),
37        }
38    }
39}
40
41/// Classes for a separator that keeps its thickness in a flex layout.
42const SEPARATOR: StaticClass = class!("shrink-0 border-0 bg-border");
43
44/// A thin rule between groups of content.
45///
46/// Uses an `<hr>` element. Its length comes from its container, so a vertical separator
47/// needs a container with a height. Pass `aria-hidden="true"` for a purely decorative
48/// rule. `attrs` are forwarded to the `<hr>`, with extra classes added to its classes.
49///
50/// ```ignore
51/// view! {
52///     <div class="flex flex-col gap-4">
53///         <p>"Everyone with access to this workspace."</p>
54///         separator()
55///         <div class="flex h-5 items-center gap-3">
56///             <a href="/docs">"Docs"</a>
57///             separator(orientation: SeparatorOrientation::Vertical)
58///             <a href="/blog">"Blog"</a>
59///         </div>
60///     </div>
61/// }
62/// ```
63#[component]
64pub async fn separator(
65    cx: &Cx,
66    /// The direction the rule runs in.
67    #[default]
68    orientation: SeparatorOrientation,
69    /// Extra attributes for the `<hr>` element.
70    #[default]
71    mut attrs: Attributes,
72) -> Result<impl View> {
73    Ok(view! {
74        <hr
75            class=(class!(SEPARATOR, orientation.classes(), attrs.remove("class")))
76            aria-orientation=(orientation.aria())
77            (attrs)
78        >
79    })
80}