From d5410375bb39ac01077cb5340b0bbf543714b2d5 Mon Sep 17 00:00:00 2001 From: luoxiao Date: Sun, 7 Apr 2024 22:38:18 +0800 Subject: [PATCH] feat: Drawer adds mask_closeable and close_on_esc prop --- demo_markdown/docs/drawer/mod.md | 2 ++ thaw/src/drawer/mod.rs | 52 ++++++++++++++++++++++++++++---- 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/demo_markdown/docs/drawer/mod.md b/demo_markdown/docs/drawer/mod.md index 3594d79..3a09803 100644 --- a/demo_markdown/docs/drawer/mod.md +++ b/demo_markdown/docs/drawer/mod.md @@ -45,6 +45,8 @@ view! { | --- | --- | --- | --- | | class | `OptionalProp>` | `Default::default()` | Addtional classes for the drawer element. | | show | `Model` | | Whether to show drawer. | +| mask_closeable | `MaybeSignal` | `true` | Whether to emit hide event when click mask. | +| close_on_esc | `bool` | `true` | Whether to close drawer on Esc is pressed. | | title | `OptionalProp>` | `Default::default()` | Drawer title. | | placement | `MaybeSignal` | `DrawerPlacement::Right` | Drawer placement. | | width | `MaybeSignal` | `520px` | Drawer width. | diff --git a/thaw/src/drawer/mod.rs b/thaw/src/drawer/mod.rs index dcc4c7f..5c8d4d2 100644 --- a/thaw/src/drawer/mod.rs +++ b/thaw/src/drawer/mod.rs @@ -1,11 +1,13 @@ use crate::Card; -use leptos::*; +use leptos::{leptos_dom::helpers::WindowListenerHandle, *}; use thaw_components::{CSSTransition, Teleport}; use thaw_utils::{class_list, mount_style, use_lock_html_scroll, Model, OptionalProp}; #[component] pub fn Drawer( #[prop(into)] show: Model, + #[prop(default = true.into(), into)] mask_closeable: MaybeSignal, + #[prop(default = true, into)] close_on_esc: bool, #[prop(optional, into)] title: OptionalProp>, #[prop(optional, into)] placement: MaybeSignal, #[prop(default = MaybeSignal::Static("520px".to_string()), into)] width: MaybeSignal, @@ -29,6 +31,8 @@ pub fn Drawer( #[component] fn DrawerInnr( show: Model, + mask_closeable: MaybeSignal, + close_on_esc: bool, title: OptionalProp>, placement: MaybeSignal, class: OptionalProp>, @@ -56,11 +60,31 @@ pub fn Drawer( }; let is_lock = RwSignal::new(show.get_untracked()); - Effect::new(move |_| { - if show.get() { + let esc_handle = StoredValue::new(None::); + Effect::new(move |prev| { + let is_show = show.get(); + if is_show { is_lock.set(true); is_css_transition.set(true); } + if close_on_esc { + if is_show && !prev.unwrap_or(false) { + let handle = window_event_listener(ev::keydown, move |e| { + if &e.code() == "Escape" { + show.set(false); + } + }); + esc_handle.set_value(Some(handle)); + } else { + esc_handle.update_value(|handle| { + if let Some(handle) = handle.take() { + handle.remove(); + } + }) + } + } + + is_show }); use_lock_html_scroll(is_lock.into()); let on_after_leave = move |_| { @@ -68,6 +92,20 @@ pub fn Drawer( is_css_transition.set(false); }; + let on_mask_click = move |_| { + if mask_closeable.get_untracked() { + show.set(false); + } + }; + + on_cleanup(move || { + esc_handle.update_value(|handle| { + if let Some(handle) = handle.take() { + handle.remove(); + } + }) + }); + view! {
@@ -113,10 +151,12 @@ pub fn Drawer( } match mount { - DrawerMount::None => view! { }, + DrawerMount::None => { + view! { } + } DrawerMount::Body => view! { - + }, }