Skip to content

Commit dbdd013

Browse files
Add while elements mounted with options
1 parent 72c2659 commit dbdd013

2 files changed

Lines changed: 66 additions & 3 deletions

File tree

packages/leptos/src/types.rs

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,11 @@ impl UseFloatingOptions {
9494
self.while_elements_mounted(auto_update_rc.into())
9595
}
9696

97-
/// Set `while_elements_mounted` option to [`auto_update`] with [`AutoUpdateOptions::default`].
98-
pub fn while_elements_mounted_auto_update_enabled(self, enabled: MaybeSignal<bool>) -> Self {
97+
/// Set `while_elements_mounted` option to [`auto_update`] with [`AutoUpdateOptions::default`] when `enabled` is `true`.
98+
pub fn while_elements_mounted_auto_update_with_enabled(
99+
self,
100+
enabled: MaybeSignal<bool>,
101+
) -> Self {
99102
let auto_update_rc: Rc<WhileElementsMountedFn> = Rc::new(|reference, floating, update| {
100103
auto_update(reference, floating, update, AutoUpdateOptions::default())
101104
});
@@ -107,6 +110,41 @@ impl UseFloatingOptions {
107110
}
108111
}))
109112
}
113+
114+
/// Set `while_elements_mounted` option to [`auto_update`] with `options`.
115+
pub fn while_elements_mounted_auto_update_with_options(
116+
self,
117+
options: MaybeSignal<AutoUpdateOptions>,
118+
) -> Self {
119+
let auto_update_rc = move |options: AutoUpdateOptions| -> Rc<WhileElementsMountedFn> {
120+
Rc::new(move |reference, floating, update| {
121+
auto_update(reference, floating, update, options.clone())
122+
})
123+
};
124+
125+
self.while_elements_mounted(MaybeProp::derive(move || Some(auto_update_rc(options()))))
126+
}
127+
128+
/// Set `while_elements_mounted` option to [`auto_update`] with `options` when `enabled` is `true`.
129+
pub fn while_elements_mounted_auto_update_with_enabled_and_options(
130+
self,
131+
enabled: MaybeSignal<bool>,
132+
options: MaybeSignal<AutoUpdateOptions>,
133+
) -> Self {
134+
let auto_update_rc = move |options: AutoUpdateOptions| -> Rc<WhileElementsMountedFn> {
135+
Rc::new(move |reference, floating, update| {
136+
auto_update(reference, floating, update, options.clone())
137+
})
138+
};
139+
140+
self.while_elements_mounted(MaybeProp::derive(move || {
141+
if enabled() {
142+
Some(auto_update_rc(options()))
143+
} else {
144+
None
145+
}
146+
}))
147+
}
110148
}
111149

112150
/// CSS styles to apply to the floating element to position it.
@@ -119,6 +157,31 @@ pub struct FloatingStyles {
119157
pub will_change: Option<String>,
120158
}
121159

160+
impl FloatingStyles {
161+
pub fn style_position(&self) -> String {
162+
match self.position {
163+
Strategy::Absolute => "absolute".into(),
164+
Strategy::Fixed => "fixed".into(),
165+
}
166+
}
167+
168+
pub fn style_top(&self) -> String {
169+
self.top.clone()
170+
}
171+
172+
pub fn style_left(&self) -> String {
173+
self.left.clone()
174+
}
175+
176+
pub fn style_transform(&self) -> Option<String> {
177+
self.transform.clone()
178+
}
179+
180+
pub fn style_will_change(&self) -> Option<String> {
181+
self.will_change.clone()
182+
}
183+
}
184+
122185
impl From<FloatingStyles> for String {
123186
fn from(value: FloatingStyles) -> Self {
124187
format!(

packages/leptos/tests/visual/src/spec/auto_update.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub fn AutoUpdate() -> impl IntoView {
5353
floating_ref,
5454
UseFloatingOptions::default()
5555
.strategy(Strategy::Fixed.into())
56-
.while_elements_mounted_auto_update_enabled(while_elements_mounted.into()),
56+
.while_elements_mounted_auto_update_with_enabled(while_elements_mounted.into()),
5757
);
5858

5959
type CleanupFn = Box<dyn Fn()>;

0 commit comments

Comments
 (0)