react-side-sheet-pro 0.1.9 → 0.2.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/README.md +5 -2
- package/dist/components/SideSheetContainer.js +28 -16
- package/dist/constants/defaultOptions.js +0 -1
- package/index.d.ts +10 -6
- package/package.json +1 -1
- package/src/components/SideSheetContainer.tsx +36 -15
- package/src/constants/defaultOptions.ts +3 -2
- package/src/types/index.ts +10 -4
package/README.md
CHANGED
|
@@ -42,10 +42,10 @@ yarn add react-side-sheet-pro
|
|
|
42
42
|
## ⚡️ Quick Start
|
|
43
43
|
```typescript jsx
|
|
44
44
|
import React from 'react'
|
|
45
|
-
import { SideSheet,
|
|
45
|
+
import { SideSheet, SideComponent, useSideSheet } from 'react-side-sheet-pro'
|
|
46
46
|
import 'react-side-sheet-pro/dist/index.css'
|
|
47
47
|
|
|
48
|
-
const UserDetails:
|
|
48
|
+
const UserDetails: SideComponent<{ user: any }> = ({
|
|
49
49
|
user,
|
|
50
50
|
sideId,
|
|
51
51
|
close,
|
|
@@ -68,6 +68,8 @@ const UserDetails: React.FC<SideElementProps & { user: any }> = ({
|
|
|
68
68
|
</>
|
|
69
69
|
)
|
|
70
70
|
|
|
71
|
+
UserDetails.defaultWidth = 900
|
|
72
|
+
|
|
71
73
|
export const App = () => {
|
|
72
74
|
const { open } = useSideSheet()
|
|
73
75
|
|
|
@@ -84,6 +86,7 @@ export const App = () => {
|
|
|
84
86
|
/>
|
|
85
87
|
),
|
|
86
88
|
{
|
|
89
|
+
// you can override the default width set on the component
|
|
87
90
|
width: 600,
|
|
88
91
|
}
|
|
89
92
|
)
|
|
@@ -24,24 +24,24 @@ function getStackItemMeta(stack, idx) {
|
|
|
24
24
|
isNextTransitioning: (next === null || next === void 0 ? void 0 : next.state) === 'closing' || (next === null || next === void 0 ? void 0 : next.state) === 'opening'
|
|
25
25
|
};
|
|
26
26
|
}
|
|
27
|
-
function computePadding(stack, idx) {
|
|
28
|
-
var
|
|
27
|
+
function computePadding(resolved, stack, idx) {
|
|
28
|
+
var _resolved;
|
|
29
29
|
var _getStackItemMeta = getStackItemMeta(stack, idx),
|
|
30
30
|
next = _getStackItemMeta.next,
|
|
31
31
|
isTop = _getStackItemMeta.isTop,
|
|
32
32
|
isPrevClosing = _getStackItemMeta.isPrevClosing,
|
|
33
33
|
isNextTransitioning = _getStackItemMeta.isNextTransitioning;
|
|
34
|
-
if (isTop || isPrevClosing || isNextTransitioning || !next) return
|
|
34
|
+
if (isTop || isPrevClosing || isNextTransitioning || !next) return;
|
|
35
35
|
var viewportWidth = document.documentElement.clientWidth;
|
|
36
|
-
var
|
|
36
|
+
var width = resolved[resolved.length - 1].width;
|
|
37
37
|
var padding = 0;
|
|
38
38
|
if (viewportWidth >= 1160) {
|
|
39
39
|
padding = Math.floor((viewportWidth - 960) / 2);
|
|
40
40
|
} else if (viewportWidth >= 768) {
|
|
41
41
|
padding = Math.floor((viewportWidth - 768) / 2);
|
|
42
42
|
}
|
|
43
|
-
padding = Math.min(padding,
|
|
44
|
-
return padding > 0 ? padding :
|
|
43
|
+
padding = Math.min(padding, width);
|
|
44
|
+
return padding > 0 ? padding : ((_resolved = resolved[idx + 1]) === null || _resolved === void 0 ? void 0 : _resolved.width) / 2;
|
|
45
45
|
}
|
|
46
46
|
var SideSheetContainer = exports.SideSheetContainer = function SideSheetContainer(_ref) {
|
|
47
47
|
var stack = _ref.stack,
|
|
@@ -64,6 +64,23 @@ var SideSheetContainer = exports.SideSheetContainer = function SideSheetContaine
|
|
|
64
64
|
};
|
|
65
65
|
}, [stack, close]);
|
|
66
66
|
var paddingKey = config.side === 'left' ? 'paddingLeft' : 'paddingRight';
|
|
67
|
+
var resolved = stack.map(function (item) {
|
|
68
|
+
var _ref2, _item$options$width;
|
|
69
|
+
var elementProps = {
|
|
70
|
+
sideId: item.id,
|
|
71
|
+
options: item.options,
|
|
72
|
+
close: close,
|
|
73
|
+
open: open,
|
|
74
|
+
update: update
|
|
75
|
+
};
|
|
76
|
+
var element = item.element(elementProps);
|
|
77
|
+
var width = (_ref2 = (_item$options$width = item.options.width) !== null && _item$options$width !== void 0 ? _item$options$width : element.type.defaultWidth) !== null && _ref2 !== void 0 ? _ref2 : 400;
|
|
78
|
+
return {
|
|
79
|
+
id: item.id,
|
|
80
|
+
element: element,
|
|
81
|
+
width: width
|
|
82
|
+
};
|
|
83
|
+
});
|
|
67
84
|
return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, stack.map(function (item, idx) {
|
|
68
85
|
var _getStackItemMeta2 = getStackItemMeta(stack, idx),
|
|
69
86
|
next = _getStackItemMeta2.next,
|
|
@@ -71,19 +88,14 @@ var SideSheetContainer = exports.SideSheetContainer = function SideSheetContaine
|
|
|
71
88
|
isPrevClosing = _getStackItemMeta2.isPrevClosing,
|
|
72
89
|
isNextTransitioning = _getStackItemMeta2.isNextTransitioning;
|
|
73
90
|
var _item$options = item.options,
|
|
74
|
-
width = _item$options.width,
|
|
75
91
|
closeOnOverlayClick = _item$options.closeOnOverlayClick,
|
|
76
92
|
className = _item$options.className;
|
|
77
93
|
var isEffectiveTop = isTop || isPrevClosing;
|
|
78
94
|
var isVisible = !(config.mountStrategy === 'top-only' && !isEffectiveTop);
|
|
79
|
-
var paddingValue = isEffectiveTop || isNextTransitioning || !next ? undefined : "".concat(computePadding(stack, idx), "px");
|
|
80
|
-
var
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
close: close,
|
|
84
|
-
open: open,
|
|
85
|
-
update: update
|
|
86
|
-
};
|
|
95
|
+
var paddingValue = isEffectiveTop || isNextTransitioning || !next ? undefined : "".concat(computePadding(resolved, stack, idx), "px");
|
|
96
|
+
var _resolved$idx = resolved[idx],
|
|
97
|
+
element = _resolved$idx.element,
|
|
98
|
+
width = _resolved$idx.width;
|
|
87
99
|
return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, {
|
|
88
100
|
key: item.id
|
|
89
101
|
}, isVisible && /*#__PURE__*/_react["default"].createElement("div", {
|
|
@@ -100,7 +112,7 @@ var SideSheetContainer = exports.SideSheetContainer = function SideSheetContaine
|
|
|
100
112
|
style: _defineProperty({
|
|
101
113
|
width: isEffectiveTop ? "".concat(width, "px") : '100%'
|
|
102
114
|
}, paddingKey, paddingValue)
|
|
103
|
-
},
|
|
115
|
+
}, element));
|
|
104
116
|
}));
|
|
105
117
|
};
|
|
106
118
|
SideSheetContainer.propTypes = {
|
package/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
|
-
import {
|
|
2
|
+
import { ComponentType, JSXElementConstructor, ReactElement } from "react";
|
|
3
3
|
type Sides = "left" | "right";
|
|
4
4
|
interface SideSheetOptions {
|
|
5
5
|
side: Sides;
|
|
@@ -24,19 +24,23 @@ interface SideOptions {
|
|
|
24
24
|
interface SideElementProps {
|
|
25
25
|
sideId: number | string;
|
|
26
26
|
close: (id: number | string | null, force?: boolean) => Promise<void>;
|
|
27
|
-
open: (element:
|
|
27
|
+
open: (element: SideComponent, options?: SideOptions) => number | string;
|
|
28
28
|
update: (id: number | string, options: SideOptions) => void;
|
|
29
29
|
options: SideOptions;
|
|
30
|
+
width?: number;
|
|
30
31
|
}
|
|
31
|
-
type
|
|
32
|
+
type SideComponent<P = {}> =
|
|
33
|
+
ComponentType<P & SideElementProps> & {
|
|
34
|
+
defaultWidth?: number;
|
|
35
|
+
};
|
|
32
36
|
interface SideStackItem {
|
|
33
37
|
id: number | string;
|
|
34
|
-
element:
|
|
38
|
+
element: SideComponent;
|
|
35
39
|
options: Required<SideOptions>;
|
|
36
40
|
state: "opening" | "open" | "closing";
|
|
37
41
|
}
|
|
38
42
|
interface SideSheetContextValue {
|
|
39
|
-
open: (el:
|
|
43
|
+
open: (el: SideComponent, opts?: SideOptions) => number | string;
|
|
40
44
|
close: (id: number | string | null, force?: boolean) => Promise<void>;
|
|
41
45
|
update: (id: number | string, opts: SideOptions) => void;
|
|
42
46
|
config: SideSheetOptions;
|
|
@@ -62,4 +66,4 @@ declare const SideSheet: {
|
|
|
62
66
|
className?: string | undefined;
|
|
63
67
|
}>;
|
|
64
68
|
};
|
|
65
|
-
export { useSideSheet, Sides, SideSheetOptions, SideOptions, SideElementProps,
|
|
69
|
+
export { useSideSheet, Sides, SideSheetOptions, SideOptions, SideElementProps, SideComponent, SideStackItem, SideSheetContextValue, SideSheet };
|
package/package.json
CHANGED
|
@@ -2,6 +2,7 @@ import React, { useEffect } from 'react';
|
|
|
2
2
|
import classNames from 'classnames';
|
|
3
3
|
import {
|
|
4
4
|
SideElement,
|
|
5
|
+
SideElementProps,
|
|
5
6
|
SideOptions,
|
|
6
7
|
SideSheetOptions,
|
|
7
8
|
SideStackItem,
|
|
@@ -15,6 +16,12 @@ type SideSheetContainerProps = {
|
|
|
15
16
|
config: Required<SideSheetOptions>;
|
|
16
17
|
};
|
|
17
18
|
|
|
19
|
+
type ResolvedItem = {
|
|
20
|
+
id: number | string;
|
|
21
|
+
element: React.ReactElement;
|
|
22
|
+
width: number;
|
|
23
|
+
};
|
|
24
|
+
|
|
18
25
|
function getStackItemMeta(stack: SideStackItem[], idx: number) {
|
|
19
26
|
const prev = stack[idx - 1];
|
|
20
27
|
const next = stack[idx + 1];
|
|
@@ -28,6 +35,7 @@ function getStackItemMeta(stack: SideStackItem[], idx: number) {
|
|
|
28
35
|
}
|
|
29
36
|
|
|
30
37
|
function computePadding(
|
|
38
|
+
resolved: ResolvedItem[],
|
|
31
39
|
stack: SideStackItem[],
|
|
32
40
|
idx: number
|
|
33
41
|
): number | undefined {
|
|
@@ -36,10 +44,10 @@ function computePadding(
|
|
|
36
44
|
idx
|
|
37
45
|
);
|
|
38
46
|
|
|
39
|
-
if (isTop || isPrevClosing || isNextTransitioning || !next) return
|
|
47
|
+
if (isTop || isPrevClosing || isNextTransitioning || !next) return;
|
|
40
48
|
|
|
41
49
|
const viewportWidth = document.documentElement.clientWidth;
|
|
42
|
-
const
|
|
50
|
+
const { width } = resolved[resolved.length - 1];
|
|
43
51
|
|
|
44
52
|
let padding = 0;
|
|
45
53
|
if (viewportWidth >= 1160) {
|
|
@@ -48,9 +56,9 @@ function computePadding(
|
|
|
48
56
|
padding = Math.floor((viewportWidth - 768) / 2);
|
|
49
57
|
}
|
|
50
58
|
|
|
51
|
-
padding = Math.min(padding,
|
|
59
|
+
padding = Math.min(padding, width);
|
|
52
60
|
|
|
53
|
-
return padding > 0 ? padding :
|
|
61
|
+
return padding > 0 ? padding : resolved[idx + 1]?.width / 2;
|
|
54
62
|
}
|
|
55
63
|
|
|
56
64
|
export const SideSheetContainer: React.FC<SideSheetContainerProps> = ({
|
|
@@ -78,6 +86,25 @@ export const SideSheetContainer: React.FC<SideSheetContainerProps> = ({
|
|
|
78
86
|
|
|
79
87
|
const paddingKey = config.side === 'left' ? 'paddingLeft' : 'paddingRight';
|
|
80
88
|
|
|
89
|
+
const resolved: ResolvedItem[] = stack.map(item => {
|
|
90
|
+
const elementProps = {
|
|
91
|
+
sideId: item.id,
|
|
92
|
+
options: item.options,
|
|
93
|
+
close,
|
|
94
|
+
open,
|
|
95
|
+
update,
|
|
96
|
+
} as SideElementProps;
|
|
97
|
+
|
|
98
|
+
const element = item.element(elementProps);
|
|
99
|
+
const width = item.options.width ?? element.type.defaultWidth ?? 400;
|
|
100
|
+
|
|
101
|
+
return {
|
|
102
|
+
id: item.id,
|
|
103
|
+
element,
|
|
104
|
+
width,
|
|
105
|
+
};
|
|
106
|
+
});
|
|
107
|
+
|
|
81
108
|
return (
|
|
82
109
|
<>
|
|
83
110
|
{stack.map((item, idx) => {
|
|
@@ -87,7 +114,8 @@ export const SideSheetContainer: React.FC<SideSheetContainerProps> = ({
|
|
|
87
114
|
isPrevClosing,
|
|
88
115
|
isNextTransitioning,
|
|
89
116
|
} = getStackItemMeta(stack, idx);
|
|
90
|
-
|
|
117
|
+
|
|
118
|
+
const { closeOnOverlayClick, className } = item.options;
|
|
91
119
|
|
|
92
120
|
const isEffectiveTop = isTop || isPrevClosing;
|
|
93
121
|
const isVisible = !(
|
|
@@ -96,15 +124,8 @@ export const SideSheetContainer: React.FC<SideSheetContainerProps> = ({
|
|
|
96
124
|
const paddingValue =
|
|
97
125
|
isEffectiveTop || isNextTransitioning || !next
|
|
98
126
|
? undefined
|
|
99
|
-
: `${computePadding(stack, idx)}px`;
|
|
100
|
-
|
|
101
|
-
const elementProps = {
|
|
102
|
-
sideId: item.id,
|
|
103
|
-
options: item.options,
|
|
104
|
-
close,
|
|
105
|
-
open,
|
|
106
|
-
update,
|
|
107
|
-
};
|
|
127
|
+
: `${computePadding(resolved, stack, idx)}px`;
|
|
128
|
+
const { element, width } = resolved[idx];
|
|
108
129
|
|
|
109
130
|
return (
|
|
110
131
|
<React.Fragment key={item.id}>
|
|
@@ -133,7 +154,7 @@ export const SideSheetContainer: React.FC<SideSheetContainerProps> = ({
|
|
|
133
154
|
[paddingKey]: paddingValue,
|
|
134
155
|
}}
|
|
135
156
|
>
|
|
136
|
-
{
|
|
157
|
+
{element}
|
|
137
158
|
</div>
|
|
138
159
|
</React.Fragment>
|
|
139
160
|
);
|
|
@@ -11,9 +11,10 @@ export const DEFAULT_OPTIONS: Required<SideSheetOptions> = {
|
|
|
11
11
|
: Promise.resolve(true),
|
|
12
12
|
};
|
|
13
13
|
|
|
14
|
-
export const DEFAULT_SHEET_OPTIONS: Required<SideOptions
|
|
14
|
+
export const DEFAULT_SHEET_OPTIONS: Required<Omit<SideOptions, 'width'>> & {
|
|
15
|
+
width?: number;
|
|
16
|
+
} = {
|
|
15
17
|
id: '',
|
|
16
|
-
width: 400,
|
|
17
18
|
className: '',
|
|
18
19
|
confirmBeforeClose: false,
|
|
19
20
|
confirmMessage: null,
|
package/src/types/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ComponentType, ReactElement } from 'react';
|
|
2
2
|
|
|
3
3
|
export type Sides = 'left' | 'right';
|
|
4
4
|
|
|
@@ -27,17 +27,23 @@ export interface SideOptions {
|
|
|
27
27
|
export interface SideElementProps {
|
|
28
28
|
sideId: number | string;
|
|
29
29
|
close: (id: number | string | null) => Promise<void>;
|
|
30
|
-
open: (element:
|
|
30
|
+
open: (element: SideComponent, options?: SideOptions) => number | string;
|
|
31
31
|
update: (id: number | string, options: SideOptions) => void;
|
|
32
32
|
options: SideOptions;
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
-
export type
|
|
35
|
+
export type SideComponent<P = {}> = ComponentType<P & SideElementProps> & {
|
|
36
|
+
defaultWidth?: number;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export type SideElement = (
|
|
40
|
+
props: SideElementProps
|
|
41
|
+
) => ReactElement<any, SideComponent>;
|
|
36
42
|
|
|
37
43
|
export interface SideStackItem {
|
|
38
44
|
id: number | string;
|
|
39
45
|
element: SideElement;
|
|
40
|
-
options: Required<SideOptions
|
|
46
|
+
options: Required<Omit<SideOptions, 'width'>> & { width?: number };
|
|
41
47
|
state: 'opening' | 'open' | 'closing';
|
|
42
48
|
}
|
|
43
49
|
|