-
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
7 changed files
with
81 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use yew::prelude::*; | ||
|
||
use super::use_event; | ||
|
||
/// A sensor hook that tracks whether HTML element is being hovered. | ||
/// | ||
/// # Example | ||
/// | ||
/// ```rust | ||
/// # use yew::prelude::*; | ||
/// # | ||
/// use yew_hooks::prelude::*; | ||
/// | ||
/// #[function_component(UseHovered)] | ||
/// fn hovered() -> Html { | ||
/// let node = use_node_ref(); | ||
/// let state = use_hovered(node.clone()); | ||
/// | ||
/// html! { | ||
/// <div ref={node}> | ||
/// <b>{ " Hovered: " }</b> | ||
/// { state } | ||
/// </div> | ||
/// } | ||
/// } | ||
/// ``` | ||
#[hook] | ||
pub fn use_hovered(node: NodeRef) -> bool { | ||
let state = use_state_eq(|| false); | ||
|
||
{ | ||
let state = state.clone(); | ||
let node = node.clone(); | ||
use_event(node, "mouseover", move |_: Event| { | ||
state.set(true); | ||
}); | ||
} | ||
|
||
{ | ||
let state = state.clone(); | ||
use_event(node, "mouseout", move |_: Event| { | ||
state.set(false); | ||
}); | ||
} | ||
|
||
*state | ||
} |
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,25 @@ | ||
use yew::prelude::*; | ||
use yew_hooks::prelude::*; | ||
|
||
/// `use_hovered` demo | ||
#[function_component] | ||
pub fn UseHovered() -> Html { | ||
let node = use_node_ref(); | ||
let hovered = use_hovered(node.clone()); | ||
|
||
html! { | ||
<div class="container"> | ||
<header class="mt-24 text-xl text-center"> | ||
<div ref={node} class="bg-emerald-800 w-[400px] h-[200px] mx-auto text-slate-100 p-4"> | ||
<p> | ||
<b>{ " Hovered: " }</b> | ||
{ hovered } | ||
</p> | ||
<p> | ||
{ "Try to hover your cursor over this element." } | ||
</p> | ||
</div> | ||
</header> | ||
</div> | ||
} | ||
} |
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