forge
Explore
Log in

Don't you dare go Hollow

project_note

Clone
★ 0

0 watching0 forksJavaScript3.6 MiBe100df4Tahinli1 month ago1 commit

tooltip.rs92 lines, 3.0 KiBHistoryRawDownload
1use topcoat::{
2 Result,
3 view::{Attributes, Child, StaticClass, View, class, component, view},
4};
5
6use super::floating::{
7 FLOATING_SCOPE, FloatingAlign, FloatingAnchor, FloatingSide, floating_variants,
8};
9
10/// A short hint shown when its trigger is hovered or focused.
11///
12/// Add the trigger and a [`tooltip_content`] as children.
13///
14/// Give the trigger its own text or accessible label. The tooltip must not be the only
15/// way to learn what the trigger does. To associate the hint with the trigger, give
16/// `tooltip_content` an `id` from [`element_id`](topcoat::view::element_id) and
17/// reference it with the trigger's `aria-describedby`.
18///
19/// ```ignore
20/// view! {
21/// tooltip(
22/// button(
23/// size: ButtonSize::Icon,
24/// variant: ButtonVariant::Outline,
25/// icon(data: iconify_icon!("lucide:copy"), label: "Copy link")
26/// )
27/// tooltip_content("Copy link")
28/// )
29/// }
30/// ```
31#[component]
32pub async fn tooltip(
33 #[default] mut attrs: Attributes,
34 #[default] child: Child<'_>,
35) -> Result<impl View> {
36 Ok(view! {
37 <span
38 class=(class!("group inline-flex", FLOATING_SCOPE, attrs.remove("class")))
39 (attrs)
40 >
41 (child)
42 </span>
43 })
44}
45
46/// Styles for a hint that fades in and out and lets pointer events pass through.
47/// Using `display: none` while hidden makes the browser recalculate its position when
48/// it appears. `@starting-style` starts the fade from transparent.
49const BUBBLE: StaticClass = class!(
50 "pointer-events-none hidden z-50 rounded-md bg-foreground px-2.5 py-1",
51 "text-xs font-medium text-background",
52 "opacity-0 shadow-float whitespace-nowrap",
53 "[transition:opacity_150ms_ease-out,display_150ms_allow-discrete]",
54 "group-hover:block group-hover:opacity-100 starting:group-hover:opacity-0",
55 "group-focus-within:block group-focus-within:opacity-100",
56 "starting:group-focus-within:opacity-0",
57);
58
59/// The hint shown beside a [`tooltip`]'s trigger.
60///
61/// Use `side` to choose where the hint appears and `align` to line it up with the
62/// trigger. By default, it appears above the trigger and is centered on it. The browser
63/// tries other positions if there is not enough room in the window.
64#[component]
65pub async fn tooltip_content(
66 /// Where the hint appears relative to the trigger.
67 #[default(FloatingSide::Top)]
68 side: FloatingSide,
69 /// Which edge or center of the trigger the hint lines up with.
70 #[default]
71 align: FloatingAlign,
72 /// Attributes to add to the hint's `<span>`.
73 #[default]
74 mut attrs: Attributes,
75 /// Content shown in the hint.
76 #[default]
77 child: Child<'_>,
78) -> Result<impl View> {
79 Ok(view! {
80 <span
81 role="tooltip"
82 class=(class!(
83 floating_variants(FloatingAnchor::Scope, side, align),
84 BUBBLE,
85 attrs.remove("class"),
86 ))
87 (attrs)
88 >
89 (child)
90 </span>
91 })
92}