forge
Explore
Log in

Don't you dare go Hollow

project_note

Clone
★ 0

0 watching0 forksRust3.6 MiBc49f5e7Tahinli1 month ago2 commits

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