A placeholder for views with nothing to show yet, with media, text and actions.
use gpui::{prelude::*, px};
use crate::ui::avatar::Avatar;
use crate::ui::button::{Button, ButtonSize, ButtonVariant};
use crate::ui::empty::Empty;
pub fn empty_demo() -> impl IntoElement {
Empty::new("User offline")
.description("This user is currently offline. Leave a message and they'll see it when they're back.")
.media(Avatar::new("HK").size(px(48.0)))
.outline(true)
.child(Button::new("message", "Leave message").size(ButtonSize::Sm))
.child(
Button::new("retry", "Try again")
.variant(ButtonVariant::Outline)
.size(ButtonSize::Sm),
)
}Installation
Copies src/ui/empty.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/empty.rs.src/ui/empty.rs use gpui::{div, prelude::*, px, AnyElement, App, FontWeight, SharedString, Window}; use crate::theme::ActiveTheme; /// A placeholder for a view with nothing to show yet: optional media, a title, a description /// and actions added with `.child(...)`. #[derive(IntoElement)] pub struct Empty { title: SharedString, description: Option<SharedString>, media: Option<AnyElement>, outline: bool, children: Vec<AnyElement>, } impl Empty { pub fn new(title: impl Into<SharedString>) -> Self { Self { title: title.into(), description: None, media: None, outline: false, children: Vec::new(), } } pub fn description(mut self, description: impl Into<SharedString>) -> Self { self.description = Some(description.into()); self } /// An icon, avatar or image shown above the title. pub fn media(mut self, media: impl IntoElement) -> Self { self.media = Some(media.into_any_element()); self } /// Draws a dashed border around the area, for drop zones and empty panels. pub fn outline(mut self, outline: bool) -> Self { self.outline = outline; self } } impl ParentElement for Empty { fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) { self.children.extend(elements); } } impl RenderOnce for Empty { fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement { let theme = cx.theme(); let has_actions = !self.children.is_empty(); div() .flex() .flex_col() .items_center() .justify_center() .gap_6() .p(px(32.0)) .rounded(theme.radius * 1.5) .when(self.outline, |this| { this.border_1().border_dashed().border_color(theme.border) }) .child( div() .max_w(px(384.0)) .flex() .flex_col() .items_center() .gap_2() .when_some(self.media, |this, media| this.child(div().mb_2().child(media))) .child( div() .text_lg() .font_weight(FontWeight::MEDIUM) .text_color(theme.foreground) .child(self.title), ) .when_some(self.description, |this, description| { this.child( div() .text_sm() .text_center() .text_color(theme.muted_foreground) .child(description), ) }), ) .when(has_actions, |this| { this.child(div().flex().items_center().gap_2().children(self.children)) }) } }Register the module
// src/ui/mod.rs pub mod empty;
Usage
use crate::ui::empty::*;Examples
Outline
outline(true) draws a dashed border around the area, the usual look for a drop zone.
use gpui::{div, prelude::*, px, svg, App, Window};
use crate::theme::ActiveTheme;
use crate::ui::button::{Button, ButtonSize, ButtonVariant};
use crate::ui::empty::Empty;
use crate::icons::CLOUD;
pub fn empty_outline(_window: &mut Window, cx: &mut App) -> impl IntoElement {
let theme = cx.theme();
let icon = div()
.size(px(40.0))
.flex()
.items_center()
.justify_center()
.rounded(theme.radius)
.bg(theme.muted)
.child(svg().data(CLOUD).size(px(24.0)).text_color(theme.foreground));
div().w(px(440.0)).child(
Empty::new("Cloud Storage Empty")
.description("Upload files to your cloud storage to access them anywhere.")
.media(icon)
.outline(true)
.child(
Button::new("empty-outline-upload", "Upload Files")
.variant(ButtonVariant::Outline)
.size(ButtonSize::Sm),
),
)
}Background
Wrap the empty state in a rounded container with a solid or gradient fill to set it apart from the page.
use gpui::{div, linear_color_stop, linear_gradient, prelude::*, px, svg, App, Window};
use crate::theme::ActiveTheme;
use crate::ui::button::{Button, ButtonSize, ButtonVariant};
use crate::ui::empty::Empty;
use crate::icons::BELL;
pub fn empty_background(_window: &mut Window, cx: &mut App) -> impl IntoElement {
let theme = cx.theme();
let icon = div()
.size(px(40.0))
.flex()
.items_center()
.justify_center()
.rounded(theme.radius)
.bg(theme.background)
.border_1()
.border_color(theme.border)
.child(svg().data(BELL).size(px(24.0)).text_color(theme.foreground));
div()
.w(px(440.0))
.rounded(theme.radius * 1.5)
.bg(linear_gradient(
180.0,
linear_color_stop(theme.muted.opacity(0.6), 0.0),
linear_color_stop(theme.background, 1.0),
))
.child(
Empty::new("No Notifications")
.description("You're all caught up. New notifications will appear here.")
.media(icon)
.child(
Button::new("empty-background-refresh", "Refresh")
.variant(ButtonVariant::Outline)
.size(ButtonSize::Sm),
),
)
}Avatar
Pass an Avatar as the media when the empty state is about a person.
use gpui::{div, prelude::*, px};
use crate::ui::avatar::Avatar;
use crate::ui::button::{Button, ButtonSize};
use crate::ui::empty::Empty;
pub fn empty_avatar() -> impl IntoElement {
div().w(px(440.0)).child(
Empty::new("User Offline")
.description("This user is currently offline. You can leave a message to notify them.")
.media(Avatar::new("LR").size(px(48.0)))
.child(Button::new("empty-avatar-message", "Leave Message").size(ButtonSize::Sm)),
)
}Avatar Group
Pass an AvatarGroup as the media to show who the empty space is waiting on.
use gpui::{div, prelude::*, px};
use crate::ui::avatar::{Avatar, AvatarGroup};
use crate::ui::button::{Button, ButtonSize};
use crate::ui::empty::Empty;
pub fn empty_avatar_group() -> impl IntoElement {
div().w(px(440.0)).child(
Empty::new("No Team Members")
.description("Invite your team to collaborate on this project.")
.media(
AvatarGroup::new()
.child(Avatar::new("CN"))
.child(Avatar::new("ML"))
.child(Avatar::new("ER")),
)
.child(Button::new("empty-avatar-group-invite", "Invite Members").size(ButtonSize::Sm)),
)
}InputGroup
Put a search field among the actions so people can find what they were after, as on a 404 page.
use gpui::{div, prelude::*, px, svg, App, Window};
use crate::theme::ActiveTheme;
use crate::ui::empty::Empty;
use crate::ui::input::TextInput;
use crate::ui::input_group::InputGroup;
use crate::ui::kbd::Kbd;
use crate::icons::SEARCH;
pub fn empty_input_group(window: &mut Window, cx: &mut App) -> impl IntoElement {
// Grouped inputs are `.bare()`: the InputGroup draws the border and focus ring.
let search = window.use_keyed_state("empty-input-group-search", cx, |_, cx| {
TextInput::new(cx).placeholder("Try searching for pages...").bare()
});
let theme = cx.theme();
div().w(px(440.0)).child(
Empty::new("404 - Not Found")
.description(
"The page you're looking for doesn't exist. Try searching for what you need below.",
)
.child(
div()
.w(px(300.0))
.flex()
.flex_col()
.items_center()
.gap_4()
.child(
div().w_full().child(
InputGroup::new(search)
.leading(
svg()
.data(SEARCH)
.size(px(16.0))
.text_color(theme.muted_foreground),
)
.trailing(Kbd::new("/")),
),
)
.child(
div()
.text_sm()
.text_color(theme.muted_foreground)
.child("Need help? Contact support"),
),
),
)
}API Reference
Empty
A placeholder for a view with nothing to show yet: optional media, a title, a description and actions added with .child(...).
Empty::new(title: impl Into<SharedString>) -> Self.description(description: impl Into<SharedString>).media(media: impl IntoElement)An icon, avatar or image shown above the title.
.outline(outline: bool)Draws a dashed border around the area, for drop zones and empty panels.