Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
chore(deps): update label to leptos 0.7
  • Loading branch information
geoffreygarrett committed Jan 1, 2025
commit f85f380d831c43c9ed9159f7dbf9550ce1e6fc2c
1 change: 1 addition & 0 deletions packages/primitives/leptos/label/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ version.workspace = true
[dependencies]
leptos.workspace = true
radix-leptos-primitive = { path = "../primitive", version = "0.0.2" }
leptos-node-ref.workspace = true
web-sys.workspace = true
43 changes: 29 additions & 14 deletions packages/primitives/leptos/label/src/label.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,53 @@
use leptos::{ev::MouseEvent, html::AnyElement, *};
use leptos::{ev::MouseEvent, html, prelude::*};
use radix_leptos_primitive::Primitive;
use leptos_node_ref::prelude::*;

/* -------------------------------------------------------------------------------------------------
* Label
* -----------------------------------------------------------------------------------------------*/

const NAME: &str = "Label";

#[component]
#[allow(non_snake_case)]
pub fn Label(
children: TypedChildrenFn<impl IntoView + 'static>,
#[prop(into, optional)] as_child: MaybeProp<bool>,
#[prop(into, optional)] on_mouse_down: Option<Callback<MouseEvent>>,
#[prop(attrs)] attrs: Vec<(&'static str, Attribute)>,
#[prop(optional)] node_ref: NodeRef<AnyElement>,
children: ChildrenFn,
#[prop(into, optional)] node_ref: AnyNodeRef,
) -> impl IntoView {
view! {
<Primitive
children={children}
element=html::label
as_child=as_child
on:mousedown=move |event: MouseEvent| {
// Only prevent text selection if clicking inside the label itself
// only prevent text selection if clicking inside the label itself
let target: web_sys::Element = event_target(&event);
if target.closest("button, input, select, textarea").expect("Element should be able to query closest.").is_some() {
return
if target
.closest("button, input, select, textarea")
.expect("Element should be able to query closest.")
.is_some()
{
return;
}

if let Some(on_mouse_down) = on_mouse_down {
Callable::call(&on_mouse_down, event.clone());
on_mouse_down.run( event.clone());
}

// Prevent text selection when double clicking label
// prevent text selection when double-clicking label
if !event.default_prevented() && event.detail() > 1 {
event.prevent_default();
}
}
node_ref=node_ref
attrs=attrs
>
{children()}
</Primitive>
/>
}
}

/* -----------------------------------------------------------------------------------------------*/

pub mod primitive {
pub use super::*;
pub use Label as Root;
}