A short label shown beside an element while it is hovered.
use gpui::{div, prelude::*};
use crate::ui::button::{Button, ButtonVariant};
use crate::ui::popover::Side;
use crate::ui::tooltip::Tooltip;
pub fn tooltip_demo() -> impl IntoElement {
div()
.flex()
.gap_2()
.child(
Tooltip::new("library-tip", "Add to library")
.trigger(Button::new("library", "Hover").variant(ButtonVariant::Outline)),
)
.child(
Tooltip::new("share-tip", "Copy a share link")
.side(Side::Bottom)
.trigger(Button::new("share", "Below").variant(ButtonVariant::Outline)),
)
}Installation
Copies src/ui/tooltip.rs into your project, along with any components it depends on, and registers the module in src/ui/mod.rs.
Install the theme
Every component reads colors from
crate::theme. Follow the installation guide if you have not added it yet.Copy the source
Paste the code below into
src/ui/tooltip.rs.src/ui/tooltip.rs use gpui::{ anchored, deferred, div, prelude::*, px, AnyElement, App, Bounds, ElementId, FontWeight, Pixels, SharedString, Window, }; use crate::theme::ActiveTheme; use crate::ui::popover::{animate_overlay, placement, Align, Presence, Side, Transition, SLIDE_IN}; /// A short label shown next to its trigger while the pointer is over it. /// /// GPUI's built-in `.tooltip()` places the label at the cursor; this one sits on a fixed side of /// the trigger like shadcn's. #[derive(IntoElement)] pub struct Tooltip { id: ElementId, text: SharedString, trigger: Option<AnyElement>, side: Side, shortcut: Option<SharedString>, } /// Hover is pointer state the app has no reason to own (shadcn leaves tooltips uncontrolled /// too), and the trigger's bounds are only known after layout, so both live here. #[derive(Default)] struct TooltipState { hovered: bool, trigger_bounds: Option<Bounds<Pixels>>, /// Keeps the label up while it fades out. presence: Presence, } impl Tooltip { pub fn new(id: impl Into<ElementId>, text: impl Into<SharedString>) -> Self { Self { id: id.into(), text: text.into(), trigger: None, side: Side::Top, shortcut: None, } } /// The element that shows the tooltip on hover. pub fn trigger(mut self, trigger: impl IntoElement) -> Self { self.trigger = Some(trigger.into_any_element()); self } pub fn side(mut self, side: Side) -> Self { self.side = side; self } /// A keyboard shortcut shown as a key cap after the text, e.g. `"⌘S"`. pub fn shortcut(mut self, keys: impl Into<SharedString>) -> Self { self.shortcut = Some(keys.into()); self } } impl RenderOnce for Tooltip { fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement { let state = window.use_keyed_state(self.id.clone(), cx, |_, _| TooltipState::default()); let (trigger_bounds, frame) = state.update(cx, |state, cx| { let frame = state.presence.update(state.hovered, Transition::POPOVER, window, cx); (state.trigger_bounds, frame) }); let theme = cx.theme(); let side = self.side; let label = trigger_bounds.filter(|_| frame.visible()).map(|bounds| { let (anchor, corner, offset) = placement(self.side, Align::Center); deferred( anchored() .attach(bounds, corner) .anchor(anchor) .offset(offset) .snap_to_window_with_margin(px(8.0)) .child(animate_overlay( div() .flex() .items_center() .gap_2() .px_3() .py(px(6.0)) .rounded(px(6.0)) .bg(theme.primary) .text_color(theme.primary_foreground) .text_xs() .child(self.text) .when_some(self.shortcut, |this, keys| { // A Kbd tinted for the inverted tooltip colors, as shadcn's tooltip-content kbd. this.child( div() .flex() .items_center() .h(px(18.0)) .px(px(5.0)) .rounded(px(4.0)) .bg(theme.primary_foreground.opacity(0.2)) .font_weight(FontWeight::MEDIUM) .child(keys), ) }), side, frame, SLIDE_IN, )), ) .with_priority(3) }); let hover_state = state.clone(); let trigger = div() .id("tooltip-trigger") .on_hover(move |hovered, _, cx| { hover_state.update(cx, |state, cx| { state.hovered = *hovered; cx.notify(); }); }) .children(self.trigger); div() .flex() .on_children_prepainted(move |bounds, _, cx| { state.update(cx, |state, cx| { let first = bounds.first().copied(); if state.trigger_bounds != first { state.trigger_bounds = first; cx.notify(); } }); }) .id(self.id) .child(trigger) .children(label) } }Register the module
// src/ui/mod.rs pub mod tooltip;
Usage
use crate::ui::tooltip::*;Examples
Side
side places the tooltip to the left, top (the default), bottom or right of the trigger.
use gpui::{div, prelude::*};
use crate::ui::button::{Button, ButtonVariant};
use crate::ui::popover::Side;
use crate::ui::tooltip::Tooltip;
pub fn tooltip_side() -> impl IntoElement {
let sides = [("Left", Side::Left), ("Top", Side::Top), ("Bottom", Side::Bottom), ("Right", Side::Right)];
div().flex().gap_2().children(sides.into_iter().map(|(label, side)| {
Tooltip::new(("tooltip-side", side as usize), "Add to library")
.side(side)
.trigger(Button::new(("tooltip-side-trigger", side as usize), label).variant(ButtonVariant::Outline))
}))
}With Keyboard Shortcut
shortcut adds a key cap after the text, so the tooltip also teaches the shortcut.
use gpui::{div, prelude::*};
use crate::ui::button::{Button, ButtonVariant};
use crate::ui::tooltip::Tooltip;
pub fn tooltip_keyboard_shortcut() -> impl IntoElement {
div()
.flex()
.gap_2()
.child(
Tooltip::new("tooltip-shortcut-save", "Save Changes")
.shortcut("⌘S")
.trigger(Button::new("tooltip-shortcut-save-trigger", "Save").variant(ButtonVariant::Outline)),
)
.child(
Tooltip::new("tooltip-shortcut-search", "Search")
.shortcut("⌘K")
.trigger(Button::new("tooltip-shortcut-search-trigger", "Search").variant(ButtonVariant::Outline)),
)
}Disabled Button
The tooltip tracks hover on its own wrapper, so a disabled trigger still shows it. On the web you'd wrap the button in a span; here no wrapper is needed.
use gpui::prelude::*;
use crate::ui::button::{Button, ButtonVariant};
use crate::ui::tooltip::Tooltip;
pub fn tooltip_disabled_button() -> impl IntoElement {
Tooltip::new("tooltip-disabled", "This feature is currently unavailable").trigger(
Button::new("tooltip-disabled-trigger", "Disabled")
.variant(ButtonVariant::Outline)
.disabled(true),
)
}API Reference
Tooltip
A short label shown next to its trigger while the pointer is over it. GPUI's built-in .tooltip() places the label at the cursor; this one sits on a fixed side of the trigger like shadcn's.
Tooltip::new(id: impl Into<ElementId>, text: impl Into<SharedString>) -> Self.trigger(trigger: impl IntoElement)The element that shows the tooltip on hover.
.side(side: Side).shortcut(keys: impl Into<SharedString>)A keyboard shortcut shown as a key cap after the text, e.g. "⌘S".