@snack-uikit/utils 3.9.0 → 3.9.1-preview-09106514.0
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/dist/cjs/hooks/index.d.ts +1 -0
- package/dist/cjs/hooks/index.js +2 -1
- package/dist/cjs/hooks/useModalOpenState.d.ts +15 -0
- package/dist/cjs/hooks/useModalOpenState.js +33 -0
- package/dist/esm/hooks/index.d.ts +1 -0
- package/dist/esm/hooks/index.js +1 -0
- package/dist/esm/hooks/useModalOpenState.d.ts +15 -0
- package/dist/esm/hooks/useModalOpenState.js +23 -0
- package/package.json +2 -2
- package/src/hooks/index.ts +1 -0
- package/src/hooks/useModalOpenState.ts +36 -0
package/dist/cjs/hooks/index.js
CHANGED
|
@@ -30,4 +30,5 @@ __exportStar(require("./useSwipeable"), exports);
|
|
|
30
30
|
__exportStar(require("./useThemeConfig"), exports);
|
|
31
31
|
__exportStar(require("./useValueControl"), exports);
|
|
32
32
|
__exportStar(require("./useDataPersist"), exports);
|
|
33
|
-
__exportStar(require("./usePopstateSubscription"), exports);
|
|
33
|
+
__exportStar(require("./usePopstateSubscription"), exports);
|
|
34
|
+
__exportStar(require("./useModalOpenState"), exports);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
type UseModalOpenStateOptions = {
|
|
2
|
+
closeOnPopstate?: boolean;
|
|
3
|
+
closeByCloseWatcher?: boolean;
|
|
4
|
+
};
|
|
5
|
+
/**
|
|
6
|
+
* Хук для управления состоянием модалки
|
|
7
|
+
* @param open состояние модалки
|
|
8
|
+
* @param onClose колбек закрытия
|
|
9
|
+
* @param options.closeOnPopstate закрывать модалку при переходе по истории браузера (по умолчанию false)
|
|
10
|
+
* @param options.closeByCloseWatcher закрывать модалку через CloseWatcher (по умолчанию true)
|
|
11
|
+
* @returns [open, onClose]
|
|
12
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/CloseWatcher
|
|
13
|
+
*/
|
|
14
|
+
export declare function useModalOpenState(open: boolean, onClose: () => void, { closeOnPopstate, closeByCloseWatcher }: UseModalOpenStateOptions): void;
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.useModalOpenState = useModalOpenState;
|
|
7
|
+
const react_1 = require("react");
|
|
8
|
+
const useEventHandler_1 = require("./useEventHandler");
|
|
9
|
+
const usePopstateSubscription_1 = require("./usePopstateSubscription");
|
|
10
|
+
/**
|
|
11
|
+
* Хук для управления состоянием модалки
|
|
12
|
+
* @param open состояние модалки
|
|
13
|
+
* @param onClose колбек закрытия
|
|
14
|
+
* @param options.closeOnPopstate закрывать модалку при переходе по истории браузера (по умолчанию false)
|
|
15
|
+
* @param options.closeByCloseWatcher закрывать модалку через CloseWatcher (по умолчанию true)
|
|
16
|
+
* @returns [open, onClose]
|
|
17
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/CloseWatcher
|
|
18
|
+
*/
|
|
19
|
+
function useModalOpenState(open, onClose, _ref) {
|
|
20
|
+
let {
|
|
21
|
+
closeOnPopstate = false,
|
|
22
|
+
closeByCloseWatcher = true
|
|
23
|
+
} = _ref;
|
|
24
|
+
const closeHandler = (0, useEventHandler_1.useEventHandler)(onClose);
|
|
25
|
+
(0, usePopstateSubscription_1.usePopstateSubscription)(() => open && closeHandler(), closeOnPopstate);
|
|
26
|
+
(0, react_1.useEffect)(() => {
|
|
27
|
+
if (open && 'CloseWatcher' in window && closeByCloseWatcher) {
|
|
28
|
+
const watcher = new CloseWatcher();
|
|
29
|
+
watcher.onclose = () => closeHandler();
|
|
30
|
+
return () => watcher.destroy();
|
|
31
|
+
}
|
|
32
|
+
}, [open, closeHandler, closeByCloseWatcher]);
|
|
33
|
+
}
|
package/dist/esm/hooks/index.js
CHANGED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
type UseModalOpenStateOptions = {
|
|
2
|
+
closeOnPopstate?: boolean;
|
|
3
|
+
closeByCloseWatcher?: boolean;
|
|
4
|
+
};
|
|
5
|
+
/**
|
|
6
|
+
* Хук для управления состоянием модалки
|
|
7
|
+
* @param open состояние модалки
|
|
8
|
+
* @param onClose колбек закрытия
|
|
9
|
+
* @param options.closeOnPopstate закрывать модалку при переходе по истории браузера (по умолчанию false)
|
|
10
|
+
* @param options.closeByCloseWatcher закрывать модалку через CloseWatcher (по умолчанию true)
|
|
11
|
+
* @returns [open, onClose]
|
|
12
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/CloseWatcher
|
|
13
|
+
*/
|
|
14
|
+
export declare function useModalOpenState(open: boolean, onClose: () => void, { closeOnPopstate, closeByCloseWatcher }: UseModalOpenStateOptions): void;
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { useEffect } from 'react';
|
|
2
|
+
import { useEventHandler } from './useEventHandler';
|
|
3
|
+
import { usePopstateSubscription } from './usePopstateSubscription';
|
|
4
|
+
/**
|
|
5
|
+
* Хук для управления состоянием модалки
|
|
6
|
+
* @param open состояние модалки
|
|
7
|
+
* @param onClose колбек закрытия
|
|
8
|
+
* @param options.closeOnPopstate закрывать модалку при переходе по истории браузера (по умолчанию false)
|
|
9
|
+
* @param options.closeByCloseWatcher закрывать модалку через CloseWatcher (по умолчанию true)
|
|
10
|
+
* @returns [open, onClose]
|
|
11
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/CloseWatcher
|
|
12
|
+
*/
|
|
13
|
+
export function useModalOpenState(open, onClose, { closeOnPopstate = false, closeByCloseWatcher = true }) {
|
|
14
|
+
const closeHandler = useEventHandler(onClose);
|
|
15
|
+
usePopstateSubscription(() => open && closeHandler(), closeOnPopstate);
|
|
16
|
+
useEffect(() => {
|
|
17
|
+
if (open && 'CloseWatcher' in window && closeByCloseWatcher) {
|
|
18
|
+
const watcher = new CloseWatcher();
|
|
19
|
+
watcher.onclose = () => closeHandler();
|
|
20
|
+
return () => watcher.destroy();
|
|
21
|
+
}
|
|
22
|
+
}, [open, closeHandler, closeByCloseWatcher]);
|
|
23
|
+
}
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
6
|
"title": "Utils",
|
|
7
|
-
"version": "3.9.0",
|
|
7
|
+
"version": "3.9.1-preview-09106514.0",
|
|
8
8
|
"sideEffects": [
|
|
9
9
|
"*.css",
|
|
10
10
|
"*.woff",
|
|
@@ -39,5 +39,5 @@
|
|
|
39
39
|
"react-swipeable": "7.0.2",
|
|
40
40
|
"uncontrollable": "8.0.4"
|
|
41
41
|
},
|
|
42
|
-
"gitHead": "
|
|
42
|
+
"gitHead": "46d1e0103764bd1f5a509559a0c3f169d66151f7"
|
|
43
43
|
}
|
package/src/hooks/index.ts
CHANGED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { useEffect } from 'react';
|
|
2
|
+
|
|
3
|
+
import { useEventHandler } from './useEventHandler';
|
|
4
|
+
import { usePopstateSubscription } from './usePopstateSubscription';
|
|
5
|
+
|
|
6
|
+
type UseModalOpenStateOptions = {
|
|
7
|
+
closeOnPopstate?: boolean;
|
|
8
|
+
closeByCloseWatcher?: boolean;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Хук для управления состоянием модалки
|
|
13
|
+
* @param open состояние модалки
|
|
14
|
+
* @param onClose колбек закрытия
|
|
15
|
+
* @param options.closeOnPopstate закрывать модалку при переходе по истории браузера (по умолчанию false)
|
|
16
|
+
* @param options.closeByCloseWatcher закрывать модалку через CloseWatcher (по умолчанию true)
|
|
17
|
+
* @returns [open, onClose]
|
|
18
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/CloseWatcher
|
|
19
|
+
*/
|
|
20
|
+
export function useModalOpenState(
|
|
21
|
+
open: boolean,
|
|
22
|
+
onClose: () => void,
|
|
23
|
+
{ closeOnPopstate = false, closeByCloseWatcher = true }: UseModalOpenStateOptions,
|
|
24
|
+
) {
|
|
25
|
+
const closeHandler = useEventHandler(onClose);
|
|
26
|
+
|
|
27
|
+
usePopstateSubscription(() => open && closeHandler(), closeOnPopstate);
|
|
28
|
+
|
|
29
|
+
useEffect(() => {
|
|
30
|
+
if (open && 'CloseWatcher' in window && closeByCloseWatcher) {
|
|
31
|
+
const watcher = new CloseWatcher();
|
|
32
|
+
watcher.onclose = () => closeHandler();
|
|
33
|
+
return () => watcher.destroy();
|
|
34
|
+
}
|
|
35
|
+
}, [open, closeHandler, closeByCloseWatcher]);
|
|
36
|
+
}
|