@mantine/hooks 7.0.0-alpha.9 → 7.0.0-beta.1
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-callback-ref/use-callback-ref.js +8 -5
- package/cjs/use-callback-ref/use-callback-ref.js.map +1 -1
- package/cjs/use-click-outside/use-click-outside.js +19 -21
- package/cjs/use-click-outside/use-click-outside.js.map +1 -1
- package/cjs/use-clipboard/use-clipboard.js +22 -26
- package/cjs/use-clipboard/use-clipboard.js.map +1 -1
- package/cjs/use-color-scheme/use-color-scheme.js +1 -3
- package/cjs/use-color-scheme/use-color-scheme.js.map +1 -1
- package/cjs/use-counter/use-counter.js +25 -9
- package/cjs/use-counter/use-counter.js.map +1 -1
- package/cjs/use-debounced-callback/use-debounced-callback.js +7 -7
- package/cjs/use-debounced-callback/use-debounced-callback.js.map +1 -1
- package/cjs/use-debounced-state/use-debounced-state.js +18 -19
- package/cjs/use-debounced-state/use-debounced-state.js.map +1 -1
- package/cjs/use-debounced-value/use-debounced-value.js +24 -25
- package/cjs/use-debounced-value/use-debounced-value.js.map +1 -1
- package/cjs/use-did-update/use-did-update.js +14 -11
- package/cjs/use-did-update/use-did-update.js.map +1 -1
- package/cjs/use-disclosure/use-disclosure.js +24 -24
- package/cjs/use-disclosure/use-disclosure.js.map +1 -1
- package/cjs/use-document-title/use-document-title.js +5 -5
- package/cjs/use-document-title/use-document-title.js.map +1 -1
- package/cjs/use-document-visibility/use-document-visibility.js +7 -7
- package/cjs/use-document-visibility/use-document-visibility.js.map +1 -1
- package/cjs/use-event-listener/use-event-listener.js +12 -9
- package/cjs/use-event-listener/use-event-listener.js.map +1 -1
- package/cjs/use-eye-dropper/use-eye-dropper.js +15 -12
- package/cjs/use-eye-dropper/use-eye-dropper.js.map +1 -1
- package/cjs/use-favicon/use-favicon.js +25 -23
- package/cjs/use-favicon/use-favicon.js.map +1 -1
- package/cjs/use-focus-return/use-focus-return.js +25 -29
- package/cjs/use-focus-return/use-focus-return.js.map +1 -1
- package/cjs/use-focus-trap/create-aria-hider.js +36 -36
- package/cjs/use-focus-trap/create-aria-hider.js.map +1 -1
- package/cjs/use-focus-trap/scope-tab.js +15 -15
- package/cjs/use-focus-trap/scope-tab.js.map +1 -1
- package/cjs/use-focus-trap/tabbable.js +33 -35
- package/cjs/use-focus-trap/tabbable.js.map +1 -1
- package/cjs/use-focus-trap/use-focus-trap.js +67 -67
- package/cjs/use-focus-trap/use-focus-trap.js.map +1 -1
- package/cjs/use-focus-within/use-focus-within.js +40 -37
- package/cjs/use-focus-within/use-focus-within.js.map +1 -1
- package/cjs/use-force-update/use-force-update.js +3 -3
- package/cjs/use-force-update/use-force-update.js.map +1 -1
- package/cjs/use-fullscreen/use-fullscreen.js +79 -83
- package/cjs/use-fullscreen/use-fullscreen.js.map +1 -1
- package/cjs/use-hash/use-hash.js +16 -16
- package/cjs/use-hash/use-hash.js.map +1 -1
- package/cjs/use-headroom/use-headroom.js +23 -23
- package/cjs/use-headroom/use-headroom.js.map +1 -1
- package/cjs/use-hotkeys/parse-hotkey.js +64 -49
- package/cjs/use-hotkeys/parse-hotkey.js.map +1 -1
- package/cjs/use-hotkeys/use-hotkeys.js +21 -22
- package/cjs/use-hotkeys/use-hotkeys.js.map +1 -1
- package/cjs/use-hover/use-hover.js +17 -17
- package/cjs/use-hover/use-hover.js.map +1 -1
- package/cjs/use-id/use-id.js +12 -12
- package/cjs/use-id/use-id.js.map +1 -1
- package/cjs/use-id/use-react-id.js +3 -3
- package/cjs/use-id/use-react-id.js.map +1 -1
- package/cjs/use-idle/use-idle.js +42 -26
- package/cjs/use-idle/use-idle.js.map +1 -1
- package/cjs/use-input-state/use-input-state.js +18 -22
- package/cjs/use-input-state/use-input-state.js.map +1 -1
- package/cjs/use-intersection/use-intersection.js +20 -17
- package/cjs/use-intersection/use-intersection.js.map +1 -1
- package/cjs/use-interval/use-interval.js +27 -28
- package/cjs/use-interval/use-interval.js.map +1 -1
- package/cjs/use-isomorphic-effect/use-isomorphic-effect.js +1 -3
- package/cjs/use-isomorphic-effect/use-isomorphic-effect.js.map +1 -1
- package/cjs/use-list-state/use-list-state.js +76 -55
- package/cjs/use-list-state/use-list-state.js.map +1 -1
- package/cjs/use-local-storage/create-storage.js +79 -71
- package/cjs/use-local-storage/create-storage.js.map +1 -1
- package/cjs/use-local-storage/use-local-storage.js +1 -1
- package/cjs/use-local-storage/use-local-storage.js.map +1 -1
- package/cjs/use-logger/use-logger.js +8 -9
- package/cjs/use-logger/use-logger.js.map +1 -1
- package/cjs/use-media-query/use-media-query.js +28 -31
- package/cjs/use-media-query/use-media-query.js.map +1 -1
- package/cjs/use-merged-ref/use-merged-ref.js +9 -11
- package/cjs/use-merged-ref/use-merged-ref.js.map +1 -1
- package/cjs/use-mouse/use-mouse.js +46 -25
- package/cjs/use-mouse/use-mouse.js.map +1 -1
- package/cjs/use-move/use-move.js +87 -88
- package/cjs/use-move/use-move.js.map +1 -1
- package/cjs/use-network/use-network.js +51 -32
- package/cjs/use-network/use-network.js.map +1 -1
- package/cjs/use-os/use-os.js +30 -30
- package/cjs/use-os/use-os.js.map +1 -1
- package/cjs/use-page-leave/use-page-leave.js +4 -4
- package/cjs/use-page-leave/use-page-leave.js.map +1 -1
- package/cjs/use-pagination/use-pagination.js +63 -58
- package/cjs/use-pagination/use-pagination.js.map +1 -1
- package/cjs/use-previous/use-previous.js +5 -5
- package/cjs/use-previous/use-previous.js.map +1 -1
- package/cjs/use-queue/use-queue.js +24 -24
- package/cjs/use-queue/use-queue.js.map +1 -1
- package/cjs/use-reduced-motion/use-reduced-motion.js +1 -1
- package/cjs/use-reduced-motion/use-reduced-motion.js.map +1 -1
- package/cjs/use-resize-observer/use-resize-observer.js +39 -38
- package/cjs/use-resize-observer/use-resize-observer.js.map +1 -1
- package/cjs/use-scroll-into-view/use-scroll-into-view.js +78 -75
- package/cjs/use-scroll-into-view/use-scroll-into-view.js.map +1 -1
- package/cjs/use-scroll-into-view/utils/ease-in-out-quad.js +1 -1
- package/cjs/use-scroll-into-view/utils/ease-in-out-quad.js.map +1 -1
- package/cjs/use-scroll-into-view/utils/get-relative-position.js +53 -46
- package/cjs/use-scroll-into-view/utils/get-relative-position.js.map +1 -1
- package/cjs/use-scroll-into-view/utils/get-scroll-start.js +9 -10
- package/cjs/use-scroll-into-view/utils/get-scroll-start.js.map +1 -1
- package/cjs/use-scroll-into-view/utils/set-scroll-param.js +11 -14
- package/cjs/use-scroll-into-view/utils/set-scroll-param.js.map +1 -1
- package/cjs/use-session-storage/use-session-storage.js +1 -1
- package/cjs/use-session-storage/use-session-storage.js.map +1 -1
- package/cjs/use-set-state/use-set-state.js +22 -3
- package/cjs/use-set-state/use-set-state.js.map +1 -1
- package/cjs/use-shallow-effect/use-shallow-effect.js +22 -22
- package/cjs/use-shallow-effect/use-shallow-effect.js.map +1 -1
- package/cjs/use-text-selection/use-text-selection.js +12 -12
- package/cjs/use-text-selection/use-text-selection.js.map +1 -1
- package/cjs/use-timeout/use-timeout.js +22 -22
- package/cjs/use-timeout/use-timeout.js.map +1 -1
- package/cjs/use-toggle/use-toggle.js +6 -6
- package/cjs/use-toggle/use-toggle.js.map +1 -1
- package/cjs/use-uncontrolled/use-uncontrolled.js +18 -10
- package/cjs/use-uncontrolled/use-uncontrolled.js.map +1 -1
- package/cjs/use-validated-state/use-validated-state.js +17 -14
- package/cjs/use-validated-state/use-validated-state.js.map +1 -1
- package/cjs/use-viewport-size/use-viewport-size.js +12 -12
- package/cjs/use-viewport-size/use-viewport-size.js.map +1 -1
- package/cjs/use-window-event/use-window-event.js +4 -4
- package/cjs/use-window-event/use-window-event.js.map +1 -1
- package/cjs/use-window-scroll/use-window-scroll.js +17 -19
- package/cjs/use-window-scroll/use-window-scroll.js.map +1 -1
- package/cjs/utils/clamp/clamp.js +10 -1
- package/cjs/utils/clamp/clamp.js.map +1 -1
- package/cjs/utils/lower-first/lower-first.js +1 -1
- package/cjs/utils/lower-first/lower-first.js.map +1 -1
- package/cjs/utils/random-id/random-id.js +1 -1
- package/cjs/utils/random-id/random-id.js.map +1 -1
- package/cjs/utils/range/range.js +2 -2
- package/cjs/utils/range/range.js.map +1 -1
- package/cjs/utils/shallow-equal/shallow-equal.js +19 -19
- package/cjs/utils/shallow-equal/shallow-equal.js.map +1 -1
- package/cjs/utils/upper-first/upper-first.js +1 -1
- package/cjs/utils/upper-first/upper-first.js.map +1 -1
- package/esm/use-callback-ref/use-callback-ref.js +8 -5
- package/esm/use-callback-ref/use-callback-ref.js.map +1 -1
- package/esm/use-click-outside/use-click-outside.js +19 -21
- package/esm/use-click-outside/use-click-outside.js.map +1 -1
- package/esm/use-clipboard/use-clipboard.js +22 -26
- package/esm/use-clipboard/use-clipboard.js.map +1 -1
- package/esm/use-color-scheme/use-color-scheme.js +1 -3
- package/esm/use-color-scheme/use-color-scheme.js.map +1 -1
- package/esm/use-counter/use-counter.js +25 -9
- package/esm/use-counter/use-counter.js.map +1 -1
- package/esm/use-debounced-callback/use-debounced-callback.js +7 -7
- package/esm/use-debounced-callback/use-debounced-callback.js.map +1 -1
- package/esm/use-debounced-state/use-debounced-state.js +18 -19
- package/esm/use-debounced-state/use-debounced-state.js.map +1 -1
- package/esm/use-debounced-value/use-debounced-value.js +24 -25
- package/esm/use-debounced-value/use-debounced-value.js.map +1 -1
- package/esm/use-did-update/use-did-update.js +14 -11
- package/esm/use-did-update/use-did-update.js.map +1 -1
- package/esm/use-disclosure/use-disclosure.js +24 -24
- package/esm/use-disclosure/use-disclosure.js.map +1 -1
- package/esm/use-document-title/use-document-title.js +5 -5
- package/esm/use-document-title/use-document-title.js.map +1 -1
- package/esm/use-document-visibility/use-document-visibility.js +7 -7
- package/esm/use-document-visibility/use-document-visibility.js.map +1 -1
- package/esm/use-event-listener/use-event-listener.js +12 -9
- package/esm/use-event-listener/use-event-listener.js.map +1 -1
- package/esm/use-eye-dropper/use-eye-dropper.js +15 -12
- package/esm/use-eye-dropper/use-eye-dropper.js.map +1 -1
- package/esm/use-favicon/use-favicon.js +25 -23
- package/esm/use-favicon/use-favicon.js.map +1 -1
- package/esm/use-focus-return/use-focus-return.js +25 -29
- package/esm/use-focus-return/use-focus-return.js.map +1 -1
- package/esm/use-focus-trap/create-aria-hider.js +36 -36
- package/esm/use-focus-trap/create-aria-hider.js.map +1 -1
- package/esm/use-focus-trap/scope-tab.js +15 -15
- package/esm/use-focus-trap/scope-tab.js.map +1 -1
- package/esm/use-focus-trap/tabbable.js +33 -35
- package/esm/use-focus-trap/tabbable.js.map +1 -1
- package/esm/use-focus-trap/use-focus-trap.js +67 -67
- package/esm/use-focus-trap/use-focus-trap.js.map +1 -1
- package/esm/use-focus-within/use-focus-within.js +40 -37
- package/esm/use-focus-within/use-focus-within.js.map +1 -1
- package/esm/use-force-update/use-force-update.js +3 -3
- package/esm/use-force-update/use-force-update.js.map +1 -1
- package/esm/use-fullscreen/use-fullscreen.js +79 -83
- package/esm/use-fullscreen/use-fullscreen.js.map +1 -1
- package/esm/use-hash/use-hash.js +16 -16
- package/esm/use-hash/use-hash.js.map +1 -1
- package/esm/use-headroom/use-headroom.js +23 -23
- package/esm/use-headroom/use-headroom.js.map +1 -1
- package/esm/use-hotkeys/parse-hotkey.js +64 -49
- package/esm/use-hotkeys/parse-hotkey.js.map +1 -1
- package/esm/use-hotkeys/use-hotkeys.js +21 -22
- package/esm/use-hotkeys/use-hotkeys.js.map +1 -1
- package/esm/use-hover/use-hover.js +17 -17
- package/esm/use-hover/use-hover.js.map +1 -1
- package/esm/use-id/use-id.js +12 -12
- package/esm/use-id/use-id.js.map +1 -1
- package/esm/use-id/use-react-id.js +3 -3
- package/esm/use-id/use-react-id.js.map +1 -1
- package/esm/use-idle/use-idle.js +42 -26
- package/esm/use-idle/use-idle.js.map +1 -1
- package/esm/use-input-state/use-input-state.js +18 -22
- package/esm/use-input-state/use-input-state.js.map +1 -1
- package/esm/use-intersection/use-intersection.js +20 -17
- package/esm/use-intersection/use-intersection.js.map +1 -1
- package/esm/use-interval/use-interval.js +27 -28
- package/esm/use-interval/use-interval.js.map +1 -1
- package/esm/use-isomorphic-effect/use-isomorphic-effect.js +1 -3
- package/esm/use-isomorphic-effect/use-isomorphic-effect.js.map +1 -1
- package/esm/use-list-state/use-list-state.js +76 -55
- package/esm/use-list-state/use-list-state.js.map +1 -1
- package/esm/use-local-storage/create-storage.js +79 -71
- package/esm/use-local-storage/create-storage.js.map +1 -1
- package/esm/use-local-storage/use-local-storage.js +1 -1
- package/esm/use-local-storage/use-local-storage.js.map +1 -1
- package/esm/use-logger/use-logger.js +8 -9
- package/esm/use-logger/use-logger.js.map +1 -1
- package/esm/use-media-query/use-media-query.js +28 -31
- package/esm/use-media-query/use-media-query.js.map +1 -1
- package/esm/use-merged-ref/use-merged-ref.js +9 -11
- package/esm/use-merged-ref/use-merged-ref.js.map +1 -1
- package/esm/use-mouse/use-mouse.js +46 -25
- package/esm/use-mouse/use-mouse.js.map +1 -1
- package/esm/use-move/use-move.js +87 -88
- package/esm/use-move/use-move.js.map +1 -1
- package/esm/use-network/use-network.js +51 -32
- package/esm/use-network/use-network.js.map +1 -1
- package/esm/use-os/use-os.js +30 -30
- package/esm/use-os/use-os.js.map +1 -1
- package/esm/use-page-leave/use-page-leave.js +4 -4
- package/esm/use-page-leave/use-page-leave.js.map +1 -1
- package/esm/use-pagination/use-pagination.js +63 -58
- package/esm/use-pagination/use-pagination.js.map +1 -1
- package/esm/use-previous/use-previous.js +5 -5
- package/esm/use-previous/use-previous.js.map +1 -1
- package/esm/use-queue/use-queue.js +24 -24
- package/esm/use-queue/use-queue.js.map +1 -1
- package/esm/use-reduced-motion/use-reduced-motion.js +1 -1
- package/esm/use-reduced-motion/use-reduced-motion.js.map +1 -1
- package/esm/use-resize-observer/use-resize-observer.js +39 -38
- package/esm/use-resize-observer/use-resize-observer.js.map +1 -1
- package/esm/use-scroll-into-view/use-scroll-into-view.js +78 -75
- package/esm/use-scroll-into-view/use-scroll-into-view.js.map +1 -1
- package/esm/use-scroll-into-view/utils/ease-in-out-quad.js +1 -1
- package/esm/use-scroll-into-view/utils/ease-in-out-quad.js.map +1 -1
- package/esm/use-scroll-into-view/utils/get-relative-position.js +53 -46
- package/esm/use-scroll-into-view/utils/get-relative-position.js.map +1 -1
- package/esm/use-scroll-into-view/utils/get-scroll-start.js +9 -10
- package/esm/use-scroll-into-view/utils/get-scroll-start.js.map +1 -1
- package/esm/use-scroll-into-view/utils/set-scroll-param.js +11 -14
- package/esm/use-scroll-into-view/utils/set-scroll-param.js.map +1 -1
- package/esm/use-session-storage/use-session-storage.js +1 -1
- package/esm/use-session-storage/use-session-storage.js.map +1 -1
- package/esm/use-set-state/use-set-state.js +22 -3
- package/esm/use-set-state/use-set-state.js.map +1 -1
- package/esm/use-shallow-effect/use-shallow-effect.js +22 -22
- package/esm/use-shallow-effect/use-shallow-effect.js.map +1 -1
- package/esm/use-text-selection/use-text-selection.js +12 -12
- package/esm/use-text-selection/use-text-selection.js.map +1 -1
- package/esm/use-timeout/use-timeout.js +22 -22
- package/esm/use-timeout/use-timeout.js.map +1 -1
- package/esm/use-toggle/use-toggle.js +6 -6
- package/esm/use-toggle/use-toggle.js.map +1 -1
- package/esm/use-uncontrolled/use-uncontrolled.js +18 -10
- package/esm/use-uncontrolled/use-uncontrolled.js.map +1 -1
- package/esm/use-validated-state/use-validated-state.js +17 -14
- package/esm/use-validated-state/use-validated-state.js.map +1 -1
- package/esm/use-viewport-size/use-viewport-size.js +12 -12
- package/esm/use-viewport-size/use-viewport-size.js.map +1 -1
- package/esm/use-window-event/use-window-event.js +4 -4
- package/esm/use-window-event/use-window-event.js.map +1 -1
- package/esm/use-window-scroll/use-window-scroll.js +17 -19
- package/esm/use-window-scroll/use-window-scroll.js.map +1 -1
- package/esm/utils/clamp/clamp.js +10 -1
- package/esm/utils/clamp/clamp.js.map +1 -1
- package/esm/utils/lower-first/lower-first.js +1 -1
- package/esm/utils/lower-first/lower-first.js.map +1 -1
- package/esm/utils/random-id/random-id.js +1 -1
- package/esm/utils/random-id/random-id.js.map +1 -1
- package/esm/utils/range/range.js +2 -2
- package/esm/utils/range/range.js.map +1 -1
- package/esm/utils/shallow-equal/shallow-equal.js +19 -19
- package/esm/utils/shallow-equal/shallow-equal.js.map +1 -1
- package/esm/utils/upper-first/upper-first.js +1 -1
- package/esm/utils/upper-first/upper-first.js.map +1 -1
- package/lib/use-hover/use-hover.d.ts +1 -0
- package/lib/use-mouse/use-mouse.d.ts +1 -0
- package/lib/use-move/use-move.d.ts +2 -1
- package/lib/use-scroll-into-view/use-scroll-into-view.d.ts +1 -0
- package/lib/utils/clamp/clamp.d.ts +1 -1
- package/package.json +5 -4
|
@@ -5,34 +5,33 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
5
5
|
var React = require('react');
|
|
6
6
|
|
|
7
7
|
function useInterval(fn, interval) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
return { start, stop, toggle, active };
|
|
8
|
+
const [active, setActive] = React.useState(false);
|
|
9
|
+
const intervalRef = React.useRef();
|
|
10
|
+
const fnRef = React.useRef();
|
|
11
|
+
React.useEffect(() => {
|
|
12
|
+
fnRef.current = fn;
|
|
13
|
+
}, [fn]);
|
|
14
|
+
const start = () => {
|
|
15
|
+
setActive((old) => {
|
|
16
|
+
if (!old && !intervalRef.current) {
|
|
17
|
+
intervalRef.current = window.setInterval(fnRef.current, interval);
|
|
18
|
+
}
|
|
19
|
+
return true;
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
const stop = () => {
|
|
23
|
+
setActive(false);
|
|
24
|
+
window.clearInterval(intervalRef.current);
|
|
25
|
+
intervalRef.current = void 0;
|
|
26
|
+
};
|
|
27
|
+
const toggle = () => {
|
|
28
|
+
if (active) {
|
|
29
|
+
stop();
|
|
30
|
+
} else {
|
|
31
|
+
start();
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
return { start, stop, toggle, active };
|
|
36
35
|
}
|
|
37
36
|
|
|
38
37
|
exports.useInterval = useInterval;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-interval.js","sources":["../../src/use-interval/use-interval.ts"],"sourcesContent":["import { useEffect, useRef, useState } from 'react';\n\nexport function useInterval(fn: () => void, interval: number) {\n const [active, setActive] = useState(false);\n const intervalRef = useRef<number>();\n const fnRef = useRef<() => void>();\n\n useEffect(() => {\n fnRef.current = fn;\n }, [fn]);\n\n const start = () => {\n setActive((old) => {\n if (!old && !intervalRef.current) {\n intervalRef.current = window.setInterval(fnRef.current!, interval);\n }\n return true;\n });\n };\n\n const stop = () => {\n setActive(false);\n window.clearInterval(intervalRef.current);\n intervalRef.current = undefined;\n };\n\n const toggle = () => {\n if (active) {\n stop();\n } else {\n start();\n }\n };\n\n return { start, stop, toggle, active };\n}\n"],"names":["useState","useRef","useEffect"],"mappings":";;;;;;
|
|
1
|
+
{"version":3,"file":"use-interval.js","sources":["../../src/use-interval/use-interval.ts"],"sourcesContent":["import { useEffect, useRef, useState } from 'react';\n\nexport function useInterval(fn: () => void, interval: number) {\n const [active, setActive] = useState(false);\n const intervalRef = useRef<number>();\n const fnRef = useRef<() => void>();\n\n useEffect(() => {\n fnRef.current = fn;\n }, [fn]);\n\n const start = () => {\n setActive((old) => {\n if (!old && !intervalRef.current) {\n intervalRef.current = window.setInterval(fnRef.current!, interval);\n }\n return true;\n });\n };\n\n const stop = () => {\n setActive(false);\n window.clearInterval(intervalRef.current);\n intervalRef.current = undefined;\n };\n\n const toggle = () => {\n if (active) {\n stop();\n } else {\n start();\n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C;;;;"}
|
|
@@ -4,9 +4,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
4
4
|
|
|
5
5
|
var React = require('react');
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
// useIsomorphicEffect removes it by replacing useLayoutEffect with useEffect during ssr
|
|
9
|
-
const useIsomorphicEffect = typeof document !== 'undefined' ? React.useLayoutEffect : React.useEffect;
|
|
7
|
+
const useIsomorphicEffect = typeof document !== "undefined" ? React.useLayoutEffect : React.useEffect;
|
|
10
8
|
|
|
11
9
|
exports.useIsomorphicEffect = useIsomorphicEffect;
|
|
12
10
|
//# sourceMappingURL=use-isomorphic-effect.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-isomorphic-effect.js","sources":["../../src/use-isomorphic-effect/use-isomorphic-effect.ts"],"sourcesContent":["import { useEffect, useLayoutEffect } from 'react';\n\n// useLayoutEffect will show warning if used during ssr, e.g. with Next.js\n// useIsomorphicEffect removes it by replacing useLayoutEffect with useEffect during ssr\nexport const useIsomorphicEffect = typeof document !== 'undefined' ? useLayoutEffect : useEffect;\n"],"names":["useLayoutEffect","useEffect"],"mappings":";;;;;;
|
|
1
|
+
{"version":3,"file":"use-isomorphic-effect.js","sources":["../../src/use-isomorphic-effect/use-isomorphic-effect.ts"],"sourcesContent":["import { useEffect, useLayoutEffect } from 'react';\n\n// useLayoutEffect will show warning if used during ssr, e.g. with Next.js\n// useIsomorphicEffect removes it by replacing useLayoutEffect with useEffect during ssr\nexport const useIsomorphicEffect = typeof document !== 'undefined' ? useLayoutEffect : useEffect;\n"],"names":["useLayoutEffect","useEffect"],"mappings":";;;;;;AACY,MAAC,mBAAmB,GAAG,OAAO,QAAQ,KAAK,WAAW,GAAGA,qBAAe,GAAGC;;;;"}
|
|
@@ -4,62 +4,83 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
4
4
|
|
|
5
5
|
var React = require('react');
|
|
6
6
|
|
|
7
|
+
var __defProp = Object.defineProperty;
|
|
8
|
+
var __defProps = Object.defineProperties;
|
|
9
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
10
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
11
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
12
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
13
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
14
|
+
var __spreadValues = (a, b) => {
|
|
15
|
+
for (var prop in b || (b = {}))
|
|
16
|
+
if (__hasOwnProp.call(b, prop))
|
|
17
|
+
__defNormalProp(a, prop, b[prop]);
|
|
18
|
+
if (__getOwnPropSymbols)
|
|
19
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
20
|
+
if (__propIsEnum.call(b, prop))
|
|
21
|
+
__defNormalProp(a, prop, b[prop]);
|
|
22
|
+
}
|
|
23
|
+
return a;
|
|
24
|
+
};
|
|
25
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
7
26
|
function useListState(initialValue = []) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
27
|
+
const [state, setState] = React.useState(initialValue);
|
|
28
|
+
const append = (...items) => setState((current) => [...current, ...items]);
|
|
29
|
+
const prepend = (...items) => setState((current) => [...items, ...current]);
|
|
30
|
+
const insert = (index, ...items) => setState((current) => [...current.slice(0, index), ...items, ...current.slice(index)]);
|
|
31
|
+
const apply = (fn) => setState((current) => current.map((item, index) => fn(item, index)));
|
|
32
|
+
const remove = (...indices) => setState((current) => current.filter((_, index) => !indices.includes(index)));
|
|
33
|
+
const pop = () => setState((current) => {
|
|
34
|
+
const cloned = [...current];
|
|
35
|
+
cloned.pop();
|
|
36
|
+
return cloned;
|
|
37
|
+
});
|
|
38
|
+
const shift = () => setState((current) => {
|
|
39
|
+
const cloned = [...current];
|
|
40
|
+
cloned.shift();
|
|
41
|
+
return cloned;
|
|
42
|
+
});
|
|
43
|
+
const reorder = ({ from, to }) => setState((current) => {
|
|
44
|
+
const cloned = [...current];
|
|
45
|
+
const item = current[from];
|
|
46
|
+
cloned.splice(from, 1);
|
|
47
|
+
cloned.splice(to, 0, item);
|
|
48
|
+
return cloned;
|
|
49
|
+
});
|
|
50
|
+
const setItem = (index, item) => setState((current) => {
|
|
51
|
+
const cloned = [...current];
|
|
52
|
+
cloned[index] = item;
|
|
53
|
+
return cloned;
|
|
54
|
+
});
|
|
55
|
+
const setItemProp = (index, prop, value) => setState((current) => {
|
|
56
|
+
const cloned = [...current];
|
|
57
|
+
cloned[index] = __spreadProps(__spreadValues({}, cloned[index]), { [prop]: value });
|
|
58
|
+
return cloned;
|
|
59
|
+
});
|
|
60
|
+
const applyWhere = (condition, fn) => setState(
|
|
61
|
+
(current) => current.map((item, index) => condition(item, index) ? fn(item, index) : item)
|
|
62
|
+
);
|
|
63
|
+
const filter = (fn) => {
|
|
64
|
+
setState((current) => current.filter(fn));
|
|
65
|
+
};
|
|
66
|
+
return [
|
|
67
|
+
state,
|
|
68
|
+
{
|
|
69
|
+
setState,
|
|
70
|
+
append,
|
|
71
|
+
prepend,
|
|
72
|
+
insert,
|
|
73
|
+
pop,
|
|
74
|
+
shift,
|
|
75
|
+
apply,
|
|
76
|
+
applyWhere,
|
|
77
|
+
remove,
|
|
78
|
+
reorder,
|
|
79
|
+
setItem,
|
|
80
|
+
setItemProp,
|
|
81
|
+
filter
|
|
82
|
+
}
|
|
83
|
+
];
|
|
63
84
|
}
|
|
64
85
|
|
|
65
86
|
exports.useListState = useListState;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-list-state.js","sources":["../../src/use-list-state/use-list-state.ts"],"sourcesContent":["import React, { useState } from 'react';\n\nexport interface UseListStateHandlers<T> {\n setState: React.Dispatch<React.SetStateAction<T[]>>;\n append: (...items: T[]) => void;\n prepend: (...items: T[]) => void;\n insert: (index: number, ...items: T[]) => void;\n pop: () => void;\n shift: () => void;\n apply: (fn: (item: T, index?: number) => T) => void;\n applyWhere: (\n condition: (item: T, index: number) => boolean,\n fn: (item: T, index?: number) => T\n ) => void;\n remove: (...indices: number[]) => void;\n reorder: ({ from, to }: { from: number; to: number }) => void;\n setItem: (index: number, item: T) => void;\n setItemProp: <K extends keyof T, U extends T[K]>(index: number, prop: K, value: U) => void;\n filter: (fn: (item: T, i: number) => boolean) => void;\n}\n\nexport type UseListState<T> = [T[], UseListStateHandlers<T>];\n\nexport function useListState<T>(initialValue: T[] = []): UseListState<T> {\n const [state, setState] = useState(initialValue);\n\n const append = (...items: T[]) => setState((current) => [...current, ...items]);\n const prepend = (...items: T[]) => setState((current) => [...items, ...current]);\n\n const insert = (index: number, ...items: T[]) =>\n setState((current) => [...current.slice(0, index), ...items, ...current.slice(index)]);\n\n const apply = (fn: (item: T, index?: number) => T) =>\n setState((current) => current.map((item, index) => fn(item, index)));\n\n const remove = (...indices: number[]) =>\n setState((current) => current.filter((_, index) => !indices.includes(index)));\n\n const pop = () =>\n setState((current) => {\n const cloned = [...current];\n cloned.pop();\n return cloned;\n });\n\n const shift = () =>\n setState((current) => {\n const cloned = [...current];\n cloned.shift();\n return cloned;\n });\n\n const reorder = ({ from, to }: { from: number; to: number }) =>\n setState((current) => {\n const cloned = [...current];\n const item = current[from];\n\n cloned.splice(from, 1);\n cloned.splice(to, 0, item);\n\n return cloned;\n });\n\n const setItem = (index: number, item: T) =>\n setState((current) => {\n const cloned = [...current];\n cloned[index] = item;\n return cloned;\n });\n\n const setItemProp = <K extends keyof T, U extends T[K]>(index: number, prop: K, value: U) =>\n setState((current) => {\n const cloned = [...current];\n cloned[index] = { ...cloned[index], [prop]: value };\n return cloned;\n });\n\n const applyWhere = (\n condition: (item: T, index: number) => boolean,\n fn: (item: T, index?: number) => T\n ) =>\n setState((current) =>\n current.map((item, index) => (condition(item, index) ? fn(item, index) : item))\n );\n\n const filter = (fn: (item: T, i: number) => boolean) => {\n setState((current) => current.filter(fn));\n };\n\n return [\n state,\n {\n setState,\n append,\n prepend,\n insert,\n pop,\n shift,\n apply,\n applyWhere,\n remove,\n reorder,\n setItem,\n setItemProp,\n filter,\n },\n ];\n}\n"],"names":["useState"],"mappings":";;;;;;
|
|
1
|
+
{"version":3,"file":"use-list-state.js","sources":["../../src/use-list-state/use-list-state.ts"],"sourcesContent":["import React, { useState } from 'react';\n\nexport interface UseListStateHandlers<T> {\n setState: React.Dispatch<React.SetStateAction<T[]>>;\n append: (...items: T[]) => void;\n prepend: (...items: T[]) => void;\n insert: (index: number, ...items: T[]) => void;\n pop: () => void;\n shift: () => void;\n apply: (fn: (item: T, index?: number) => T) => void;\n applyWhere: (\n condition: (item: T, index: number) => boolean,\n fn: (item: T, index?: number) => T\n ) => void;\n remove: (...indices: number[]) => void;\n reorder: ({ from, to }: { from: number; to: number }) => void;\n setItem: (index: number, item: T) => void;\n setItemProp: <K extends keyof T, U extends T[K]>(index: number, prop: K, value: U) => void;\n filter: (fn: (item: T, i: number) => boolean) => void;\n}\n\nexport type UseListState<T> = [T[], UseListStateHandlers<T>];\n\nexport function useListState<T>(initialValue: T[] = []): UseListState<T> {\n const [state, setState] = useState(initialValue);\n\n const append = (...items: T[]) => setState((current) => [...current, ...items]);\n const prepend = (...items: T[]) => setState((current) => [...items, ...current]);\n\n const insert = (index: number, ...items: T[]) =>\n setState((current) => [...current.slice(0, index), ...items, ...current.slice(index)]);\n\n const apply = (fn: (item: T, index?: number) => T) =>\n setState((current) => current.map((item, index) => fn(item, index)));\n\n const remove = (...indices: number[]) =>\n setState((current) => current.filter((_, index) => !indices.includes(index)));\n\n const pop = () =>\n setState((current) => {\n const cloned = [...current];\n cloned.pop();\n return cloned;\n });\n\n const shift = () =>\n setState((current) => {\n const cloned = [...current];\n cloned.shift();\n return cloned;\n });\n\n const reorder = ({ from, to }: { from: number; to: number }) =>\n setState((current) => {\n const cloned = [...current];\n const item = current[from];\n\n cloned.splice(from, 1);\n cloned.splice(to, 0, item);\n\n return cloned;\n });\n\n const setItem = (index: number, item: T) =>\n setState((current) => {\n const cloned = [...current];\n cloned[index] = item;\n return cloned;\n });\n\n const setItemProp = <K extends keyof T, U extends T[K]>(index: number, prop: K, value: U) =>\n setState((current) => {\n const cloned = [...current];\n cloned[index] = { ...cloned[index], [prop]: value };\n return cloned;\n });\n\n const applyWhere = (\n condition: (item: T, index: number) => boolean,\n fn: (item: T, index?: number) => T\n ) =>\n setState((current) =>\n current.map((item, index) => (condition(item, index) ? fn(item, index) : item))\n );\n\n const filter = (fn: (item: T, i: number) => boolean) => {\n setState((current) => current.filter(fn));\n };\n\n return [\n state,\n {\n setState,\n append,\n prepend,\n insert,\n pop,\n shift,\n apply,\n applyWhere,\n remove,\n reorder,\n setItem,\n setItemProp,\n filter,\n },\n ];\n}\n"],"names":["useState"],"mappings":";;;;;;AAAA,IAAI,SAAS,GAAG,MAAM,CAAC,cAAc,CAAC;AACtC,IAAI,UAAU,GAAG,MAAM,CAAC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,GAAG,KAAK,EAAE,CAAC,CAAC;AACxF,IAAI,OAAO,MAAM,CAAC;AAClB,GAAG,CAAC,CAAC;AACL,EAAE,MAAM,UAAU,GAAG,CAAC,SAAS,EAAE,EAAE,KAAK,QAAQ;AAChD,IAAI,CAAC,OAAO,KAAK,OAAO,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,KAAK,KAAK,SAAS,CAAC,IAAI,EAAE,KAAK,CAAC,GAAG,EAAE,CAAC,IAAI,EAAE,KAAK,CAAC,GAAG,IAAI,CAAC;AAC9F,GAAG,CAAC;AACJ,EAAE,MAAM,MAAM,GAAG,CAAC,EAAE,KAAK;AACzB,IAAI,QAAQ,CAAC,CAAC,OAAO,KAAK,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,CAAC;AAC9C,GAAG,CAAC;AACJ,EAAE,OAAO;AACT,IAAI,KAAK;AACT,IAAI;AACJ,MAAM,QAAQ;AACd,MAAM,MAAM;AACZ,MAAM,OAAO;AACb,MAAM,MAAM;AACZ,MAAM,GAAG;AACT,MAAM,KAAK;AACX,MAAM,KAAK;AACX,MAAM,UAAU;AAChB,MAAM,MAAM;AACZ,MAAM,OAAO;AACb,MAAM,OAAO;AACb,MAAM,WAAW;AACjB,MAAM,MAAM;AACZ,KAAK;AACL,GAAG,CAAC;AACJ;;;;"}
|
|
@@ -6,81 +6,89 @@ var React = require('react');
|
|
|
6
6
|
var useWindowEvent = require('../use-window-event/use-window-event.js');
|
|
7
7
|
|
|
8
8
|
function serializeJSON(value, hookName) {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
9
|
+
try {
|
|
10
|
+
return JSON.stringify(value);
|
|
11
|
+
} catch (error) {
|
|
12
|
+
throw new Error(`@mantine/hooks ${hookName}: Failed to serialize the value`);
|
|
13
|
+
}
|
|
15
14
|
}
|
|
16
15
|
function deserializeJSON(value) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
16
|
+
try {
|
|
17
|
+
return JSON.parse(value);
|
|
18
|
+
} catch (e) {
|
|
19
|
+
return value;
|
|
20
|
+
}
|
|
23
21
|
}
|
|
24
22
|
function createStorage(type, hookName) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
23
|
+
const eventName = type === "localStorage" ? "mantine-local-storage" : "mantine-session-storage";
|
|
24
|
+
return function useStorage({
|
|
25
|
+
key,
|
|
26
|
+
defaultValue = void 0,
|
|
27
|
+
getInitialValueInEffect = true,
|
|
28
|
+
deserialize = deserializeJSON,
|
|
29
|
+
serialize = (value) => serializeJSON(value, hookName)
|
|
30
|
+
}) {
|
|
31
|
+
const readStorageValue = React.useCallback(
|
|
32
|
+
(skipStorage) => {
|
|
33
|
+
if (typeof window === "undefined" || !(type in window) || window[type] === null || skipStorage) {
|
|
34
|
+
return defaultValue;
|
|
35
|
+
}
|
|
36
|
+
const storageValue = window[type].getItem(key);
|
|
37
|
+
return storageValue !== null ? deserialize(storageValue) : defaultValue;
|
|
38
|
+
},
|
|
39
|
+
[key, defaultValue]
|
|
40
|
+
);
|
|
41
|
+
const [value, setValue] = React.useState(readStorageValue(getInitialValueInEffect));
|
|
42
|
+
const setStorageValue = React.useCallback(
|
|
43
|
+
(val) => {
|
|
44
|
+
if (val instanceof Function) {
|
|
45
|
+
setValue((current) => {
|
|
46
|
+
const result = val(current);
|
|
47
|
+
window[type].setItem(key, serialize(result));
|
|
48
|
+
window.dispatchEvent(
|
|
49
|
+
new CustomEvent(eventName, { detail: { key, value: val(current) } })
|
|
50
|
+
);
|
|
51
|
+
return result;
|
|
52
|
+
});
|
|
53
|
+
} else {
|
|
54
|
+
window[type].setItem(key, serialize(val));
|
|
55
|
+
window.dispatchEvent(new CustomEvent(eventName, { detail: { key, value: val } }));
|
|
56
|
+
setValue(val);
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
[key]
|
|
60
|
+
);
|
|
61
|
+
const removeStorageValue = React.useCallback(() => {
|
|
62
|
+
window[type].removeItem(key);
|
|
63
|
+
window.dispatchEvent(new CustomEvent(eventName, { detail: { key, value: defaultValue } }));
|
|
64
|
+
}, []);
|
|
65
|
+
useWindowEvent.useWindowEvent("storage", (event) => {
|
|
66
|
+
var _a;
|
|
67
|
+
if (event.storageArea === window[type] && event.key === key) {
|
|
68
|
+
setValue(deserialize((_a = event.newValue) != null ? _a : void 0));
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
useWindowEvent.useWindowEvent(eventName, (event) => {
|
|
72
|
+
if (event.detail.key === key) {
|
|
73
|
+
setValue(event.detail.value);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
React.useEffect(() => {
|
|
77
|
+
if (defaultValue !== void 0 && value === void 0) {
|
|
78
|
+
setStorageValue(defaultValue);
|
|
79
|
+
}
|
|
80
|
+
}, [defaultValue, value, setStorageValue]);
|
|
81
|
+
React.useEffect(() => {
|
|
82
|
+
if (getInitialValueInEffect) {
|
|
83
|
+
setValue(readStorageValue());
|
|
84
|
+
}
|
|
85
|
+
}, []);
|
|
86
|
+
return [
|
|
87
|
+
value === void 0 ? defaultValue : value,
|
|
88
|
+
setStorageValue,
|
|
89
|
+
removeStorageValue
|
|
90
|
+
];
|
|
91
|
+
};
|
|
84
92
|
}
|
|
85
93
|
|
|
86
94
|
exports.createStorage = createStorage;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create-storage.js","sources":["../../src/use-local-storage/create-storage.ts"],"sourcesContent":["import { useState, useCallback, useEffect } from 'react';\nimport { useWindowEvent } from '../use-window-event/use-window-event';\n\nexport type StorageType = 'localStorage' | 'sessionStorage';\n\nexport interface IStorageProperties<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) {\n try {\n return JSON.parse(value);\n } catch {\n return value;\n }\n}\n\nexport function createStorage<T>(type: StorageType, hookName: string) {\n const eventName = type === 'localStorage' ? 'mantine-local-storage' : 'mantine-session-storage';\n\n return function useStorage({\n key,\n defaultValue = undefined,\n getInitialValueInEffect = true,\n deserialize = deserializeJSON,\n serialize = (value: T) => serializeJSON(value, hookName),\n }: IStorageProperties<T>) {\n const readStorageValue = useCallback(\n (skipStorage?: boolean): T => {\n if (\n typeof window === 'undefined' ||\n !(type in window) ||\n window[type] === null ||\n skipStorage\n ) {\n return defaultValue as T;\n }\n\n const storageValue = window[type].getItem(key);\n\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 window[type].setItem(key, serialize(result));\n window.dispatchEvent(\n new CustomEvent(eventName, { detail: { key, value: val(current) } })\n );\n return result;\n });\n } else {\n window[type].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 window[type].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":";;;;;;;
|
|
1
|
+
{"version":3,"file":"create-storage.js","sources":["../../src/use-local-storage/create-storage.ts"],"sourcesContent":["import { useState, useCallback, useEffect } from 'react';\nimport { useWindowEvent } from '../use-window-event/use-window-event';\n\nexport type StorageType = 'localStorage' | 'sessionStorage';\n\nexport interface IStorageProperties<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) {\n try {\n return JSON.parse(value);\n } catch {\n return value;\n }\n}\n\nexport function createStorage<T>(type: StorageType, hookName: string) {\n const eventName = type === 'localStorage' ? 'mantine-local-storage' : 'mantine-session-storage';\n\n return function useStorage({\n key,\n defaultValue = undefined,\n getInitialValueInEffect = true,\n deserialize = deserializeJSON,\n serialize = (value: T) => serializeJSON(value, hookName),\n }: IStorageProperties<T>) {\n const readStorageValue = useCallback(\n (skipStorage?: boolean): T => {\n if (\n typeof window === 'undefined' ||\n !(type in window) ||\n window[type] === null ||\n skipStorage\n ) {\n return defaultValue as T;\n }\n\n const storageValue = window[type].getItem(key);\n\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 window[type].setItem(key, serialize(result));\n window.dispatchEvent(\n new CustomEvent(eventName, { detail: { key, value: val(current) } })\n );\n return result;\n });\n } else {\n window[type].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 window[type].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,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;AAC7B,GAAG,CAAC,OAAO,CAAC,EAAE;AACd,IAAI,OAAO,KAAK,CAAC;AACjB,GAAG;AACH,CAAC;AACM,SAAS,aAAa,CAAC,IAAI,EAAE,QAAQ,EAAE;AAC9C,EAAE,MAAM,SAAS,GAAG,IAAI,KAAK,cAAc,GAAG,uBAAuB,GAAG,yBAAyB,CAAC;AAClG,EAAE,OAAO,SAAS,UAAU,CAAC;AAC7B,IAAI,GAAG;AACP,IAAI,YAAY,GAAG,KAAK,CAAC;AACzB,IAAI,uBAAuB,GAAG,IAAI;AAClC,IAAI,WAAW,GAAG,eAAe;AACjC,IAAI,SAAS,GAAG,CAAC,KAAK,KAAK,aAAa,CAAC,KAAK,EAAE,QAAQ,CAAC;AACzD,GAAG,EAAE;AACL,IAAI,MAAM,gBAAgB,GAAGA,iBAAW;AACxC,MAAM,CAAC,WAAW,KAAK;AACvB,QAAQ,IAAI,OAAO,MAAM,KAAK,WAAW,IAAI,EAAE,IAAI,I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tC,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;;;;"}
|
|
@@ -5,7 +5,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
5
5
|
var createStorage = require('./create-storage.js');
|
|
6
6
|
|
|
7
7
|
function useLocalStorage(props) {
|
|
8
|
-
|
|
8
|
+
return createStorage.createStorage("localStorage", "use-local-storage")(props);
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
exports.useLocalStorage = useLocalStorage;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-local-storage.js","sources":["../../src/use-local-storage/use-local-storage.ts"],"sourcesContent":["import { createStorage, IStorageProperties } from './create-storage';\n\nexport function useLocalStorage<T = string>(props: IStorageProperties<T>) {\n return createStorage<T>('localStorage', 'use-local-storage')(props);\n}\n"],"names":["createStorage"],"mappings":";;;;;;
|
|
1
|
+
{"version":3,"file":"use-local-storage.js","sources":["../../src/use-local-storage/use-local-storage.ts"],"sourcesContent":["import { createStorage, IStorageProperties } from './create-storage';\n\nexport function useLocalStorage<T = string>(props: IStorageProperties<T>) {\n return createStorage<T>('localStorage', 'use-local-storage')(props);\n}\n"],"names":["createStorage"],"mappings":";;;;;;AACO,SAAS,eAAe,CAAC,KAAK,EAAE;AACvC,EAAE,OAAOA,2BAAa,CAAC,cAAc,EAAE,mBAAmB,CAAC,CAAC,KAAK,CAAC,CAAC;AACnE;;;;"}
|
|
@@ -5,16 +5,15 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
5
5
|
var React = require('react');
|
|
6
6
|
var useDidUpdate = require('../use-did-update/use-did-update.js');
|
|
7
7
|
|
|
8
|
-
/* eslint-disable no-console */
|
|
9
8
|
function useLogger(componentName, props) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
9
|
+
React.useEffect(() => {
|
|
10
|
+
console.log(`${componentName} mounted`, ...props);
|
|
11
|
+
return () => console.log(`${componentName} unmounted`);
|
|
12
|
+
}, []);
|
|
13
|
+
useDidUpdate.useDidUpdate(() => {
|
|
14
|
+
console.log(`${componentName} updated`, ...props);
|
|
15
|
+
}, props);
|
|
16
|
+
return null;
|
|
18
17
|
}
|
|
19
18
|
|
|
20
19
|
exports.useLogger = useLogger;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-logger.js","sources":["../../src/use-logger/use-logger.ts"],"sourcesContent":["/* eslint-disable no-console */\nimport { useEffect } from 'react';\nimport { useDidUpdate } from '../use-did-update/use-did-update';\n\nexport function useLogger(componentName: string, props: any[]) {\n useEffect(() => {\n console.log(`${componentName} mounted`, ...props);\n return () => console.log(`${componentName} unmounted`);\n }, []);\n\n useDidUpdate(() => {\n console.log(`${componentName} updated`, ...props);\n }, props);\n\n return null;\n}\n"],"names":["useEffect","useDidUpdate"],"mappings":";;;;;;;
|
|
1
|
+
{"version":3,"file":"use-logger.js","sources":["../../src/use-logger/use-logger.ts"],"sourcesContent":["/* eslint-disable no-console */\nimport { useEffect } from 'react';\nimport { useDidUpdate } from '../use-did-update/use-did-update';\n\nexport function useLogger(componentName: string, props: any[]) {\n useEffect(() => {\n console.log(`${componentName} mounted`, ...props);\n return () => console.log(`${componentName} unmounted`);\n }, []);\n\n useDidUpdate(() => {\n console.log(`${componentName} updated`, ...props);\n }, props);\n\n return null;\n}\n"],"names":["useEffect","useDidUpdate"],"mappings":";;;;;;;AAEO,SAAS,SAAS,CAAC,aAAa,EAAE,KAAK,EAAE;AAChD,EAAEA,eAAS,CAAC,MAAM;AAClB,IAAI,OAAO,CAAC,GAAG,CAAC,CAAC,EAAE,aAAa,CAAC,QAAQ,CAAC,EAAE,GAAG,KAAK,CAAC,CAAC;AACtD,IAAI,OAAO,MAAM,OAAO,CAAC,GAAG,CAAC,CAAC,EAAE,aAAa,CAAC,UAAU,CAAC,CAAC,CAAC;AAC3D,GAAG,EAAE,EAAE,CAAC,CAAC;AACT,EAAEC,yBAAY,CAAC,MAAM;AACrB,IAAI,OAAO,CAAC,GAAG,CAAC,CAAC,EAAE,aAAa,CAAC,QAAQ,CAAC,EAAE,GAAG,KAAK,CAAC,CAAC;AACtD,GAAG,EAAE,KAAK,CAAC,CAAC;AACZ,EAAE,OAAO,IAAI,CAAC;AACd;;;;"}
|
|
@@ -4,43 +4,40 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
4
4
|
|
|
5
5
|
var React = require('react');
|
|
6
6
|
|
|
7
|
-
/**
|
|
8
|
-
* Older versions of Safari (shipped withCatalina and before) do not support addEventListener on matchMedia
|
|
9
|
-
* https://stackoverflow.com/questions/56466261/matchmedia-addlistener-marked-as-deprecated-addeventlistener-equivalent
|
|
10
|
-
* */
|
|
11
7
|
function attachMediaListener(query, callback) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
8
|
+
try {
|
|
9
|
+
query.addEventListener("change", callback);
|
|
10
|
+
return () => query.removeEventListener("change", callback);
|
|
11
|
+
} catch (e) {
|
|
12
|
+
query.addListener(callback);
|
|
13
|
+
return () => query.removeListener(callback);
|
|
14
|
+
}
|
|
20
15
|
}
|
|
21
16
|
function getInitialValue(query, initialValue) {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
17
|
+
if (typeof initialValue === "boolean") {
|
|
18
|
+
return initialValue;
|
|
19
|
+
}
|
|
20
|
+
if (typeof window !== "undefined" && "matchMedia" in window) {
|
|
21
|
+
return window.matchMedia(query).matches;
|
|
22
|
+
}
|
|
23
|
+
return false;
|
|
29
24
|
}
|
|
30
25
|
function useMediaQuery(query, initialValue, { getInitialValueInEffect } = {
|
|
31
|
-
|
|
26
|
+
getInitialValueInEffect: true
|
|
32
27
|
}) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
43
|
-
return
|
|
28
|
+
const [matches, setMatches] = React.useState(
|
|
29
|
+
getInitialValueInEffect ? initialValue : getInitialValue(query, initialValue)
|
|
30
|
+
);
|
|
31
|
+
const queryRef = React.useRef();
|
|
32
|
+
React.useEffect(() => {
|
|
33
|
+
if ("matchMedia" in window) {
|
|
34
|
+
queryRef.current = window.matchMedia(query);
|
|
35
|
+
setMatches(queryRef.current.matches);
|
|
36
|
+
return attachMediaListener(queryRef.current, (event) => setMatches(event.matches));
|
|
37
|
+
}
|
|
38
|
+
return void 0;
|
|
39
|
+
}, [query]);
|
|
40
|
+
return matches;
|
|
44
41
|
}
|
|
45
42
|
|
|
46
43
|
exports.useMediaQuery = useMediaQuery;
|