leptos-use/examples/on_click_outside/src/main.rs

85 lines
2.4 KiB
Rust
Raw Normal View History

2023-07-03 16:10:58 +01:00
use leptos::html::Div;
2023-06-13 17:48:32 +01:00
use leptos::*;
use leptos_use::docs::demo_or_body;
use leptos_use::on_click_outside;
#[component]
2023-07-27 18:06:36 +01:00
fn Demo() -> impl IntoView {
let (show_modal, set_show_modal) = create_signal(false);
let modal_ref = create_node_ref::<Div>();
2023-06-13 17:48:32 +01:00
2023-07-27 18:06:36 +01:00
let _ = on_click_outside(modal_ref, move |_| set_show_modal.set(false));
2023-06-13 17:48:32 +01:00
2023-07-27 19:48:21 +01:00
view! {
<button on:click=move |_| set_show_modal.set(true)>"Open Modal"</button>
2023-06-13 17:48:32 +01:00
2023-07-27 18:06:36 +01:00
<Show when=move || show_modal.get() fallback=|| ()>
2023-06-13 17:48:32 +01:00
<div node_ref=modal_ref class="modal">
<div class="inner">
2023-07-27 19:48:21 +01:00
<button
class="button small"
title="Close"
on:click=move |_| set_show_modal.set(false)
>
"𝖷"
</button>
2023-06-13 17:48:32 +01:00
<p class="heading">"Demo Modal"</p>
<p>"Click outside this modal to close it."</p>
</div>
</div>
</Show>
2023-07-27 19:48:21 +01:00
<style>
"
.modal {
position: fixed;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
width: 420px;
max-width: 100%;
z-index: 10;
}
.inner {
background-color: var(--bg);
padding: 0.4em 2em;
border-radius: 5px;
border: 1px solid var(--theme-popup-border);
box-shadow: 2px 2px 10px rgba(10, 10, 10, 0.1);
}
.dropdown-inner {
background-color: var(--bg);
padding: 0.5em;
position: absolute;
left: 0;
z-index: 10;
border-radius: 5px;
border: 1px solid var(--theme-popup-border);
box-shadow: 2px 2px 5px rgba(10, 10, 10, 0.1);
}
.heading {
font-weight: bold;
font-size: 1.4rem;
margin-bottom: 2rem;
}
.modal > .inner > .button {
position: absolute;
top: 0;
right: 0;
margin: 0;
font-weight: bold;
}
"
</style>
2023-06-13 17:48:32 +01:00
}
}
fn main() {
_ = console_log::init_with_level(log::Level::Debug);
console_error_panic_hook::set_once();
2023-07-27 18:06:36 +01:00
mount_to(demo_or_body(), || {
2023-07-27 19:48:21 +01:00
view! { <Demo/> }
2023-06-13 17:48:32 +01:00
})
}