Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: PopoverTrigger can only contain one child #330

Merged
merged 4 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions demo/src/components/site_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ pub fn SiteHeader() -> impl IntoView {
display: none !important;
}
.demo-header__menu-mobile {
display: block !important;
display: inline-block !important;
}
}
"
Expand Down Expand Up @@ -187,8 +187,9 @@ pub fn SiteHeader() -> impl IntoView {
_ => navigate_signal.get()(&value, Default::default()),
}
>
<MenuTrigger slot class="demo-header__menu-mobile">
<MenuTrigger slot>
<Button
class="demo-header__menu-mobile"
appearance=ButtonAppearance::Subtle
icon=icondata::AiUnorderedListOutlined
attr:style="font-size: 22px; padding: 0px 6px;"
Expand Down
4 changes: 1 addition & 3 deletions thaw/src/auto_complete/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ pub fn AutoComplete(
mount_style("auto-complete", include_str!("./auto-complete.css"));
let input_ref = ComponentRef::<InputRef>::new();
let listbox_ref = NodeRef::<html::Div>::new();
let auto_complete_ref = NodeRef::<html::Div>::new();
let open_listbox = RwSignal::new(false);
let options = StoredValue::new(HashMap::<String, String>::new());

Expand Down Expand Up @@ -110,10 +109,9 @@ pub fn AutoComplete(
comp_ref.load(AutoCompleteRef { input_ref });

view! {
<Binder target_ref=auto_complete_ref>
<Binder>
<div
class=class_list!["thaw-auto-complete", class]
node_ref=auto_complete_ref
on:keydown=on_keydown
>
<Input
Expand Down
2 changes: 1 addition & 1 deletion thaw/src/color_picker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ pub fn ColorPicker(
}

view! {
<Binder target_ref=trigger_ref>
<Binder>
<div
class=class_list!["thaw-color-picker-trigger", class]
on:click=show_popover
Expand Down
4 changes: 1 addition & 3 deletions thaw/src/combobox/combobox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ pub fn Combobox(
mount_style("combobox", include_str!("./combobox.css"));
let (id, name) = FieldInjection::use_id_and_name(id, name);
let validate = Rule::validate(rules, selected_options, name);
let trigger_ref = NodeRef::<html::Div>::new();
let input_ref = NodeRef::<html::Input>::new();
let listbox_ref = NodeRef::<html::Div>::new();
let is_show_listbox = RwSignal::new(false);
Expand Down Expand Up @@ -167,14 +166,13 @@ pub fn Combobox(
};

view! {
<Binder target_ref=trigger_ref>
<Binder>
<div
class=class_list![
"thaw-combobox",
("thaw-combobox--disabled", move || disabled.get()),
class
]
node_ref=trigger_ref
>
<input
type="text"
Expand Down
2 changes: 1 addition & 1 deletion thaw/src/date_picker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub fn DatePicker(
};

view! {
<Binder target_ref=date_picker_ref>
<Binder>
<div
node_ref=date_picker_ref
class=class_list!["thaw-date-picker", class]
Expand Down
7 changes: 3 additions & 4 deletions thaw/src/menu/docs/mod.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,9 @@ view! {

### MenuTriger Props

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

### MenuItem Props

Expand Down
44 changes: 25 additions & 19 deletions thaw/src/menu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ mod menu_item;
pub use menu_item::*;

use crate::ConfigInjection;
use leptos::{context::Provider, ev, html::Div, leptos_dom::helpers::TimeoutHandle, prelude::*};
use leptos::{
context::Provider,
ev::{self, on},
html::Div,
leptos_dom::helpers::TimeoutHandle,
prelude::*,
tachys::html::{class::class as tachys_class, node_ref::node_ref},
};
use std::time::Duration;
use thaw_components::{Binder, CSSTransition, Follower, FollowerPlacement};
use thaw_utils::{
Expand All @@ -12,17 +19,15 @@ use thaw_utils::{
};

#[slot]
pub struct MenuTrigger {
#[prop(optional, into)]
class: MaybeProp<String>,
children: Children,
pub struct MenuTrigger<T> {
children: TypedChildren<T>,
}

#[component]
pub fn Menu(
pub fn Menu<T>(
#[prop(optional, into)] class: MaybeProp<String>,
/// The element or component that triggers menu.
menu_trigger: MenuTrigger,
menu_trigger: MenuTrigger<T>,
/// Action that displays the menu.
#[prop(optional)]
trigger_type: MenuTriggerType,
Expand All @@ -34,12 +39,15 @@ pub fn Menu(
on_select: BoxOneCallback<String>,
#[prop(optional, into)] appearance: MaybeProp<MenuAppearance>,
children: Children,
) -> impl IntoView {
) -> impl IntoView
where
T: AddAnyAttr + IntoView + Send + 'static,
{
mount_style("menu", include_str!("./menu.css"));
let config_provider = ConfigInjection::expect_context();

let menu_ref = NodeRef::<Div>::new();
let target_ref = NodeRef::<Div>::new();
let target_ref = NodeRef::<thaw_utils::Element>::new();
let is_show_menu = RwSignal::new(false);
let show_menu_handle = StoredValue::new(None::<TimeoutHandle>);

Expand Down Expand Up @@ -91,7 +99,6 @@ pub fn Menu(
});

let MenuTrigger {
class: trigger_class,
children: trigger_children,
} = menu_trigger;

Expand All @@ -104,15 +111,14 @@ pub fn Menu(
};

view! {
<Binder target_ref>
<div
class=class_list!["thaw-menu-trigger", trigger_class]
node_ref=target_ref
on:mouseenter=on_mouse_enter
on:mouseleave=on_mouse_leave
>
{trigger_children()}
</div>
<Binder>
{trigger_children
.into_inner()()
.into_inner()
.add_any_attr(tachys_class(("thaw-menu-trigger", true)))
.add_any_attr(node_ref(target_ref))
.add_any_attr(on(ev::mouseenter, on_mouse_enter))
.add_any_attr(on(ev::mouseleave, on_mouse_leave))}
<Follower slot show=is_show_menu placement=position>
<Provider value=menu_injection>
<CSSTransition
Expand Down
27 changes: 23 additions & 4 deletions thaw/src/popover/docs/mod.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
# Popover

```rust
/// Prerequisite
/// PopoverTrigger can only contain one child.
view!{
// ✅
<PopoverTrigger slot>
<button>"Click"</button>
</PopoverTrigger>
// ✅
<PopoverTrigger slot>
<Button>"Click"</Button>
</PopoverTrigger>
// 🙅
<PopoverTrigger slot>
<button>"Click"</button>
<button>"Click"</button>
</PopoverTrigger>
}
```

```rust demo
view! {
<Space>
Expand Down Expand Up @@ -165,7 +185,6 @@ view! {

### PopoverTrigger Props

| Name | Type | Default | Description |
| -------- | ------------------- | -------------------- | ----------- |
| class | `MaybeProp<String>` | `Default::default()` | |
| children | `Children` | | |
| Name | Type | Default | Description |
| -------- | ------------------------------------------- | ------- | ----------- |
| children | `T: AddAnyAttr + IntoView + Send + 'static` | | |
44 changes: 25 additions & 19 deletions thaw/src/popover/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,25 @@ mod types;
pub use types::*;

use crate::ConfigInjection;
use leptos::{ev, html, leptos_dom::helpers::TimeoutHandle, prelude::*};
use leptos::{
ev::{self, on},
html,
leptos_dom::helpers::TimeoutHandle,
prelude::*,
tachys::html::{class::class as tachys_class, node_ref::node_ref},
};
use std::time::Duration;
use thaw_components::{Binder, CSSTransition, Follower};
use thaw_utils::{add_event_listener, class_list, mount_style, BoxCallback};

#[component]
pub fn Popover(
pub fn Popover<T>(
#[prop(optional, into)] class: MaybeProp<String>,
/// Action that displays the popover.
#[prop(optional)]
trigger_type: PopoverTriggerType,
/// The element or component that triggers popover.
popover_trigger: PopoverTrigger,
popover_trigger: PopoverTrigger<T>,
/// Configures the position of the Popover.
#[prop(optional)]
position: PopoverPosition,
Expand All @@ -27,12 +33,15 @@ pub fn Popover(
#[prop(optional, into)] on_open: Option<BoxCallback>,
#[prop(optional, into)] on_close: Option<BoxCallback>,
children: Children,
) -> impl IntoView {
) -> impl IntoView
where
T: AddAnyAttr + IntoView + Send + 'static,
{
mount_style("popover", include_str!("./popover.css"));
let config_provider = ConfigInjection::expect_context();

let popover_ref = NodeRef::<html::Div>::new();
let target_ref = NodeRef::<html::Div>::new();
let target_ref = NodeRef::<thaw_utils::Element>::new();
let is_show_popover = RwSignal::new(false);
let show_popover_handle = StoredValue::new(None::<TimeoutHandle>);

Expand Down Expand Up @@ -130,24 +139,21 @@ pub fn Popover(
});

let PopoverTrigger {
class: trigger_class,
children: trigger_children,
} = popover_trigger;

view! {
<Binder target_ref>
<div
class=class_list![
"thaw-popover-trigger",
move || is_show_popover.get().then(|| "thaw-popover-trigger--open".to_string()),
trigger_class
]
node_ref=target_ref
on:mouseenter=on_mouse_enter
on:mouseleave=on_mouse_leave
>
{trigger_children()}
</div>
<Binder>
{trigger_children
.into_inner()()
.into_inner()
.add_any_attr(tachys_class(("thaw-popover-trigger", true)))
.add_any_attr(
tachys_class(("thaw-popover-trigger--open", move || is_show_popover.get())),
)
.add_any_attr(node_ref(target_ref))
.add_any_attr(on(ev::mouseenter, on_mouse_enter))
.add_any_attr(on(ev::mouseleave, on_mouse_leave))}
<Follower slot show=is_show_popover placement=position>
<CSSTransition
node_ref=popover_ref
Expand Down
4 changes: 0 additions & 4 deletions thaw/src/popover/popover.css
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ div.thaw-popover-surface--inverted {
height: 10px;
}

.thaw-popover-trigger {
display: inline-block;
}

[data-thaw-placement="top-start"] > .thaw-popover-surface,
[data-thaw-placement="top-end"] > .thaw-popover-surface,
[data-thaw-placement="top"] > .thaw-popover-surface {
Expand Down
6 changes: 2 additions & 4 deletions thaw/src/popover/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ impl From<PopoverPosition> for FollowerPlacement {
}

#[slot]
pub struct PopoverTrigger {
#[prop(optional, into)]
class: MaybeProp<String>,
children: Children,
pub struct PopoverTrigger<T> {
children: TypedChildren<T>,
}
2 changes: 1 addition & 1 deletion thaw/src/tag_picker/tag_picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub fn TagPicker(
);
};
view! {
<Binder target_ref=trigger_ref>
<Binder>
<div
class=class_list!["thaw-tag-picker-control", class]
node_ref=trigger_ref
Expand Down
2 changes: 1 addition & 1 deletion thaw/src/time_picker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ pub fn TimePicker(
};

view! {
<Binder target_ref=time_picker_ref>
<Binder>
<div
node_ref=time_picker_ref
class=class_list!["thaw-time-picker", class]
Expand Down
15 changes: 7 additions & 8 deletions thaw/src/tooltip/docs/mod.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ view! {
}
```

### Textarea Props
### Tooltip Props

| Name | Type | Default | Description |
| ---------- | --------------------------- | ------------------------- | -------------------------------------- |
| class | `MaybeProp<String>` | `Default::default()` | |
| content | `Option<Signal<String>>` | `None` | The text of the tooltip. |
| position | `TooltipPosition` | `TooltipPosition::Top` | Configure the position of the tooltip. |
| appearance | `Signal<TooltipAppearance>` | `TooltipAppearance::None` | The tooltip's visual appearance. |
| children | `Children` | | |
| Name | Type | Default | Description |
| --- | --- | --- | --- |
| content | `Option<Signal<String>>` | `None` | The text of the tooltip. |
| position | `TooltipPosition` | `TooltipPosition::Top` | Configure the position of the tooltip. |
| appearance | `Signal<TooltipAppearance>` | `TooltipAppearance::None` | The tooltip's visual appearance. |
| children | `T: AddAnyAttr + IntoView + Send + 'static` | | |
4 changes: 0 additions & 4 deletions thaw/src/tooltip/tooltip.css
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ div.thaw-tooltip-content--inverted {
height: 8px;
}

.thaw-tooltip {
display: inline-block;
}

[data-thaw-placement="top-start"] > .thaw-tooltip-content,
[data-thaw-placement="top-end"] > .thaw-tooltip-content,
[data-thaw-placement="top"] > .thaw-tooltip-content {
Expand Down
Loading
Loading