@snack-uikit/utils 3.10.0 → 3.10.2-preview-23549ae9.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.1 (2025-07-16)
7
+
8
+
9
+ ### Bug Fixes
10
+
11
+ * **PDS-2594:** close watcher ([c7678e0](https://github.com/cloud-ru-tech/snack-uikit/commit/c7678e0f4167e0d6e3a4286cca7460d0fe25dec5))
12
+
13
+
14
+
15
+
16
+
6
17
  # 3.10.0 (2025-07-15)
7
18
 
8
19
 
package/README.md CHANGED
@@ -75,14 +75,13 @@
75
75
  ### Props
76
76
  | name | type | default value | description |
77
77
  |------|------|---------------|-------------|
78
- | options | `FilterStateOptions<TFilter>` | - | |
79
- | parser | `(jsonFilter: string) => TFilter` | - | |
80
- | serializer | `(filter: TFilter) => string` | - | |
78
+ | options | `DataPersistOptions<TData>` | - | |
79
+ | parser | `(jsonData: string) => TData` | - | |
80
+ | serializer | `(data: TData) => string` | - | |
81
81
  ## useModalOpenState
82
+ `hook`
83
+
82
84
  Хук для управления состоянием модалки
83
- ### Props
84
- | name | type | default value | description |
85
- |------|------|---------------|-------------|
86
85
  ## ThemeProvider
87
86
  Провайдер, предназначенный для работы с темами
88
87
  ### Props
@@ -1,16 +1,16 @@
1
1
  import { StateProps } from './useSource';
2
- export type FilterStateOptions<T> = {
3
- filterQueryKey: string;
4
- filterLocalStorageKey: string;
2
+ export type DataPersistOptions<T> = {
3
+ queryKey: string;
4
+ localStorageKey: string;
5
5
  validateData(value: unknown): value is T;
6
6
  };
7
- type FilterStateProps<TFilter> = Omit<StateProps<TFilter>, 'source'> & {
8
- options?: FilterStateOptions<TFilter>;
9
- parser?(jsonFilter: string): TFilter;
10
- serializer?(filter: TFilter): string;
7
+ type DataPersistProps<TData> = Omit<StateProps<TData>, 'source'> & {
8
+ options?: DataPersistOptions<TData>;
9
+ parser?(jsonData: string): TData;
10
+ serializer?(data: TData): string;
11
11
  };
12
- export declare const useDataPersist: <TFilter>({ options, parser, serializer }: FilterStateProps<TFilter>) => {
13
- getDefaultFilter: () => NonNullable<TFilter> | null | undefined;
14
- setDataToStorages: (data: TFilter) => void;
12
+ export declare const useDataPersist: <TData>({ options, parser, serializer }: DataPersistProps<TData>) => {
13
+ getDefaultData: () => NonNullable<TData> | null | undefined;
14
+ setDataToStorages: (data: TData) => void;
15
15
  };
16
16
  export {};
@@ -13,8 +13,8 @@ const useDataPersist = _ref => {
13
13
  parser,
14
14
  serializer
15
15
  } = _ref;
16
- const querySource = (0, react_1.useMemo)(() => options ? new sources_1.QueryParamSource(options.filterQueryKey, options.validateData, parser, serializer) : undefined, [options, parser, serializer]);
17
- const localStorageSource = (0, react_1.useMemo)(() => options ? new sources_1.LocalStorageSource(options.filterLocalStorageKey, options.validateData) : undefined, [options]);
16
+ const querySource = (0, react_1.useMemo)(() => options ? new sources_1.QueryParamSource(options.queryKey, options.validateData, parser, serializer) : undefined, [options, parser, serializer]);
17
+ const localStorageSource = (0, react_1.useMemo)(() => options ? new sources_1.LocalStorageSource(options.localStorageKey, options.validateData) : undefined, [options]);
18
18
  const {
19
19
  getData: getLocalStorageData,
20
20
  setData: setLocalStorageData
@@ -31,12 +31,12 @@ const useDataPersist = _ref => {
31
31
  setLocalStorageData(data);
32
32
  setQueryParamsData(data);
33
33
  }, [setLocalStorageData, setQueryParamsData]);
34
- const getDefaultFilter = (0, react_1.useCallback)(() => {
34
+ const getDefaultData = (0, react_1.useCallback)(() => {
35
35
  const queryData = getQueryParamsData();
36
36
  return queryData ? queryData : getLocalStorageData();
37
37
  }, [getLocalStorageData, getQueryParamsData]);
38
38
  return {
39
- getDefaultFilter,
39
+ getDefaultData,
40
40
  setDataToStorages
41
41
  };
42
42
  };
@@ -1,17 +1,18 @@
1
1
  type UseModalOpenStateOptions = {
2
+ /** закрывать модалку при переходе по истории браузера (по умолчанию false) */
2
3
  closeOnPopstate?: boolean;
4
+ /** закрывать модалку через CloseWatcher (по умолчанию true) */
3
5
  closeByCloseWatcher?: boolean;
4
- onCloseRequest?: () => boolean;
5
6
  };
6
7
  /**
7
8
  * Хук для управления состоянием модалки
8
- * @param open состояние модалки
9
- * @param onClose колбек закрытия
10
- * @param options.closeOnPopstate закрывать модалку при переходе по истории браузера (по умолчанию false)
11
- * @param options.closeByCloseWatcher закрывать модалку через CloseWatcher (по умолчанию true)
12
- * @param options.onCloseRequest колбек запроса закрытия модалки (если возвращает true, то модалка закроется, если false, то не закроется)
13
9
  * @returns [open, onClose]
14
10
  * @see https://developer.mozilla.org/en-US/docs/Web/API/CloseWatcher
11
+ * @function hook
15
12
  */
16
- export declare function useModalOpenState(open: boolean, onClose: () => void, { closeOnPopstate, closeByCloseWatcher, onCloseRequest, }: UseModalOpenStateOptions): void;
13
+ export declare function useModalOpenState(
14
+ /** состояние модалки */
15
+ open: boolean,
16
+ /** колбек закрытия */
17
+ onClose: () => void, { closeOnPopstate, closeByCloseWatcher }: UseModalOpenStateOptions): void;
17
18
  export {};
@@ -7,32 +7,26 @@ 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;
11
10
  /**
12
11
  * Хук для управления состоянием модалки
13
- * @param open состояние модалки
14
- * @param onClose колбек закрытия
15
- * @param options.closeOnPopstate закрывать модалку при переходе по истории браузера (по умолчанию false)
16
- * @param options.closeByCloseWatcher закрывать модалку через CloseWatcher (по умолчанию true)
17
- * @param options.onCloseRequest колбек запроса закрытия модалки (если возвращает true, то модалка закроется, если false, то не закроется)
18
12
  * @returns [open, onClose]
19
13
  * @see https://developer.mozilla.org/en-US/docs/Web/API/CloseWatcher
14
+ * @function hook
20
15
  */
21
- function useModalOpenState(open, onClose, _ref) {
16
+ function useModalOpenState(/** состояние модалки */
17
+ open, /** колбек закрытия */
18
+ onClose, _ref) {
22
19
  let {
23
20
  closeOnPopstate = false,
24
- closeByCloseWatcher = true,
25
- onCloseRequest = defaultOnCloseRequest
21
+ closeByCloseWatcher = true
26
22
  } = _ref;
27
23
  const closeHandler = (0, useEventHandler_1.useEventHandler)(onClose);
28
- const onCloseRequestHandler = (0, useEventHandler_1.useEventHandler)(onCloseRequest);
29
24
  (0, usePopstateSubscription_1.usePopstateSubscription)(() => open && closeHandler(), closeOnPopstate);
30
25
  (0, react_1.useEffect)(() => {
31
26
  if (open && 'CloseWatcher' in window && closeByCloseWatcher) {
32
27
  const watcher = new CloseWatcher();
33
- watcher.oncancel = e => !onCloseRequestHandler() && e.preventDefault();
34
28
  watcher.onclose = () => closeHandler();
35
29
  return () => watcher.destroy();
36
30
  }
37
- }, [open, closeHandler, closeByCloseWatcher, onCloseRequestHandler]);
31
+ }, [open, closeHandler, closeByCloseWatcher]);
38
32
  }
@@ -1,16 +1,16 @@
1
1
  import { StateProps } from './useSource';
2
- export type FilterStateOptions<T> = {
3
- filterQueryKey: string;
4
- filterLocalStorageKey: string;
2
+ export type DataPersistOptions<T> = {
3
+ queryKey: string;
4
+ localStorageKey: string;
5
5
  validateData(value: unknown): value is T;
6
6
  };
7
- type FilterStateProps<TFilter> = Omit<StateProps<TFilter>, 'source'> & {
8
- options?: FilterStateOptions<TFilter>;
9
- parser?(jsonFilter: string): TFilter;
10
- serializer?(filter: TFilter): string;
7
+ type DataPersistProps<TData> = Omit<StateProps<TData>, 'source'> & {
8
+ options?: DataPersistOptions<TData>;
9
+ parser?(jsonData: string): TData;
10
+ serializer?(data: TData): string;
11
11
  };
12
- export declare const useDataPersist: <TFilter>({ options, parser, serializer }: FilterStateProps<TFilter>) => {
13
- getDefaultFilter: () => NonNullable<TFilter> | null | undefined;
14
- setDataToStorages: (data: TFilter) => void;
12
+ export declare const useDataPersist: <TData>({ options, parser, serializer }: DataPersistProps<TData>) => {
13
+ getDefaultData: () => NonNullable<TData> | null | undefined;
14
+ setDataToStorages: (data: TData) => void;
15
15
  };
16
16
  export {};
@@ -2,10 +2,8 @@ import { useCallback, useMemo } from 'react';
2
2
  import { LocalStorageSource, QueryParamSource } from './sources';
3
3
  import { useSource } from './useSource';
4
4
  export const useDataPersist = ({ options, parser, serializer }) => {
5
- const querySource = useMemo(() => options
6
- ? new QueryParamSource(options.filterQueryKey, options.validateData, parser, serializer)
7
- : undefined, [options, parser, serializer]);
8
- const localStorageSource = useMemo(() => (options ? new LocalStorageSource(options.filterLocalStorageKey, options.validateData) : undefined), [options]);
5
+ const querySource = useMemo(() => options ? new QueryParamSource(options.queryKey, options.validateData, parser, serializer) : undefined, [options, parser, serializer]);
6
+ const localStorageSource = useMemo(() => (options ? new LocalStorageSource(options.localStorageKey, options.validateData) : undefined), [options]);
9
7
  const { getData: getLocalStorageData, setData: setLocalStorageData } = useSource({
10
8
  source: querySource,
11
9
  });
@@ -16,9 +14,9 @@ export const useDataPersist = ({ options, parser, serializer }) => {
16
14
  setLocalStorageData(data);
17
15
  setQueryParamsData(data);
18
16
  }, [setLocalStorageData, setQueryParamsData]);
19
- const getDefaultFilter = useCallback(() => {
17
+ const getDefaultData = useCallback(() => {
20
18
  const queryData = getQueryParamsData();
21
19
  return queryData ? queryData : getLocalStorageData();
22
20
  }, [getLocalStorageData, getQueryParamsData]);
23
- return { getDefaultFilter, setDataToStorages };
21
+ return { getDefaultData, setDataToStorages };
24
22
  };
@@ -1,17 +1,18 @@
1
1
  type UseModalOpenStateOptions = {
2
+ /** закрывать модалку при переходе по истории браузера (по умолчанию false) */
2
3
  closeOnPopstate?: boolean;
4
+ /** закрывать модалку через CloseWatcher (по умолчанию true) */
3
5
  closeByCloseWatcher?: boolean;
4
- onCloseRequest?: () => boolean;
5
6
  };
6
7
  /**
7
8
  * Хук для управления состоянием модалки
8
- * @param open состояние модалки
9
- * @param onClose колбек закрытия
10
- * @param options.closeOnPopstate закрывать модалку при переходе по истории браузера (по умолчанию false)
11
- * @param options.closeByCloseWatcher закрывать модалку через CloseWatcher (по умолчанию true)
12
- * @param options.onCloseRequest колбек запроса закрытия модалки (если возвращает true, то модалка закроется, если false, то не закроется)
13
9
  * @returns [open, onClose]
14
10
  * @see https://developer.mozilla.org/en-US/docs/Web/API/CloseWatcher
11
+ * @function hook
15
12
  */
16
- export declare function useModalOpenState(open: boolean, onClose: () => void, { closeOnPopstate, closeByCloseWatcher, onCloseRequest, }: UseModalOpenStateOptions): void;
13
+ export declare function useModalOpenState(
14
+ /** состояние модалки */
15
+ open: boolean,
16
+ /** колбек закрытия */
17
+ onClose: () => void, { closeOnPopstate, closeByCloseWatcher }: UseModalOpenStateOptions): void;
17
18
  export {};
@@ -1,27 +1,24 @@
1
1
  import { useEffect } from 'react';
2
2
  import { useEventHandler } from './useEventHandler';
3
3
  import { usePopstateSubscription } from './usePopstateSubscription';
4
- const defaultOnCloseRequest = () => true;
5
4
  /**
6
5
  * Хук для управления состоянием модалки
7
- * @param open состояние модалки
8
- * @param onClose колбек закрытия
9
- * @param options.closeOnPopstate закрывать модалку при переходе по истории браузера (по умолчанию false)
10
- * @param options.closeByCloseWatcher закрывать модалку через CloseWatcher (по умолчанию true)
11
- * @param options.onCloseRequest колбек запроса закрытия модалки (если возвращает true, то модалка закроется, если false, то не закроется)
12
6
  * @returns [open, onClose]
13
7
  * @see https://developer.mozilla.org/en-US/docs/Web/API/CloseWatcher
8
+ * @function hook
14
9
  */
15
- export function useModalOpenState(open, onClose, { closeOnPopstate = false, closeByCloseWatcher = true, onCloseRequest = defaultOnCloseRequest, }) {
10
+ export function useModalOpenState(
11
+ /** состояние модалки */
12
+ open,
13
+ /** колбек закрытия */
14
+ onClose, { closeOnPopstate = false, closeByCloseWatcher = true }) {
16
15
  const closeHandler = useEventHandler(onClose);
17
- const onCloseRequestHandler = useEventHandler(onCloseRequest);
18
16
  usePopstateSubscription(() => open && closeHandler(), closeOnPopstate);
19
17
  useEffect(() => {
20
18
  if (open && 'CloseWatcher' in window && closeByCloseWatcher) {
21
19
  const watcher = new CloseWatcher();
22
- watcher.oncancel = e => !onCloseRequestHandler() && e.preventDefault();
23
20
  watcher.onclose = () => closeHandler();
24
21
  return () => watcher.destroy();
25
22
  }
26
- }, [open, closeHandler, closeByCloseWatcher, onCloseRequestHandler]);
23
+ }, [open, closeHandler, closeByCloseWatcher]);
27
24
  }
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "access": "public"
5
5
  },
6
6
  "title": "Utils",
7
- "version": "3.10.0",
7
+ "version": "3.10.2-preview-23549ae9.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": "60a306ca6f30a3dc7363ecc71b1584acc98dea58"
42
+ "gitHead": "e42f92b88079738efc564d7fa74f5fbd18e2fd6d"
43
43
  }
@@ -3,50 +3,49 @@ import { useCallback, useMemo } from 'react';
3
3
  import { LocalStorageSource, QueryParamSource } from './sources';
4
4
  import { StateProps, useSource } from './useSource';
5
5
 
6
- export type FilterStateOptions<T> = {
7
- filterQueryKey: string;
8
- filterLocalStorageKey: string;
6
+ export type DataPersistOptions<T> = {
7
+ queryKey: string;
8
+ localStorageKey: string;
9
9
  validateData(value: unknown): value is T;
10
10
  };
11
11
 
12
- type FilterStateProps<TFilter> = Omit<StateProps<TFilter>, 'source'> & {
13
- options?: FilterStateOptions<TFilter>;
14
- parser?(jsonFilter: string): TFilter;
15
- serializer?(filter: TFilter): string;
12
+ type DataPersistProps<TData> = Omit<StateProps<TData>, 'source'> & {
13
+ options?: DataPersistOptions<TData>;
14
+ parser?(jsonData: string): TData;
15
+ serializer?(data: TData): string;
16
16
  };
17
17
 
18
- export const useDataPersist = <TFilter>({ options, parser, serializer }: FilterStateProps<TFilter>) => {
18
+ export const useDataPersist = <TData>({ options, parser, serializer }: DataPersistProps<TData>) => {
19
19
  const querySource = useMemo(
20
20
  () =>
21
- options
22
- ? new QueryParamSource<TFilter>(options.filterQueryKey, options.validateData, parser, serializer)
23
- : undefined,
21
+ options ? new QueryParamSource<TData>(options.queryKey, options.validateData, parser, serializer) : undefined,
24
22
  [options, parser, serializer],
25
23
  );
26
24
  const localStorageSource = useMemo(
27
- () => (options ? new LocalStorageSource<TFilter>(options.filterLocalStorageKey, options.validateData) : undefined),
25
+ () => (options ? new LocalStorageSource<TData>(options.localStorageKey, options.validateData) : undefined),
28
26
  [options],
29
27
  );
30
28
 
31
- const { getData: getLocalStorageData, setData: setLocalStorageData } = useSource<TFilter>({
29
+ const { getData: getLocalStorageData, setData: setLocalStorageData } = useSource<TData>({
32
30
  source: querySource,
33
31
  });
34
- const { getData: getQueryParamsData, setData: setQueryParamsData } = useSource<TFilter>({
32
+
33
+ const { getData: getQueryParamsData, setData: setQueryParamsData } = useSource<TData>({
35
34
  source: localStorageSource,
36
35
  });
37
36
 
38
37
  const setDataToStorages = useCallback(
39
- (data: TFilter) => {
38
+ (data: TData) => {
40
39
  setLocalStorageData(data);
41
40
  setQueryParamsData(data);
42
41
  },
43
42
  [setLocalStorageData, setQueryParamsData],
44
43
  );
45
44
 
46
- const getDefaultFilter = useCallback(() => {
45
+ const getDefaultData = useCallback(() => {
47
46
  const queryData = getQueryParamsData();
48
47
  return queryData ? queryData : getLocalStorageData();
49
48
  }, [getLocalStorageData, getQueryParamsData]);
50
49
 
51
- return { getDefaultFilter, setDataToStorages };
50
+ return { getDefaultData, setDataToStorages };
52
51
  };
@@ -4,44 +4,34 @@ import { useEventHandler } from './useEventHandler';
4
4
  import { usePopstateSubscription } from './usePopstateSubscription';
5
5
 
6
6
  type UseModalOpenStateOptions = {
7
+ /** закрывать модалку при переходе по истории браузера (по умолчанию false) */
7
8
  closeOnPopstate?: boolean;
9
+ /** закрывать модалку через CloseWatcher (по умолчанию true) */
8
10
  closeByCloseWatcher?: boolean;
9
- onCloseRequest?: () => boolean;
10
11
  };
11
12
 
12
- const defaultOnCloseRequest = () => true;
13
-
14
13
  /**
15
14
  * Хук для управления состоянием модалки
16
- * @param open состояние модалки
17
- * @param onClose колбек закрытия
18
- * @param options.closeOnPopstate закрывать модалку при переходе по истории браузера (по умолчанию false)
19
- * @param options.closeByCloseWatcher закрывать модалку через CloseWatcher (по умолчанию true)
20
- * @param options.onCloseRequest колбек запроса закрытия модалки (если возвращает true, то модалка закроется, если false, то не закроется)
21
15
  * @returns [open, onClose]
22
16
  * @see https://developer.mozilla.org/en-US/docs/Web/API/CloseWatcher
17
+ * @function hook
23
18
  */
24
19
  export function useModalOpenState(
20
+ /** состояние модалки */
25
21
  open: boolean,
22
+ /** колбек закрытия */
26
23
  onClose: () => void,
27
- {
28
- closeOnPopstate = false,
29
- closeByCloseWatcher = true,
30
- onCloseRequest = defaultOnCloseRequest,
31
- }: UseModalOpenStateOptions,
24
+ { closeOnPopstate = false, closeByCloseWatcher = true }: UseModalOpenStateOptions,
32
25
  ) {
33
26
  const closeHandler = useEventHandler(onClose);
34
- const onCloseRequestHandler = useEventHandler(onCloseRequest);
35
27
 
36
28
  usePopstateSubscription(() => open && closeHandler(), closeOnPopstate);
37
29
 
38
30
  useEffect(() => {
39
31
  if (open && 'CloseWatcher' in window && closeByCloseWatcher) {
40
32
  const watcher = new CloseWatcher();
41
- watcher.oncancel = e => !onCloseRequestHandler() && e.preventDefault();
42
33
  watcher.onclose = () => closeHandler();
43
-
44
34
  return () => watcher.destroy();
45
35
  }
46
- }, [open, closeHandler, closeByCloseWatcher, onCloseRequestHandler]);
36
+ }, [open, closeHandler, closeByCloseWatcher]);
47
37
  }