A pulsing placeholder block shown while content loads.
use gpui::{div, prelude::*, px};
use crate::ui::skeleton::Skeleton;
pub fn skeleton_demo() -> impl IntoElement {
div()
.flex()
.items_center()
.gap_4()
.child(Skeleton::new("avatar").size(px(48.0)).rounded_full())
.child(
div()
.flex()
.flex_col()
.gap_2()
.child(Skeleton::new("title").w(px(250.0)).h(px(16.0)))
.child(Skeleton::new("subtitle").w(px(200.0)).h(px(16.0))),
)
}Installation
Copies src/ui/skeleton.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/skeleton.rs.src/ui/skeleton.rs use std::time::Duration; use gpui::{ div, prelude::*, Animation, AnimationExt, App, ElementId, Refineable, StyleRefinement, Window, }; use crate::theme::ActiveTheme; /// Tailwind's `animate-pulse`: `pulse 2s cubic-bezier(0.4, 0, 0.6, 1) infinite`. const PULSE: Duration = Duration::from_secs(2); /// The opacity at the `50%` keyframe; the loop starts and ends fully opaque. const PULSE_LOW: f32 = 0.5; /// A pulsing placeholder shown while content loads. Size and shape it like any div: /// `Skeleton::new("avatar").size(px(48.0)).rounded_full()`. #[derive(IntoElement)] pub struct Skeleton { id: ElementId, style: StyleRefinement, } impl Skeleton { /// Each skeleton needs its own id: GPUI keys the pulse animation by it. pub fn new(id: impl Into<ElementId>) -> Self { Self { id: id.into(), style: StyleRefinement::default(), } } } impl Styled for Skeleton { fn style(&mut self) -> &mut StyleRefinement { &mut self.style } } impl RenderOnce for Skeleton { fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement { let theme = cx.theme(); let mut block = div().flex_none().rounded(theme.radius).bg(theme.muted); // Caller styles go on top of the defaults, so `.rounded_full()` wins over the radius. block.style().refine(&self.style); block.with_animation( self.id, // Synced so every skeleton on screen pulses together, like CSS `animate-pulse`. Animation::new(PULSE).repeat_synced().with_easing(pulse), |block, opacity| block.opacity(opacity), ) } } /// Opacity over one loop: 1 to 0.5 and back. CSS applies the timing function to each keyframe /// segment separately, so the curve runs once per half. Reduced motion holds the loop at its /// start, which is fully opaque, as Tailwind's pulse is between loops. fn pulse(delta: f32) -> f32 { let (half, falling) = if delta < 0.5 { (delta * 2.0, true) } else { (delta * 2.0 - 1.0, false) }; let eased = pulse_timing(half); if falling { 1.0 - (1.0 - PULSE_LOW) * eased } else { PULSE_LOW + (1.0 - PULSE_LOW) * eased } } /// `cubic-bezier(0.4, 0, 0.6, 1)` at progress `x`: solves the curve's x for its parameter by /// bisection (x is monotonic for these control points), then returns its y. fn pulse_timing(x: f32) -> f32 { const X1: f32 = 0.4; const X2: f32 = 0.6; let bezier = |t: f32, p1: f32, p2: f32| { let u = 1.0 - t; 3.0 * u * u * t * p1 + 3.0 * u * t * t * p2 + t * t * t }; let x = x.clamp(0.0, 1.0); let (mut lo, mut hi) = (0.0_f32, 1.0_f32); for _ in 0..20 { let mid = (lo + hi) / 2.0; if bezier(mid, X1, X2) < x { lo = mid; } else { hi = mid; } } // y's control points are 0 and 1. bezier((lo + hi) / 2.0, 0.0, 1.0) }Register the module
// src/ui/mod.rs pub mod skeleton;
Usage
use crate::ui::skeleton::*;Examples
Avatar
A round skeleton with two short lines, sized like an avatar beside a name and handle.
use gpui::{div, prelude::*, px};
use crate::ui::skeleton::Skeleton;
pub fn skeleton_avatar() -> impl IntoElement {
div()
.flex()
.items_center()
.gap_3()
.child(Skeleton::new("avatar-image").size(px(40.0)).rounded_full())
.child(
div()
.flex()
.flex_col()
.gap_2()
.child(Skeleton::new("avatar-name").w(px(120.0)).h(px(14.0)))
.child(Skeleton::new("avatar-handle").w(px(80.0)).h(px(12.0))),
)
}Card
Skeletons in place of a card's heading, text and image while the card loads.
use gpui::{div, prelude::*, px, relative};
use crate::ui::card::Card;
use crate::ui::skeleton::Skeleton;
pub fn skeleton_card() -> impl IntoElement {
div().w(px(320.0)).child(
Card::new().child(
div()
.flex()
.flex_col()
.gap_2()
.child(Skeleton::new("card-title").w(relative(0.66)).h(px(16.0)))
.child(Skeleton::new("card-description").w(relative(0.5)).h(px(16.0)))
.child(Skeleton::new("card-image").mt_4().w_full().h(px(160.0))),
),
)
}Text
Full-width lines with a shorter last line stand in for a paragraph.
use gpui::{div, prelude::*, px, relative};
use crate::ui::skeleton::Skeleton;
pub fn skeleton_text() -> impl IntoElement {
div()
.w(px(320.0))
.flex()
.flex_col()
.gap_2()
.child(Skeleton::new("text-1").w_full().h(px(16.0)))
.child(Skeleton::new("text-2").w_full().h(px(16.0)))
.child(Skeleton::new("text-3").w(relative(0.75)).h(px(16.0)))
}Form
Short label bars over input-height blocks, then a button-sized block, while a form loads.
use gpui::{div, prelude::*, px};
use crate::ui::skeleton::Skeleton;
pub fn skeleton_form() -> impl IntoElement {
let field = |id: &'static str, label_width: f32| {
div()
.flex()
.flex_col()
.gap_3()
.child(Skeleton::new((id, 0usize)).w(px(label_width)).h(px(16.0)))
.child(Skeleton::new((id, 1usize)).w_full().h(px(36.0)))
};
div()
.w(px(320.0))
.flex()
.flex_col()
.gap_6()
.child(field("form-name", 80.0))
.child(field("form-email", 128.0))
.child(Skeleton::new("form-submit").w(px(96.0)).h(px(36.0)))
}Table
One row of skeleton cells per table row while the data loads. Each skeleton gets its own id from the row index.
use gpui::{div, prelude::*, px};
use crate::ui::skeleton::Skeleton;
pub fn skeleton_table() -> impl IntoElement {
div()
.w(px(400.0))
.flex()
.flex_col()
.gap_4()
.children((0..5usize).map(|row| {
div()
.flex()
.gap_4()
.child(Skeleton::new(("table-name", row)).flex_1().h(px(16.0)))
.child(Skeleton::new(("table-email", row)).w(px(96.0)).h(px(16.0)))
.child(Skeleton::new(("table-amount", row)).w(px(80.0)).h(px(16.0)))
}))
}API Reference
Skeleton
A pulsing placeholder shown while content loads. Size and shape it like any div: Skeleton::new("avatar").size(px(48.0)).rounded_full().
Skeleton::new(id: impl Into<ElementId>) -> SelfEach skeleton needs its own id: GPUI keys the pulse animation by it.