Skip to content

Update Primitive to Leptos 0.7 #417

New issue

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

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

Already on GitHub? # to your account

Merged
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
34 changes: 26 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ members = [
"packages/primitives/leptos/focus-guards",
"packages/primitives/leptos/id",
"packages/primitives/leptos/label",
"packages/primitives/leptos/primitive",
"packages/primitives/leptos/use-controllable-state",
"packages/primitives/leptos/use-escape-keydown",
"packages/primitives/leptos/use-previous",
Expand All @@ -41,14 +42,16 @@ repository = "https://github.com/RustForWeb/radix"
version = "0.0.2"

[workspace.dependencies]
console_log = "1.0.0"
console_error_panic_hook = "0.1.7"
console_log = "1.0.0"
dioxus = "0.6.1"
leptos = "0.7.2"
leptos_dom = "0.7.2"
leptos_router = "0.7.2"
leptos-node-ref = "0.0.3"
leptos-style = "0.0.3"
leptos-maybe-callback = "0.1.0"
leptos-node-ref = "0.1.0"
leptos-style = "0.1.0"
leptos-typed-fallback-show = "0.1.0"
log = "0.4.22"
send_wrapper = "0.6.0"
serde = "1.0.198"
Expand Down
14 changes: 14 additions & 0 deletions packages/primitives/leptos/primitive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "radix-leptos-primitive"
description = "Leptos port of Radix Primitive."

authors.workspace = true
edition.workspace = true
license.workspace = true
repository.workspace = true
version.workspace = true

[dependencies]
leptos.workspace = true
leptos-node-ref.workspace = true
leptos-typed-fallback-show.workspace = true
21 changes: 21 additions & 0 deletions packages/primitives/leptos/primitive/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<p align="center">
<a href="../../../../logo.svg">
<img src="../../../../logo.svg" width="300" height="200" alt="Rust Radix Logo">
</a>
</p>

<h1 align="center">radix-leptos-primitive</h1>

This is an internal utility, not intended for public usage.

[Rust Radix](https://github.com/RustForWeb/radix) is a Rust port of [Radix](https://www.radix-ui.com/primitives).

## Documentation

See [the Rust Radix book](https://radix.rustforweb.org/) for documentation.

## Rust For Web

The Rust Radix project is part of [Rust For Web](https://github.com/RustForWeb).

[Rust For Web](https://github.com/RustForWeb) creates and ports web UI libraries for Rust. All projects are free and open source.
9 changes: 9 additions & 0 deletions packages/primitives/leptos/primitive/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//! Leptos port of [Radix Primitive](https://www.radix-ui.com/primitives).
//!
//! This is an internal utility, not intended for public usage.

//! See [`@radix-ui/react-primitive`](https://www.npmjs.com/package/@radix-ui/react-primitive) for the original package.

mod primitive;

pub use primitive::*;
88 changes: 88 additions & 0 deletions packages/primitives/leptos/primitive/src/primitive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
use leptos::{
ev::Event,
html::{ElementType, HtmlElement},
prelude::*,
tachys::html::node_ref::NodeRefContainer,
wasm_bindgen::JsCast,
};
use leptos_node_ref::{any_node_ref, AnyNodeRef};
use leptos_typed_fallback_show::TypedFallbackShow;

#[component]
pub fn Primitive<E, C>(
element: fn() -> HtmlElement<E, (), ()>,
#[prop(into, optional)] as_child: MaybeProp<bool>,
#[prop(into, optional)] node_ref: AnyNodeRef,
children: TypedChildrenFn<C>,
) -> impl IntoView
where
E: ElementType + 'static,
C: IntoView + 'static,
View<C>: RenderHtml,
HtmlElement<E, (), ()>: ElementChild<View<C>>,
<HtmlElement<E, (), ()> as ElementChild<View<C>>>::Output: IntoView,
<E as ElementType>::Output: JsCast,
AnyNodeRef: NodeRefContainer<E>,
{
let children = StoredValue::new(children.into_inner());

view! {
<TypedFallbackShow
when=move || as_child.get().unwrap_or_default()
fallback=move || {
element().child(children.with_value(|children| children())).add_any_attr(any_node_ref(node_ref))
}
>
{children.with_value(|children| children()).add_any_attr(any_node_ref(node_ref))}
</TypedFallbackShow>
}
}

#[component]
pub fn VoidPrimitive<E, C>(
element: fn() -> HtmlElement<E, (), ()>,
#[prop(into, optional)] as_child: MaybeProp<bool>,
#[prop(into, optional)] node_ref: AnyNodeRef,
children: TypedChildrenFn<C>,
) -> impl IntoView
where
E: ElementType + 'static,
C: IntoView + 'static,
View<C>: RenderHtml,
<E as ElementType>::Output: JsCast,
AnyNodeRef: NodeRefContainer<E>,
{
let children = StoredValue::new(children.into_inner());

view! {
<TypedFallbackShow
when=move || as_child.get().unwrap_or_default()
fallback=move || { element().add_any_attr(any_node_ref(node_ref)) }
>
{children.with_value(|children| children()).add_any_attr(any_node_ref(node_ref))}
</TypedFallbackShow>
}
}

pub fn compose_callbacks<E>(
original_handler: Option<Callback<E>>,
our_handler: Option<Callback<E>>,
check_default_prevented: Option<bool>,
) -> impl Fn(E)
where
E: Clone + Into<Event> + 'static,
{
let check_default_prevented = check_default_prevented.unwrap_or(true);

move |event: E| {
if let Some(original) = &original_handler {
original.run(event.clone());
}

if !check_default_prevented || !event.clone().into().default_prevented() {
if let Some(our) = &our_handler {
our.run(event);
}
}
}
}