Skip to content

Commit

Permalink
Attempt to fix use_scroll behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielleHuisman committed Apr 1, 2024
1 parent 0df9135 commit ae8dd4d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
14 changes: 13 additions & 1 deletion packages/leptos/tests/visual/src/spec/arrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,25 @@ pub fn Arrow() -> impl IntoView {
let arrow_y = move || arrow_data().and_then(|arrow_data| arrow_data.y);
let center_offset_value = move || arrow_data().map(|arrow_data| arrow_data.center_offset);

let UseScrollReturn { scroll_ref, .. } = use_scroll(UseScrollOptions {
let UseScrollReturn {
scroll_ref,
update_scroll,
..
} = use_scroll(UseScrollOptions {
reference_ref,
floating_ref,
update,
rtl: None,
});

// Match React test behaviour
let placement_update_scroll = update_scroll.clone();
let padding_update_scroll = update_scroll.clone();
let add_offset_update_scroll = update_scroll.clone();
_ = watch(placement, move |_, _, _| placement_update_scroll(), false);
_ = watch(padding, move |_, _, _| padding_update_scroll(), false);
_ = watch(add_offset, move |_, _, _| add_offset_update_scroll(), false);

view! {
<h1>Arrow</h1>
<p></p>
Expand Down
1 change: 1 addition & 0 deletions packages/leptos/tests/visual/src/spec/auto_placement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ pub fn AutoPlacement() -> impl IntoView {
let UseScrollReturn {
scroll_ref,
indicator,
..
} = use_scroll(UseScrollOptions {
reference_ref,
floating_ref,
Expand Down
1 change: 1 addition & 0 deletions packages/leptos/tests/visual/src/spec/scroll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub fn Scroll() -> impl IntoView {
let UseScrollReturn {
scroll_ref,
indicator,
..
} = use_scroll(UseScrollOptions {
reference_ref,
floating_ref,
Expand Down
11 changes: 11 additions & 0 deletions packages/leptos/tests/visual/src/utils/use_scroll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub struct UseScrollOptions {
pub struct UseScrollReturn {
pub scroll_ref: NodeRef<Div>,
pub indicator: Rc<dyn Fn() -> HtmlElement<Div>>,
pub update_scroll: Rc<dyn Fn()>,
}

pub fn use_scroll(
Expand Down Expand Up @@ -60,6 +61,7 @@ pub fn use_scroll(

let (ancestors, set_ancestors) = create_signal::<Vec<OverflowAncestor>>(vec![]);
let (scroll, set_scroll) = create_signal::<Option<(i32, i32)>>(None);
let is_update_required = create_rw_signal(false);

let local_update_update = update.clone();
let local_update_indicator_update = indicator_update_rc.clone();
Expand All @@ -74,6 +76,10 @@ pub fn use_scroll(

let effect_local_update = local_update.clone();
create_effect(move |_| {
if is_update_required.get() {
is_update_required.set_untracked(false);
}

if let Some(reference) = reference_ref() {
let mut ancestors = get_overflow_ancestors(&reference, vec![], true);

Expand Down Expand Up @@ -137,8 +143,13 @@ pub fn use_scroll(
}
};

let update_scroll = move || {
is_update_required.set(true);
};

UseScrollReturn {
scroll_ref,
indicator: Rc::new(indicator),
update_scroll: Rc::new(update_scroll),
}
}

0 comments on commit ae8dd4d

Please sign in to comment.