|
| 1 | +use floating_ui_yew::{ |
| 2 | + use_auto_update, use_floating, Arrow, ArrowData, ArrowOptions, DetectOverflowOptions, Flip, |
| 3 | + FlipOptions, MiddlewareVec, Offset, OffsetOptions, Padding, Placement, Shift, ShiftOptions, |
| 4 | + Side, UseFloatingOptions, UseFloatingReturn, ARROW_NAME, |
| 5 | +}; |
1 | 6 | use yew::prelude::*; |
2 | 7 |
|
3 | 8 | #[function_component] |
4 | 9 | pub fn App() -> Html { |
5 | | - // TODO |
6 | | - html! {} |
| 10 | + let reference_ref = use_node_ref(); |
| 11 | + let floating_ref = use_node_ref(); |
| 12 | + let arrow_ref = use_node_ref(); |
| 13 | + |
| 14 | + let open = use_state_eq(|| false); |
| 15 | + |
| 16 | + let middleware: MiddlewareVec = vec![ |
| 17 | + Box::new(Offset::new(OffsetOptions::Value(6.0))), |
| 18 | + Box::new(Flip::new(FlipOptions::default())), |
| 19 | + Box::new(Shift::new(ShiftOptions::default().detect_overflow( |
| 20 | + DetectOverflowOptions::default().padding(Padding::All(5.0)), |
| 21 | + ))), |
| 22 | + Box::new(Arrow::new(ArrowOptions::new(arrow_ref.clone()))), |
| 23 | + ]; |
| 24 | + |
| 25 | + let auto_update = use_auto_update(); |
| 26 | + |
| 27 | + let UseFloatingReturn { |
| 28 | + placement, |
| 29 | + floating_styles, |
| 30 | + middleware_data, |
| 31 | + .. |
| 32 | + } = use_floating( |
| 33 | + reference_ref.clone().into(), |
| 34 | + floating_ref.clone(), |
| 35 | + UseFloatingOptions::default() |
| 36 | + .open(*open) |
| 37 | + .placement(Placement::Top) |
| 38 | + .middleware(middleware) |
| 39 | + .while_elements_mounted((*auto_update).clone()), |
| 40 | + ); |
| 41 | + |
| 42 | + let static_side = placement.side().opposite(); |
| 43 | + let arrow_data = use_memo( |
| 44 | + middleware_data, |
| 45 | + move |middleware_data| -> Option<ArrowData> { middleware_data.get_as(ARROW_NAME) }, |
| 46 | + ); |
| 47 | + let arrow_x = use_memo(arrow_data.clone(), |arrow_data| { |
| 48 | + arrow_data |
| 49 | + .as_ref() |
| 50 | + .clone() |
| 51 | + .and_then(|arrow_data| arrow_data.x.map(|x| format!("{}px", x))) |
| 52 | + }); |
| 53 | + let arrow_y = use_memo(arrow_data.clone(), |arrow_data| { |
| 54 | + arrow_data |
| 55 | + .as_ref() |
| 56 | + .clone() |
| 57 | + .and_then(|arrow_data| arrow_data.y.map(|y| format!("{}px", y))) |
| 58 | + }); |
| 59 | + |
| 60 | + let onmouseenter = use_callback(open.clone(), |_, open| open.set(true)); |
| 61 | + let onmouseleave = use_callback(open.clone(), |_, open| open.set(false)); |
| 62 | + let onfocus = use_callback(open.clone(), |_, open| open.set(true)); |
| 63 | + let onblur = use_callback(open.clone(), |_, open| open.set(false)); |
| 64 | + |
| 65 | + html! { |
| 66 | + <> |
| 67 | + <button |
| 68 | + ref={reference_ref} |
| 69 | + id="button" |
| 70 | + aria-describedby="tooltip" |
| 71 | + onmouseenter={onmouseenter} |
| 72 | + onmouseleave={onmouseleave} |
| 73 | + onfocus={onfocus} |
| 74 | + onblur={onblur} |
| 75 | + > |
| 76 | + {"My button"} |
| 77 | + </button> |
| 78 | + |
| 79 | + <div |
| 80 | + ref={floating_ref} |
| 81 | + id="tooltip" |
| 82 | + role="tooltip" |
| 83 | + style={format!( |
| 84 | + "display: {}; {}", |
| 85 | + match *open { |
| 86 | + true => "block", |
| 87 | + false => "none", |
| 88 | + }, |
| 89 | + *floating_styles |
| 90 | + )} |
| 91 | + > |
| 92 | + {"My tooltip with more content"} |
| 93 | + <div |
| 94 | + ref={arrow_ref} |
| 95 | + id="arrow" |
| 96 | + style={format!( |
| 97 | + "{}{}{}{}", |
| 98 | + (match static_side { |
| 99 | + Side::Left => Some("-4px".into()), |
| 100 | + _ => (*arrow_x).clone() |
| 101 | + }).map(|value| format!("left: {value};")).unwrap_or("".into()), |
| 102 | + (match static_side { |
| 103 | + Side::Top => Some("-4px".into()), |
| 104 | + _ => (*arrow_y).clone() |
| 105 | + }).map(|value| format!("top: {value};")).unwrap_or("".into()), |
| 106 | + (match static_side { |
| 107 | + Side::Right => Some("-4px"), |
| 108 | + _ => None |
| 109 | + }).map(|value| format!("right: {value};")).unwrap_or("".into()), |
| 110 | + (match static_side { |
| 111 | + Side::Bottom => Some("-4px"), |
| 112 | + _ => None |
| 113 | + }).map(|value| format!("left: {value};")).unwrap_or("".into()), |
| 114 | + )} |
| 115 | + /> |
| 116 | + </div> |
| 117 | + </> |
| 118 | + } |
7 | 119 | } |
0 commit comments