Skip to content

Commit 7ec43b4

Browse files
committed
feat: Menu can only contain one child
1 parent d63c5d2 commit 7ec43b4

File tree

3 files changed

+30
-24
lines changed

3 files changed

+30
-24
lines changed

demo/src/components/site_header.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ pub fn SiteHeader() -> impl IntoView {
135135
display: none !important;
136136
}
137137
.demo-header__menu-mobile {
138-
display: block !important;
138+
display: inline-block !important;
139139
}
140140
}
141141
"
@@ -187,8 +187,9 @@ pub fn SiteHeader() -> impl IntoView {
187187
_ => navigate_signal.get()(&value, Default::default()),
188188
}
189189
>
190-
<MenuTrigger slot class="demo-header__menu-mobile">
190+
<MenuTrigger slot>
191191
<Button
192+
class="demo-header__menu-mobile"
192193
appearance=ButtonAppearance::Subtle
193194
icon=icondata::AiUnorderedListOutlined
194195
attr:style="font-size: 22px; padding: 0px 6px;"

thaw/src/menu/docs/mod.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,9 @@ view! {
162162

163163
### MenuTriger Props
164164

165-
| Name | Type | Default | Description |
166-
| -------- | ------------------- | -------------------- | ----------- |
167-
| class | `MaybeProp<String>` | `Default::default()` | |
168-
| children | `Children` | | |
165+
| Name | Type | Default | Description |
166+
| -------- | ------------------------------------------- | ------- | ----------- |
167+
| children | `T: AddAnyAttr + IntoView + Send + 'static` | | |
169168

170169
### MenuItem Props
171170

thaw/src/menu/mod.rs

+24-18
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@ mod menu_item;
33
pub use menu_item::*;
44

55
use crate::ConfigInjection;
6-
use leptos::{context::Provider, ev, html::Div, leptos_dom::helpers::TimeoutHandle, prelude::*};
6+
use leptos::{
7+
context::Provider,
8+
ev::{self, on},
9+
html::Div,
10+
leptos_dom::helpers::TimeoutHandle,
11+
prelude::*,
12+
tachys::html::{class::class as tachys_class, node_ref::node_ref},
13+
};
714
use std::time::Duration;
815
use thaw_components::{Binder, CSSTransition, Follower, FollowerPlacement};
916
use thaw_utils::{
@@ -12,17 +19,15 @@ use thaw_utils::{
1219
};
1320

1421
#[slot]
15-
pub struct MenuTrigger {
16-
#[prop(optional, into)]
17-
class: MaybeProp<String>,
18-
children: Children,
22+
pub struct MenuTrigger<T> {
23+
children: TypedChildren<T>,
1924
}
2025

2126
#[component]
22-
pub fn Menu(
27+
pub fn Menu<T>(
2328
#[prop(optional, into)] class: MaybeProp<String>,
2429
/// The element or component that triggers menu.
25-
menu_trigger: MenuTrigger,
30+
menu_trigger: MenuTrigger<T>,
2631
/// Action that displays the menu.
2732
#[prop(optional)]
2833
trigger_type: MenuTriggerType,
@@ -34,12 +39,15 @@ pub fn Menu(
3439
on_select: BoxOneCallback<String>,
3540
#[prop(optional, into)] appearance: MaybeProp<MenuAppearance>,
3641
children: Children,
37-
) -> impl IntoView {
42+
) -> impl IntoView
43+
where
44+
T: AddAnyAttr + IntoView + Send + 'static,
45+
{
3846
mount_style("menu", include_str!("./menu.css"));
3947
let config_provider = ConfigInjection::expect_context();
4048

4149
let menu_ref = NodeRef::<Div>::new();
42-
let target_ref = NodeRef::<Div>::new();
50+
let target_ref = NodeRef::<thaw_utils::Element>::new();
4351
let is_show_menu = RwSignal::new(false);
4452
let show_menu_handle = StoredValue::new(None::<TimeoutHandle>);
4553

@@ -91,7 +99,6 @@ pub fn Menu(
9199
});
92100

93101
let MenuTrigger {
94-
class: trigger_class,
95102
children: trigger_children,
96103
} = menu_trigger;
97104

@@ -105,14 +112,13 @@ pub fn Menu(
105112

106113
view! {
107114
<Binder>
108-
<div
109-
class=class_list!["thaw-menu-trigger", trigger_class]
110-
node_ref=target_ref
111-
on:mouseenter=on_mouse_enter
112-
on:mouseleave=on_mouse_leave
113-
>
114-
{trigger_children()}
115-
</div>
115+
{trigger_children
116+
.into_inner()()
117+
.into_inner()
118+
.add_any_attr(tachys_class(("thaw-menu-trigger", true)))
119+
.add_any_attr(node_ref(target_ref))
120+
.add_any_attr(on(ev::mouseenter, on_mouse_enter))
121+
.add_any_attr(on(ev::mouseleave, on_mouse_leave))}
116122
<Follower slot show=is_show_menu placement=position>
117123
<Provider value=menu_injection>
118124
<CSSTransition

0 commit comments

Comments
 (0)