@mantine/hooks 4.0.9 → 4.1.2
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.
- package/cjs/use-local-storage/use-local-storage.js +8 -1
- package/cjs/use-local-storage/use-local-storage.js.map +1 -1
- package/cjs/use-move/use-move.js +1 -1
- package/cjs/use-move/use-move.js.map +1 -1
- package/esm/use-local-storage/use-local-storage.js +8 -1
- package/esm/use-local-storage/use-local-storage.js.map +1 -1
- package/esm/use-move/use-move.js +1 -1
- package/esm/use-move/use-move.js.map +1 -1
- package/lib/use-local-storage/use-local-storage.d.ts +7 -1
- package/lib/use-local-storage/use-local-storage.d.ts.map +1 -1
- package/package.json +1 -1
|
@@ -22,11 +22,12 @@ function deserializeJSON(value) {
|
|
|
22
22
|
function useLocalStorage({
|
|
23
23
|
key,
|
|
24
24
|
defaultValue = void 0,
|
|
25
|
+
getInitialValueInEffect = false,
|
|
25
26
|
deserialize = deserializeJSON,
|
|
26
27
|
serialize = serializeJSON
|
|
27
28
|
}) {
|
|
28
29
|
var _a;
|
|
29
|
-
const [value, setValue] = react.useState(typeof window !== "undefined" && "localStorage" in window ? deserialize((_a = window.localStorage.getItem(key)) != null ? _a : void 0) : defaultValue != null ? defaultValue : "");
|
|
30
|
+
const [value, setValue] = react.useState(typeof window !== "undefined" && "localStorage" in window && !getInitialValueInEffect ? deserialize((_a = window.localStorage.getItem(key)) != null ? _a : void 0) : defaultValue != null ? defaultValue : "");
|
|
30
31
|
const setLocalStorageValue = react.useCallback((val) => {
|
|
31
32
|
if (val instanceof Function) {
|
|
32
33
|
setValue((current) => {
|
|
@@ -50,6 +51,12 @@ function useLocalStorage({
|
|
|
50
51
|
setLocalStorageValue(defaultValue);
|
|
51
52
|
}
|
|
52
53
|
}, [defaultValue, value, setLocalStorageValue]);
|
|
54
|
+
react.useEffect(() => {
|
|
55
|
+
var _a2;
|
|
56
|
+
if (getInitialValueInEffect) {
|
|
57
|
+
setValue(deserialize((_a2 = window.localStorage.getItem(key)) != null ? _a2 : void 0) || (defaultValue != null ? defaultValue : ""));
|
|
58
|
+
}
|
|
59
|
+
}, []);
|
|
53
60
|
return [value === void 0 ? defaultValue : value, setLocalStorageValue];
|
|
54
61
|
}
|
|
55
62
|
const useLocalStorageValue = useLocalStorage;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-local-storage.js","sources":["../../src/use-local-storage/use-local-storage.ts"],"sourcesContent":["import { useState, useCallback, useEffect } from 'react';\nimport { useWindowEvent } from '../use-window-event/use-window-event';\n\ninterface UseLocalStorage<T> {\n key: string;\n defaultValue?: T;\n serialize?(value: T): string;\n deserialize?(value: string): T;\n}\n\nfunction serializeJSON<T>(value: T) {\n try {\n return JSON.stringify(value);\n } catch (error) {\n throw new Error('@mantine/hooks use-local-storage: Failed to serialize the value');\n }\n}\n\nfunction deserializeJSON(value: string) {\n try {\n return JSON.parse(value);\n } catch {\n return value;\n }\n}\n\nexport function useLocalStorage<T = string>({\n key,\n defaultValue = undefined,\n deserialize = deserializeJSON,\n serialize = serializeJSON,\n}: UseLocalStorage<T>) {\n const [value, setValue] = useState<T>(\n typeof window !== 'undefined' && 'localStorage' in window\n ? deserialize(window.localStorage.getItem(key) ?? undefined)\n : ((defaultValue ?? '') as T)\n );\n\n const setLocalStorageValue = useCallback(\n (val: T | ((prevState: T) => T)) => {\n if (val instanceof Function) {\n setValue((current) => {\n const result = val(current);\n window.localStorage.setItem(key, serialize(result));\n return result;\n });\n } else {\n window.localStorage.setItem(key, serialize(val));\n setValue(val);\n }\n },\n [key]\n );\n\n useWindowEvent('storage', (event) => {\n if (event.storageArea === window.localStorage && event.key === key) {\n setValue(deserialize(event.newValue ?? undefined));\n }\n });\n\n useEffect(() => {\n if (defaultValue !== undefined && value === undefined) {\n setLocalStorageValue(defaultValue);\n }\n }, [defaultValue, value, setLocalStorageValue]);\n\n return [value === undefined ? defaultValue : value, setLocalStorageValue] as const;\n}\n\nexport const useLocalStorageValue = useLocalStorage;\n"],"names":["useState","useCallback","useWindowEvent","useEffect"],"mappings":";;;;;;;AAEA,SAAS,aAAa,CAAC,KAAK,EAAE;AAC9B,EAAE,IAAI;AACN,IAAI,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;AACjC,GAAG,CAAC,OAAO,KAAK,EAAE;AAClB,IAAI,MAAM,IAAI,KAAK,CAAC,iEAAiE,CAAC,CAAC;AACvF,GAAG;AACH,CAAC;AACD,SAAS,eAAe,CAAC,KAAK,EAAE;AAChC,EAAE,IAAI;AACN,IAAI,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;AAC7B,GAAG,CAAC,OAAO,CAAC,EAAE;AACd,IAAI,OAAO,KAAK,CAAC;AACjB,GAAG;AACH,CAAC;AACM,SAAS,eAAe,CAAC;AAChC,EAAE,GAAG;AACL,EAAE,YAAY,GAAG,KAAK,CAAC;AACvB,EAAE,WAAW,GAAG,eAAe;AAC/B,EAAE,SAAS,GAAG,aAAa;AAC3B,CAAC,EAAE;AACH,EAAE,IAAI,EAAE,CAAC;AACT,EAAE,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAGA,cAAQ,CAAC,OAAO,MAAM,KAAK,WAAW,IAAI,cAAc,IAAI,MAAM,GAAG,WAAW,CAAC,CAAC,EAAE,GAAG,MAAM,CAAC,YAAY,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,IAAI,GAAG,EAAE,GAAG,KAAK,CAAC,CAAC,GAAG,YAAY,IAAI,IAAI,GAAG,YAAY,GAAG,EAAE,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"use-local-storage.js","sources":["../../src/use-local-storage/use-local-storage.ts"],"sourcesContent":["import { useState, useCallback, useEffect } from 'react';\nimport { useWindowEvent } from '../use-window-event/use-window-event';\n\ninterface UseLocalStorage<T> {\n /** Local storage key */\n key: string;\n\n /** Default value that will be set if value is not found in local 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 local storage */\n serialize?(value: T): string;\n\n /** Function to deserialize string value from local storage to value */\n deserialize?(value: string): T;\n}\n\nfunction serializeJSON<T>(value: T) {\n try {\n return JSON.stringify(value);\n } catch (error) {\n throw new Error('@mantine/hooks use-local-storage: Failed to serialize the value');\n }\n}\n\nfunction deserializeJSON(value: string) {\n try {\n return JSON.parse(value);\n } catch {\n return value;\n }\n}\n\nexport function useLocalStorage<T = string>({\n key,\n defaultValue = undefined,\n getInitialValueInEffect = false,\n deserialize = deserializeJSON,\n serialize = serializeJSON,\n}: UseLocalStorage<T>) {\n const [value, setValue] = useState<T>(\n typeof window !== 'undefined' && 'localStorage' in window && !getInitialValueInEffect\n ? deserialize(window.localStorage.getItem(key) ?? undefined)\n : ((defaultValue ?? '') as T)\n );\n\n const setLocalStorageValue = useCallback(\n (val: T | ((prevState: T) => T)) => {\n if (val instanceof Function) {\n setValue((current) => {\n const result = val(current);\n window.localStorage.setItem(key, serialize(result));\n return result;\n });\n } else {\n window.localStorage.setItem(key, serialize(val));\n setValue(val);\n }\n },\n [key]\n );\n\n useWindowEvent('storage', (event) => {\n if (event.storageArea === window.localStorage && event.key === key) {\n setValue(deserialize(event.newValue ?? undefined));\n }\n });\n\n useEffect(() => {\n if (defaultValue !== undefined && value === undefined) {\n setLocalStorageValue(defaultValue);\n }\n }, [defaultValue, value, setLocalStorageValue]);\n\n useEffect(() => {\n if (getInitialValueInEffect) {\n setValue(\n deserialize(window.localStorage.getItem(key) ?? undefined) || ((defaultValue ?? '') as T)\n );\n }\n }, []);\n\n return [value === undefined ? defaultValue : value, setLocalStorageValue] as const;\n}\n\nexport const useLocalStorageValue = useLocalStorage;\n"],"names":["useState","useCallback","useWindowEvent","useEffect"],"mappings":";;;;;;;AAEA,SAAS,aAAa,CAAC,KAAK,EAAE;AAC9B,EAAE,IAAI;AACN,IAAI,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;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AAG,KAAK,EAAE,oBAAoB,CAAC,CAAC;AACzE,CAAC;AACW,MAAC,oBAAoB,GAAG;;;;;"}
|
package/cjs/use-move/use-move.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-move.js","sources":["../../src/use-move/use-move.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-use-before-define */\nimport { useEffect, useState, useRef } from 'react';\nimport { clamp } from '../utils';\n\nexport interface UseMovePosition {\n x: number;\n y: number;\n}\n\nexport const clampUseMovePosition = (position: UseMovePosition) => ({\n x: clamp({ min: 0, max: 1, value: position.x }),\n y: clamp({ min: 0, max: 1, value: position.y }),\n});\n\ninterface useMoveHandlers {\n onScrubStart?(): void;\n onScrubEnd?(): void;\n}\n\nexport function useMove<T extends HTMLElement = HTMLDivElement>(\n onChange: (value: UseMovePosition) => void,\n handlers?: useMoveHandlers,\n dir: 'ltr' | 'rtl' = 'ltr'\n) {\n const ref = useRef<T>();\n const mounted = useRef<boolean>(false);\n const isSliding = useRef(false);\n const frame = useRef(0);\n const [active, setActive] = useState(false);\n\n useEffect(() => {\n mounted.current = true;\n }, []);\n\n useEffect(() => {\n const onScrub = ({ x, y }: UseMovePosition) => {\n cancelAnimationFrame(frame.current);\n\n frame.current = requestAnimationFrame(() => {\n if (mounted.current && ref.current) {\n ref.current.style.userSelect = 'none';\n const rect = ref.current.getBoundingClientRect();\n\n if (rect.width && rect.height) {\n const _x = clamp({ value: (x - rect.left) / rect.width, min: 0, max: 1 });\n onChange({\n x: dir === 'ltr' ? _x : 1 - _x,\n y: clamp({ value: (y - rect.top) / rect.height, min: 0, max: 1 }),\n });\n }\n }\n });\n };\n\n const bindEvents = () => {\n document.addEventListener('mousemove', onMouseMove);\n document.addEventListener('mouseup', stopScrubbing);\n document.addEventListener('touchmove', onTouchMove);\n document.addEventListener('touchend', stopScrubbing);\n };\n\n const unbindEvents = () => {\n document.removeEventListener('mousemove', onMouseMove);\n document.removeEventListener('mouseup', stopScrubbing);\n document.removeEventListener('touchmove', onTouchMove);\n document.removeEventListener('touchend', stopScrubbing);\n };\n\n const startScrubbing = () => {\n if (!isSliding.current && mounted.current) {\n isSliding.current = true;\n typeof handlers?.onScrubStart === 'function' && handlers.onScrubStart();\n setActive(true);\n bindEvents();\n }\n };\n\n const stopScrubbing = () => {\n if (isSliding.current && mounted.current) {\n isSliding.current = false;\n typeof handlers?.onScrubEnd === 'function' && handlers.onScrubEnd();\n setActive(false);\n unbindEvents();\n }\n };\n\n const onMouseDown = (event: MouseEvent) => {\n startScrubbing();\n onMouseMove(event);\n };\n\n const onMouseMove = (event: MouseEvent) => onScrub({ x: event.clientX, y: event.clientY });\n\n const onTouchStart = (event: TouchEvent) => {\n startScrubbing();\n event?.preventDefault();\n onTouchMove(event);\n };\n\n const onTouchMove = (event: TouchEvent) => {\n event?.preventDefault();\n onScrub({ x: event.changedTouches[0].clientX, y: event.changedTouches[0].clientY });\n };\n\n ref.current.addEventListener('mousedown', onMouseDown);\n ref.current.addEventListener('touchstart', onTouchStart);\n\n return () => {\n if (ref.current) {\n ref.current.removeEventListener('mousedown', onMouseDown);\n ref.current.removeEventListener('touchstart', onTouchStart);\n }\n };\n }, [
|
|
1
|
+
{"version":3,"file":"use-move.js","sources":["../../src/use-move/use-move.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-use-before-define */\nimport { useEffect, useState, useRef } from 'react';\nimport { clamp } from '../utils';\n\nexport interface UseMovePosition {\n x: number;\n y: number;\n}\n\nexport const clampUseMovePosition = (position: UseMovePosition) => ({\n x: clamp({ min: 0, max: 1, value: position.x }),\n y: clamp({ min: 0, max: 1, value: position.y }),\n});\n\ninterface useMoveHandlers {\n onScrubStart?(): void;\n onScrubEnd?(): void;\n}\n\nexport function useMove<T extends HTMLElement = HTMLDivElement>(\n onChange: (value: UseMovePosition) => void,\n handlers?: useMoveHandlers,\n dir: 'ltr' | 'rtl' = 'ltr'\n) {\n const ref = useRef<T>();\n const mounted = useRef<boolean>(false);\n const isSliding = useRef(false);\n const frame = useRef(0);\n const [active, setActive] = useState(false);\n\n useEffect(() => {\n mounted.current = true;\n }, []);\n\n useEffect(() => {\n const onScrub = ({ x, y }: UseMovePosition) => {\n cancelAnimationFrame(frame.current);\n\n frame.current = requestAnimationFrame(() => {\n if (mounted.current && ref.current) {\n ref.current.style.userSelect = 'none';\n const rect = ref.current.getBoundingClientRect();\n\n if (rect.width && rect.height) {\n const _x = clamp({ value: (x - rect.left) / rect.width, min: 0, max: 1 });\n onChange({\n x: dir === 'ltr' ? _x : 1 - _x,\n y: clamp({ value: (y - rect.top) / rect.height, min: 0, max: 1 }),\n });\n }\n }\n });\n };\n\n const bindEvents = () => {\n document.addEventListener('mousemove', onMouseMove);\n document.addEventListener('mouseup', stopScrubbing);\n document.addEventListener('touchmove', onTouchMove);\n document.addEventListener('touchend', stopScrubbing);\n };\n\n const unbindEvents = () => {\n document.removeEventListener('mousemove', onMouseMove);\n document.removeEventListener('mouseup', stopScrubbing);\n document.removeEventListener('touchmove', onTouchMove);\n document.removeEventListener('touchend', stopScrubbing);\n };\n\n const startScrubbing = () => {\n if (!isSliding.current && mounted.current) {\n isSliding.current = true;\n typeof handlers?.onScrubStart === 'function' && handlers.onScrubStart();\n setActive(true);\n bindEvents();\n }\n };\n\n const stopScrubbing = () => {\n if (isSliding.current && mounted.current) {\n isSliding.current = false;\n typeof handlers?.onScrubEnd === 'function' && handlers.onScrubEnd();\n setActive(false);\n unbindEvents();\n }\n };\n\n const onMouseDown = (event: MouseEvent) => {\n startScrubbing();\n onMouseMove(event);\n };\n\n const onMouseMove = (event: MouseEvent) => onScrub({ x: event.clientX, y: event.clientY });\n\n const onTouchStart = (event: TouchEvent) => {\n startScrubbing();\n event?.preventDefault();\n onTouchMove(event);\n };\n\n const onTouchMove = (event: TouchEvent) => {\n event?.preventDefault();\n onScrub({ x: event.changedTouches[0].clientX, y: event.changedTouches[0].clientY });\n };\n\n ref.current.addEventListener('mousedown', onMouseDown);\n ref.current.addEventListener('touchstart', onTouchStart);\n\n return () => {\n if (ref.current) {\n ref.current.removeEventListener('mousedown', onMouseDown);\n ref.current.removeEventListener('touchstart', onTouchStart);\n }\n };\n }, [dir, onChange]);\n\n return { ref, active };\n}\n"],"names":["clamp","useRef","useState","useEffect"],"mappings":";;;;;;;AAEY,MAAC,oBAAoB,GAAG,CAAC,QAAQ,MAAM;AACnD,EAAE,CAAC,EAAEA,WAAK,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,KAAK,EAAE,QAAQ,CAAC,CAAC,EAAE,CAAC;AACjD,EAAE,CAAC,EAAEA,WAAK,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,KAAK,EAAE,QAAQ,CAAC,CAAC,EAAE,CAAC;AACjD,CAAC,EAAE;AACI,SAAS,OAAO,CAAC,QAAQ,EAAE,QAAQ,EAAE,GAAG,GAAG,KAAK,EAAE;AACzD,EAAE,MAAM,GAAG,GAAGC,YAAM,EAAE,CAAC;AACvB,EAAE,MAAM,OAAO,GAAGA,YAAM,CAAC,KAAK,CAAC,CAAC;AAChC,EAAE,MAAM,SAAS,GAAGA,YAAM,CAAC,KAAK,CAAC,CAAC;AAClC,EAAE,MAAM,KAAK,GAAGA,YAAM,CAAC,CAAC,CAAC,CAAC;AAC1B,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,GAAGC,cAAQ,CAAC,KAAK,CAAC,CAAC;AAC9C,EAAEC,eAAS,CAAC,MAAM;AAClB,IAAI,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC;AAC3B,GAAG,EAAE,EAAE,CAAC,CAAC;AACT,EAAEA,eAAS,CAAC,MAAM;AAClB,IAAI,MAAM,OAAO,GAAG,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,KAAK;AAClC,MAAM,oBAAoB,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;AAC1C,MAAM,KAAK,CAAC,OAAO,GAAG,qBA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;;;;;"}
|
|
@@ -18,11 +18,12 @@ function deserializeJSON(value) {
|
|
|
18
18
|
function useLocalStorage({
|
|
19
19
|
key,
|
|
20
20
|
defaultValue = void 0,
|
|
21
|
+
getInitialValueInEffect = false,
|
|
21
22
|
deserialize = deserializeJSON,
|
|
22
23
|
serialize = serializeJSON
|
|
23
24
|
}) {
|
|
24
25
|
var _a;
|
|
25
|
-
const [value, setValue] = useState(typeof window !== "undefined" && "localStorage" in window ? deserialize((_a = window.localStorage.getItem(key)) != null ? _a : void 0) : defaultValue != null ? defaultValue : "");
|
|
26
|
+
const [value, setValue] = useState(typeof window !== "undefined" && "localStorage" in window && !getInitialValueInEffect ? deserialize((_a = window.localStorage.getItem(key)) != null ? _a : void 0) : defaultValue != null ? defaultValue : "");
|
|
26
27
|
const setLocalStorageValue = useCallback((val) => {
|
|
27
28
|
if (val instanceof Function) {
|
|
28
29
|
setValue((current) => {
|
|
@@ -46,6 +47,12 @@ function useLocalStorage({
|
|
|
46
47
|
setLocalStorageValue(defaultValue);
|
|
47
48
|
}
|
|
48
49
|
}, [defaultValue, value, setLocalStorageValue]);
|
|
50
|
+
useEffect(() => {
|
|
51
|
+
var _a2;
|
|
52
|
+
if (getInitialValueInEffect) {
|
|
53
|
+
setValue(deserialize((_a2 = window.localStorage.getItem(key)) != null ? _a2 : void 0) || (defaultValue != null ? defaultValue : ""));
|
|
54
|
+
}
|
|
55
|
+
}, []);
|
|
49
56
|
return [value === void 0 ? defaultValue : value, setLocalStorageValue];
|
|
50
57
|
}
|
|
51
58
|
const useLocalStorageValue = useLocalStorage;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-local-storage.js","sources":["../../src/use-local-storage/use-local-storage.ts"],"sourcesContent":["import { useState, useCallback, useEffect } from 'react';\nimport { useWindowEvent } from '../use-window-event/use-window-event';\n\ninterface UseLocalStorage<T> {\n key: string;\n defaultValue?: T;\n serialize?(value: T): string;\n deserialize?(value: string): T;\n}\n\nfunction serializeJSON<T>(value: T) {\n try {\n return JSON.stringify(value);\n } catch (error) {\n throw new Error('@mantine/hooks use-local-storage: Failed to serialize the value');\n }\n}\n\nfunction deserializeJSON(value: string) {\n try {\n return JSON.parse(value);\n } catch {\n return value;\n }\n}\n\nexport function useLocalStorage<T = string>({\n key,\n defaultValue = undefined,\n deserialize = deserializeJSON,\n serialize = serializeJSON,\n}: UseLocalStorage<T>) {\n const [value, setValue] = useState<T>(\n typeof window !== 'undefined' && 'localStorage' in window\n ? deserialize(window.localStorage.getItem(key) ?? undefined)\n : ((defaultValue ?? '') as T)\n );\n\n const setLocalStorageValue = useCallback(\n (val: T | ((prevState: T) => T)) => {\n if (val instanceof Function) {\n setValue((current) => {\n const result = val(current);\n window.localStorage.setItem(key, serialize(result));\n return result;\n });\n } else {\n window.localStorage.setItem(key, serialize(val));\n setValue(val);\n }\n },\n [key]\n );\n\n useWindowEvent('storage', (event) => {\n if (event.storageArea === window.localStorage && event.key === key) {\n setValue(deserialize(event.newValue ?? undefined));\n }\n });\n\n useEffect(() => {\n if (defaultValue !== undefined && value === undefined) {\n setLocalStorageValue(defaultValue);\n }\n }, [defaultValue, value, setLocalStorageValue]);\n\n return [value === undefined ? defaultValue : value, setLocalStorageValue] as const;\n}\n\nexport const useLocalStorageValue = useLocalStorage;\n"],"names":[],"mappings":";;;AAEA,SAAS,aAAa,CAAC,KAAK,EAAE;AAC9B,EAAE,IAAI;AACN,IAAI,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;AACjC,GAAG,CAAC,OAAO,KAAK,EAAE;AAClB,IAAI,MAAM,IAAI,KAAK,CAAC,iEAAiE,CAAC,CAAC;AACvF,GAAG;AACH,CAAC;AACD,SAAS,eAAe,CAAC,KAAK,EAAE;AAChC,EAAE,IAAI;AACN,IAAI,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;AAC7B,GAAG,CAAC,OAAO,CAAC,EAAE;AACd,IAAI,OAAO,KAAK,CAAC;AACjB,GAAG;AACH,CAAC;AACM,SAAS,eAAe,CAAC;AAChC,EAAE,GAAG;AACL,EAAE,YAAY,GAAG,KAAK,CAAC;AACvB,EAAE,WAAW,GAAG,eAAe;AAC/B,EAAE,SAAS,GAAG,aAAa;AAC3B,CAAC,EAAE;AACH,EAAE,IAAI,EAAE,CAAC;AACT,EAAE,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,CAAC,OAAO,MAAM,KAAK,WAAW,IAAI,cAAc,IAAI,MAAM,GAAG,WAAW,CAAC,CAAC,EAAE,GAAG,MAAM,CAAC,YAAY,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,IAAI,GAAG,EAAE,GAAG,KAAK,CAAC,CAAC,GAAG,YAAY,IAAI,IAAI,GAAG,YAAY,GAAG,EAAE,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"use-local-storage.js","sources":["../../src/use-local-storage/use-local-storage.ts"],"sourcesContent":["import { useState, useCallback, useEffect } from 'react';\nimport { useWindowEvent } from '../use-window-event/use-window-event';\n\ninterface UseLocalStorage<T> {\n /** Local storage key */\n key: string;\n\n /** Default value that will be set if value is not found in local 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 local storage */\n serialize?(value: T): string;\n\n /** Function to deserialize string value from local storage to value */\n deserialize?(value: string): T;\n}\n\nfunction serializeJSON<T>(value: T) {\n try {\n return JSON.stringify(value);\n } catch (error) {\n throw new Error('@mantine/hooks use-local-storage: Failed to serialize the value');\n }\n}\n\nfunction deserializeJSON(value: string) {\n try {\n return JSON.parse(value);\n } catch {\n return value;\n }\n}\n\nexport function useLocalStorage<T = string>({\n key,\n defaultValue = undefined,\n getInitialValueInEffect = false,\n deserialize = deserializeJSON,\n serialize = serializeJSON,\n}: UseLocalStorage<T>) {\n const [value, setValue] = useState<T>(\n typeof window !== 'undefined' && 'localStorage' in window && !getInitialValueInEffect\n ? deserialize(window.localStorage.getItem(key) ?? undefined)\n : ((defaultValue ?? '') as T)\n );\n\n const setLocalStorageValue = useCallback(\n (val: T | ((prevState: T) => T)) => {\n if (val instanceof Function) {\n setValue((current) => {\n const result = val(current);\n window.localStorage.setItem(key, serialize(result));\n return result;\n });\n } else {\n window.localStorage.setItem(key, serialize(val));\n setValue(val);\n }\n },\n [key]\n );\n\n useWindowEvent('storage', (event) => {\n if (event.storageArea === window.localStorage && event.key === key) {\n setValue(deserialize(event.newValue ?? undefined));\n }\n });\n\n useEffect(() => {\n if (defaultValue !== undefined && value === undefined) {\n setLocalStorageValue(defaultValue);\n }\n }, [defaultValue, value, setLocalStorageValue]);\n\n useEffect(() => {\n if (getInitialValueInEffect) {\n setValue(\n deserialize(window.localStorage.getItem(key) ?? undefined) || ((defaultValue ?? '') as T)\n );\n }\n }, []);\n\n return [value === undefined ? defaultValue : value, setLocalStorageValue] as const;\n}\n\nexport const useLocalStorageValue = useLocalStorage;\n"],"names":[],"mappings":";;;AAEA,SAAS,aAAa,CAAC,KAAK,EAAE;AAC9B,EAAE,IAAI;AACN,IAAI,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;AACjC,GAAG,CAAC,OAAO,KAAK,EAAE;AAClB,IAAI,MAAM,IAAI,KAAK,CAAC,iEAAiE,CAAC,CAAC;AACvF,GAAG;AACH,CAAC;AACD,SAAS,eAAe,CAAC,KAAK,EAAE;AAChC,EAAE,IAAI;AACN,IAAI,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;AAC7B,GAAG,CAAC,OAAO,CAAC,EAAE;AACd,IAAI,OAAO,KAAK,CAAC;AACjB,GAAG;AACH,CAAC;AACM,SAAS,eAAe,CAAC;AAChC,EAAE,GAAG;AACL,EAAE,YAAY,GAAG,KAAK,CAAC;AACvB,EAAE,uBAAuB,GAAG,KAAK;AACjC,EAAE,WAAW,GAAG,eAAe;AAC/B,EAAE,SAAS,GAAG,aAAa;AAC3B,CAAC,EAAE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AK,CAAC,CAAC,CAAC,CAAC;AAC3E,KAAK;AACL,GAAG,CAAC,CAAC;AACL,EAAE,SAAS,CAAC,MAAM;AAClB,IAAI,IAAI,YAAY,KAAK,KAAK,CAAC,IAAI,KAAK,KAAK,KAAK,CAAC,EAAE;AACrD,MAAM,oBAAoB,CAAC,YAAY,CAAC,CAAC;AACzC,KAAK;AACL,GAAG,EAAE,CAAC,YAAY,EAAE,KAAK,EAAE,oBAAoB,CAAC,CAAC,CAAC;AAClD,EAAE,SAAS,CAAC,MAAM;AAClB,IAAI,IAAI,GAAG,CAAC;AACZ,IAAI,IAAI,uBAAuB,EAAE;AACjC,MAAM,QAAQ,CAAC,WAAW,CAAC,CAAC,GAAG,GAAG,MAAM,CAAC,YAAY,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,IAAI,GAAG,GAAG,GAAG,KAAK,CAAC,CAAC,KAAK,YAAY,IAAI,IAAI,GAAG,YAAY,GAAG,EAAE,CAAC,CAAC,CAAC;AAC3I,KAAK;AACL,GAAG,EAAE,EAAE,CAAC,CAAC;AACT,EAAE,OAAO,CAAC,KAAK,KAAK,KAAK,CAAC,GAAG,YAAY,GAAG,KAAK,EAAE,oBAAoB,CAAC,CAAC;AACzE,CAAC;AACW,MAAC,oBAAoB,GAAG;;;;"}
|
package/esm/use-move/use-move.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-move.js","sources":["../../src/use-move/use-move.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-use-before-define */\nimport { useEffect, useState, useRef } from 'react';\nimport { clamp } from '../utils';\n\nexport interface UseMovePosition {\n x: number;\n y: number;\n}\n\nexport const clampUseMovePosition = (position: UseMovePosition) => ({\n x: clamp({ min: 0, max: 1, value: position.x }),\n y: clamp({ min: 0, max: 1, value: position.y }),\n});\n\ninterface useMoveHandlers {\n onScrubStart?(): void;\n onScrubEnd?(): void;\n}\n\nexport function useMove<T extends HTMLElement = HTMLDivElement>(\n onChange: (value: UseMovePosition) => void,\n handlers?: useMoveHandlers,\n dir: 'ltr' | 'rtl' = 'ltr'\n) {\n const ref = useRef<T>();\n const mounted = useRef<boolean>(false);\n const isSliding = useRef(false);\n const frame = useRef(0);\n const [active, setActive] = useState(false);\n\n useEffect(() => {\n mounted.current = true;\n }, []);\n\n useEffect(() => {\n const onScrub = ({ x, y }: UseMovePosition) => {\n cancelAnimationFrame(frame.current);\n\n frame.current = requestAnimationFrame(() => {\n if (mounted.current && ref.current) {\n ref.current.style.userSelect = 'none';\n const rect = ref.current.getBoundingClientRect();\n\n if (rect.width && rect.height) {\n const _x = clamp({ value: (x - rect.left) / rect.width, min: 0, max: 1 });\n onChange({\n x: dir === 'ltr' ? _x : 1 - _x,\n y: clamp({ value: (y - rect.top) / rect.height, min: 0, max: 1 }),\n });\n }\n }\n });\n };\n\n const bindEvents = () => {\n document.addEventListener('mousemove', onMouseMove);\n document.addEventListener('mouseup', stopScrubbing);\n document.addEventListener('touchmove', onTouchMove);\n document.addEventListener('touchend', stopScrubbing);\n };\n\n const unbindEvents = () => {\n document.removeEventListener('mousemove', onMouseMove);\n document.removeEventListener('mouseup', stopScrubbing);\n document.removeEventListener('touchmove', onTouchMove);\n document.removeEventListener('touchend', stopScrubbing);\n };\n\n const startScrubbing = () => {\n if (!isSliding.current && mounted.current) {\n isSliding.current = true;\n typeof handlers?.onScrubStart === 'function' && handlers.onScrubStart();\n setActive(true);\n bindEvents();\n }\n };\n\n const stopScrubbing = () => {\n if (isSliding.current && mounted.current) {\n isSliding.current = false;\n typeof handlers?.onScrubEnd === 'function' && handlers.onScrubEnd();\n setActive(false);\n unbindEvents();\n }\n };\n\n const onMouseDown = (event: MouseEvent) => {\n startScrubbing();\n onMouseMove(event);\n };\n\n const onMouseMove = (event: MouseEvent) => onScrub({ x: event.clientX, y: event.clientY });\n\n const onTouchStart = (event: TouchEvent) => {\n startScrubbing();\n event?.preventDefault();\n onTouchMove(event);\n };\n\n const onTouchMove = (event: TouchEvent) => {\n event?.preventDefault();\n onScrub({ x: event.changedTouches[0].clientX, y: event.changedTouches[0].clientY });\n };\n\n ref.current.addEventListener('mousedown', onMouseDown);\n ref.current.addEventListener('touchstart', onTouchStart);\n\n return () => {\n if (ref.current) {\n ref.current.removeEventListener('mousedown', onMouseDown);\n ref.current.removeEventListener('touchstart', onTouchStart);\n }\n };\n }, [
|
|
1
|
+
{"version":3,"file":"use-move.js","sources":["../../src/use-move/use-move.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-use-before-define */\nimport { useEffect, useState, useRef } from 'react';\nimport { clamp } from '../utils';\n\nexport interface UseMovePosition {\n x: number;\n y: number;\n}\n\nexport const clampUseMovePosition = (position: UseMovePosition) => ({\n x: clamp({ min: 0, max: 1, value: position.x }),\n y: clamp({ min: 0, max: 1, value: position.y }),\n});\n\ninterface useMoveHandlers {\n onScrubStart?(): void;\n onScrubEnd?(): void;\n}\n\nexport function useMove<T extends HTMLElement = HTMLDivElement>(\n onChange: (value: UseMovePosition) => void,\n handlers?: useMoveHandlers,\n dir: 'ltr' | 'rtl' = 'ltr'\n) {\n const ref = useRef<T>();\n const mounted = useRef<boolean>(false);\n const isSliding = useRef(false);\n const frame = useRef(0);\n const [active, setActive] = useState(false);\n\n useEffect(() => {\n mounted.current = true;\n }, []);\n\n useEffect(() => {\n const onScrub = ({ x, y }: UseMovePosition) => {\n cancelAnimationFrame(frame.current);\n\n frame.current = requestAnimationFrame(() => {\n if (mounted.current && ref.current) {\n ref.current.style.userSelect = 'none';\n const rect = ref.current.getBoundingClientRect();\n\n if (rect.width && rect.height) {\n const _x = clamp({ value: (x - rect.left) / rect.width, min: 0, max: 1 });\n onChange({\n x: dir === 'ltr' ? _x : 1 - _x,\n y: clamp({ value: (y - rect.top) / rect.height, min: 0, max: 1 }),\n });\n }\n }\n });\n };\n\n const bindEvents = () => {\n document.addEventListener('mousemove', onMouseMove);\n document.addEventListener('mouseup', stopScrubbing);\n document.addEventListener('touchmove', onTouchMove);\n document.addEventListener('touchend', stopScrubbing);\n };\n\n const unbindEvents = () => {\n document.removeEventListener('mousemove', onMouseMove);\n document.removeEventListener('mouseup', stopScrubbing);\n document.removeEventListener('touchmove', onTouchMove);\n document.removeEventListener('touchend', stopScrubbing);\n };\n\n const startScrubbing = () => {\n if (!isSliding.current && mounted.current) {\n isSliding.current = true;\n typeof handlers?.onScrubStart === 'function' && handlers.onScrubStart();\n setActive(true);\n bindEvents();\n }\n };\n\n const stopScrubbing = () => {\n if (isSliding.current && mounted.current) {\n isSliding.current = false;\n typeof handlers?.onScrubEnd === 'function' && handlers.onScrubEnd();\n setActive(false);\n unbindEvents();\n }\n };\n\n const onMouseDown = (event: MouseEvent) => {\n startScrubbing();\n onMouseMove(event);\n };\n\n const onMouseMove = (event: MouseEvent) => onScrub({ x: event.clientX, y: event.clientY });\n\n const onTouchStart = (event: TouchEvent) => {\n startScrubbing();\n event?.preventDefault();\n onTouchMove(event);\n };\n\n const onTouchMove = (event: TouchEvent) => {\n event?.preventDefault();\n onScrub({ x: event.changedTouches[0].clientX, y: event.changedTouches[0].clientY });\n };\n\n ref.current.addEventListener('mousedown', onMouseDown);\n ref.current.addEventListener('touchstart', onTouchStart);\n\n return () => {\n if (ref.current) {\n ref.current.removeEventListener('mousedown', onMouseDown);\n ref.current.removeEventListener('touchstart', onTouchStart);\n }\n };\n }, [dir, onChange]);\n\n return { ref, active };\n}\n"],"names":[],"mappings":";;;AAEY,MAAC,oBAAoB,GAAG,CAAC,QAAQ,MAAM;AACnD,EAAE,CAAC,EAAE,KAAK,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,KAAK,EAAE,QAAQ,CAAC,CAAC,EAAE,CAAC;AACjD,EAAE,CAAC,EAAE,KAAK,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,KAAK,EAAE,QAAQ,CAAC,CAAC,EAAE,CAAC;AACjD,CAAC,EAAE;AACI,SAAS,OAAO,CAAC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cAAc,CAAC,CAAC,CAAC,CAAC,OAAO,EAAE,CAAC,EAAE,KAAK,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC;AAC1F,KAAK,CAAC;AACN,IAAI,GAAG,CAAC,OAAO,CAAC,gBAAgB,CAAC,WAAW,EAAE,WAAW,CAAC,CAAC;AAC3D,IAAI,GAAG,CAAC,OAAO,CAAC,gBAAgB,CAAC,YAAY,EAAE,YAAY,CAAC,CAAC;AAC7D,IAAI,OAAO,MAAM;AACjB,MAAM,IAAI,GAAG,CAAC,OAAO,EAAE;AACvB,QAAQ,GAAG,CAAC,OAAO,CAAC,mBAAmB,CAAC,WAAW,EAAE,WAAW,CAAC,CAAC;AAClE,QAAQ,GAAG,CAAC,OAAO,CAAC,mBAAmB,CAAC,YAAY,EAAE,YAAY,CAAC,CAAC;AACpE,OAAO;AACP,KAAK,CAAC;AACN,GAAG,EAAE,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC,CAAC;AACtB,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,CAAC;AACzB;;;;"}
|
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
interface UseLocalStorage<T> {
|
|
2
|
+
/** Local storage key */
|
|
2
3
|
key: string;
|
|
4
|
+
/** Default value that will be set if value is not found in local storage */
|
|
3
5
|
defaultValue?: T;
|
|
6
|
+
/** If set to true, value will be update is useEffect after mount */
|
|
7
|
+
getInitialValueInEffect?: boolean;
|
|
8
|
+
/** Function to serialize value into string to be save in local storage */
|
|
4
9
|
serialize?(value: T): string;
|
|
10
|
+
/** Function to deserialize string value from local storage to value */
|
|
5
11
|
deserialize?(value: string): T;
|
|
6
12
|
}
|
|
7
|
-
export declare function useLocalStorage<T = string>({ key, defaultValue, deserialize, serialize, }: UseLocalStorage<T>): readonly [T, (val: T | ((prevState: T) => T)) => void];
|
|
13
|
+
export declare function useLocalStorage<T = string>({ key, defaultValue, getInitialValueInEffect, deserialize, serialize, }: UseLocalStorage<T>): readonly [T, (val: T | ((prevState: T) => T)) => void];
|
|
8
14
|
export declare const useLocalStorageValue: typeof useLocalStorage;
|
|
9
15
|
export {};
|
|
10
16
|
//# sourceMappingURL=use-local-storage.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-local-storage.d.ts","sourceRoot":"","sources":["../../src/use-local-storage/use-local-storage.ts"],"names":[],"mappings":"AAGA,UAAU,eAAe,CAAC,CAAC;IACzB,GAAG,EAAE,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"use-local-storage.d.ts","sourceRoot":"","sources":["../../src/use-local-storage/use-local-storage.ts"],"names":[],"mappings":"AAGA,UAAU,eAAe,CAAC,CAAC;IACzB,wBAAwB;IACxB,GAAG,EAAE,MAAM,CAAC;IAEZ,4EAA4E;IAC5E,YAAY,CAAC,EAAE,CAAC,CAAC;IAEjB,oEAAoE;IACpE,uBAAuB,CAAC,EAAE,OAAO,CAAC;IAElC,0EAA0E;IAC1E,SAAS,CAAC,CAAC,KAAK,EAAE,CAAC,GAAG,MAAM,CAAC;IAE7B,uEAAuE;IACvE,WAAW,CAAC,CAAC,KAAK,EAAE,MAAM,GAAG,CAAC,CAAC;CAChC;AAkBD,wBAAgB,eAAe,CAAC,CAAC,GAAG,MAAM,EAAE,EAC1C,GAAG,EACH,YAAwB,EACxB,uBAA+B,EAC/B,WAA6B,EAC7B,SAAyB,GAC1B,EAAE,eAAe,CAAC,CAAC,CAAC,uCAQM,CAAC,KAAK,CAAC,YAoCjC;AAED,eAAO,MAAM,oBAAoB,wBAAkB,CAAC"}
|