@snack-uikit/utils 3.9.1-preview-09106514.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
@@ -1,6 +1,7 @@
1
1
  type UseModalOpenStateOptions = {
2
2
  closeOnPopstate?: boolean;
3
3
  closeByCloseWatcher?: boolean;
4
+ onCloseRequest?: () => boolean;
4
5
  };
5
6
  /**
6
7
  * Хук для управления состоянием модалки
@@ -8,8 +9,9 @@ type UseModalOpenStateOptions = {
8
9
  * @param onClose колбек закрытия
9
10
  * @param options.closeOnPopstate закрывать модалку при переходе по истории браузера (по умолчанию false)
10
11
  * @param options.closeByCloseWatcher закрывать модалку через CloseWatcher (по умолчанию true)
12
+ * @param options.onCloseRequest колбек запроса закрытия модалки (если возвращает true, то модалка закроется, если false, то не закроется)
11
13
  * @returns [open, onClose]
12
14
  * @see https://developer.mozilla.org/en-US/docs/Web/API/CloseWatcher
13
15
  */
14
- export declare function useModalOpenState(open: boolean, onClose: () => void, { closeOnPopstate, closeByCloseWatcher }: UseModalOpenStateOptions): void;
16
+ export declare function useModalOpenState(open: boolean, onClose: () => void, { closeOnPopstate, closeByCloseWatcher, onCloseRequest, }: UseModalOpenStateOptions): void;
15
17
  export {};
@@ -7,27 +7,32 @@ exports.useModalOpenState = useModalOpenState;
7
7
  const react_1 = require("react");
8
8
  const useEventHandler_1 = require("./useEventHandler");
9
9
  const usePopstateSubscription_1 = require("./usePopstateSubscription");
10
+ const defaultOnCloseRequest = () => true;
10
11
  /**
11
12
  * Хук для управления состоянием модалки
12
13
  * @param open состояние модалки
13
14
  * @param onClose колбек закрытия
14
15
  * @param options.closeOnPopstate закрывать модалку при переходе по истории браузера (по умолчанию false)
15
16
  * @param options.closeByCloseWatcher закрывать модалку через CloseWatcher (по умолчанию true)
17
+ * @param options.onCloseRequest колбек запроса закрытия модалки (если возвращает true, то модалка закроется, если false, то не закроется)
16
18
  * @returns [open, onClose]
17
19
  * @see https://developer.mozilla.org/en-US/docs/Web/API/CloseWatcher
18
20
  */
19
21
  function useModalOpenState(open, onClose, _ref) {
20
22
  let {
21
23
  closeOnPopstate = false,
22
- closeByCloseWatcher = true
24
+ closeByCloseWatcher = true,
25
+ onCloseRequest = defaultOnCloseRequest
23
26
  } = _ref;
24
27
  const closeHandler = (0, useEventHandler_1.useEventHandler)(onClose);
28
+ const onCloseRequestHandler = (0, useEventHandler_1.useEventHandler)(onCloseRequest);
25
29
  (0, usePopstateSubscription_1.usePopstateSubscription)(() => open && closeHandler(), closeOnPopstate);
26
30
  (0, react_1.useEffect)(() => {
27
31
  if (open && 'CloseWatcher' in window && closeByCloseWatcher) {
28
32
  const watcher = new CloseWatcher();
33
+ watcher.oncancel = e => !onCloseRequestHandler() && e.preventDefault();
29
34
  watcher.onclose = () => closeHandler();
30
35
  return () => watcher.destroy();
31
36
  }
32
- }, [open, closeHandler, closeByCloseWatcher]);
37
+ }, [open, closeHandler, closeByCloseWatcher, onCloseRequestHandler]);
33
38
  }
@@ -1,6 +1,7 @@
1
1
  type UseModalOpenStateOptions = {
2
2
  closeOnPopstate?: boolean;
3
3
  closeByCloseWatcher?: boolean;
4
+ onCloseRequest?: () => boolean;
4
5
  };
5
6
  /**
6
7
  * Хук для управления состоянием модалки
@@ -8,8 +9,9 @@ type UseModalOpenStateOptions = {
8
9
  * @param onClose колбек закрытия
9
10
  * @param options.closeOnPopstate закрывать модалку при переходе по истории браузера (по умолчанию false)
10
11
  * @param options.closeByCloseWatcher закрывать модалку через CloseWatcher (по умолчанию true)
12
+ * @param options.onCloseRequest колбек запроса закрытия модалки (если возвращает true, то модалка закроется, если false, то не закроется)
11
13
  * @returns [open, onClose]
12
14
  * @see https://developer.mozilla.org/en-US/docs/Web/API/CloseWatcher
13
15
  */
14
- export declare function useModalOpenState(open: boolean, onClose: () => void, { closeOnPopstate, closeByCloseWatcher }: UseModalOpenStateOptions): void;
16
+ export declare function useModalOpenState(open: boolean, onClose: () => void, { closeOnPopstate, closeByCloseWatcher, onCloseRequest, }: UseModalOpenStateOptions): void;
15
17
  export {};
@@ -1,23 +1,27 @@
1
1
  import { useEffect } from 'react';
2
2
  import { useEventHandler } from './useEventHandler';
3
3
  import { usePopstateSubscription } from './usePopstateSubscription';
4
+ const defaultOnCloseRequest = () => true;
4
5
  /**
5
6
  * Хук для управления состоянием модалки
6
7
  * @param open состояние модалки
7
8
  * @param onClose колбек закрытия
8
9
  * @param options.closeOnPopstate закрывать модалку при переходе по истории браузера (по умолчанию false)
9
10
  * @param options.closeByCloseWatcher закрывать модалку через CloseWatcher (по умолчанию true)
11
+ * @param options.onCloseRequest колбек запроса закрытия модалки (если возвращает true, то модалка закроется, если false, то не закроется)
10
12
  * @returns [open, onClose]
11
13
  * @see https://developer.mozilla.org/en-US/docs/Web/API/CloseWatcher
12
14
  */
13
- export function useModalOpenState(open, onClose, { closeOnPopstate = false, closeByCloseWatcher = true }) {
15
+ export function useModalOpenState(open, onClose, { closeOnPopstate = false, closeByCloseWatcher = true, onCloseRequest = defaultOnCloseRequest, }) {
14
16
  const closeHandler = useEventHandler(onClose);
17
+ const onCloseRequestHandler = useEventHandler(onCloseRequest);
15
18
  usePopstateSubscription(() => open && closeHandler(), closeOnPopstate);
16
19
  useEffect(() => {
17
20
  if (open && 'CloseWatcher' in window && closeByCloseWatcher) {
18
21
  const watcher = new CloseWatcher();
22
+ watcher.oncancel = e => !onCloseRequestHandler() && e.preventDefault();
19
23
  watcher.onclose = () => closeHandler();
20
24
  return () => watcher.destroy();
21
25
  }
22
- }, [open, closeHandler, closeByCloseWatcher]);
26
+ }, [open, closeHandler, closeByCloseWatcher, onCloseRequestHandler]);
23
27
  }
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "access": "public"
5
5
  },
6
6
  "title": "Utils",
7
- "version": "3.9.1-preview-09106514.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": "46d1e0103764bd1f5a509559a0c3f169d66151f7"
42
+ "gitHead": "60a306ca6f30a3dc7363ecc71b1584acc98dea58"
43
43
  }
@@ -6,31 +6,42 @@ import { usePopstateSubscription } from './usePopstateSubscription';
6
6
  type UseModalOpenStateOptions = {
7
7
  closeOnPopstate?: boolean;
8
8
  closeByCloseWatcher?: boolean;
9
+ onCloseRequest?: () => boolean;
9
10
  };
10
11
 
12
+ const defaultOnCloseRequest = () => true;
13
+
11
14
  /**
12
15
  * Хук для управления состоянием модалки
13
16
  * @param open состояние модалки
14
17
  * @param onClose колбек закрытия
15
18
  * @param options.closeOnPopstate закрывать модалку при переходе по истории браузера (по умолчанию false)
16
19
  * @param options.closeByCloseWatcher закрывать модалку через CloseWatcher (по умолчанию true)
20
+ * @param options.onCloseRequest колбек запроса закрытия модалки (если возвращает true, то модалка закроется, если false, то не закроется)
17
21
  * @returns [open, onClose]
18
22
  * @see https://developer.mozilla.org/en-US/docs/Web/API/CloseWatcher
19
23
  */
20
24
  export function useModalOpenState(
21
25
  open: boolean,
22
26
  onClose: () => void,
23
- { closeOnPopstate = false, closeByCloseWatcher = true }: UseModalOpenStateOptions,
27
+ {
28
+ closeOnPopstate = false,
29
+ closeByCloseWatcher = true,
30
+ onCloseRequest = defaultOnCloseRequest,
31
+ }: UseModalOpenStateOptions,
24
32
  ) {
25
33
  const closeHandler = useEventHandler(onClose);
34
+ const onCloseRequestHandler = useEventHandler(onCloseRequest);
26
35
 
27
36
  usePopstateSubscription(() => open && closeHandler(), closeOnPopstate);
28
37
 
29
38
  useEffect(() => {
30
39
  if (open && 'CloseWatcher' in window && closeByCloseWatcher) {
31
40
  const watcher = new CloseWatcher();
41
+ watcher.oncancel = e => !onCloseRequestHandler() && e.preventDefault();
32
42
  watcher.onclose = () => closeHandler();
43
+
33
44
  return () => watcher.destroy();
34
45
  }
35
- }, [open, closeHandler, closeByCloseWatcher]);
46
+ }, [open, closeHandler, closeByCloseWatcher, onCloseRequestHandler]);
36
47
  }