From 85c7a3b7ae63a1c57b1611cf50f4db887050cb17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABlle=20Huisman?= Date: Sun, 30 Jun 2024 12:19:55 +0200 Subject: [PATCH] Add use escape key down --- .../leptos/use-escape-keydown/Cargo.toml | 12 +++++ .../leptos/use-escape-keydown/README.md | 13 +++++ .../leptos/use-escape-keydown/src/lib.rs | 9 ++++ .../src/use_escape_keydown.rs | 53 +++++++++++++++++++ 4 files changed, 87 insertions(+) create mode 100644 packages/primitives/leptos/use-escape-keydown/Cargo.toml create mode 100644 packages/primitives/leptos/use-escape-keydown/README.md create mode 100644 packages/primitives/leptos/use-escape-keydown/src/lib.rs create mode 100644 packages/primitives/leptos/use-escape-keydown/src/use_escape_keydown.rs diff --git a/packages/primitives/leptos/use-escape-keydown/Cargo.toml b/packages/primitives/leptos/use-escape-keydown/Cargo.toml new file mode 100644 index 00000000..7085ada5 --- /dev/null +++ b/packages/primitives/leptos/use-escape-keydown/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "radix-leptos-use-escape-keydown" +description = "Leptos port of Radix Use Escape Key Down." + +authors.workspace = true +edition.workspace = true +license.workspace = true +repository.workspace = true +version.workspace = true + +[dependencies] +leptos.workspace = true diff --git a/packages/primitives/leptos/use-escape-keydown/README.md b/packages/primitives/leptos/use-escape-keydown/README.md new file mode 100644 index 00000000..4636866f --- /dev/null +++ b/packages/primitives/leptos/use-escape-keydown/README.md @@ -0,0 +1,13 @@ +

+ + + +

+ +

radix-leptos-use-keyboard-keydown

+ +This is an internal utility, not intended for public usage. + +## Rust Radix + +[Rust Radix](https://github.com/RustForWeb/radix) is a Rust port of [Radix](https://www.radix-ui.com/primitives). diff --git a/packages/primitives/leptos/use-escape-keydown/src/lib.rs b/packages/primitives/leptos/use-escape-keydown/src/lib.rs new file mode 100644 index 00000000..313982fa --- /dev/null +++ b/packages/primitives/leptos/use-escape-keydown/src/lib.rs @@ -0,0 +1,9 @@ +//! Leptos port of [Radix Use Escape Key Down](https://www.radix-ui.com/primitives). +//! +//! This is an internal utility, not intended for public usage. +//! +//! See [`@radix-ui/react-use-escape-keydown`](https://www.npmjs.com/package/@radix-ui/react-use-escpae-keydown) for the original package. + +mod use_escape_keydown; + +pub use use_escape_keydown::*; diff --git a/packages/primitives/leptos/use-escape-keydown/src/use_escape_keydown.rs b/packages/primitives/leptos/use-escape-keydown/src/use_escape_keydown.rs new file mode 100644 index 00000000..abdcfbb3 --- /dev/null +++ b/packages/primitives/leptos/use-escape-keydown/src/use_escape_keydown.rs @@ -0,0 +1,53 @@ +use std::rc::Rc; + +use leptos::{ + create_effect, document, + ev::KeyboardEvent, + on_cleanup, + web_sys::{ + wasm_bindgen::{closure::Closure, JsCast}, + AddEventListenerOptions, Document, + }, + Callable, Callback, StoredValue, +}; + +/// Listens for when the escape key is down. +pub fn use_escape_keydown( + on_escape_key_down: Option>, + owner_document: Option, +) { + let owner_document = StoredValue::new(owner_document.unwrap_or(document())); + + let handle_key_down: Rc> = + Rc::new(Closure::new(move |event: KeyboardEvent| { + if event.key() == "Escape" { + if let Some(on_escape_key_down) = on_escape_key_down { + on_escape_key_down.call(event); + } + } + })); + let cleanup_handle_key_down = handle_key_down.clone(); + + create_effect(move |_| { + owner_document + .get_value() + .add_event_listener_with_callback_and_add_event_listener_options( + "keydown", + (*handle_key_down).as_ref().unchecked_ref(), + AddEventListenerOptions::new().capture(true), + ) + .expect("Key down event listener should be added."); + }); + + on_cleanup(move || { + // TODO: change to options once web_sys supports it + owner_document + .get_value() + .remove_event_listener_with_callback_and_bool( + "keydown", + (*cleanup_handle_key_down).as_ref().unchecked_ref(), + true, + ) + .expect("Key down event listener should be removed."); + }); +}