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
use gloo::utils::document;
use web_sys::{HtmlLinkElement, Node};
use wasm_bindgen::{JsCast, UnwrapThrowExt};
use yew::prelude::*;
/// A side-effect hook that sets favicon of the page.
///
/// # Example
///
/// ```rust
/// # use yew::prelude::*;
/// #
/// use yew_hooks::prelude::*;
///
/// #[function_component(Favicon)]
/// fn favicon() -> Html {
/// use_favicon("https://crates.io/favicon.ico".to_string());
///
/// html! {
/// <>
/// </>
/// }
/// }
/// ```
#[hook]
pub fn use_favicon(href: String) {
use_effect_with(href, move |href| {
let link = {
if let Ok(Some(link)) = document().query_selector("link[rel*='icon']") {
link
} else {
document().create_element("link").unwrap_throw()
}
}
.dyn_into::<HtmlLinkElement>()
.unwrap_throw();
link.set_type("image/x-icon");
link.set_rel("shortcut icon");
link.set_href(href);
let head = document()
.get_elements_by_tag_name("head")
.item(0)
.unwrap_throw();
let _ = head.append_child(&link.dyn_into::<Node>().unwrap_throw());
|| ()
});
}