A panel that slides in from any edge of the window.
use gpui::{div, prelude::*, Context, Window};
use crate::ui::button::{Button, ButtonVariant};
use crate::ui::sheet::{Sheet, SheetSide};
use crate::ui::switch::Switch;
#[derive(Default)]
pub struct NotificationsView {
/// The side the sheet is open on, or `None` when closed.
open_on: Option<SheetSide>,
mentions: bool,
digest: bool,
}
impl Render for NotificationsView {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let sides = [
("Top", SheetSide::Top),
("Right", SheetSide::Right),
("Bottom", SheetSide::Bottom),
("Left", SheetSide::Left),
];
div()
.flex()
.gap_2()
.children(sides.into_iter().map(|(label, side)| {
Button::new(label, label)
.variant(ButtonVariant::Outline)
.on_click(cx.listener(move |this, _, _, cx| {
this.open_on = Some(side);
cx.notify();
}))
}))
.child(
Sheet::new("notifications", self.open_on.is_some())
.side(self.open_on.unwrap_or_default())
.title("Notifications")
.description("Choose what we email you about.")
.on_open_change(cx.listener(|this, open: &bool, _, cx| {
if !open {
this.open_on = None;
}
cx.notify();
}))
.child(
Switch::new("mentions", self.mentions)
.label("Mentions and replies")
.on_toggle(cx.listener(|this, on: &bool, _, cx| {
this.mentions = *on;
cx.notify();
})),
)
.child(
Switch::new("digest", self.digest)
.label("Weekly digest")
.on_toggle(cx.listener(|this, on: &bool, _, cx| {
this.digest = *on;
cx.notify();
})),
)
.footer(Button::new("save", "Save changes").on_click(cx.listener(
|this, _, _, cx| {
this.open_on = None;
cx.notify();
},
))),
)
}
}Installation
Copies src/ui/sheet.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/sheet.rs.src/ui/sheet.rs use std::{rc::Rc, sync::Arc}; use gpui::{div, prelude::*, px, AnyElement, App, ElementId, FontWeight, SharedString, Window}; use crate::theme::ActiveTheme; use crate::ui::dialog::{close_button, Modal, ModalPlacement, ModalTransition}; type OpenChangeHandler = Rc<dyn Fn(&bool, &mut Window, &mut App) + 'static>; /// The window edge a sheet slides in from. #[derive(Clone, Copy, Default, PartialEq)] pub enum SheetSide { Top, #[default] Right, Bottom, Left, } /// A panel that slides in from a window edge over a dimmed page. The parent owns `open`. #[derive(IntoElement)] pub struct Sheet { id: ElementId, open: bool, side: SheetSide, trigger: Option<AnyElement>, title: Option<SharedString>, description: Option<SharedString>, children: Vec<AnyElement>, footer: Option<AnyElement>, show_close_button: bool, on_open_change: Option<OpenChangeHandler>, } impl Sheet { pub fn new(id: impl Into<ElementId>, open: bool) -> Self { Self { id: id.into(), open, side: SheetSide::default(), trigger: None, title: None, description: None, children: Vec::new(), footer: None, show_close_button: true, on_open_change: None, } } pub fn side(mut self, side: SheetSide) -> Self { self.side = side; self } /// Clicking this element asks the parent to open the sheet. pub fn trigger(mut self, trigger: impl IntoElement) -> Self { self.trigger = Some(trigger.into_any_element()); self } pub fn title(mut self, title: impl Into<SharedString>) -> Self { self.title = Some(title.into()); self } pub fn description(mut self, description: impl Into<SharedString>) -> Self { self.description = Some(description.into()); self } /// Actions pinned to the end of the sheet. pub fn footer(mut self, footer: impl IntoElement) -> Self { self.footer = Some(footer.into_any_element()); self } /// `false` hides the "×" in the top-right corner; the sheet then closes through Escape, the /// scrim, or a button of your own. pub fn show_close_button(mut self, show: bool) -> Self { self.show_close_button = show; self } /// Receives the *next* open value. pub fn on_open_change( mut self, handler: impl Fn(&bool, &mut Window, &mut App) + 'static, ) -> Self { self.on_open_change = Some(Rc::new(handler)); self } } impl ParentElement for Sheet { fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) { self.children.extend(elements); } } impl RenderOnce for Sheet { fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement { let viewport = window.viewport_size(); // Callers often reset the side as they close (`open_on: None`), so keep sliding out // on the side the sheet opened on. let opened_side = window.use_keyed_state( ElementId::NamedChild(Arc::new(self.id.clone()), "side".into()), cx, |_, _| self.side, ); let side = if self.open { opened_side.update(cx, |side, _| *side = self.side); self.side } else { *opened_side.read(cx) }; let theme = cx.theme(); let vertical = matches!(side, SheetSide::Left | SheetSide::Right); // shadcn: three quarters of the window, capped at 24rem. let width = (viewport.width * 0.75).min(px(384.0)); let on_open_change = self.on_open_change; let close = on_open_change.clone(); let panel = div() .relative() .flex() .flex_col() .gap_4() .bg(theme.background) .text_color(theme.foreground) .border_color(theme.border) .shadow_lg() .map(|this| match side { SheetSide::Left => this.w(width).h(viewport.height).border_r_1(), SheetSide::Right => this.w(width).h(viewport.height).border_l_1(), SheetSide::Top => this.w(viewport.width).border_b_1(), SheetSide::Bottom => this.w(viewport.width).border_t_1(), }) .when(self.title.is_some() || self.description.is_some(), |this| { this.child( div() .flex() .flex_col() .gap(px(6.0)) .p_4() .when(self.show_close_button, |this| this.pr_10()) .when_some(self.title, |this, title| { this.child(div().font_weight(FontWeight::SEMIBOLD).child(title)) }) .when_some(self.description, |this, description| { this.child(div().text_sm().text_color(theme.muted_foreground).child(description)) }), ) }) .child(div().flex().flex_col().gap_4().px_4().children(self.children)) .when_some(self.footer, |this, footer| { this.child( div() .flex() .gap_2() .p_4() .when(vertical, |this| this.mt_auto().flex_col()) .child(footer), ) }) .when(self.show_close_button, |this| { this.child(close_button( "sheet-close", move |window, cx| { if let Some(close) = &close { close(&false, window, cx); } }, cx, )) }); let placement = match side { SheetSide::Top => ModalPlacement::Top, SheetSide::Right => ModalPlacement::Right, SheetSide::Bottom => ModalPlacement::Bottom, SheetSide::Left => ModalPlacement::Left, }; // The modal slides the panel in from `side` and back out before unmounting it. Modal::new(self.id, self.open) .placement(placement) .transition(ModalTransition::SHEET) .when_some(self.trigger, |this, trigger| this.trigger(trigger)) .when_some(on_open_change, |this, handler| { this.on_open_change(move |open, window, cx| handler(open, window, cx)) }) .panel(panel) } }Register the module
// src/ui/mod.rs pub mod sheet;
Usage
use crate::ui::sheet::*;Examples
Side
side sets the window edge the sheet opens from: Top, Right (the default), Bottom or Left.
use gpui::{div, prelude::*, Context, Window};
use crate::ui::button::{Button, ButtonVariant};
use crate::ui::field::Field;
use crate::ui::input::TextInput;
use crate::ui::sheet::{Sheet, SheetSide};
#[derive(Default)]
pub struct SheetSideExample {
/// The side the sheet is open on, or `None` when closed.
open_on: Option<SheetSide>,
}
impl Render for SheetSideExample {
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let name = window.use_keyed_state("sheet-side-name", cx, |_, cx| {
TextInput::new(cx).default_value("Pedro Duarte")
});
let username = window.use_keyed_state("sheet-side-username", cx, |_, cx| {
TextInput::new(cx).default_value("@peduarte")
});
let sides = [
("Top", SheetSide::Top),
("Right", SheetSide::Right),
("Bottom", SheetSide::Bottom),
("Left", SheetSide::Left),
];
div()
.flex()
.gap_2()
.children(sides.into_iter().map(|(label, side)| {
Button::new(("sheet-side", side as usize), label)
.variant(ButtonVariant::Outline)
.on_click(cx.listener(move |this, _, _, cx| {
this.open_on = Some(side);
cx.notify();
}))
}))
.child(
Sheet::new("sheet-side", self.open_on.is_some())
.side(self.open_on.unwrap_or_default())
.title("Edit profile")
.description("Make changes to your profile here. Click save when you're done.")
.on_open_change(cx.listener(|this, open: &bool, _, cx| {
if !open {
this.open_on = None;
}
cx.notify();
}))
.child(Field::new().label("Name").child(name))
.child(Field::new().label("Username").child(username))
.footer(
div()
.flex()
.flex_col()
.gap_2()
.child(Button::new("sheet-side-save", "Save changes").on_click(cx.listener(
|this, _, _, cx| {
this.open_on = None;
cx.notify();
},
)))
.child(
Button::new("sheet-side-close", "Close")
.variant(ButtonVariant::Outline)
.on_click(cx.listener(|this, _, _, cx| {
this.open_on = None;
cx.notify();
})),
),
),
)
}
}No Close Button
show_close_button(false) hides the "×". The sheet still closes on Escape or a click on the dimmed page.
use gpui::{div, prelude::*, Context, Window};
use crate::theme::ActiveTheme;
use crate::ui::button::{Button, ButtonVariant};
use crate::ui::sheet::Sheet;
#[derive(Default)]
pub struct SheetNoCloseButton {
open: bool,
}
impl Render for SheetNoCloseButton {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let theme = cx.theme();
Sheet::new("sheet-no-close", self.open)
.trigger(Button::new("sheet-no-close-trigger", "Open Sheet").variant(ButtonVariant::Outline))
.title("No Close Button")
.description("This sheet doesn't have a close button in the top-right corner.")
.show_close_button(false)
.on_open_change(cx.listener(|this, open: &bool, _, cx| {
this.open = *open;
cx.notify();
}))
.child(
div()
.text_sm()
.text_color(theme.muted_foreground)
.child("Press Escape or click outside the sheet to close it."),
)
}
}API Reference
SheetSide
The window edge a sheet slides in from.
SheetSide::TopSheetSide::RightSheetSide::BottomSheetSide::LeftSheet
A panel that slides in from a window edge over a dimmed page. The parent owns open.
Sheet::new(id: impl Into<ElementId>, open: bool) -> Self.side(side: SheetSide).trigger(trigger: impl IntoElement)Clicking this element asks the parent to open the sheet.
.title(title: impl Into<SharedString>).description(description: impl Into<SharedString>).footer(footer: impl IntoElement)Actions pinned to the end of the sheet.
.show_close_button(show: bool)false hides the "×" in the top-right corner; the sheet then closes through Escape, the scrim, or a button of your own.
.on_open_change(handler: impl Fn(&bool, &mut Window, &mut App) + 'static)Receives the *next* open value.