Alert
A callout that draws attention to a message.
Web replica of the native GPUI output
Update available
hyuskui 0.2 adds Tabs and Avatar. Run `cargo hyuskui diff` to see changes.
Build failed
The GPU process exited unexpectedly.
use gpui::{div, prelude::*};
use crate::ui::alert::{Alert, AlertVariant};
pub fn alert_demo() -> impl IntoElement {
div()
.flex()
.flex_col()
.gap_3()
.child(
Alert::new("Update available")
.description("hyuskui 0.2 adds Tabs and Avatar. Run `cargo hyuskui diff` to see changes."),
)
.child(
Alert::new("Build failed")
.description("The GPU process exited unexpectedly.")
.variant(AlertVariant::Destructive),
)
}Installation
Add the component
$ cargo hyuskui add alertOr copy it by hand: paste the source below into
src/ui/alert.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::alert::*;Source
This file is yours after install. Change the styles, add variants, or delete what you do not need.
use gpui::{div, prelude::*, px, App, FontWeight, SharedString, Window};
use crate::theme::ActiveTheme;
#[derive(Clone, Copy, Default, PartialEq)]
pub enum AlertVariant {
#[default]
Default,
Destructive,
}
#[derive(IntoElement)]
pub struct Alert {
title: SharedString,
description: Option<SharedString>,
variant: AlertVariant,
}
impl Alert {
pub fn new(title: impl Into<SharedString>) -> Self {
Self {
title: title.into(),
description: None,
variant: AlertVariant::default(),
}
}
pub fn description(mut self, description: impl Into<SharedString>) -> Self {
self.description = Some(description.into());
self
}
pub fn variant(mut self, variant: AlertVariant) -> Self {
self.variant = variant;
self
}
}
impl RenderOnce for Alert {
fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
let theme = cx.theme();
let (fg, body) = match self.variant {
AlertVariant::Default => (theme.card_foreground, theme.muted_foreground),
AlertVariant::Destructive => (theme.destructive, theme.destructive.opacity(0.9)),
};
div()
.w_full()
.flex()
.flex_col()
.gap(px(2.0))
.px(px(16.0))
.py(px(12.0))
.rounded(theme.radius)
.border_1()
.border_color(theme.border)
.bg(theme.card)
.text_sm()
.child(
div()
.text_color(fg)
.font_weight(FontWeight::MEDIUM)
.child(self.title),
)
.when_some(self.description, |this, description| {
this.child(div().text_color(body).child(description))
})
}
}