-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Darius Clark <dariusc93@users.noreply.github.com>
- Loading branch information
Showing
5 changed files
with
130 additions
and
54 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
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,28 @@ | ||
use dioxus::prelude::*; | ||
use warp::crypto::rand::{self, distributions::Alphanumeric, Rng}; | ||
|
||
#[derive(Props)] | ||
pub struct Props<'a> { | ||
children: Element<'a>, | ||
} | ||
|
||
#[allow(non_snake_case)] | ||
pub fn OutsideClick<'a>(cx: Scope<'a, Props<'a>>) -> Element<'a> { | ||
let rand_s: &String = cx.use_hook(|_| { | ||
rand::thread_rng() | ||
.sample_iter(&Alphanumeric) | ||
.take(7) | ||
.map(char::from) | ||
.collect() | ||
}); | ||
|
||
let script = include_str!("outside.js").replace("ID", rand_s); | ||
|
||
cx.render(rsx! { | ||
span { | ||
id: "outside-container-{rand_s}", | ||
&cx.props.children, | ||
script { "{script}" }, | ||
} | ||
}) | ||
} |
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 @@ | ||
function hideOnClickOutside(element, trigger) { | ||
const outsideClickListener = (event) => { | ||
if ( | ||
!element.contains(event.target) && | ||
!trigger.contains(event.target) && | ||
isVisible(element) | ||
) { | ||
element.style.display = "none" | ||
trigger.style.backgroundColor = "var(--theme-secondary)" | ||
removeClickListener() | ||
} | ||
} | ||
|
||
const removeClickListener = () => { | ||
document.removeEventListener("click", outsideClickListener) | ||
} | ||
|
||
document.addEventListener("click", outsideClickListener) | ||
} | ||
|
||
function isVisible(elem) { | ||
return ( | ||
!!elem && | ||
!!(elem.offsetWidth || elem.offsetHeight || elem.getClientRects().length) | ||
) | ||
} | ||
|
||
function initClick() { | ||
const container = document.getElementById("outside-container-ID") | ||
if (container.children.length < 2) { | ||
throw new Error( | ||
"Outside click needs a container (the hiding container) as first child and a trigger button as second child", | ||
) | ||
} | ||
|
||
const el = container.firstChild | ||
el.style.display = "none" | ||
|
||
const trigger = container.children[1] | ||
trigger.style.backgroundColor = "var(--theme-secondary)" | ||
|
||
trigger.addEventListener("click", () => { | ||
el.style.display = el.style.display === "none" ? "" : "none" | ||
trigger.style.backgroundColor = | ||
el.style.display === "none" | ||
? "var(--theme-secondary)" | ||
: "var(--theme-primary)" | ||
|
||
hideOnClickOutside(el, trigger) | ||
}) | ||
} | ||
|
||
initClick() |