A rotating indicator for work in progress.
use gpui::{div, prelude::*, px, App, FontWeight, Window};
use crate::theme::ActiveTheme;
use crate::ui::spinner::{Spinner, SpinnerSize};
pub fn spinner_demo(_window: &mut Window, cx: &mut App) -> impl IntoElement {
let theme = cx.theme();
div()
.flex()
.flex_col()
.items_center()
.gap_6()
.child(
div()
.flex()
.items_center()
.gap_3()
.w(px(320.0))
.px_4()
.py_3()
.rounded(theme.radius)
.border_1()
.border_color(theme.border)
.bg(theme.muted.opacity(0.5))
.text_sm()
.child(Spinner::new())
.child(div().flex_1().font_weight(FontWeight::MEDIUM).child("Processing payment"))
.child(div().text_color(theme.muted_foreground).child("$100.00")),
)
.child(
div()
.flex()
.items_center()
.gap_6()
.child(Spinner::new().size(SpinnerSize::Sm))
.child(Spinner::new())
.child(Spinner::new().size(SpinnerSize::Lg))
.child(Spinner::new().size(SpinnerSize::Lg).color(theme.muted_foreground)),
)
}Installation
Copies src/ui/spinner.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.Install the icons
This component draws icons from
crate::icons. Copy src/icons.rs into your project and declaremod icons;next tomod theme;.Copy the source
Paste the code below into
src/ui/spinner.rs.src/ui/spinner.rs use std::time::Duration; use gpui::{ prelude::*, px, svg, App, Hsla, Pixels, TimedTransformation, Transformation, Window, }; use crate::theme::ActiveTheme; use crate::icons::LOADER_CIRCLE; const TURN: Duration = Duration::from_millis(1000); #[derive(Clone, Copy, Default, PartialEq)] pub enum SpinnerSize { Sm, #[default] Md, Lg, } /// A rotating loading indicator. It holds still when the platform asks for reduced motion. #[derive(IntoElement, Default)] pub struct Spinner { size: SpinnerSize, color: Option<Hsla>, icon: Option<&'static [u8]>, } impl Spinner { pub fn new() -> Self { Self::default() } pub fn size(mut self, size: SpinnerSize) -> Self { self.size = size; self } /// Defaults to the theme foreground. Set it to match the surface the spinner sits on, /// such as `theme.primary_foreground` inside a primary button. pub fn color(mut self, color: Hsla) -> Self { self.color = Some(color); self } /// Spins a different SVG instead of Lucide's `loader-circle`. Draw it in an opaque stroke /// or fill; the spinner tints it with its color. pub fn icon(mut self, svg: &'static [u8]) -> Self { self.icon = Some(svg); self } } impl RenderOnce for Spinner { fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement { let size: Pixels = match self.size { SpinnerSize::Sm => px(12.0), SpinnerSize::Md => px(16.0), SpinnerSize::Lg => px(24.0), }; // A timed transformation spins the already-painted sprite each frame instead of // re-rendering the view, so a spinner costs nothing while the rest of the UI idles. svg() .data(self.icon.unwrap_or(LOADER_CIRCLE)) .flex_none() .size(size) .text_color(self.color.unwrap_or(cx.theme().foreground)) .with_timed_transformation(TimedTransformation::rotation(TURN, Transformation::default())) } }Register the module
// src/ui/mod.rs pub mod spinner;
Usage
use crate::ui::spinner::*;Examples
Customization
icon swaps the default loader-circle for any SVG, here Lucide's loader. It spins and takes the spinner's color like the default.
use gpui::{div, prelude::*};
use crate::ui::spinner::{Spinner, SpinnerSize};
use crate::icons::LOADER;
pub fn spinner_customization() -> impl IntoElement {
div()
.flex()
.items_center()
.gap_4()
.child(Spinner::new().icon(LOADER))
.child(Spinner::new().size(SpinnerSize::Lg).icon(LOADER))
}Size
SpinnerSize::Sm is 12px, Md (the default) 16px and Lg 24px.
use gpui::{div, prelude::*};
use crate::ui::spinner::{Spinner, SpinnerSize};
pub fn spinner_size() -> impl IntoElement {
div()
.flex()
.items_center()
.gap_6()
.child(Spinner::new().size(SpinnerSize::Sm))
.child(Spinner::new())
.child(Spinner::new().size(SpinnerSize::Lg))
}Button
A spinner in a button's leading or trailing slot shows that its action is running. Color it to match the button's text.
use gpui::{div, prelude::*, App, Window};
use crate::theme::ActiveTheme;
use crate::ui::button::{Button, ButtonSize, ButtonVariant};
use crate::ui::spinner::Spinner;
pub fn spinner_button(_window: &mut Window, cx: &mut App) -> impl IntoElement {
let theme = cx.theme();
div()
.flex()
.flex_col()
.items_center()
.gap_4()
.child(
Button::new("loading", "Loading...")
.size(ButtonSize::Sm)
.disabled(true)
.leading(Spinner::new().color(theme.primary_foreground)),
)
.child(
Button::new("please-wait", "Please wait")
.variant(ButtonVariant::Outline)
.size(ButtonSize::Sm)
.disabled(true)
.leading(Spinner::new().color(theme.foreground)),
)
.child(
Button::new("processing", "Processing")
.variant(ButtonVariant::Secondary)
.size(ButtonSize::Sm)
.disabled(true)
.trailing(Spinner::new().color(theme.secondary_foreground)),
)
}Badge
A small spinner at the start or end of a badge marks a status that is still in progress.
use gpui::{div, prelude::*, App, Window};
use crate::theme::ActiveTheme;
use crate::ui::badge::{Badge, BadgeVariant};
use crate::ui::spinner::{Spinner, SpinnerSize};
pub fn spinner_badge(_window: &mut Window, cx: &mut App) -> impl IntoElement {
let theme = cx.theme();
let spinner = |color| Spinner::new().size(SpinnerSize::Sm).color(color);
div()
.flex()
.items_center()
.gap_4()
.child(Badge::new("Syncing").leading(spinner(theme.primary_foreground)))
.child(
Badge::new("Updating")
.variant(BadgeVariant::Secondary)
.leading(spinner(theme.secondary_foreground)),
)
.child(
Badge::new("Processing")
.variant(BadgeVariant::Outline)
.trailing(spinner(theme.foreground)),
)
}Input Group
A spinner as an InputGroup addon shows that the value is being checked or sent.
use gpui::{div, prelude::*, px, App, Window};
use crate::theme::ActiveTheme;
use crate::ui::input::TextInput;
use crate::ui::input_group::InputGroup;
use crate::ui::spinner::Spinner;
pub fn spinner_input_group(window: &mut Window, cx: &mut App) -> impl IntoElement {
// Grouped inputs are `.bare()`: the InputGroup draws the border and focus ring.
let message = window.use_keyed_state("spinner-message", cx, |_, cx| {
TextInput::new(cx).placeholder("Send a message...").bare()
});
let muted = cx.theme().muted_foreground;
div().w(px(320.0)).child(
InputGroup::new(message).trailing(
div()
.flex()
.items_center()
.gap_2()
.child("Validating")
.child(Spinner::new().color(muted)),
),
)
}Empty
A spinner as the Empty state's media while a request is being processed, with a way to cancel it.
use gpui::{div, prelude::*, px};
use crate::ui::button::{Button, ButtonSize, ButtonVariant};
use crate::ui::empty::Empty;
use crate::ui::spinner::{Spinner, SpinnerSize};
pub fn spinner_empty() -> impl IntoElement {
div().w(px(400.0)).child(
Empty::new("Processing your request")
.description("Please wait while we process your request. Do not refresh the page.")
.media(Spinner::new().size(SpinnerSize::Lg))
.outline(true)
.child(
Button::new("cancel", "Cancel")
.variant(ButtonVariant::Outline)
.size(ButtonSize::Sm),
),
)
}API Reference
SpinnerSize
SpinnerSize::SmSpinnerSize::MdSpinnerSize::LgSpinner
A rotating loading indicator. It holds still when the platform asks for reduced motion.
Spinner::new() -> Self.size(size: SpinnerSize).color(color: Hsla)Defaults to the theme foreground. Set it to match the surface the spinner sits on, such as theme.primary_foreground inside a primary button.
.icon(svg: &'static [u8])Spins a different SVG instead of Lucide's loader-circle. Draw it in an opaque stroke or fill; the spinner tints it with its color.