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
use dioxus::prelude::*;
use dioxus_sdk::clipboard::use_clipboard;
use zino_core::SharedString;
/// A button to copy the content to clipboard when clicked.
pub fn CopyToClipboard(props: CopyToClipboardProps) -> Element {
if props.hidden {
return rsx! {};
}
let mut clipboard = use_clipboard();
rsx! {
a {
onclick: move |event| {
let content = props.content.as_ref();
match clipboard.set(content.to_owned()) {
Ok(_) => if let Some(handler) = props.on_success.as_ref() {
handler.call(());
},
Err(_) => if let Some(handler) = props.on_error.as_ref() {
handler.call(());
}
}
event.stop_propagation();
},
{ props.children }
}
}
}
/// The [`CopyToClipboard`] properties struct for the configuration of the component.
#[derive(Clone, PartialEq, Props)]
pub struct CopyToClipboardProps {
/// A flag to determine whether the button is hidden or not.
#[props(default)]
pub hidden: bool,
/// The content value to be copied to clipboard.
#[props(into)]
pub content: SharedString,
/// An event handler to be called when the content value is copied to clipboard.
pub on_success: Option<EventHandler<()>>,
/// An event handler to be called when the content value is failed to copy to clipboard.
pub on_error: Option<EventHandler<()>>,
/// The children to render within the component.
children: Element,
}