forge
Explore
Log in

Don't you dare go Hollow

project_note

Clone
★ 0

0 watching0 forksRust3.6 MiBc49f5e7Tahinli1 month ago2 commits

floating.rs167 lines, 6.2 KiBHistoryRawDownload
1use topcoat::view::{Class, StaticClass, class};
2
3/// Where a floating panel appears relative to the element it follows.
4///
5/// The default is [`Bottom`](Self::Bottom).
6#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
7#[allow(dead_code)]
8pub enum FloatingSide {
9 /// Place the panel above the anchor.
10 Top,
11 /// Place the panel to the anchor's right.
12 Right,
13 /// Place the panel below the anchor.
14 #[default]
15 Bottom,
16 /// Place the panel to the anchor's left.
17 Left,
18}
19
20impl FloatingSide {
21 /// Sets the gap from the anchor and the fallback positions. If the panel does not
22 /// fit, the browser tries flipping its side, its alignment, then both. Panels on
23 /// the left or right can also move above or below the anchor.
24 fn classes(self) -> StaticClass {
25 match self {
26 Self::Top => class!(
27 "mb-1 [position-try-fallbacks:flip-block,flip-inline,flip-block_flip-inline]",
28 ),
29 Self::Right => class!(
30 "ml-1 [position-try-fallbacks:flip-inline,flip-block,flip-inline_flip-block,",
31 "top_span-left,bottom_span-left]",
32 ),
33 Self::Bottom => class!(
34 "mt-1 [position-try-fallbacks:flip-block,flip-inline,flip-block_flip-inline]",
35 ),
36 Self::Left => class!(
37 "mr-1 [position-try-fallbacks:flip-inline,flip-block,flip-inline_flip-block,",
38 "top_span-right,bottom_span-right]",
39 ),
40 }
41 }
42}
43
44/// Which edge or center of the anchor a floating panel lines up with.
45///
46/// The default is [`Center`](Self::Center).
47#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
48#[allow(dead_code)]
49pub enum FloatingAlign {
50 /// Align the panel with the anchor's left or top edge.
51 Start,
52 /// Center the panel on the anchor.
53 #[default]
54 Center,
55 /// Align the panel with the anchor's right or bottom edge.
56 End,
57}
58
59impl FloatingAlign {
60 /// Chooses the CSS `position-area` for this side and alignment.
61 fn area(self, side: FloatingSide) -> StaticClass {
62 match (side, self) {
63 (FloatingSide::Top, Self::Start) => class!("[position-area:top_span-right]"),
64 (FloatingSide::Top, Self::Center) => class!("[position-area:top]"),
65 (FloatingSide::Top, Self::End) => class!("[position-area:top_span-left]"),
66 (FloatingSide::Right, Self::Start) => class!("[position-area:right_span-bottom]"),
67 (FloatingSide::Right, Self::Center) => class!("[position-area:right]"),
68 (FloatingSide::Right, Self::End) => class!("[position-area:right_span-top]"),
69 (FloatingSide::Bottom, Self::Start) => class!("[position-area:bottom_span-right]"),
70 (FloatingSide::Bottom, Self::Center) => class!("[position-area:bottom]"),
71 (FloatingSide::Bottom, Self::End) => class!("[position-area:bottom_span-left]"),
72 (FloatingSide::Left, Self::Start) => class!("[position-area:left_span-bottom]"),
73 (FloatingSide::Left, Self::Center) => class!("[position-area:left]"),
74 (FloatingSide::Left, Self::End) => class!("[position-area:left_span-top]"),
75 }
76 }
77}
78
79/// The element a floating panel follows.
80#[derive(Clone, Copy, Debug, PartialEq, Eq)]
81#[allow(dead_code)]
82pub enum FloatingAnchor {
83 /// Follow the button that opened the panel through `popovertarget`. The panel
84 /// needs a `popover` attribute. It appears above the page, where parent containers
85 /// cannot cut it off.
86 Invoker,
87 /// Follow the nearest parent or ancestor with [`FLOATING_SCOPE`] classes. Use this
88 /// for panels shown without the popover API, such as hints shown on hover. Parent
89 /// overflow settings do not cut off the panel unless a parent also uses a CSS
90 /// transform, filter, or containment.
91 Scope,
92}
93
94impl FloatingAnchor {
95 /// Positions the panel relative to its anchor. For popovers, also removes the
96 /// browser's default centering and adds a fade when the panel opens.
97 fn classes(self) -> StaticClass {
98 match self {
99 Self::Invoker => class!(
100 "inset-auto m-0 [position-anchor:auto]",
101 "transition-opacity starting:open:opacity-0 motion-reduce:transition-none",
102 ),
103 Self::Scope => class!("fixed [position-anchor:--floating-anchor]"),
104 }
105 }
106}
107
108/// Add these classes to the element that [`FloatingAnchor::Scope`] panels should follow.
109///
110/// Place the panels inside this element. Each copy of the element positions its own
111/// panels independently.
112pub const FLOATING_SCOPE: StaticClass =
113 class!("[anchor-name:--floating-anchor] [anchor-scope:--floating-anchor]");
114
115/// Returns CSS classes that position a panel next to its anchor.
116///
117/// `anchor` chooses the element to follow, `side` chooses where the panel appears,
118/// and `align` chooses how they line up. The browser tries other positions if the
119/// panel does not fit in the window. For a panel opened by a button:
120///
121/// ```ignore
122/// view! {
123/// let filters = element_id(cx);
124/// <button type="button" popovertarget=(&filters)>"Filters"</button>
125/// <div
126/// id=(&filters)
127/// popover="auto"
128/// class=(floating_variants(
129/// FloatingAnchor::Invoker,
130/// FloatingSide::Right,
131/// FloatingAlign::Start,
132/// ))
133/// >
134/// (filter_form)
135/// </div>
136/// }
137/// ```
138///
139/// To show a hint when its parent is hovered, make that parent the anchor:
140///
141/// ```ignore
142/// view! {
143/// <span class=(class!("group inline-flex", FLOATING_SCOPE))>
144/// (trigger)
145/// <span
146/// class=(class!(
147/// floating_variants(
148/// FloatingAnchor::Scope,
149/// FloatingSide::Top,
150/// FloatingAlign::Center,
151/// ),
152/// "hidden group-hover:block",
153/// ))
154/// >
155/// (hint)
156/// </span>
157/// </span>
158/// }
159/// ```
160#[must_use]
161pub fn floating_variants(
162 anchor: FloatingAnchor,
163 side: FloatingSide,
164 align: FloatingAlign,
165) -> Class<(StaticClass, StaticClass, StaticClass)> {
166 class!(anchor.classes(), side.classes(), align.area(side))
167}