A button showing the chosen date that opens a calendar in a popover.
use gpui::{div, prelude::*, px, Context, Window};
use crate::ui::calendar::CivilDate;
use crate::ui::date_picker::DatePicker;
/// Fixed so the preview looks the same every day; pass your app's real date instead.
const TODAY: CivilDate = CivilDate { year: 2026, month: 11, day: 12 };
pub struct DatePickerView {
open: bool,
date: Option<CivilDate>,
month: CivilDate,
}
impl Default for DatePickerView {
fn default() -> Self {
Self {
open: false,
date: None,
month: TODAY,
}
}
}
impl Render for DatePickerView {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
// Pinned to the top of the preview so the open calendar has room below it.
div().h(px(312.0)).child(
DatePicker::new("due-date", self.open, self.date, self.month)
.today(Some(TODAY))
.on_open_change(cx.listener(|this, open: &bool, _, cx| {
this.open = *open;
// Reopen on the chosen month, not wherever the user last browsed to.
if let Some(date) = this.date.filter(|_| *open) {
this.month = date;
}
cx.notify();
}))
.on_select(cx.listener(|this, date: &CivilDate, _, cx| {
this.date = Some(*date);
cx.notify();
}))
.on_month_change(cx.listener(|this, month: &CivilDate, _, cx| {
this.month = *month;
cx.notify();
})),
)
}
}Installation
Copies src/ui/date_picker.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/date_picker.rs.src/ui/date_picker.rs use std::{rc::Rc, sync::Arc}; use gpui::{ div, point, prelude::*, px, svg, App, BoxShadow, ElementId, FocusHandle, KeyDownEvent, Pixels, SharedString, Window, }; use crate::theme::{focus_visible, ActiveTheme, FocusRing}; use crate::ui::calendar::{Calendar, CivilDate, Weekday, CALENDAR_WIDTH}; use crate::ui::popover::{return_focus, Align, Popover, Side}; use crate::icons::CALENDAR; type DateHandler = Rc<dyn Fn(&CivilDate, &mut Window, &mut App) + 'static>; type OpenHandler = Rc<dyn Fn(&bool, &mut Window, &mut App) + 'static>; /// A button showing the chosen date that opens a calendar in a popover. The parent view owns /// `open`, the selected date and the month the calendar shows; picking a day closes it. /// The trigger is a tab stop; Enter, Space or the down arrow open the calendar. #[derive(IntoElement)] pub struct DatePicker { id: ElementId, open: bool, selected: Option<CivilDate>, month: CivilDate, today: Option<CivilDate>, week_starts_on: Weekday, placeholder: SharedString, width: Pixels, invalid: bool, focus_handle: Option<FocusHandle>, on_select: Option<DateHandler>, on_open_change: Option<OpenHandler>, on_month_change: Option<DateHandler>, } impl DatePicker { /// `month` is any day in the month the calendar shows when open. pub fn new( id: impl Into<ElementId>, open: bool, selected: Option<CivilDate>, month: CivilDate, ) -> Self { Self { id: id.into(), open, selected, month, today: None, week_starts_on: Weekday::default(), placeholder: "Pick a date".into(), width: px(240.0), invalid: false, focus_handle: None, on_select: None, on_open_change: None, on_month_change: None, } } pub fn today(mut self, today: Option<CivilDate>) -> Self { self.today = today; self } pub fn week_starts_on(mut self, weekday: Weekday) -> Self { self.week_starts_on = weekday; self } pub fn placeholder(mut self, placeholder: impl Into<SharedString>) -> Self { self.placeholder = placeholder.into(); self } /// Draws a destructive border and ring, like `aria-invalid` on shadcn/ui's trigger. pub fn invalid(mut self, invalid: bool) -> Self { self.invalid = invalid; self } /// Focus the trigger through this handle, such as `form.focus_handle(..)` or one from /// `cx.focus_handle()`, to move focus to it from code. Without one it keeps its own. pub fn focus_handle(mut self, handle: FocusHandle) -> Self { self.focus_handle = Some(handle); self } /// The trigger's width. pub fn width(mut self, width: impl Into<Pixels>) -> Self { self.width = width.into(); self } /// Receives the picked day. The picker then asks to close through `on_open_change`. pub fn on_select(mut self, handler: impl Fn(&CivilDate, &mut Window, &mut App) + 'static) -> Self { self.on_select = Some(Rc::new(handler)); 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 } /// Receives the first day of the month to show next. pub fn on_month_change( mut self, handler: impl Fn(&CivilDate, &mut Window, &mut App) + 'static, ) -> Self { self.on_month_change = Some(Rc::new(handler)); self } } impl RenderOnce for DatePicker { fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement { // A RenderOnce picker is rebuilt every frame, but focus has to stay on the same handle, // so it keeps its own under its id unless the caller passes one. let focus = self .focus_handle .unwrap_or_else(|| { let key = ElementId::NamedChild(Arc::new(self.id.clone()), "focus".into()); window.use_keyed_state(key, cx, |_, cx| cx.focus_handle()).read(cx).clone() }) .tab_stop(true); // Like CSS `:focus-visible`: the ring shows for keyboard use and after the form moves focus // here, not after a click. let focus_visible = focus_visible(&focus, window, cx); let theme = cx.theme(); let ring = if self.invalid { Some(theme.destructive.opacity(0.2)) } else { focus_visible.then(|| theme.ring.opacity(0.5)) }; let open = self.open; let open_handler = self.on_open_change.clone(); let (label, label_color) = match self.selected { Some(date) => ( SharedString::from(format!("{} {}, {}", date.month_name(), date.day, date.year)), theme.foreground, ), None => (self.placeholder, theme.muted_foreground), }; // Styled like an outline button; `Button` takes only a text label, not an icon. let trigger = div() .id("date-picker-trigger") .flex() .items_center() .gap_2() .w(self.width) .h(px(36.0)) .px(px(12.0)) .rounded(theme.radius) .border_1() .border_color(match (self.invalid, focus_visible) { (true, _) => theme.destructive, (false, true) => theme.ring, (false, false) => theme.input, }) .bg(theme.background) // The opaque background keeps the ring outside the border. .map(|this| match ring { Some(color) => this.shadow(vec![BoxShadow { color, offset: point(px(0.), px(0.)), blur_radius: px(0.), spread_radius: px(3.), inset: false, }]), None => this.shadow_xs(), }) .text_sm() .text_color(label_color) .cursor_pointer() .hover(|style| style.bg(theme.accent)) .child(svg().data(CALENDAR).flex_none().size(px(16.0)).text_color(label_color)) .child(label); let on_select = self.on_select; let close = self.on_open_change.clone(); let refocus = focus.clone(); let mut calendar = Calendar::new("calendar", self.month) .today(self.today) .week_starts_on(self.week_starts_on) .selected(self.selected) .on_select(move |date, window, cx| { if let Some(handler) = &on_select { handler(date, window, cx); } if let Some(close) = &close { close(&false, window, cx); } // Back to the trigger, so the keyboard can reopen the calendar. window.focus(&refocus, cx); }); if let Some(handler) = self.on_month_change { calendar = calendar.on_month_change(move |month, window, cx| handler(month, window, cx)); } let mut popover = Popover::new(self.id, self.open) .side(Side::Bottom) .align(Align::Start) // The calendar plus the popover's 1px border on each side. .width(CALENDAR_WIDTH + px(2.0)) .padded(false) .trigger(trigger) .child(calendar); if let Some(handler) = self.on_open_change { let focus = focus.clone(); popover = popover.on_open_change(move |open, window, cx| { handler(open, window, cx); return_focus(*open, &focus, window, cx); }); } // The trigger's focus is tracked around the whole popover, which the calendar renders // inside even though it floats elsewhere: focus in the calendar still counts as in the // picker, so a form doesn't see it blur when it opens. div() .flex() .track_focus(&focus) .end_visible_focus_on_press(&focus, cx) .on_key_down(move |event: &KeyDownEvent, window, cx| { // Opening moves focus into the calendar, so the key's release can't also click // the trigger and close it again. if !open && matches!(event.keystroke.key.as_str(), "down" | "enter" | "space") { if let Some(handler) = &open_handler { handler(&true, window, cx); } cx.stop_propagation(); } }) .child(popover) } }Register the module
// src/ui/mod.rs pub mod date_picker;
Usage
use crate::ui::date_picker::*;API Reference
DatePicker
A button showing the chosen date that opens a calendar in a popover. The parent view owns open, the selected date and the month the calendar shows; picking a day closes it. The trigger is a tab stop; Enter, Space or the down arrow open the calendar.
DatePicker::new(id: impl Into<ElementId>, open: bool, selected: Option<CivilDate>, month: CivilDate) -> Selfmonth is any day in the month the calendar shows when open.
.today(today: Option<CivilDate>).week_starts_on(weekday: Weekday).placeholder(placeholder: impl Into<SharedString>).invalid(invalid: bool)Draws a destructive border and ring, like aria-invalid on shadcn/ui's trigger.
.focus_handle(handle: FocusHandle)Focus the trigger through this handle, such as form.focus_handle(..) or one from cx.focus_handle(), to move focus to it from code. Without one it keeps its own.
.width(width: impl Into<Pixels>)The trigger's width.
.on_select(handler: impl Fn(&CivilDate, &mut Window, &mut App) + 'static)Receives the picked day. The picker then asks to close through on_open_change.
.on_open_change(handler: impl Fn(&bool, &mut Window, &mut App) + 'static)Receives the *next* open value.
.on_month_change(handler: impl Fn(&CivilDate, &mut Window, &mut App) + 'static)Receives the first day of the month to show next.