Skip to content

Commit

Permalink
feat(useSessionStorage): init (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
2nthony authored Sep 17, 2021
1 parent c5aba7f commit d1f1426
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/core/useSessionStorage/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
category: State
---

# useSessionStorage

Reactive [SessionStorage](https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage).

## Usage

Please refer to `useStorage`
49 changes: 49 additions & 0 deletions packages/core/useSessionStorage/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { MaybeWritable, RemovableWritable } from '@svelte-use/shared'
import { useStorage, StorageOptions } from '../useStorage'
import { defaultWindow } from '../_configurable'

export function useSessionStorage(
key: string,
initialValue: MaybeWritable<string>,
options?: StorageOptions<string>,
): RemovableWritable<string>
export function useSessionStorage(
key: string,
initialValue: MaybeWritable<boolean>,
options?: StorageOptions<boolean>,
): RemovableWritable<boolean>
export function useSessionStorage(
key: string,
initialValue: MaybeWritable<number>,
options?: StorageOptions<number>,
): RemovableWritable<number>
export function useSessionStorage<T>(
key: string,
initialValue: MaybeWritable<T>,
options?: StorageOptions<T>,
): RemovableWritable<T>
export function useSessionStorage<T = unknown>(
key: string,
initialValue: MaybeWritable<null>,
options?: StorageOptions<T>,
): RemovableWritable<T>

/**
* Reactive SessionStorage
*
* @see https://svelte-use.vercel.app/core/useSessionStorage
* @param key
* @param initialValue
* @param options
*/
export function useSessionStorage<
T extends boolean | object | number | string | null,
>(
key: string,
initialValue: MaybeWritable<T>,
options: StorageOptions<T> = {},
): RemovableWritable<T> {
const { window = defaultWindow } = options

return useStorage(key, initialValue, window?.sessionStorage, options)
}

0 comments on commit d1f1426

Please sign in to comment.