@snack-uikit/utils 3.9.0 → 3.10.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/CHANGELOG.md CHANGED
@@ -3,6 +3,17 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # 3.10.0 (2025-07-15)
7
+
8
+
9
+ ### Features
10
+
11
+ * **PDS-2594:** add CloseWatcher using in modals and drawers ([7699e55](https://github.com/cloud-ru-tech/snack-uikit/commit/7699e55dc172f6082369b6fb83815c12d28b3f7a))
12
+
13
+
14
+
15
+
16
+
6
17
  # 3.9.0 (2025-07-07)
7
18
 
8
19
 
package/README.md CHANGED
@@ -78,6 +78,11 @@
78
78
  | options | `FilterStateOptions<TFilter>` | - | |
79
79
  | parser | `(jsonFilter: string) => TFilter` | - | |
80
80
  | serializer | `(filter: TFilter) => string` | - | |
81
+ ## useModalOpenState
82
+ Хук для управления состоянием модалки
83
+ ### Props
84
+ | name | type | default value | description |
85
+ |------|------|---------------|-------------|
81
86
  ## ThemeProvider
82
87
  Провайдер, предназначенный для работы с темами
83
88
  ### Props
@@ -7,3 +7,4 @@ export * from './useThemeConfig';
7
7
  export * from './useValueControl';
8
8
  export * from './useDataPersist';
9
9
  export * from './usePopstateSubscription';
10
+ export * from './useModalOpenState';
@@ -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,17 @@
1
+ type UseModalOpenStateOptions = {
2
+ closeOnPopstate?: boolean;
3
+ closeByCloseWatcher?: boolean;
4
+ onCloseRequest?: () => boolean;
5
+ };
6
+ /**
7
+ * Хук для управления состоянием модалки
8
+ * @param open состояние модалки
9
+ * @param onClose колбек закрытия
10
+ * @param options.closeOnPopstate закрывать модалку при переходе по истории браузера (по умолчанию false)
11
+ * @param options.closeByCloseWatcher закрывать модалку через CloseWatcher (по умолчанию true)
12
+ * @param options.onCloseRequest колбек запроса закрытия модалки (если возвращает true, то модалка закроется, если false, то не закроется)
13
+ * @returns [open, onClose]
14
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/CloseWatcher
15
+ */
16
+ export declare function useModalOpenState(open: boolean, onClose: () => void, { closeOnPopstate, closeByCloseWatcher, onCloseRequest, }: UseModalOpenStateOptions): void;
17
+ export {};
@@ -0,0 +1,38 @@
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
+ const defaultOnCloseRequest = () => true;
11
+ /**
12
+ * Хук для управления состоянием модалки
13
+ * @param open состояние модалки
14
+ * @param onClose колбек закрытия
15
+ * @param options.closeOnPopstate закрывать модалку при переходе по истории браузера (по умолчанию false)
16
+ * @param options.closeByCloseWatcher закрывать модалку через CloseWatcher (по умолчанию true)
17
+ * @param options.onCloseRequest колбек запроса закрытия модалки (если возвращает true, то модалка закроется, если false, то не закроется)
18
+ * @returns [open, onClose]
19
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/CloseWatcher
20
+ */
21
+ function useModalOpenState(open, onClose, _ref) {
22
+ let {
23
+ closeOnPopstate = false,
24
+ closeByCloseWatcher = true,
25
+ onCloseRequest = defaultOnCloseRequest
26
+ } = _ref;
27
+ const closeHandler = (0, useEventHandler_1.useEventHandler)(onClose);
28
+ const onCloseRequestHandler = (0, useEventHandler_1.useEventHandler)(onCloseRequest);
29
+ (0, usePopstateSubscription_1.usePopstateSubscription)(() => open && closeHandler(), closeOnPopstate);
30
+ (0, react_1.useEffect)(() => {
31
+ if (open && 'CloseWatcher' in window && closeByCloseWatcher) {
32
+ const watcher = new CloseWatcher();
33
+ watcher.oncancel = e => !onCloseRequestHandler() && e.preventDefault();
34
+ watcher.onclose = () => closeHandler();
35
+ return () => watcher.destroy();
36
+ }
37
+ }, [open, closeHandler, closeByCloseWatcher, onCloseRequestHandler]);
38
+ }
@@ -7,3 +7,4 @@ export * from './useThemeConfig';
7
7
  export * from './useValueControl';
8
8
  export * from './useDataPersist';
9
9
  export * from './usePopstateSubscription';
10
+ export * from './useModalOpenState';
@@ -7,3 +7,4 @@ export * from './useThemeConfig';
7
7
  export * from './useValueControl';
8
8
  export * from './useDataPersist';
9
9
  export * from './usePopstateSubscription';
10
+ export * from './useModalOpenState';
@@ -0,0 +1,17 @@
1
+ type UseModalOpenStateOptions = {
2
+ closeOnPopstate?: boolean;
3
+ closeByCloseWatcher?: boolean;
4
+ onCloseRequest?: () => boolean;
5
+ };
6
+ /**
7
+ * Хук для управления состоянием модалки
8
+ * @param open состояние модалки
9
+ * @param onClose колбек закрытия
10
+ * @param options.closeOnPopstate закрывать модалку при переходе по истории браузера (по умолчанию false)
11
+ * @param options.closeByCloseWatcher закрывать модалку через CloseWatcher (по умолчанию true)
12
+ * @param options.onCloseRequest колбек запроса закрытия модалки (если возвращает true, то модалка закроется, если false, то не закроется)
13
+ * @returns [open, onClose]
14
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/CloseWatcher
15
+ */
16
+ export declare function useModalOpenState(open: boolean, onClose: () => void, { closeOnPopstate, closeByCloseWatcher, onCloseRequest, }: UseModalOpenStateOptions): void;
17
+ export {};
@@ -0,0 +1,27 @@
1
+ import { useEffect } from 'react';
2
+ import { useEventHandler } from './useEventHandler';
3
+ import { usePopstateSubscription } from './usePopstateSubscription';
4
+ const defaultOnCloseRequest = () => true;
5
+ /**
6
+ * Хук для управления состоянием модалки
7
+ * @param open состояние модалки
8
+ * @param onClose колбек закрытия
9
+ * @param options.closeOnPopstate закрывать модалку при переходе по истории браузера (по умолчанию false)
10
+ * @param options.closeByCloseWatcher закрывать модалку через CloseWatcher (по умолчанию true)
11
+ * @param options.onCloseRequest колбек запроса закрытия модалки (если возвращает true, то модалка закроется, если false, то не закроется)
12
+ * @returns [open, onClose]
13
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/CloseWatcher
14
+ */
15
+ export function useModalOpenState(open, onClose, { closeOnPopstate = false, closeByCloseWatcher = true, onCloseRequest = defaultOnCloseRequest, }) {
16
+ const closeHandler = useEventHandler(onClose);
17
+ const onCloseRequestHandler = useEventHandler(onCloseRequest);
18
+ usePopstateSubscription(() => open && closeHandler(), closeOnPopstate);
19
+ useEffect(() => {
20
+ if (open && 'CloseWatcher' in window && closeByCloseWatcher) {
21
+ const watcher = new CloseWatcher();
22
+ watcher.oncancel = e => !onCloseRequestHandler() && e.preventDefault();
23
+ watcher.onclose = () => closeHandler();
24
+ return () => watcher.destroy();
25
+ }
26
+ }, [open, closeHandler, closeByCloseWatcher, onCloseRequestHandler]);
27
+ }
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.10.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": "9e08a5c61a90e739f44ba128242a95d866452dc0"
42
+ "gitHead": "60a306ca6f30a3dc7363ecc71b1584acc98dea58"
43
43
  }
@@ -7,3 +7,4 @@ export * from './useThemeConfig';
7
7
  export * from './useValueControl';
8
8
  export * from './useDataPersist';
9
9
  export * from './usePopstateSubscription';
10
+ export * from './useModalOpenState';
@@ -0,0 +1,47 @@
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
+ onCloseRequest?: () => boolean;
10
+ };
11
+
12
+ const defaultOnCloseRequest = () => true;
13
+
14
+ /**
15
+ * Хук для управления состоянием модалки
16
+ * @param open состояние модалки
17
+ * @param onClose колбек закрытия
18
+ * @param options.closeOnPopstate закрывать модалку при переходе по истории браузера (по умолчанию false)
19
+ * @param options.closeByCloseWatcher закрывать модалку через CloseWatcher (по умолчанию true)
20
+ * @param options.onCloseRequest колбек запроса закрытия модалки (если возвращает true, то модалка закроется, если false, то не закроется)
21
+ * @returns [open, onClose]
22
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/CloseWatcher
23
+ */
24
+ export function useModalOpenState(
25
+ open: boolean,
26
+ onClose: () => void,
27
+ {
28
+ closeOnPopstate = false,
29
+ closeByCloseWatcher = true,
30
+ onCloseRequest = defaultOnCloseRequest,
31
+ }: UseModalOpenStateOptions,
32
+ ) {
33
+ const closeHandler = useEventHandler(onClose);
34
+ const onCloseRequestHandler = useEventHandler(onCloseRequest);
35
+
36
+ usePopstateSubscription(() => open && closeHandler(), closeOnPopstate);
37
+
38
+ useEffect(() => {
39
+ if (open && 'CloseWatcher' in window && closeByCloseWatcher) {
40
+ const watcher = new CloseWatcher();
41
+ watcher.oncancel = e => !onCloseRequestHandler() && e.preventDefault();
42
+ watcher.onclose = () => closeHandler();
43
+
44
+ return () => watcher.destroy();
45
+ }
46
+ }, [open, closeHandler, closeByCloseWatcher, onCloseRequestHandler]);
47
+ }