fixed use_color_mode without cookies

fixes #100
This commit is contained in:
Maccesch 2024-04-27 16:45:38 +01:00
parent ae60e38db3
commit 2cda3f8aca
2 changed files with 22 additions and 31 deletions

View file

@ -3,6 +3,12 @@
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [0.10.9] - 2024-04-27
### Fixes 🍕
- Fixed `use_color_mode` without cookies and make cookies sync properly with local storage
## [0.10.8] - 2024-04-19 ## [0.10.8] - 2024-04-19
### Change 🔥 ### Change 🔥

View file

@ -3,10 +3,7 @@ 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};
use crate::utils::FromToStringCodec; use crate::utils::FromToStringCodec;
use crate::{ use crate::{sync_signal_with_options, use_cookie, use_preferred_dark, SyncSignalOptions};
sync_signal_with_options, use_cookie_with_options, use_preferred_dark, SyncSignalOptions,
UseCookieOptions,
};
use default_struct_builder::DefaultBuilder; use default_struct_builder::DefaultBuilder;
use leptos::*; use leptos::*;
use std::fmt::{Display, Formatter}; use std::fmt::{Display, Formatter};
@ -183,9 +180,8 @@ where
} }
} }
let initial_stored_value = initial_value_from_url.clone().unwrap_or(initial_value);
let (store, set_store) = get_store_signal( let (store, set_store) = get_store_signal(
initial_stored_value.clone(), initial_value_from_url.clone().unwrap_or(initial_value),
storage_signal, storage_signal,
&storage_key, &storage_key,
storage_enabled, storage_enabled,
@ -193,27 +189,20 @@ where
listen_to_storage_changes, listen_to_storage_changes,
); );
let (initial_stored_value, _) = initial_stored_value.into_signal(); let (cookie, set_cookie) = get_cookie_signal(&cookie_name, cookie_enabled);
let initial_stored_value = initial_stored_value.get_untracked();
let (cookie, set_cookie) =
get_cookie_signal(initial_stored_value.clone(), &cookie_name, cookie_enabled);
let _ = sync_signal_with_options(
(cookie, set_cookie),
(store, set_store),
SyncSignalOptions::with_transforms(
{
let initial_stored_value = initial_stored_value.clone();
if cookie_enabled {
let _ = sync_signal_with_options(
(cookie, set_cookie),
(store, set_store),
SyncSignalOptions::with_transforms(
move |cookie: &Option<ColorMode>| { move |cookie: &Option<ColorMode>| {
cookie cookie.clone().unwrap_or_else(|| store.get_untracked())
.clone() },
.unwrap_or_else(|| initial_stored_value.clone()) move |store: &ColorMode| Some(store.clone()),
} ),
}, );
move |store: &ColorMode| Some(store.clone()), }
),
);
if let Some(initial_value_from_url) = initial_value_from_url { if let Some(initial_value_from_url) = initial_value_from_url {
let value = initial_value_from_url.into_signal().0.get_untracked(); let value = initial_value_from_url.into_signal().0.get_untracked();
@ -328,17 +317,13 @@ pub enum ColorMode {
} }
fn get_cookie_signal( fn get_cookie_signal(
initial_value: ColorMode,
cookie_name: &str, cookie_name: &str,
cookie_enabled: bool, cookie_enabled: bool,
) -> (Signal<Option<ColorMode>>, WriteSignal<Option<ColorMode>>) { ) -> (Signal<Option<ColorMode>>, WriteSignal<Option<ColorMode>>) {
if cookie_enabled { if cookie_enabled {
use_cookie_with_options::<ColorMode, FromToStringCodec>( use_cookie::<ColorMode, FromToStringCodec>(cookie_name)
cookie_name,
UseCookieOptions::<ColorMode, _>::default().default_value(Some(initial_value)),
)
} else { } else {
let (value, set_value) = create_signal(Some(initial_value)); let (value, set_value) = create_signal(None);
(value.into(), set_value) (value.into(), set_value)
} }
} }