Skip to content

Commit

Permalink
Fix SSR support
Browse files Browse the repository at this point in the history
  • Loading branch information
mondeja committed Mar 12, 2024
1 parent 28c0683 commit 64eaa09
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 42 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

1 change: 0 additions & 1 deletion leptos_hotkeys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ keywords = ["leptos", "hotkeys", "wasm"]
leptos = { version = "0.6.5", features = ["nightly"] }
wasm-bindgen = { version = "0.2.92" }
log = "0.4.20"
cfg-if = { version = "1.0.0", features = [] }
web-sys = { version = "0.3.66" }

[lib]
Expand Down
14 changes: 7 additions & 7 deletions leptos_hotkeys/src/hotkeys_provider.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use leptos::html::ElementDescriptor;
use leptos::*;
#[cfg_attr(feature = "ssr", allow(unused_imports))]
use std::collections::HashMap;
use std::collections::HashSet;

Expand All @@ -11,21 +12,20 @@ use wasm_bindgen::JsCast;
pub struct HotkeysContext {
#[cfg(not(feature = "ssr"))]
pub(crate) pressed_keys: RwSignal<HashMap<String, web_sys::KeyboardEvent>>,

#[cfg(not(feature = "ssr"))]
pub active_ref_target: RwSignal<Option<web_sys::EventTarget>>,

#[cfg(not(feature = "ssr"))]
pub set_ref_target: Callback<Option<web_sys::EventTarget>>,

pub active_scopes: RwSignal<HashSet<String>>,
pub enable_scope: Callback<String>,
pub disable_scope: Callback<String>,
pub toggle_scope: Callback<String>,
}

pub fn provide_hotkeys_context<T>(
node_ref: NodeRef<T>,
allow_blur_event: bool,
#[cfg_attr(feature = "ssr", allow(unused_variables))] node_ref: NodeRef<T>,
#[cfg_attr(feature = "ssr", allow(unused_variables))] allow_blur_event: bool,
initially_active_scopes: HashSet<String>,
) -> HotkeysContext
where
Expand Down Expand Up @@ -81,13 +81,13 @@ where
})
});

#[cfg(feature = "debug")]
#[cfg(all(feature = "debug", not(feature = "ssr")))]
create_effect(move |_| {
let pressed_keys_list =
move || pressed_keys.get().keys().cloned().collect::<Vec<String>>();
let pressed_keys_list = move || pressed_keys.get().keys().cloned().collect::<Vec<String>>();
logging::log!("keys pressed: {:?}", pressed_keys_list());
});

#[cfg(not(feature = "ssr"))]
node_ref.on_load(move |_| {
let blur_listener = wasm_bindgen::closure::Closure::wrap(Box::new(move || {
if cfg!(feature = "debug") {
Expand Down
81 changes: 48 additions & 33 deletions leptos_hotkeys/src/use_hotkeys.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use crate::hotkeys_provider::use_hotkeys_context;
use crate::types::Hotkey;

use leptos::{ev::DOMEventResponder, html::ElementDescriptor, *};
use leptos::{html::ElementDescriptor, *};

use std::collections::{HashMap, HashSet};
use std::collections::HashMap;

fn is_hotkey_match(hotkey: &Hotkey, pressed_keyset: &mut HashMap<String, web_sys::KeyboardEvent>) -> bool {
#[cfg_attr(feature = "ssr", allow(dead_code))]
fn is_hotkey_match(
hotkey: &Hotkey,
pressed_keyset: &mut HashMap<String, web_sys::KeyboardEvent>,
) -> bool {
let mut modifiers_match = true;

if hotkey.modifiers.ctrl {
Expand Down Expand Up @@ -41,47 +44,58 @@ fn is_hotkey_match(hotkey: &Hotkey, pressed_keyset: &mut HashMap<String, web_sys
}

pub fn use_hotkeys_scoped(
key_combination: String,
on_triggered: Callback<()>,
scopes: Vec<String>,
#[cfg_attr(feature = "ssr", allow(unused_variables))] key_combination: String,
#[cfg_attr(feature = "ssr", allow(unused_variables))] on_triggered: Callback<()>,
#[cfg_attr(feature = "ssr", allow(unused_variables))] scopes: Vec<String>,
) {
let parsed_keys: HashSet<Hotkey> = key_combination.split(',').map(Hotkey::new).collect();
#[cfg(not(feature = "ssr"))]
{
use crate::hotkeys_provider::use_hotkeys_context;
use std::collections::HashSet;

let hotkeys_context = use_hotkeys_context();
let pressed_keys = hotkeys_context.pressed_keys;
let parsed_keys: HashSet<Hotkey> = key_combination.split(',').map(Hotkey::new).collect();

create_effect(move |_| {
let active_scopes = hotkeys_context.active_scopes.get();
let within_scope = scopes.iter().any(|scope| active_scopes.contains(scope));

if within_scope {
let mut pressed_keyset = pressed_keys.get();
if let Some(matching_hotkey) = parsed_keys
.iter()
.find(|hotkey| is_hotkey_match(hotkey, &mut pressed_keyset))
{
if cfg!(feature = "debug") {
let message = format!("%cfiring hotkey: {}", &matching_hotkey);
web_sys::console::log_2(
&wasm_bindgen::JsValue::from_str(&message),
&wasm_bindgen::JsValue::from_str("color: #39FF14;"),
);
let hotkeys_context = use_hotkeys_context();
let pressed_keys = hotkeys_context.pressed_keys;

create_effect(move |_| {
let active_scopes = hotkeys_context.active_scopes.get();
let within_scope = scopes.iter().any(|scope| active_scopes.contains(scope));

if within_scope {
let mut pressed_keyset = pressed_keys.get();
if let Some(matching_hotkey) = parsed_keys
.iter()
.find(|hotkey| is_hotkey_match(hotkey, &mut pressed_keyset))
{
if cfg!(feature = "debug") {
let message = format!("%cfiring hotkey: {}", &matching_hotkey);
web_sys::console::log_2(
&wasm_bindgen::JsValue::from_str(&message),
&wasm_bindgen::JsValue::from_str("color: #39FF14;"),
);
}
Callable::call(&on_triggered, ());
}
Callable::call(&on_triggered, ());
}
}
});
});
}
}

pub fn use_hotkeys_ref_scoped<T>(
node_ref: NodeRef<T>,
key_combination: String,
on_triggered: Callback<()>,
scopes: Vec<String>,
#[cfg_attr(feature = "ssr", allow(unused_variables))] node_ref: NodeRef<T>,
#[cfg_attr(feature = "ssr", allow(unused_variables))] key_combination: String,
#[cfg_attr(feature = "ssr", allow(unused_variables))] on_triggered: Callback<()>,
#[cfg_attr(feature = "ssr", allow(unused_variables))] scopes: Vec<String>,
) where
T: ElementDescriptor + 'static + Clone,
{
#[cfg(not(feature = "ssr"))]
create_effect(move |_| {
use crate::hotkeys_provider::use_hotkeys_context;
use leptos::ev::DOMEventResponder;
use std::collections::HashSet;

let parsed_keys: HashSet<Hotkey> = key_combination.split(',').map(Hotkey::new).collect();
let scopes = scopes.clone();
if let Some(element) = node_ref.get() {
Expand All @@ -108,6 +122,7 @@ pub fn use_hotkeys_ref_scoped<T>(
}
};

// element.add needs `leptos::ev::DOMEventResponder`
let _ = element.add(ev::keypress, keydown_closure);
}
});
Expand Down

0 comments on commit 64eaa09

Please sign in to comment.