|
| 1 | +use floating_ui_leptos::{ |
| 2 | + Boundary, DetectOverflowOptions, MiddlewareVec, Offset, OffsetOptions, Padding, |
| 3 | + PartialSideObject, Placement, RootBoundary, Shift, ShiftOptions, |
| 4 | +}; |
| 5 | +use leptos::{html::Div, *}; |
| 6 | + |
| 7 | +use crate::{ |
| 8 | + components::{Chrome, Floating, GridItem, Reference, Scrollable}, |
| 9 | + utils::rem_to_px, |
| 10 | +}; |
| 11 | + |
| 12 | +#[component] |
| 13 | +pub fn ShiftDemo() -> impl IntoView { |
| 14 | + let boundary_ref: NodeRef<Div> = NodeRef::new(); |
| 15 | + |
| 16 | + Effect::new(move |_| { |
| 17 | + if let Some(boundary) = boundary_ref.get() { |
| 18 | + boundary |
| 19 | + .first_element_child() |
| 20 | + .expect("First element child should exist.") |
| 21 | + .set_scroll_top(rem_to_px(200.0 / 16.0) as i32); |
| 22 | + } |
| 23 | + }); |
| 24 | + |
| 25 | + view! { |
| 26 | + <GridItem |
| 27 | + title="Shift" |
| 28 | + description="Shifts your floating element to keep it in view." |
| 29 | + chrome=move || view! { |
| 30 | + <div ref={boundary_ref} class="relative overflow-hidden"> |
| 31 | + <Chrome |
| 32 | + label="Scroll the container" |
| 33 | + scrollable=Scrollable::Y |
| 34 | + relative=false |
| 35 | + shadow=false |
| 36 | + > |
| 37 | + <Floating |
| 38 | + placement=Placement::Right |
| 39 | + middleware=MaybeProp::derive(move || { |
| 40 | + let mut detect_overflow_options = DetectOverflowOptions::default() |
| 41 | + .root_boundary(RootBoundary::Document) |
| 42 | + .padding(Padding::PerSide(PartialSideObject { |
| 43 | + top: Some(rem_to_px(54.0 / 16.0)), |
| 44 | + right: None, |
| 45 | + bottom: Some(rem_to_px(5.0 / 16.0)), |
| 46 | + left: None |
| 47 | + })); |
| 48 | + |
| 49 | + if let Some(boundary) = boundary_ref.get() { |
| 50 | + let boundary: &web_sys::Element = &boundary; |
| 51 | + detect_overflow_options = detect_overflow_options.boundary(Boundary::Element(boundary.clone())); |
| 52 | + } |
| 53 | + |
| 54 | + let middleware: MiddlewareVec = vec![ |
| 55 | + Box::new(Offset::new(OffsetOptions::Value(5.0))), |
| 56 | + Box::new(Shift::new(ShiftOptions::default().detect_overflow(detect_overflow_options))), |
| 57 | + ]; |
| 58 | + |
| 59 | + Some(middleware) |
| 60 | + }) |
| 61 | + content=move || view! { |
| 62 | + <div class="grid h-48 w-20 place-items-center text-sm font-bold"> |
| 63 | + Popover |
| 64 | + </div> |
| 65 | + } |
| 66 | + reference=move |node_ref| view! { |
| 67 | + <Reference node_ref=node_ref class="ml-[5%] sm:ml-[33%]" /> |
| 68 | + } |
| 69 | + /> |
| 70 | + </Chrome> |
| 71 | + </div> |
| 72 | + } |
| 73 | + /> |
| 74 | + } |
| 75 | +} |
0 commit comments