@mantine/hooks
Advanced tools
Comparing version 7.0.0 to 7.0.1-alpha.0
@@ -13,3 +13,3 @@ 'use strict'; | ||
React.useEffect(() => clearTimeout, []); | ||
const debouncedSetValue = (newValue) => { | ||
const debouncedSetValue = React.useCallback((newValue) => { | ||
clearTimeout(); | ||
@@ -25,3 +25,3 @@ if (leadingRef.current && options.leading) { | ||
leadingRef.current = false; | ||
}; | ||
}, [options.leading]); | ||
return [value, debouncedSetValue]; | ||
@@ -28,0 +28,0 @@ } |
@@ -22,4 +22,32 @@ 'use strict'; | ||
} | ||
function createStorageHandler(type) { | ||
const getItem = (key) => { | ||
try { | ||
return window[type].getItem(key); | ||
} catch (error) { | ||
console.warn("use-local-storage: Failed to get value from storage, localStorage is blocked"); | ||
return null; | ||
} | ||
}; | ||
const setItem = (key, value) => { | ||
try { | ||
window[type].setItem(key, value); | ||
} catch (error) { | ||
console.warn("use-local-storage: Failed to set value to storage, localStorage is blocked"); | ||
} | ||
}; | ||
const removeItem = (key) => { | ||
try { | ||
window[type].removeItem(key); | ||
} catch (error) { | ||
console.warn( | ||
"use-local-storage: Failed to remove value from storage, localStorage is blocked" | ||
); | ||
} | ||
}; | ||
return { getItem, setItem, removeItem }; | ||
} | ||
function createStorage(type, hookName) { | ||
const eventName = type === "localStorage" ? "mantine-local-storage" : "mantine-session-storage"; | ||
const { getItem, setItem, removeItem } = createStorageHandler(type); | ||
return function useStorage({ | ||
@@ -37,3 +65,3 @@ key, | ||
} | ||
const storageValue = window[type].getItem(key); | ||
const storageValue = getItem(key); | ||
return storageValue !== null ? deserialize(storageValue) : defaultValue; | ||
@@ -49,3 +77,3 @@ }, | ||
const result = val(current); | ||
window[type].setItem(key, serialize(result)); | ||
setItem(key, serialize(result)); | ||
window.dispatchEvent( | ||
@@ -57,3 +85,3 @@ new CustomEvent(eventName, { detail: { key, value: val(current) } }) | ||
} else { | ||
window[type].setItem(key, serialize(val)); | ||
setItem(key, serialize(val)); | ||
window.dispatchEvent(new CustomEvent(eventName, { detail: { key, value: val } })); | ||
@@ -66,3 +94,3 @@ setValue(val); | ||
const removeStorageValue = React.useCallback(() => { | ||
window[type].removeItem(key); | ||
removeItem(key); | ||
window.dispatchEvent(new CustomEvent(eventName, { detail: { key, value: defaultValue } })); | ||
@@ -69,0 +97,0 @@ }, []); |
export type StorageType = 'localStorage' | 'sessionStorage'; | ||
export interface IStorageProperties<T> { | ||
export interface StorageProperties<T> { | ||
/** Storage key */ | ||
@@ -14,2 +14,2 @@ key: string; | ||
} | ||
export declare function createStorage<T>(type: StorageType, hookName: string): ({ key, defaultValue, getInitialValueInEffect, deserialize, serialize, }: IStorageProperties<T>) => readonly [T | undefined, (val: T | ((prevState: T) => T)) => void, () => void]; | ||
export declare function createStorage<T>(type: StorageType, hookName: string): ({ key, defaultValue, getInitialValueInEffect, deserialize, serialize, }: StorageProperties<T>) => readonly [T | undefined, (val: T | ((prevState: T) => T)) => void, () => void]; |
@@ -1,2 +0,2 @@ | ||
import { IStorageProperties } from './create-storage'; | ||
export declare function useLocalStorage<T = string>(props: IStorageProperties<T>): readonly [T | undefined, (val: T | ((prevState: T) => T)) => void, () => void]; | ||
import { StorageProperties } from './create-storage'; | ||
export declare function useLocalStorage<T = string>(props: StorageProperties<T>): readonly [T | undefined, (val: T | ((prevState: T) => T)) => void, () => void]; |
@@ -1,2 +0,2 @@ | ||
import { IStorageProperties } from '../use-local-storage/create-storage'; | ||
export declare function useSessionStorage<T = string>(props: IStorageProperties<T>): readonly [T | undefined, (val: T | ((prevState: T) => T)) => void, () => void]; | ||
import { StorageProperties } from '../use-local-storage/create-storage'; | ||
export declare function useSessionStorage<T = string>(props: StorageProperties<T>): readonly [T | undefined, (val: T | ((prevState: T) => T)) => void, () => void]; |
{ | ||
"name": "@mantine/hooks", | ||
"version": "7.0.0", | ||
"version": "7.0.1-alpha.0", | ||
"main": "./cjs/index.js", | ||
"types": "./lib/index.d.ts", | ||
"module": "./esm/index.js", | ||
"module": "./esm/index.mjs", | ||
"exports": { | ||
".": { | ||
"import": "./esm/index.js", | ||
"require": "./cjs/index.js", | ||
"types": "./lib/index.d.ts" | ||
"import": { | ||
"types": "./lib/index.d.mts", | ||
"default": "./esm/index.mjs" | ||
}, | ||
"require": { | ||
"types": "./lib/index.d.ts", | ||
"default": "./cjs/index.js" | ||
} | ||
} | ||
@@ -12,0 +18,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
530328
374
5177
1