-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
78 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
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::use_favicon; | ||
/// | ||
/// #[function_component(Favicon)] | ||
/// fn favicon() -> Html { | ||
/// use_favicon("https://crates.io/favicon.ico".to_string()); | ||
/// | ||
/// html! { | ||
/// <> | ||
/// </> | ||
/// } | ||
/// } | ||
/// ``` | ||
pub fn use_favicon(href: String) { | ||
use_effect_with_deps( | ||
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()); | ||
|
||
|| () | ||
}, | ||
href, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use yew::prelude::*; | ||
|
||
use yew_hooks::use_favicon; | ||
|
||
/// `use_favicon` demo | ||
#[function_component(UseFavicon)] | ||
pub fn favicon() -> Html { | ||
use_favicon("https://crates.io/favicon.ico".to_string()); | ||
|
||
html! { | ||
<>{ "View the favicon of this page" }</> | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters