@mantine/hooks 7.1.4 → 7.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -59,7 +59,13 @@ function createStorage(type, hookName) {
|
|
|
59
59
|
}) {
|
|
60
60
|
const readStorageValue = React.useCallback(
|
|
61
61
|
(skipStorage) => {
|
|
62
|
-
|
|
62
|
+
let storageBlockedOrSkipped;
|
|
63
|
+
try {
|
|
64
|
+
storageBlockedOrSkipped = typeof window === "undefined" || !(type in window) || window[type] === null || !!skipStorage;
|
|
65
|
+
} catch (_e) {
|
|
66
|
+
storageBlockedOrSkipped = true;
|
|
67
|
+
}
|
|
68
|
+
if (storageBlockedOrSkipped) {
|
|
63
69
|
return defaultValue;
|
|
64
70
|
}
|
|
65
71
|
const storageValue = getItem(key);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create-storage.js","sources":["../../src/use-local-storage/create-storage.ts"],"sourcesContent":["/* eslint-disable no-console */\nimport { useState, useCallback, useEffect } from 'react';\nimport { useWindowEvent } from '../use-window-event/use-window-event';\n\nexport type StorageType = 'localStorage' | 'sessionStorage';\n\nexport interface StorageProperties<T> {\n /** Storage key */\n key: string;\n\n /** Default value that will be set if value is not found in storage */\n defaultValue?: T;\n\n /** If set to true, value will be update is useEffect after mount */\n getInitialValueInEffect?: boolean;\n\n /** Function to serialize value into string to be save in storage */\n serialize?: (value: T) => string;\n\n /** Function to deserialize string value from storage to value */\n deserialize?: (value: string | undefined) => T;\n}\n\nfunction serializeJSON<T>(value: T, hookName: string) {\n try {\n return JSON.stringify(value);\n } catch (error) {\n throw new Error(`@mantine/hooks ${hookName}: Failed to serialize the value`);\n }\n}\n\nfunction deserializeJSON(value: string | undefined) {\n try {\n return value && JSON.parse(value);\n } catch {\n return value;\n }\n}\n\nfunction createStorageHandler(type: StorageType) {\n const getItem = (key: string) => {\n try {\n return window[type].getItem(key);\n } catch (error) {\n console.warn('use-local-storage: Failed to get value from storage, localStorage is blocked');\n return null;\n }\n };\n\n const setItem = (key: string, value: string) => {\n try {\n window[type].setItem(key, value);\n } catch (error) {\n console.warn('use-local-storage: Failed to set value to storage, localStorage is blocked');\n }\n };\n\n const removeItem = (key: string) => {\n try {\n window[type].removeItem(key);\n } catch (error) {\n console.warn(\n 'use-local-storage: Failed to remove value from storage, localStorage is blocked'\n );\n }\n };\n\n return { getItem, setItem, removeItem };\n}\n\nexport function createStorage<T>(type: StorageType, hookName: string) {\n const eventName = type === 'localStorage' ? 'mantine-local-storage' : 'mantine-session-storage';\n const { getItem, setItem, removeItem } = createStorageHandler(type);\n\n return function useStorage({\n key,\n defaultValue = undefined,\n getInitialValueInEffect = true,\n deserialize = deserializeJSON,\n serialize = (value: T) => serializeJSON(value, hookName),\n }: StorageProperties<T>) {\n const readStorageValue = useCallback(\n (skipStorage?: boolean): T => {\n
|
|
1
|
+
{"version":3,"file":"create-storage.js","sources":["../../src/use-local-storage/create-storage.ts"],"sourcesContent":["/* eslint-disable no-console */\nimport { useState, useCallback, useEffect } from 'react';\nimport { useWindowEvent } from '../use-window-event/use-window-event';\n\nexport type StorageType = 'localStorage' | 'sessionStorage';\n\nexport interface StorageProperties<T> {\n /** Storage key */\n key: string;\n\n /** Default value that will be set if value is not found in storage */\n defaultValue?: T;\n\n /** If set to true, value will be update is useEffect after mount */\n getInitialValueInEffect?: boolean;\n\n /** Function to serialize value into string to be save in storage */\n serialize?: (value: T) => string;\n\n /** Function to deserialize string value from storage to value */\n deserialize?: (value: string | undefined) => T;\n}\n\nfunction serializeJSON<T>(value: T, hookName: string) {\n try {\n return JSON.stringify(value);\n } catch (error) {\n throw new Error(`@mantine/hooks ${hookName}: Failed to serialize the value`);\n }\n}\n\nfunction deserializeJSON(value: string | undefined) {\n try {\n return value && JSON.parse(value);\n } catch {\n return value;\n }\n}\n\nfunction createStorageHandler(type: StorageType) {\n const getItem = (key: string) => {\n try {\n return window[type].getItem(key);\n } catch (error) {\n console.warn('use-local-storage: Failed to get value from storage, localStorage is blocked');\n return null;\n }\n };\n\n const setItem = (key: string, value: string) => {\n try {\n window[type].setItem(key, value);\n } catch (error) {\n console.warn('use-local-storage: Failed to set value to storage, localStorage is blocked');\n }\n };\n\n const removeItem = (key: string) => {\n try {\n window[type].removeItem(key);\n } catch (error) {\n console.warn(\n 'use-local-storage: Failed to remove value from storage, localStorage is blocked'\n );\n }\n };\n\n return { getItem, setItem, removeItem };\n}\n\nexport function createStorage<T>(type: StorageType, hookName: string) {\n const eventName = type === 'localStorage' ? 'mantine-local-storage' : 'mantine-session-storage';\n const { getItem, setItem, removeItem } = createStorageHandler(type);\n\n return function useStorage({\n key,\n defaultValue = undefined,\n getInitialValueInEffect = true,\n deserialize = deserializeJSON,\n serialize = (value: T) => serializeJSON(value, hookName),\n }: StorageProperties<T>) {\n const readStorageValue = useCallback(\n (skipStorage?: boolean): T => {\n let storageBlockedOrSkipped;\n\n try {\n storageBlockedOrSkipped =\n typeof window === 'undefined' ||\n !(type in window) ||\n window[type] === null ||\n !!skipStorage;\n } catch (_e) {\n storageBlockedOrSkipped = true;\n }\n\n if (storageBlockedOrSkipped) {\n return defaultValue as T;\n }\n\n const storageValue = getItem(key);\n return storageValue !== null ? deserialize(storageValue) : (defaultValue as T);\n },\n [key, defaultValue]\n );\n\n const [value, setValue] = useState<T>(readStorageValue(getInitialValueInEffect));\n\n const setStorageValue = useCallback(\n (val: T | ((prevState: T) => T)) => {\n if (val instanceof Function) {\n setValue((current) => {\n const result = val(current);\n setItem(key, serialize(result));\n window.dispatchEvent(\n new CustomEvent(eventName, { detail: { key, value: val(current) } })\n );\n return result;\n });\n } else {\n setItem(key, serialize(val));\n window.dispatchEvent(new CustomEvent(eventName, { detail: { key, value: val } }));\n setValue(val);\n }\n },\n [key]\n );\n\n const removeStorageValue = useCallback(() => {\n removeItem(key);\n window.dispatchEvent(new CustomEvent(eventName, { detail: { key, value: defaultValue } }));\n }, []);\n\n useWindowEvent('storage', (event) => {\n if (event.storageArea === window[type] && event.key === key) {\n setValue(deserialize(event.newValue ?? undefined));\n }\n });\n\n useWindowEvent(eventName, (event) => {\n if (event.detail.key === key) {\n setValue(event.detail.value);\n }\n });\n\n useEffect(() => {\n if (defaultValue !== undefined && value === undefined) {\n setStorageValue(defaultValue);\n }\n }, [defaultValue, value, setStorageValue]);\n\n useEffect(() => {\n if (getInitialValueInEffect) {\n setValue(readStorageValue());\n }\n }, []);\n\n return [\n value === undefined ? defaultValue : value,\n setStorageValue,\n removeStorageValue,\n ] as const;\n };\n}\n"],"names":["useCallback","useState","useWindowEvent","useEffect"],"mappings":";;;;;;;;AAEA,SAAS,aAAa,CAAC,KAAK,EAAE,QAAQ,EAAE;AACxC,EAAE,IAAI;AACN,IAAI,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;AACjC,GAAG,CAAC,OAAO,KAAK,EAAE;AAClB,IAAI,MAAM,IAAI,KAAK,CAAC,CAAC,eAAe,EAAE,QAAQ,CAAC,+BAA+B,CAAC,CAAC,CAAC;AACjF,GAAG;AACH,CAAC;AACD,SAAS,eAAe,CAAC,KAAK,EAAE;AAChC,EAAE,IAAI;AACN,IAAI,OAAO,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;AACtC,GAAG,CAAC,OAAO,CAAC,EAAE;AACd,IAAI,OAAO,KAAK,CAAC;AACjB,GAAG;AACH,CAAC;AACD,SAAS,oBAAoB,CAAC,IAAI,EAAE;AACpC,EAAE,MAAM,OAAO,GAAG,CAAC,GAAG,KAAK;AAC3B,IAAI,IAAI;AACR,MAAM,OAAO,MAAM,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;AACvC,KAAK,CAAC,OAAO,KAAK,EAAE;AACpB,MAAM,OAAO,CAAC,IAAI,CAAC,8EAA8E,CAAC,CAAC;AACnG,MAAM,OAAO,IAAI,CAAC;AAClB,KAAK;AACL,GAAG,CAAC;AACJ,EAAE,MAAM,OAAO,GA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AAK,CAAC,EAAE;AACvD,QAAQ,eAAe,CAAC,YAAY,CAAC,CAAC;AACtC,OAAO;AACP,KAAK,EAAE,CAAC,YAAY,EAAE,KAAK,EAAE,eAAe,CAAC,CAAC,CAAC;AAC/C,IAAIA,eAAS,CAAC,MAAM;AACpB,MAAM,IAAI,uBAAuB,EAAE;AACnC,QAAQ,QAAQ,CAAC,gBAAgB,EAAE,CAAC,CAAC;AACrC,OAAO;AACP,KAAK,EAAE,EAAE,CAAC,CAAC;AACX,IAAI,OAAO;AACX,MAAM,KAAK,KAAK,KAAK,CAAC,GAAG,YAAY,GAAG,KAAK;AAC7C,MAAM,eAAe;AACrB,MAAM,kBAAkB;AACxB,KAAK,CAAC;AACN,GAAG,CAAC;AACJ;;"}
|
|
@@ -55,7 +55,13 @@ function createStorage(type, hookName) {
|
|
|
55
55
|
}) {
|
|
56
56
|
const readStorageValue = useCallback(
|
|
57
57
|
(skipStorage) => {
|
|
58
|
-
|
|
58
|
+
let storageBlockedOrSkipped;
|
|
59
|
+
try {
|
|
60
|
+
storageBlockedOrSkipped = typeof window === "undefined" || !(type in window) || window[type] === null || !!skipStorage;
|
|
61
|
+
} catch (_e) {
|
|
62
|
+
storageBlockedOrSkipped = true;
|
|
63
|
+
}
|
|
64
|
+
if (storageBlockedOrSkipped) {
|
|
59
65
|
return defaultValue;
|
|
60
66
|
}
|
|
61
67
|
const storageValue = getItem(key);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create-storage.mjs","sources":["../../src/use-local-storage/create-storage.ts"],"sourcesContent":["/* eslint-disable no-console */\nimport { useState, useCallback, useEffect } from 'react';\nimport { useWindowEvent } from '../use-window-event/use-window-event';\n\nexport type StorageType = 'localStorage' | 'sessionStorage';\n\nexport interface StorageProperties<T> {\n /** Storage key */\n key: string;\n\n /** Default value that will be set if value is not found in storage */\n defaultValue?: T;\n\n /** If set to true, value will be update is useEffect after mount */\n getInitialValueInEffect?: boolean;\n\n /** Function to serialize value into string to be save in storage */\n serialize?: (value: T) => string;\n\n /** Function to deserialize string value from storage to value */\n deserialize?: (value: string | undefined) => T;\n}\n\nfunction serializeJSON<T>(value: T, hookName: string) {\n try {\n return JSON.stringify(value);\n } catch (error) {\n throw new Error(`@mantine/hooks ${hookName}: Failed to serialize the value`);\n }\n}\n\nfunction deserializeJSON(value: string | undefined) {\n try {\n return value && JSON.parse(value);\n } catch {\n return value;\n }\n}\n\nfunction createStorageHandler(type: StorageType) {\n const getItem = (key: string) => {\n try {\n return window[type].getItem(key);\n } catch (error) {\n console.warn('use-local-storage: Failed to get value from storage, localStorage is blocked');\n return null;\n }\n };\n\n const setItem = (key: string, value: string) => {\n try {\n window[type].setItem(key, value);\n } catch (error) {\n console.warn('use-local-storage: Failed to set value to storage, localStorage is blocked');\n }\n };\n\n const removeItem = (key: string) => {\n try {\n window[type].removeItem(key);\n } catch (error) {\n console.warn(\n 'use-local-storage: Failed to remove value from storage, localStorage is blocked'\n );\n }\n };\n\n return { getItem, setItem, removeItem };\n}\n\nexport function createStorage<T>(type: StorageType, hookName: string) {\n const eventName = type === 'localStorage' ? 'mantine-local-storage' : 'mantine-session-storage';\n const { getItem, setItem, removeItem } = createStorageHandler(type);\n\n return function useStorage({\n key,\n defaultValue = undefined,\n getInitialValueInEffect = true,\n deserialize = deserializeJSON,\n serialize = (value: T) => serializeJSON(value, hookName),\n }: StorageProperties<T>) {\n const readStorageValue = useCallback(\n (skipStorage?: boolean): T => {\n
|
|
1
|
+
{"version":3,"file":"create-storage.mjs","sources":["../../src/use-local-storage/create-storage.ts"],"sourcesContent":["/* eslint-disable no-console */\nimport { useState, useCallback, useEffect } from 'react';\nimport { useWindowEvent } from '../use-window-event/use-window-event';\n\nexport type StorageType = 'localStorage' | 'sessionStorage';\n\nexport interface StorageProperties<T> {\n /** Storage key */\n key: string;\n\n /** Default value that will be set if value is not found in storage */\n defaultValue?: T;\n\n /** If set to true, value will be update is useEffect after mount */\n getInitialValueInEffect?: boolean;\n\n /** Function to serialize value into string to be save in storage */\n serialize?: (value: T) => string;\n\n /** Function to deserialize string value from storage to value */\n deserialize?: (value: string | undefined) => T;\n}\n\nfunction serializeJSON<T>(value: T, hookName: string) {\n try {\n return JSON.stringify(value);\n } catch (error) {\n throw new Error(`@mantine/hooks ${hookName}: Failed to serialize the value`);\n }\n}\n\nfunction deserializeJSON(value: string | undefined) {\n try {\n return value && JSON.parse(value);\n } catch {\n return value;\n }\n}\n\nfunction createStorageHandler(type: StorageType) {\n const getItem = (key: string) => {\n try {\n return window[type].getItem(key);\n } catch (error) {\n console.warn('use-local-storage: Failed to get value from storage, localStorage is blocked');\n return null;\n }\n };\n\n const setItem = (key: string, value: string) => {\n try {\n window[type].setItem(key, value);\n } catch (error) {\n console.warn('use-local-storage: Failed to set value to storage, localStorage is blocked');\n }\n };\n\n const removeItem = (key: string) => {\n try {\n window[type].removeItem(key);\n } catch (error) {\n console.warn(\n 'use-local-storage: Failed to remove value from storage, localStorage is blocked'\n );\n }\n };\n\n return { getItem, setItem, removeItem };\n}\n\nexport function createStorage<T>(type: StorageType, hookName: string) {\n const eventName = type === 'localStorage' ? 'mantine-local-storage' : 'mantine-session-storage';\n const { getItem, setItem, removeItem } = createStorageHandler(type);\n\n return function useStorage({\n key,\n defaultValue = undefined,\n getInitialValueInEffect = true,\n deserialize = deserializeJSON,\n serialize = (value: T) => serializeJSON(value, hookName),\n }: StorageProperties<T>) {\n const readStorageValue = useCallback(\n (skipStorage?: boolean): T => {\n let storageBlockedOrSkipped;\n\n try {\n storageBlockedOrSkipped =\n typeof window === 'undefined' ||\n !(type in window) ||\n window[type] === null ||\n !!skipStorage;\n } catch (_e) {\n storageBlockedOrSkipped = true;\n }\n\n if (storageBlockedOrSkipped) {\n return defaultValue as T;\n }\n\n const storageValue = getItem(key);\n return storageValue !== null ? deserialize(storageValue) : (defaultValue as T);\n },\n [key, defaultValue]\n );\n\n const [value, setValue] = useState<T>(readStorageValue(getInitialValueInEffect));\n\n const setStorageValue = useCallback(\n (val: T | ((prevState: T) => T)) => {\n if (val instanceof Function) {\n setValue((current) => {\n const result = val(current);\n setItem(key, serialize(result));\n window.dispatchEvent(\n new CustomEvent(eventName, { detail: { key, value: val(current) } })\n );\n return result;\n });\n } else {\n setItem(key, serialize(val));\n window.dispatchEvent(new CustomEvent(eventName, { detail: { key, value: val } }));\n setValue(val);\n }\n },\n [key]\n );\n\n const removeStorageValue = useCallback(() => {\n removeItem(key);\n window.dispatchEvent(new CustomEvent(eventName, { detail: { key, value: defaultValue } }));\n }, []);\n\n useWindowEvent('storage', (event) => {\n if (event.storageArea === window[type] && event.key === key) {\n setValue(deserialize(event.newValue ?? undefined));\n }\n });\n\n useWindowEvent(eventName, (event) => {\n if (event.detail.key === key) {\n setValue(event.detail.value);\n }\n });\n\n useEffect(() => {\n if (defaultValue !== undefined && value === undefined) {\n setStorageValue(defaultValue);\n }\n }, [defaultValue, value, setStorageValue]);\n\n useEffect(() => {\n if (getInitialValueInEffect) {\n setValue(readStorageValue());\n }\n }, []);\n\n return [\n value === undefined ? defaultValue : value,\n setStorageValue,\n removeStorageValue,\n ] as const;\n };\n}\n"],"names":[],"mappings":";;;;AAEA,SAAS,aAAa,CAAC,KAAK,EAAE,QAAQ,EAAE;AACxC,EAAE,IAAI;AACN,IAAI,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;AACjC,GAAG,CAAC,OAAO,KAAK,EAAE;AAClB,IAAI,MAAM,IAAI,KAAK,CAAC,CAAC,eAAe,EAAE,QAAQ,CAAC,+BAA+B,CAAC,CAAC,CAAC;AACjF,GAAG;AACH,CAAC;AACD,SAAS,eAAe,CAAC,KAAK,EAAE;AAChC,EAAE,IAAI;AACN,IAAI,OAAO,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;AACtC,GAAG,CAAC,OAAO,CAAC,EAAE;AACd,IAAI,OAAO,KAAK,CAAC;AACjB,GAAG;AACH,CAAC;AACD,SAAS,oBAAoB,CAAC,I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