dynamic_measurement/
dynamic_measurement.rs

1// Example: dynamic measurement and scroll jump prevention.
2use virtualizer::{Align, Virtualizer, VirtualizerOptions};
3
4fn main() {
5    // Example: dynamic measurement + scroll jump prevention.
6    let mut v = Virtualizer::new(VirtualizerOptions::new(100, |_| 10));
7    v.set_viewport_and_scroll_clamped(30, 200);
8
9    println!(
10        "before: off={} total={} range={:?}",
11        v.scroll_offset(),
12        v.total_size(),
13        v.virtual_range()
14    );
15
16    // If an item before the viewport changes size, `resize_item` can adjust scroll_offset to
17    // prevent visual jumps.
18    let applied = v.resize_item(0, 30);
19    println!(
20        "resize_item(0): applied_delta={applied} off={} total={}",
21        v.scroll_offset(),
22        v.total_size()
23    );
24
25    // Measuring an item updates sizes and may adjust scroll (TanStack-aligned behavior).
26    v.measure(1, 50);
27    println!(
28        "measure(1): off={} total={}",
29        v.scroll_offset(),
30        v.total_size()
31    );
32
33    // If you want to update measurements without changing scroll, use `measure_unadjusted`.
34    v.measure_unadjusted(2, 30);
35
36    // Scroll-to helpers still work with updated measurements.
37    let to = v.scroll_to_index_offset(10, Align::Start);
38    v.set_scroll_offset_clamped(to);
39    println!(
40        "set_scroll_offset_clamped(scroll_to_index_offset(10)): off={} range={:?}",
41        v.scroll_offset(),
42        v.virtual_range()
43    );
44}