react-side-sheet-pro 0.1.5 → 0.1.7

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.
@@ -45,8 +45,9 @@ var SideSheetProvider = exports.SideSheetProvider = function SideSheetProvider(_
45
45
  stackRef.current = stack;
46
46
  }, [stack]);
47
47
  var open = (0, _react.useCallback)(function (element) {
48
+ var _opts$id;
48
49
  var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
49
- var id = ++idRef.current;
50
+ var id = (_opts$id = opts.id) !== null && _opts$id !== void 0 ? _opts$id : ++idRef.current;
50
51
  var options = _objectSpread(_objectSpread({}, _defaultOptions.DEFAULT_SHEET_OPTIONS), opts);
51
52
  if (!config.enableOverflow && stackRef.current.length === 0) {
52
53
  overflowRef.current = document.body.style.overflow;
@@ -74,53 +75,66 @@ var SideSheetProvider = exports.SideSheetProvider = function SideSheetProvider(_
74
75
  var close = (0, _react.useCallback)(/*#__PURE__*/function () {
75
76
  var _ref2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(id) {
76
77
  var _itemsToClose;
77
- var itemsToClose, _iterator, _step, _item$options$onClose, _item$options, item, _item$options$confirm, _item$options$confirm2, confirmCallback, confirmed, duration;
78
+ var force,
79
+ itemsToClose,
80
+ _iterator,
81
+ _step,
82
+ _item$options$onClose,
83
+ _item$options,
84
+ item,
85
+ _item$options$confirm,
86
+ _item$options$confirm2,
87
+ confirmCallback,
88
+ confirmed,
89
+ duration,
90
+ _args = arguments;
78
91
  return _regeneratorRuntime().wrap(function _callee$(_context) {
79
92
  while (1) switch (_context.prev = _context.next) {
80
93
  case 0:
94
+ force = _args.length > 1 && _args[1] !== undefined ? _args[1] : false;
81
95
  itemsToClose = id === null ? _toConsumableArray(stackRef.current) : stackRef.current.filter(function (i) {
82
96
  return i.id === id;
83
97
  });
84
98
  _iterator = _createForOfIteratorHelper(itemsToClose);
85
- _context.prev = 2;
99
+ _context.prev = 3;
86
100
  _iterator.s();
87
- case 4:
101
+ case 5:
88
102
  if ((_step = _iterator.n()).done) {
89
- _context.next = 16;
103
+ _context.next = 17;
90
104
  break;
91
105
  }
92
106
  item = _step.value;
93
- if (!item.options.confirmBeforeClose) {
94
- _context.next = 13;
107
+ if (!(!force && item.options.confirmBeforeClose)) {
108
+ _context.next = 14;
95
109
  break;
96
110
  }
97
111
  confirmCallback = (_item$options$confirm = item.options.confirmCallback) !== null && _item$options$confirm !== void 0 ? _item$options$confirm : config.confirmCallback;
98
- _context.next = 10;
112
+ _context.next = 11;
99
113
  return confirmCallback((_item$options$confirm2 = item.options.confirmMessage) !== null && _item$options$confirm2 !== void 0 ? _item$options$confirm2 : config.confirmMessage);
100
- case 10:
114
+ case 11:
101
115
  confirmed = _context.sent;
102
116
  if (confirmed) {
103
- _context.next = 13;
117
+ _context.next = 14;
104
118
  break;
105
119
  }
106
120
  return _context.abrupt("return");
107
- case 13:
108
- (_item$options$onClose = (_item$options = item.options).onClose) === null || _item$options$onClose === void 0 || _item$options$onClose.call(_item$options, item.id);
109
121
  case 14:
110
- _context.next = 4;
122
+ (_item$options$onClose = (_item$options = item.options).onClose) === null || _item$options$onClose === void 0 || _item$options$onClose.call(_item$options, item.id);
123
+ case 15:
124
+ _context.next = 5;
111
125
  break;
112
- case 16:
113
- _context.next = 21;
126
+ case 17:
127
+ _context.next = 22;
114
128
  break;
115
- case 18:
116
- _context.prev = 18;
117
- _context.t0 = _context["catch"](2);
129
+ case 19:
130
+ _context.prev = 19;
131
+ _context.t0 = _context["catch"](3);
118
132
  _iterator.e(_context.t0);
119
- case 21:
120
- _context.prev = 21;
133
+ case 22:
134
+ _context.prev = 22;
121
135
  _iterator.f();
122
- return _context.finish(21);
123
- case 24:
136
+ return _context.finish(22);
137
+ case 25:
124
138
  if (!config.enableOverflow && stackRef.current.length <= itemsToClose.length) {
125
139
  document.body.style.overflow = overflowRef.current;
126
140
  }
@@ -144,11 +158,11 @@ var SideSheetProvider = exports.SideSheetProvider = function SideSheetProvider(_
144
158
  });
145
159
  }
146
160
  }, duration);
147
- case 28:
161
+ case 29:
148
162
  case "end":
149
163
  return _context.stop();
150
164
  }
151
- }, _callee, null, [[2, 18, 21, 24]]);
165
+ }, _callee, null, [[3, 19, 22, 25]]);
152
166
  }));
153
167
  return function (_x) {
154
168
  return _ref2.apply(this, arguments);
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) => 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) => 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.5",
4
+ "version": "0.1.7",
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
 
@@ -39,7 +39,7 @@ export const SideSheetProvider: React.FC<{
39
39
  }, [stack]);
40
40
 
41
41
  const open = useCallback((element: SideElement, opts: SideOptions = {}) => {
42
- const id = ++idRef.current;
42
+ const id = opts.id ?? ++idRef.current;
43
43
  const options = { ...DEFAULT_SHEET_OPTIONS, ...opts };
44
44
  if (!config.enableOverflow && stackRef.current.length === 0) {
45
45
  overflowRef.current = document.body.style.overflow;
@@ -56,14 +56,14 @@ export const SideSheetProvider: React.FC<{
56
56
  return id;
57
57
  }, []);
58
58
 
59
- const close = useCallback(async (id: number | null) => {
59
+ const close = useCallback(async (id: number | string | null, force = false) => {
60
60
  const itemsToClose =
61
61
  id === null
62
62
  ? [...stackRef.current]
63
63
  : stackRef.current.filter(i => i.id === id);
64
64
 
65
65
  for (const item of itemsToClose) {
66
- if (item.options.confirmBeforeClose) {
66
+ if (! force && item.options.confirmBeforeClose) {
67
67
  const confirmCallback =
68
68
  item.options.confirmCallback ?? config.confirmCallback;
69
69
  const confirmed = await confirmCallback(
@@ -93,7 +93,7 @@ export const SideSheetProvider: React.FC<{
93
93
  }, duration);
94
94
  }, []);
95
95
 
96
- const update = useCallback((id: number, options: Partial<SideOptions>) => {
96
+ const update = useCallback((id: number | string, options: Partial<SideOptions>) => {
97
97
  dispatch({ type: 'UPDATE', id, options });
98
98
  }, []);
99
99
  const config = { ...DEFAULT_OPTIONS, ...configuration } as Required<
@@ -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) => 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
  }