Session Storage Utilities
Safely and conveniently interact with the browser's Session Storage API with full type safety and custom serialization.
Overview
A simple set of utilities to interact safely with the browser's Session Storage API. Handles storing, retrieving, and removing typed items scoped to a browser tab session.
getFromSessionStorage
Retrieves a value from session storage, parsing it back into its original type.
Function Signature
function getFromSessionStorage<T>(
key: string,
deserialize?: Deserializer<T>
): T | nullParameters
| Parameter | Type | Description |
|---|---|---|
key | string | The key of the item. |
deserialize | Deserializer<T> (optional) | Custom deserialization function. Defaults to JSON.parse. |
Returns
T | null: The parsed value of typeT, ornullif the key does not exist or parsing fails.
Example Usage
import { getFromSessionStorage } from 'toolbox-x/dom';
const color = getFromSessionStorage<string>('color');
console.log(color); // → e.g. 'blue'saveToSessionStorage
Stores a value in session storage, automatically stringifying objects/arrays.
Function Signature
function saveToSessionStorage<T>(
key: string,
value: T,
serialize?: Serializer<T>
): voidParameters
| Parameter | Type | Description |
|---|---|---|
key | string | The key under which to store the value. |
value | T | The value to store. |
serialize | Serializer<T> (optional) | Custom serialization function. Defaults to JSON.stringify. |
Example Usage
import { saveToSessionStorage } from 'toolbox-x/dom';
saveToSessionStorage('count', 10);
saveToSessionStorage('profile', { id: 7, username: 'user7' });removeFromSessionStorage
Removes an item from session storage.
Function Signature
function removeFromSessionStorage(key: string): voidParameters
| Parameter | Type | Description |
|---|---|---|
key | string | The key of the item to remove. |
Example Usage
import { removeFromSessionStorage } from 'toolbox-x/dom';
removeFromSessionStorage('cart');Related Utilities
- localStorage — Scopes equivalent utility operations to the browser's persistent storage.
Last updated: Mon, Jun 15, 2026 11:04:47AM (UTC)
Local Storage Utilities
Safely and conveniently interact with the browser's Local Storage API, supporting typed items and custom serialization.
Copy Text to Clipboard
Copies text to the user's clipboard using modern clipboard APIs when available, falling back to legacy methods for browser compatibility.
