Skip to main content

leptos_use/storage/
use_session_storage.rs

1use super::{StorageType, UseStorageOptions, use_storage_with_options};
2use codee::{Decoder, Encoder};
3use leptos::prelude::*;
4
5/// Reactive [SessionStorage](https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage).
6///
7/// SessionStorages stores data in the browser that is deleted when the page session ends. A page session ends when the browser closes the tab. Data is not shared between pages. While data doesn't expire the user can view, modify and delete all data stored. Browsers allow 5MB of data to be stored.
8///
9/// Use [`use_local_storage`](https://leptos-use.rs/storage/use_local_storage.html) to store data that is shared amongst all pages with the same origin and persists between page sessions.
10///
11/// ## Usage
12/// See [`use_storage`](https://leptos-use.rs/storage/use_storage.html) for more details on how to use.
13pub fn use_session_storage<T, C>(
14    key: impl Into<Signal<String>>,
15) -> (Signal<T>, WriteSignal<T>, impl Fn() + Clone + Send + Sync)
16where
17    T: Clone + Default + PartialEq + Send + Sync + 'static,
18    C: Encoder<T, Encoded = String> + Decoder<T, Encoded = str>,
19{
20    use_storage_with_options::<T, C>(
21        StorageType::Session,
22        key,
23        UseStorageOptions::<T, <C as Encoder<T>>::Error, <C as Decoder<T>>::Error>::default(),
24    )
25}
26
27/// Accepts [`UseStorageOptions`]. See [`use_session_storage`] for details.
28pub fn use_session_storage_with_options<T, C>(
29    key: impl Into<Signal<String>>,
30    options: UseStorageOptions<T, <C as Encoder<T>>::Error, <C as Decoder<T>>::Error>,
31) -> (Signal<T>, WriteSignal<T>, impl Fn() + Clone + Send + Sync)
32where
33    T: Clone + PartialEq + Send + Sync,
34    C: Encoder<T, Encoded = String> + Decoder<T, Encoded = str>,
35{
36    use_storage_with_options::<T, C>(StorageType::Session, key, options)
37}