|
| 1 | +use dioxus::prelude::*; |
| 2 | +use dioxus_primitives::dialog::{ |
| 3 | + self, DialogCtx, DialogDescriptionProps, DialogRootProps, DialogTitleProps, |
| 4 | +}; |
| 5 | + |
| 6 | +#[derive(Debug, Clone, Copy, Default, PartialEq)] |
| 7 | +pub enum SheetSide { |
| 8 | + Top, |
| 9 | + #[default] |
| 10 | + Right, |
| 11 | + Bottom, |
| 12 | + Left, |
| 13 | +} |
| 14 | + |
| 15 | +impl SheetSide { |
| 16 | + pub fn as_str(&self) -> &'static str { |
| 17 | + match self { |
| 18 | + SheetSide::Top => "top", |
| 19 | + SheetSide::Right => "right", |
| 20 | + SheetSide::Bottom => "bottom", |
| 21 | + SheetSide::Left => "left", |
| 22 | + } |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +#[component] |
| 27 | +pub fn Sheet(props: DialogRootProps) -> Element { |
| 28 | + rsx! { |
| 29 | + SheetRoot { |
| 30 | + id: props.id, |
| 31 | + is_modal: props.is_modal, |
| 32 | + open: props.open, |
| 33 | + default_open: props.default_open, |
| 34 | + on_open_change: props.on_open_change, |
| 35 | + attributes: props.attributes, |
| 36 | + {props.children} |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +#[component] |
| 42 | +fn SheetRoot(props: DialogRootProps) -> Element { |
| 43 | + rsx! { |
| 44 | + document::Link { rel: "stylesheet", href: asset!("./style.css") } |
| 45 | + dialog::DialogRoot { |
| 46 | + class: "sheet-root", |
| 47 | + "data-slot": "sheet-root", |
| 48 | + id: props.id, |
| 49 | + is_modal: props.is_modal, |
| 50 | + open: props.open, |
| 51 | + default_open: props.default_open, |
| 52 | + on_open_change: props.on_open_change, |
| 53 | + attributes: props.attributes, |
| 54 | + {props.children} |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +#[component] |
| 60 | +pub fn SheetContent( |
| 61 | + #[props(default = ReadSignal::new(Signal::new(None)))] id: ReadSignal<Option<String>>, |
| 62 | + #[props(default)] side: SheetSide, |
| 63 | + #[props(default)] class: Option<String>, |
| 64 | + #[props(extends = GlobalAttributes)] attributes: Vec<Attribute>, |
| 65 | + children: Element, |
| 66 | +) -> Element { |
| 67 | + let class = class |
| 68 | + .map(|c| format!("sheet {c}")) |
| 69 | + .unwrap_or("sheet".to_string()); |
| 70 | + |
| 71 | + rsx! { |
| 72 | + dialog::DialogContent { |
| 73 | + class, |
| 74 | + id, |
| 75 | + "data-slot": "sheet-content", |
| 76 | + "data-side": side.as_str(), |
| 77 | + attributes, |
| 78 | + {children} |
| 79 | + SheetClose { class: "sheet-close", |
| 80 | + svg { |
| 81 | + class: "sheet-close-icon", |
| 82 | + view_box: "0 0 24 24", |
| 83 | + xmlns: "http://www.w3.org/2000/svg", |
| 84 | + path { d: "M18 6 6 18" } |
| 85 | + path { d: "m6 6 12 12" } |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +#[component] |
| 93 | +pub fn SheetHeader( |
| 94 | + #[props(extends = GlobalAttributes)] attributes: Vec<Attribute>, |
| 95 | + children: Element, |
| 96 | +) -> Element { |
| 97 | + rsx! { |
| 98 | + div { class: "sheet-header", "data-slot": "sheet-header", ..attributes, {children} } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +#[component] |
| 103 | +pub fn SheetFooter( |
| 104 | + #[props(extends = GlobalAttributes)] attributes: Vec<Attribute>, |
| 105 | + children: Element, |
| 106 | +) -> Element { |
| 107 | + rsx! { |
| 108 | + div { class: "sheet-footer", "data-slot": "sheet-footer", ..attributes, {children} } |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +#[component] |
| 113 | +pub fn SheetTitle(props: DialogTitleProps) -> Element { |
| 114 | + rsx! { |
| 115 | + dialog::DialogTitle { |
| 116 | + id: props.id, |
| 117 | + class: "sheet-title", |
| 118 | + "data-slot": "sheet-title", |
| 119 | + attributes: props.attributes, |
| 120 | + {props.children} |
| 121 | + } |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +#[component] |
| 126 | +pub fn SheetDescription(props: DialogDescriptionProps) -> Element { |
| 127 | + rsx! { |
| 128 | + dialog::DialogDescription { |
| 129 | + id: props.id, |
| 130 | + class: "sheet-description", |
| 131 | + "data-slot": "sheet-description", |
| 132 | + attributes: props.attributes, |
| 133 | + {props.children} |
| 134 | + } |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +#[component] |
| 139 | +pub fn SheetClose( |
| 140 | + #[props(extends = GlobalAttributes)] attributes: Vec<Attribute>, |
| 141 | + r#as: Option<Callback<Vec<Attribute>, Element>>, |
| 142 | + children: Option<Element>, |
| 143 | +) -> Element { |
| 144 | + let ctx: DialogCtx = use_context(); |
| 145 | + |
| 146 | + let mut merged_attributes: Vec<Attribute> = vec![onclick(move |_| { |
| 147 | + ctx.set_open(false); |
| 148 | + })]; |
| 149 | + merged_attributes.extend(attributes); |
| 150 | + |
| 151 | + if let Some(dynamic) = r#as { |
| 152 | + dynamic.call(merged_attributes) |
| 153 | + } else { |
| 154 | + rsx! { |
| 155 | + button { ..merged_attributes, {children} } |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments