dioxus-element-plug 0.1.5

Element UI components for Dioxus applications with pure Rust styling system
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
use dioxus::prelude::*;

// Alert and message CSS class constants
pub const ALERT: &str = "el-alert";
pub const ALERT_SUCCESS: &str = "el-alert--success";
pub const ALERT_INFO: &str = "el-alert--info";
pub const ALERT_WARNING: &str = "el-alert--warning";
pub const ALERT_ERROR: &str = "el-alert--error";
pub const ALERT_DISMISSIBLE: &str = "is-dismissible";
pub const ALERT_CLOSE: &str = "el-alert__close-btn";
pub const ALERT_ICON: &str = "el-alert__icon";
pub const ALERT_DESCRIPTION: &str = "el-alert__description";
pub const NOTIFICATION: &str = "el-notification";
pub const MESSAGE: &str = "el-message";
pub const MESSAGE_BOX: &str = "el-message-box";
pub const TOOLTIP: &str = "el-tooltip";

/// Alert types matching theme-chalk styles
#[derive(Clone, PartialEq)]
pub enum AlertType {
    Success,
    Warning,
    Info,
    Error,
}

impl AlertType {
    pub fn as_class(&self) -> &'static str {
        match self {
            AlertType::Success => "el-alert--success",
            AlertType::Warning => "el-alert--warning",
            AlertType::Info => "el-alert--info",
            AlertType::Error => "el-alert--error",
        }
    }
}

/// Alert props
#[derive(Props, Clone, PartialEq)]
pub struct AlertProps {
    /// Alert title
    pub title: String,

    /// Alert content description
    #[props(default)]
    pub description: Option<String>,

    /// Alert type/style
    #[props(default = AlertType::Info)]
    pub alert_type: AlertType,

    /// Whether the alert can be closed
    #[props(default = false)]
    pub closable: bool,

    /// Whether to show icon
    #[props(default = false)]
    pub show_icon: bool,

    /// Whether the alert is center aligned
    #[props(default = false)]
    pub center: bool,

    /// Close event handler
    #[props(default)]
    pub on_close: Option<EventHandler<MouseEvent>>,

    /// Additional CSS classes
    #[props(default)]
    pub class: Option<String>,

    /// Inline styles
    #[props(default)]
    pub style: Option<String>,
}

/// An alert component for displaying messages
///
/// This component displays important information with different styles
/// for success, warning, info, and error states.
///
/// ## Example
///
/// ```rust,ignore
/// use dioxus_element_plug::components::alert::{Alert, AlertType};
///
/// rsx! {
///     Alert {
///         title: "Success!".to_string(),
///         description: Some("Operation completed successfully".to_string()),
///         alert_type: AlertType::Success,
///         closable: true,
///         on_close: move |_| println!("Alert closed"),
///     }
/// }
/// ```
#[component]
pub fn Alert(props: AlertProps) -> Element {
    let mut class_names = vec![ALERT];
    
    class_names.push(props.alert_type.as_class());
    
    if props.closable {
        class_names.push("is-closable");
    }
    
    if props.show_icon {
        class_names.push("with-icon");
    }
    
    if props.center {
        class_names.push("is-center");
    }
    
    if let Some(ref custom_class) = props.class {
        class_names.push(custom_class);
    }
    
    let class_string = class_names.join(" ");
    let style_string = props.style.as_ref().cloned().unwrap_or_default();
    
    let icon_class = match props.alert_type {
        AlertType::Success => "el-icon-success",
        AlertType::Warning => "el-icon-warning",
        AlertType::Info => "el-icon-info",
        AlertType::Error => "el-icon-error",
    };
    
    rsx! {
        div {
            class: "{class_string}",
            style: "{style_string}",
            role: "alert",
            
            if props.show_icon {
                div {
                    class: "el-alert__icon",
                    i {
                        class: "{icon_class}"
                    }
                }
            }
            
            div {
                class: "el-alert__content",
                
                h4 {
                    class: "el-alert__title",
                    "{props.title}"
                }
                
                if let Some(ref desc) = props.description {
                    p {
                        class: "el-alert__description",
                        "{desc}"
                    }
                }
            }
            
            if props.closable {
                button {
                    class: "el-alert__close-btn",
                    type: "button",
                    onclick: move |event| {
                        if let Some(handler) = props.on_close {
                            handler.call(event);
                        }
                    },
                    "×"
                }
            }
        }
    }
}

/// Callout component for important information
#[derive(Props, Clone, PartialEq)]
pub struct CalloutProps {
    /// Callout content
    pub children: Element,

    /// Callout title
    #[props(default)]
    pub title: Option<String>,

    /// Callout type
    #[props(default = AlertType::Info)]
    pub callout_type: AlertType,

    /// Whether to show icon
    #[props(default = true)]
    pub show_icon: bool,

    /// Additional CSS classes
    #[props(default)]
    pub class: Option<String>,

    /// Inline styles
    #[props(default)]
    pub style: Option<String>,
}

/// A callout component for highlighting important content
///
/// This component provides a more prominent way to display important
/// information with optional title and icon.
#[component]
pub fn Callout(props: CalloutProps) -> Element {
    let mut class_names = vec!["el-callout".to_string()];
    
    class_names.push(props.callout_type.as_class().to_string());
    
    if props.show_icon {
        class_names.push("with-icon".to_string());
    }
    
    if let Some(ref custom_class) = props.class {
        class_names.push(custom_class.to_string());
    }
    
    let class_string = class_names.join(" ");
    let style_string = props.style.as_ref().cloned().unwrap_or_default();
    
    let icon_class = match props.callout_type {
        AlertType::Success => "el-icon-check-circle",
        AlertType::Warning => "el-icon-warning-outline",
        AlertType::Info => "el-icon-info-circle",
        AlertType::Error => "el-icon-error-circle",
    };
    
    rsx! {
        div {
            class: "{class_string}",
            style: "{style_string}",
            
            if props.show_icon {
                i {
                    class: "{icon_class} el-callout__icon"
                }
            }
            
            if let Some(ref title_text) = props.title {
                h4 {
                    class: "el-callout__title",
                    "{title_text}"
                }
            }
            
            div {
                class: "el-callout__content",
                {props.children}
            }
        }
    }
}

/// Notification component
#[derive(Props, Clone, PartialEq)]
pub struct NotificationProps {
    /// Notification title
    pub title: String,

    /// Notification message
    pub message: String,

    /// Notification type
    #[props(default = AlertType::Info)]
    pub notification_type: AlertType,

    /// Position of the notification
    #[props(default = "top-right".to_string())]
    pub position: String,

    /// Auto close timeout in milliseconds (0 to disable)
    #[props(default = 4500)]
    pub duration: u64,

    /// Whether the notification can be closed manually
    #[props(default = true)]
    pub closable: bool,

    /// Whether to show icon
    #[props(default = true)]
    pub show_icon: bool,

    /// Close event handler
    #[props(default)]
    pub on_close: Option<EventHandler<>>,
}

/// A notification component for temporary messages
///
/// This component creates a temporary notification that can auto-close
/// after a specified duration.
#[component]
pub fn Notification(props: NotificationProps) -> Element {
    let mut class_names = vec![NOTIFICATION.to_string()];
    
    class_names.push(format!("el-notification--{}", 
        match props.notification_type {
            AlertType::Success => "success",
            AlertType::Warning => "warning",
            AlertType::Info => "info",
            AlertType::Error => "error",
        }
    ));
    
    class_names.push(format!("is-{}", props.position));
    
    let class_string = class_names.join(" ");
    
    let icon_class = match props.notification_type {
        AlertType::Success => "el-icon-check-circle",
        AlertType::Warning => "el-icon-warning-outline",
        AlertType::Info => "el-icon-info-circle",
        AlertType::Error => "el-icon-error-circle",
    };
    
    rsx! {
        div {
            class: "{class_string}",
            role: "alert",
            
            div {
                class: "el-notification__content",
                
                if props.show_icon {
                    i {
                        class: "{icon_class} el-notification__icon"
                    }
                }
                
                div {
                    class: "el-notification__group",
                    
                    h3 {
                        class: "el-notification__title",
                        "{props.title}"
                    }
                    
                    div {
                        class: "el-notification__message",
                        "{props.message}"
                    }
                }
            }
            
            if props.closable {
                button {
                    class: "el-notification__close-btn",
                    type: "button",
                    onclick: move |_| {
                        if let Some(handler) = props.on_close {
                            handler.call(());
                        }
                    },
                    "×"
                }
            }
        }
    }
}

/// Tooltip component for additional information
#[derive(Props, Clone, PartialEq)]
pub struct TooltipProps {
    /// Tooltip content
    pub content: String,

    /// Element to wrap with tooltip
    pub children: Element,

    /// Tooltip position
    #[props(default = "top".to_string())]
    pub placement: String,

    /// Tooltip effect (dark/light)
    #[props(default = "dark".to_string())]
    pub effect: String,

    /// Tooltip trigger (hover/click/focus)
    #[props(default = "hover".to_string())]
    pub trigger: String,

    /// Whether the tooltip is disabled
    #[props(default = false)]
    pub disabled: bool,

    /// Tooltip offset
    #[props(default = 0)]
    pub offset: i32,
}

/// A tooltip component for providing additional context
///
/// This component wraps another element and shows additional information
/// when the user hovers over or clicks on it.
#[component]
pub fn Tooltip(props: TooltipProps) -> Element {
    if props.disabled {
        return rsx! { {props.children} };
    }
    
    let mut class_names = vec![TOOLTIP.to_string()];
    
    class_names.push(format!("is-{}", props.effect));
    class_names.push(format!("popper--{}", props.placement));
    
    let class_string = class_names.join(" ");
    
    rsx! {
        div {
            class: "el-tooltip",
            
            {props.children}
            
            div {
                class: "{class_string}",
                style: "display: none;",
                role: "tooltip",
                
                div {
                    class: "el-tooltip__content",
                    "{props.content}"
                }
                
                div {
                    class: "popper__arrow",
                    style: "position: absolute;"
                }
            }
        }
    }
}