use maud::{html, Markup};
#[derive(Clone, Debug)]
pub struct Props {
pub id: String,
pub title: String,
pub description: Option<String>,
pub children: Markup,
pub footer: Option<Markup>,
pub open: bool,
}
impl Default for Props {
fn default() -> Self {
Self {
id: "alert-dialog".to_string(),
title: "Confirm".to_string(),
description: None,
children: html! {},
footer: None,
open: false,
}
}
}
pub fn trigger(target_id: &str, label: &str, variant: &str) -> Markup {
html! {
button type="button"
class=(format!("mui-btn mui-btn--{} mui-btn--md", variant))
data-mui="alert-dialog-trigger"
data-target=(target_id)
{
(label)
}
}
}
pub fn render(props: Props) -> Markup {
let title_id = format!("{}-title", props.id);
let desc_id = format!("{}-desc", props.id);
let has_desc = props.description.is_some();
html! {
dialog class="mui-alert-dialog"
id=(props.id)
data-mui="alert-dialog"
role="alertdialog"
aria-labelledby=(title_id)
aria-describedby=[if has_desc { Some(desc_id.as_str()) } else { None }]
open[props.open]
{
div class="mui-alert-dialog__header" {
h2 class="mui-alert-dialog__title" id=(title_id) {
(props.title)
}
}
@if let Some(desc) = props.description {
p class="mui-alert-dialog__description" id=(desc_id) {
(desc)
}
}
div class="mui-alert-dialog__body" {
(props.children)
}
@if let Some(footer) = props.footer {
div class="mui-alert-dialog__footer" {
(footer)
}
}
}
}
}
pub fn showcase() -> Markup {
html! {
div.mui-showcase__grid {
{
(trigger("demo-alert-delete", "Delete account", "danger"))
}
{
(render(Props {
id: "demo-alert-delete".to_string(),
title: "Delete Account".to_string(),
description: Some("This action cannot be undone. Your account, projects, and all associated data will be permanently deleted.".to_string()),
children: html! {},
footer: Some(html! {
button class="mui-btn mui-btn--ghost mui-btn--md" data-mui-close { "Cancel" }
button class="mui-btn mui-btn--danger mui-btn--md" data-mui-close { "Delete account" }
}),
open: false,
}))
}
{
(trigger("demo-alert-discard", "Discard changes", "default"))
}
{
(render(Props {
id: "demo-alert-discard".to_string(),
title: "Discard Changes".to_string(),
description: Some("Your unsaved changes will be lost. This cannot be recovered.".to_string()),
children: html! {},
footer: Some(html! {
button class="mui-btn mui-btn--ghost mui-btn--md" data-mui-close { "Keep editing" }
button class="mui-btn mui-btn--primary mui-btn--md" data-mui-close { "Discard" }
}),
open: false,
}))
}
}
}
}