Card
A bordered surface with an optional header and footer.
Web replica of the native GPUI output
Deploy your build
Ship a signed binary to every platform in one step.
Targets: macOS, Linux, Windows
use gpui::{div, prelude::*, px};
use crate::ui::button::{Button, ButtonVariant};
use crate::ui::card::Card;
pub fn card_demo() -> impl IntoElement {
Card::new()
.title("Deploy your build")
.description("Ship a signed binary to every platform in one step.")
.child(div().w(px(320.0)).text_sm().child("Targets: macOS, Linux, Windows"))
.footer(
div()
.flex()
.gap_2()
.child(Button::new("cancel", "Cancel").variant(ButtonVariant::Outline))
.child(Button::new("deploy", "Deploy")),
)
}Installation
Add the component
$ cargo hyuskui add cardOr copy it by hand: paste the source below into
src/ui/card.rsand register it insrc/ui/mod.rs.Make sure the theme is installed
Every component reads colors from
crate::theme. If you have not runcargo hyuskui init, follow the installation guide first.
Usage
use crate::ui::card::*;Source
This file is yours after install. Change the styles, add variants, or delete what you do not need.
use gpui::{div, prelude::*, px, AnyElement, App, FontWeight, SharedString, Window};
use crate::theme::ActiveTheme;
/// A bordered surface. Add any elements with `.child(...)`.
#[derive(IntoElement)]
pub struct Card {
title: Option<SharedString>,
description: Option<SharedString>,
children: Vec<AnyElement>,
footer: Option<AnyElement>,
}
impl Card {
pub fn new() -> Self {
Self {
title: None,
description: None,
children: Vec::new(),
footer: None,
}
}
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
}
pub fn footer(mut self, footer: impl IntoElement) -> Self {
self.footer = Some(footer.into_any_element());
self
}
}
impl ParentElement for Card {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl RenderOnce for Card {
fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
let theme = cx.theme();
let has_header = self.title.is_some() || self.description.is_some();
div()
.flex()
.flex_col()
.gap(px(24.0))
.py(px(24.0))
.rounded(theme.radius * 1.5)
.border_1()
.border_color(theme.border)
.bg(theme.card)
.text_color(theme.card_foreground)
.shadow_sm()
.when(has_header, |this| {
this.child(
div()
.flex()
.flex_col()
.gap(px(6.0))
.px(px(24.0))
.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().px(px(24.0)).children(self.children))
.when_some(self.footer, |this, footer| {
this.child(div().flex().items_center().px(px(24.0)).child(footer))
})
}
}