-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Serhii Chernenko
committed
Sep 10, 2024
1 parent
cdd3796
commit f3ffc75
Showing
2 changed files
with
59 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,29 @@ | ||
export function useLocalStorage(key, defaultValue) { | ||
return defaultValue | ||
import type { Ref } from 'vue' | ||
import { shallowRef, watchEffect, toValue, onMounted, onUnmounted } from 'vue' | ||
|
||
export const useLocalStorage = ( | ||
key: string, | ||
defaultValue: string, | ||
): Ref<string> => { | ||
const value = shallowRef<string>(localStorage.getItem(key) ?? defaultValue) | ||
|
||
watchEffect(() => localStorage.setItem(key, toValue(value))) | ||
|
||
const listener = (event: StorageEvent) => { | ||
if (event.key !== key) { | ||
return | ||
} | ||
|
||
value.value = event.newValue ?? defaultValue | ||
} | ||
|
||
onMounted(() => { | ||
window.addEventListener('storage', listener) | ||
}) | ||
|
||
onUnmounted(() => { | ||
window.removeEventListener('storage', listener) | ||
}) | ||
|
||
return value | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,51 @@ | ||
import { describe, it, expect, beforeEach } from 'vitest' | ||
import { createApp } from 'vue' | ||
import { useLocalStorage } from '../src/composables/useLocalStorage' | ||
|
||
const storageKey = 'test' | ||
|
||
describe('useLocalStorage', () => { | ||
beforeEach(function () { | ||
localStorage.clear() | ||
const withSetup = (composable) => { | ||
let result | ||
const app = createApp({ | ||
setup() { | ||
result = composable() | ||
|
||
return () => {} | ||
}, | ||
}) | ||
|
||
app.mount(document.createElement('div')) | ||
|
||
return [result, app] | ||
} | ||
|
||
beforeEach(() => localStorage.clear()) | ||
|
||
describe('useLocalStorage', () => { | ||
it('should work w/ default value', () => { | ||
const storage = useLocalStorage(storageKey, 'default') | ||
const [storage, app] = withSetup(() => { | ||
return useLocalStorage(storageKey, 'default') | ||
}) | ||
|
||
expect(storage.value).toBe('default') | ||
expect(localStorage.getItem(storageKey)).toBe('default') | ||
|
||
app.unmount() | ||
}) | ||
|
||
it('should work w/ storage event', () => { | ||
const storage = useLocalStorage(storageKey, 'default') | ||
const [storage, app] = withSetup(() => { | ||
return useLocalStorage(storageKey, 'default') | ||
}) | ||
const storageEvent = new StorageEvent('storage', { | ||
key: storageKey, | ||
newValue: 'new value', | ||
}) | ||
|
||
window.dispatchEvent(storageEvent) | ||
|
||
expect(storage.value).toBe('new value') | ||
|
||
app.unmount() | ||
}) | ||
}) |