react-side-sheet-pro 0.1.6 → 0.1.8

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.
@@ -39,19 +39,31 @@ var SideSheetProvider = exports.SideSheetProvider = function SideSheetProvider(_
39
39
  stack = _useReducer2[0],
40
40
  dispatch = _useReducer2[1];
41
41
  var stackRef = (0, _react.useRef)(stack);
42
- var overflowRef = (0, _react.useRef)('');
42
+ var overflowRef = (0, _react.useRef)(null);
43
43
  var idRef = (0, _react.useRef)(0);
44
44
  (0, _react.useEffect)(function () {
45
45
  stackRef.current = stack;
46
+ if (!config.enableOverflow) {
47
+ console.warn([document.body.style.overflow, stack.length, overflowRef.current]);
48
+ if (!stack.length) {
49
+ console.warn(overflowRef.current);
50
+ if (overflowRef.current !== null) {
51
+ document.body.style.overflow = overflowRef.current;
52
+ overflowRef.current = null;
53
+ }
54
+ } else if (stack.length === 1) {
55
+ if (overflowRef.current === null) {
56
+ overflowRef.current = document.body.style.overflow;
57
+ }
58
+ document.body.style.overflow = 'hidden';
59
+ }
60
+ }
46
61
  }, [stack]);
47
62
  var open = (0, _react.useCallback)(function (element) {
63
+ var _opts$id;
48
64
  var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
49
- var id = ++idRef.current;
65
+ var id = (_opts$id = opts.id) !== null && _opts$id !== void 0 ? _opts$id : ++idRef.current;
50
66
  var options = _objectSpread(_objectSpread({}, _defaultOptions.DEFAULT_SHEET_OPTIONS), opts);
51
- if (!config.enableOverflow && stackRef.current.length === 0) {
52
- overflowRef.current = document.body.style.overflow;
53
- document.body.style.overflow = 'hidden';
54
- }
55
67
  dispatch({
56
68
  type: 'OPEN',
57
69
  payload: {
@@ -134,30 +146,30 @@ var SideSheetProvider = exports.SideSheetProvider = function SideSheetProvider(_
134
146
  _iterator.f();
135
147
  return _context.finish(22);
136
148
  case 25:
137
- if (!config.enableOverflow && stackRef.current.length <= itemsToClose.length) {
138
- document.body.style.overflow = overflowRef.current;
139
- }
140
149
  duration = (_itemsToClose = itemsToClose[itemsToClose.length - 1]) === null || _itemsToClose === void 0 ? void 0 : _itemsToClose.options.animationDuration;
141
150
  dispatch({
142
151
  type: 'CLOSE',
143
152
  id: id
144
153
  });
145
- setTimeout(function () {
146
- if (id === null) {
147
- itemsToClose.forEach(function (item) {
148
- return dispatch({
154
+ return _context.abrupt("return", new Promise(function (resolve) {
155
+ setTimeout(function () {
156
+ if (id === null) {
157
+ itemsToClose.forEach(function (item) {
158
+ return dispatch({
159
+ type: 'REMOVE',
160
+ id: item.id
161
+ });
162
+ });
163
+ } else {
164
+ dispatch({
149
165
  type: 'REMOVE',
150
- id: item.id
166
+ id: id
151
167
  });
152
- });
153
- } else {
154
- dispatch({
155
- type: 'REMOVE',
156
- id: id
157
- });
158
- }
159
- }, duration);
160
- case 29:
168
+ }
169
+ resolve();
170
+ }, duration);
171
+ }));
172
+ case 28:
161
173
  case "end":
162
174
  return _context.stop();
163
175
  }
@@ -32,6 +32,7 @@ var DEFAULT_OPTIONS = exports.DEFAULT_OPTIONS = {
32
32
  }()
33
33
  };
34
34
  var DEFAULT_SHEET_OPTIONS = exports.DEFAULT_SHEET_OPTIONS = {
35
+ id: '',
35
36
  width: 400,
36
37
  className: '',
37
38
  confirmBeforeClose: false,
package/index.d.ts CHANGED
@@ -9,6 +9,7 @@ interface SideSheetOptions {
9
9
  confirmCallback: (message: string) => Promise<boolean>;
10
10
  }
11
11
  interface SideOptions {
12
+ id?: number | string;
12
13
  width?: number;
13
14
  className?: string;
14
15
  confirmBeforeClose?: boolean;
@@ -17,27 +18,27 @@ interface SideOptions {
17
18
  closeOnOverlayClick?: boolean;
18
19
  closeOnEsc?: boolean;
19
20
  animationDuration?: number;
20
- onOpen?: (id: number) => void;
21
- onClose?: (id: number) => void;
21
+ onOpen?: (id: number | string) => void;
22
+ onClose?: (id: number | string) => void;
22
23
  }
23
24
  interface SideElementProps {
24
- sideId: number;
25
- close: (id: number | null, force?: boolean) => Promise<void>;
26
- open: (element: SideElement, options?: SideOptions) => number;
27
- update: (id: number, options: SideOptions) => void;
25
+ sideId: number | string;
26
+ close: (id: number | string | null, force?: boolean) => Promise<void>;
27
+ open: (element: SideElement, options?: SideOptions) => number | string;
28
+ update: (id: number | string, options: SideOptions) => void;
28
29
  options: SideOptions;
29
30
  }
30
31
  type SideElement = (props: SideElementProps) => ReactNode;
31
32
  interface SideStackItem {
32
- id: number;
33
+ id: number | string;
33
34
  element: SideElement;
34
35
  options: Required<SideOptions>;
35
36
  state: "opening" | "open" | "closing";
36
37
  }
37
38
  interface SideSheetContextValue {
38
- open: (el: SideElement, opts?: SideOptions) => number;
39
- close: (id: number | null, force?: boolean) => Promise<void>;
40
- update: (id: number, opts: SideOptions) => void;
39
+ open: (el: SideElement, opts?: SideOptions) => number | string;
40
+ close: (id: number | string | null, force?: boolean) => Promise<void>;
41
+ update: (id: number | string, opts: SideOptions) => void;
41
42
  config: SideSheetOptions;
42
43
  }
43
44
  declare const useSideSheet: () => SideSheetContextValue;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "react-side-sheet-pro",
3
3
  "description": "A flexible React SideSheet component for displaying contextual information.",
4
- "version": "0.1.6",
4
+ "version": "0.1.8",
5
5
  "license": "MIT",
6
6
  "keywords": [
7
7
  "react",
@@ -9,9 +9,9 @@ import {
9
9
 
10
10
  type SideSheetContainerProps = {
11
11
  stack: SideStackItem[];
12
- open: (el: SideElement, opts?: SideOptions) => number;
13
- close: (id: number | null) => Promise<void>;
14
- update: (id: number, opts: SideOptions) => void;
12
+ open: (el: SideElement, opts?: SideOptions) => number | string;
13
+ close: (id: number | string | null) => Promise<void>;
14
+ update: (id: number | string, opts: SideOptions) => void;
15
15
  config: Required<SideSheetOptions>;
16
16
  };
17
17
 
@@ -31,20 +31,30 @@ export const SideSheetProvider: React.FC<{
31
31
  }> = ({ children, configuration }) => {
32
32
  const [stack, dispatch] = useReducer(SideSheetReducer, []);
33
33
  const stackRef = useRef<SideStackItem[]>(stack);
34
- const overflowRef = useRef('');
34
+ const overflowRef = useRef<string | null>(null);
35
35
  const idRef = useRef(0);
36
36
 
37
37
  useEffect(() => {
38
38
  stackRef.current = stack;
39
+
40
+ if (!config.enableOverflow) {
41
+ if (!stack.length) {
42
+ if (overflowRef.current !== null) {
43
+ document.body.style.overflow = overflowRef.current;
44
+ overflowRef.current = null;
45
+ }
46
+ } else if (stack.length === 1) {
47
+ if (overflowRef.current === null) {
48
+ overflowRef.current = document.body.style.overflow;
49
+ }
50
+ document.body.style.overflow = 'hidden';
51
+ }
52
+ }
39
53
  }, [stack]);
40
54
 
41
55
  const open = useCallback((element: SideElement, opts: SideOptions = {}) => {
42
- const id = ++idRef.current;
56
+ const id = opts.id ?? ++idRef.current;
43
57
  const options = { ...DEFAULT_SHEET_OPTIONS, ...opts };
44
- if (!config.enableOverflow && stackRef.current.length === 0) {
45
- overflowRef.current = document.body.style.overflow;
46
- document.body.style.overflow = 'hidden';
47
- }
48
58
  dispatch({
49
59
  type: 'OPEN',
50
60
  payload: { id, element, options, state: 'opening' },
@@ -56,46 +66,50 @@ export const SideSheetProvider: React.FC<{
56
66
  return id;
57
67
  }, []);
58
68
 
59
- const close = useCallback(async (id: number | null, force = false) => {
60
- const itemsToClose =
61
- id === null
62
- ? [...stackRef.current]
63
- : stackRef.current.filter(i => i.id === id);
69
+ const close = useCallback(
70
+ async (id: number | string | null, force = false) => {
71
+ const itemsToClose =
72
+ id === null
73
+ ? [...stackRef.current]
74
+ : stackRef.current.filter(i => i.id === id);
64
75
 
65
- for (const item of itemsToClose) {
66
- if (! force && item.options.confirmBeforeClose) {
67
- const confirmCallback =
68
- item.options.confirmCallback ?? config.confirmCallback;
69
- const confirmed = await confirmCallback(
70
- item.options.confirmMessage ?? config.confirmMessage
71
- );
72
- if (!confirmed) return;
76
+ for (const item of itemsToClose) {
77
+ if (!force && item.options.confirmBeforeClose) {
78
+ const confirmCallback =
79
+ item.options.confirmCallback ?? config.confirmCallback;
80
+ const confirmed = await confirmCallback(
81
+ item.options.confirmMessage ?? config.confirmMessage
82
+ );
83
+ if (!confirmed) return;
84
+ }
85
+ item.options.onClose?.(item.id);
73
86
  }
74
- item.options.onClose?.(item.id);
75
- }
76
-
77
- if (
78
- !config.enableOverflow &&
79
- stackRef.current.length <= itemsToClose.length
80
- ) {
81
- document.body.style.overflow = overflowRef.current;
82
- }
83
87
 
84
- const duration =
85
- itemsToClose[itemsToClose.length - 1]?.options.animationDuration;
86
- dispatch({ type: 'CLOSE', id });
87
- setTimeout(() => {
88
- if (id === null) {
89
- itemsToClose.forEach(item => dispatch({ type: 'REMOVE', id: item.id }));
90
- } else {
91
- dispatch({ type: 'REMOVE', id: id! });
92
- }
93
- }, duration);
94
- }, []);
88
+ const duration =
89
+ itemsToClose[itemsToClose.length - 1]?.options.animationDuration;
90
+ dispatch({ type: 'CLOSE', id });
91
+ return new Promise<void>(resolve => {
92
+ setTimeout(() => {
93
+ if (id === null) {
94
+ itemsToClose.forEach(item =>
95
+ dispatch({ type: 'REMOVE', id: item.id })
96
+ );
97
+ } else {
98
+ dispatch({ type: 'REMOVE', id: id! });
99
+ }
100
+ resolve();
101
+ }, duration);
102
+ });
103
+ },
104
+ []
105
+ );
95
106
 
96
- const update = useCallback((id: number, options: Partial<SideOptions>) => {
97
- dispatch({ type: 'UPDATE', id, options });
98
- }, []);
107
+ const update = useCallback(
108
+ (id: number | string, options: Partial<SideOptions>) => {
109
+ dispatch({ type: 'UPDATE', id, options });
110
+ },
111
+ []
112
+ );
99
113
  const config = { ...DEFAULT_OPTIONS, ...configuration } as Required<
100
114
  SideSheetOptions
101
115
  >;
@@ -12,6 +12,7 @@ export const DEFAULT_OPTIONS: Required<SideSheetOptions> = {
12
12
  };
13
13
 
14
14
  export const DEFAULT_SHEET_OPTIONS: Required<SideOptions> = {
15
+ id: '',
15
16
  width: 400,
16
17
  className: '',
17
18
  confirmBeforeClose: false,
@@ -2,10 +2,10 @@ import { SideOptions, SideStackItem } from '../types';
2
2
 
3
3
  type Action =
4
4
  | { type: 'OPEN'; payload: SideStackItem }
5
- | { type: 'SET_OPEN'; id: number }
6
- | { type: 'CLOSE'; id: number | null }
7
- | { type: 'REMOVE'; id: number }
8
- | { type: 'UPDATE'; id: number; options: Partial<SideOptions> };
5
+ | { type: 'SET_OPEN'; id: number | string }
6
+ | { type: 'CLOSE'; id: number | string | null }
7
+ | { type: 'REMOVE'; id: number | string }
8
+ | { type: 'UPDATE'; id: number | string; options: Partial<SideOptions> };
9
9
 
10
10
  export const SideSheetReducer = (
11
11
  state: SideStackItem[],
@@ -11,6 +11,7 @@ export interface SideSheetOptions {
11
11
  }
12
12
 
13
13
  export interface SideOptions {
14
+ id?: string | number;
14
15
  width?: number;
15
16
  className?: string;
16
17
  confirmBeforeClose?: boolean;
@@ -19,30 +20,30 @@ export interface SideOptions {
19
20
  closeOnOverlayClick?: boolean;
20
21
  closeOnEsc?: boolean;
21
22
  animationDuration?: number;
22
- onOpen?: (id: number) => void;
23
- onClose?: (id: number) => void;
23
+ onOpen?: (id: number | string) => void;
24
+ onClose?: (id: number | string) => void;
24
25
  }
25
26
 
26
27
  export interface SideElementProps {
27
- sideId: number;
28
- close: (id: number | null) => Promise<void>;
29
- open: (element: SideElement, options?: SideOptions) => number;
30
- update: (id: number, options: SideOptions) => void;
28
+ sideId: number | string;
29
+ close: (id: number | string | null) => Promise<void>;
30
+ open: (element: SideElement, options?: SideOptions) => number | string;
31
+ update: (id: number | string, options: SideOptions) => void;
31
32
  options: SideOptions;
32
33
  }
33
34
 
34
35
  export type SideElement = (props: SideElementProps) => ReactNode;
35
36
 
36
37
  export interface SideStackItem {
37
- id: number;
38
+ id: number | string;
38
39
  element: SideElement;
39
40
  options: Required<SideOptions>;
40
41
  state: 'opening' | 'open' | 'closing';
41
42
  }
42
43
 
43
44
  export interface SideSheetContextValue {
44
- open: (el: SideElement, opts?: SideOptions) => number;
45
- close: (id: number | null, force: boolean) => Promise<void>;
46
- update: (id: number, opts: SideOptions) => void;
45
+ open: (el: SideElement, opts?: SideOptions) => number | string;
46
+ close: (id: number | string | null, force: boolean) => Promise<void>;
47
+ update: (id: number | string, opts: SideOptions) => void;
47
48
  config: SideSheetOptions;
48
49
  }