leptos_use/storage/
use_local_storage.rs

1use super::{use_storage_with_options, StorageType, UseStorageOptions};
2use codee::{Decoder, Encoder};
3use leptos::prelude::*;
4use leptos::reactive::wrappers::read::Signal;
5
6#[allow(rustdoc::bare_urls)]
7/// Reactive [LocalStorage](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage).
8///
9/// LocalStorage stores data in the browser with no expiration time. Access is given to all pages
10/// from the same origin (e.g., all pages from "https://example.com" share the same origin).
11/// While data doesn't expire the user can view, modify and delete all data stored.
12/// Browsers allow 5MB of data to be stored.
13///
14/// This is in contrast to [`use_session_storage`](https://leptos-use.rs/storage/use_session_storage.html) which clears data when the page session ends and is not shared.
15///
16/// ## Usage
17///
18/// See [`use_storage`](https://leptos-use.rs/storage/use_storage.html) for more details on how to use.
19pub fn use_local_storage<T, C>(
20    key: impl Into<Signal<String>>,
21) -> (Signal<T>, WriteSignal<T>, impl Fn() + Clone + Send + Sync)
22where
23    T: Clone + Default + PartialEq + Send + Sync + 'static,
24    C: Encoder<T, Encoded = String> + Decoder<T, Encoded = str>,
25{
26    use_storage_with_options::<T, C>(
27        StorageType::Local,
28        key,
29        UseStorageOptions::<T, <C as Encoder<T>>::Error, <C as Decoder<T>>::Error>::default(),
30    )
31}
32
33/// Accepts [`UseStorageOptions`]. See [`use_local_storage`] for details.
34pub fn use_local_storage_with_options<T, C>(
35    key: impl Into<Signal<String>>,
36    options: UseStorageOptions<T, <C as Encoder<T>>::Error, <C as Decoder<T>>::Error>,
37) -> (Signal<T>, WriteSignal<T>, impl Fn() + Clone + Send + Sync)
38where
39    T: Clone + PartialEq + Send + Sync,
40    C: Encoder<T, Encoded = String> + Decoder<T, Encoded = str>,
41{
42    use_storage_with_options::<T, C>(StorageType::Local, key, options)
43}