From 68f81a04ebfc73470ceceb6a3eed79ff8869e6e6 Mon Sep 17 00:00:00 2001 From: luoxiaozero <48741584+luoxiaozero@users.noreply.github.com> Date: Wed, 10 Jul 2024 16:34:46 +0800 Subject: [PATCH] fix: use_lock_html_scroll (#215) --- thaw_utils/src/hooks/use_lock_html_scroll.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/thaw_utils/src/hooks/use_lock_html_scroll.rs b/thaw_utils/src/hooks/use_lock_html_scroll.rs index 7a9daf2..dc09547 100644 --- a/thaw_utils/src/hooks/use_lock_html_scroll.rs +++ b/thaw_utils/src/hooks/use_lock_html_scroll.rs @@ -8,14 +8,16 @@ pub fn use_lock_html_scroll(is_lock: MaybeSignal) { let remove_style_el = move || { style_el.update_value(move |el| { if let Some(el) = el.take() { - let head = document().head().expect("head no exist"); - _ = head.remove_child(&el); + el.remove(); } }); }; - create_render_effect(move |_| { - if is_lock.get() { + create_render_effect(move |prev| { + let is_lock = is_lock.get(); + let prev: bool = prev.unwrap_or_default(); + + if is_lock && !prev { let head = document().head().expect("head no exist"); let style = document() .create_element("style") @@ -24,9 +26,11 @@ pub fn use_lock_html_scroll(is_lock: MaybeSignal) { style.set_text_content(Some("html { overflow: hidden; }")); _ = head.append_child(&style); style_el.set_value(Some(style)); - } else { + } else if !is_lock && prev { remove_style_el(); } + + is_lock }); on_cleanup(remove_style_el)