Merge pull request #79 from mondeja/initial_value_from_url_param

Add options to discover initial color mode from URL param
This commit is contained in:
Marc-Stefan Cassola 2024-02-16 02:22:47 +00:00 committed by GitHub
commit 3ffe3da49d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 55 additions and 2 deletions

View file

@ -70,6 +70,7 @@ features = [
"IntersectionObserver", "IntersectionObserver",
"IntersectionObserverInit", "IntersectionObserverInit",
"IntersectionObserverEntry", "IntersectionObserverEntry",
"Location",
"MediaDevices", "MediaDevices",
"MediaQueryList", "MediaQueryList",
"MediaStream", "MediaStream",
@ -112,6 +113,8 @@ features = [
"Touch", "Touch",
"TouchEvent", "TouchEvent",
"TouchList", "TouchList",
"Url",
"UrlSearchParams",
"VisibilityState", "VisibilityState",
"WebSocket", "WebSocket",
"WebTransport", "WebTransport",

View file

@ -9,6 +9,7 @@ mod position;
mod size; mod size;
mod ssr_safe_method; mod ssr_safe_method;
mod storage; mod storage;
pub(crate) mod url;
pub use connection_ready_state::*; pub use connection_ready_state::*;
pub(crate) use datetime::*; pub(crate) use datetime::*;

20
src/core/url.rs Normal file
View file

@ -0,0 +1,20 @@
use leptos::window;
fn get() -> web_sys::Url {
web_sys::Url::new(
&window()
.location()
.href()
.expect("Failed to get location.href from the browser"),
)
.expect("Failed to parse location.href from the browser")
}
pub mod params {
use super::get as current_url;
/// Get a URL param value from the URL of the browser
pub fn get(k: &str) -> Option<String> {
current_url().search_params().get(k)
}
}

View file

@ -67,7 +67,7 @@ const INTERNAL_STORAGE_EVENT: &str = "leptos-use-storage";
/// pub greeting: String, /// pub greeting: String,
/// } /// }
/// ///
/// // Default can be used to implement intial or deleted values. /// // Default can be used to implement initial or deleted values.
/// // You can also use a signal via UseStorageOptions::default_value` /// // You can also use a signal via UseStorageOptions::default_value`
/// impl Default for MyState { /// impl Default for MyState {
/// fn default() -> Self { /// fn default() -> Self {

View file

@ -1,3 +1,4 @@
use crate::core::url;
use crate::core::StorageType; use crate::core::StorageType;
use crate::core::{ElementMaybeSignal, MaybeRwSignal}; use crate::core::{ElementMaybeSignal, MaybeRwSignal};
use crate::storage::{use_storage_with_options, UseStorageOptions}; use crate::storage::{use_storage_with_options, UseStorageOptions};
@ -106,6 +107,8 @@ where
target, target,
attribute, attribute,
initial_value, initial_value,
initial_value_from_url_param,
initial_value_from_url_param_to_storage,
on_changed, on_changed,
storage_signal, storage_signal,
custom_modes, custom_modes,
@ -136,8 +139,15 @@ where
} }
}); });
let mut initial_value_from_url = None;
if let Some(param) = initial_value_from_url_param.as_ref() {
if let Some(value) = url::params::get(param) {
initial_value_from_url = ColorMode::from_str(&value).map(MaybeRwSignal::Static).ok()
}
}
let (store, set_store) = get_store_signal( let (store, set_store) = get_store_signal(
initial_value, initial_value_from_url.clone().unwrap_or(initial_value),
storage_signal, storage_signal,
&storage_key, &storage_key,
storage_enabled, storage_enabled,
@ -145,6 +155,15 @@ where
listen_to_storage_changes, listen_to_storage_changes,
); );
if let Some(initial_value_from_url) = initial_value_from_url {
let value = initial_value_from_url.into_signal().0.get_untracked();
if initial_value_from_url_param_to_storage {
set_store.set(value);
} else {
set_store.set_untracked(value);
}
}
let state = Signal::derive(move || { let state = Signal::derive(move || {
let value = store.get(); let value = store.get();
if value == ColorMode::Auto { if value == ColorMode::Auto {
@ -330,6 +349,14 @@ where
#[builder(into)] #[builder(into)]
initial_value: MaybeRwSignal<ColorMode>, initial_value: MaybeRwSignal<ColorMode>,
/// Discover the initial value of the color mode from an URL parameter. Defaults to `None`.
#[builder(into)]
initial_value_from_url_param: Option<String>,
/// Update the initial value of the discovered color mode from URL parameter into storage.
/// Defaults to `false`.
initial_value_from_url_param_to_storage: bool,
/// Custom modes that you plan to use as `ColorMode::Custom(x)`. Defaults to `vec![]`. /// Custom modes that you plan to use as `ColorMode::Custom(x)`. Defaults to `vec![]`.
custom_modes: Vec<String>, custom_modes: Vec<String>,
@ -386,6 +413,8 @@ impl Default for UseColorModeOptions<&'static str, web_sys::Element> {
target: "html", target: "html",
attribute: "class".into(), attribute: "class".into(),
initial_value: ColorMode::Auto.into(), initial_value: ColorMode::Auto.into(),
initial_value_from_url_param: None,
initial_value_from_url_param_to_storage: false,
custom_modes: vec![], custom_modes: vec![],
on_changed: Rc::new(move |mode, default_handler| (default_handler)(mode)), on_changed: Rc::new(move |mode, default_handler| (default_handler)(mode)),
storage_signal: None, storage_signal: None,