@semcore/side-panel 1.1.0 → 1.3.1

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/src/index.d.ts ADDED
@@ -0,0 +1,60 @@
1
+ import React from 'react';
2
+ import { IPortalProps } from '@semcore/portal';
3
+ import { IFadeInOutProps, ITransformProps } from '@semcore/animation';
4
+ import { CProps, PropGetterFn, ReturnEl } from '@semcore/core';
5
+ import { Box, IBoxProps } from '@semcore/flex-box';
6
+
7
+ export type OnCloseTriggerType = 'onOutsideClick' | 'onEscape' | 'onCloseClick';
8
+ export type OnCloseType = (
9
+ trigger: OnCloseTriggerType,
10
+ e?: React.MouseEvent | React.KeyboardEvent,
11
+ ) => void;
12
+
13
+ export type SidePanelPlacement = 'top' | 'left' | 'right' | 'bottom';
14
+
15
+ export interface ISidePanelProps extends IPortalProps {
16
+ /** Animation display duration in ms
17
+ * @default 350
18
+ */
19
+ duration?: number;
20
+ /**
21
+ * The property responsible for the visibility of the component
22
+ */
23
+ visible?: boolean;
24
+ /**
25
+ * Callback for the component closure
26
+ */
27
+ onClose?: OnCloseType;
28
+ /**
29
+ * Property for displaying the close button
30
+ * @default true
31
+ */
32
+ closable?: boolean;
33
+ /**
34
+ * Position of a dropdown window
35
+ * @default right
36
+ */
37
+ placement?: SidePanelPlacement;
38
+ }
39
+
40
+ export interface ISidePanelOverlayProps extends IFadeInOutProps, IBoxProps {}
41
+
42
+ export interface ISidePanelPanelProps extends ITransformProps, IBoxProps {
43
+ onOutsideClick?: (e?: React.SyntheticEvent) => void;
44
+ }
45
+
46
+ export interface ISidePanelContext {
47
+ getOverlayProps?: PropGetterFn;
48
+ getPanelProps?: PropGetterFn;
49
+ getCloseProps?: PropGetterFn;
50
+ }
51
+
52
+ declare const SidePanel: (<T>(
53
+ props: CProps<ISidePanelProps & T, ISidePanelContext>,
54
+ ) => ReturnEl) & {
55
+ Overlay: <T>(props: ISidePanelOverlayProps & T) => ReturnEl;
56
+ Panel: <T>(props: ISidePanelPanelProps & T) => ReturnEl;
57
+ Close: typeof Box;
58
+ };
59
+
60
+ export default SidePanel;
File without changes
@@ -89,7 +89,7 @@ SClose {
89
89
  position: absolute;
90
90
  right: 0;
91
91
  top: 0;
92
- padding: media(12px);
92
+ padding: 12px;
93
93
  color: var(--stone);
94
94
  cursor: pointer;
95
95
  z-index: 1;
@@ -1,110 +0,0 @@
1
- import React, { ComponentProps } from 'react';
2
- import { Component, Merge, PropGetter } from '@semcore/core';
3
- import { Box } from '@semcore/flex-box';
4
- import { IFadeInOutProps, ITransformProps } from '@semcore/animation';
5
- import { IPortalProps } from '@semcore/portal';
6
- export declare type OnCloseTriggerType = 'onOutsideClick' | 'onEscape' | 'onCloseClick';
7
- export declare type OnCloseType = (trigger: OnCloseTriggerType, e?: React.MouseEvent | React.KeyboardEvent) => void;
8
- export declare type SidePanelPlacement = 'top' | 'left' | 'right' | 'bottom';
9
- export interface ISidePanelProps extends IPortalProps {
10
- /** Animation display duration in ms
11
- * @default 350
12
- */
13
- duration?: number;
14
- /**
15
- * The property responsible for the visibility of the component
16
- */
17
- visible?: boolean;
18
- /**
19
- * Callback for the component closure
20
- */
21
- onClose?: OnCloseType;
22
- /**
23
- * Property for displaying the close button
24
- * @default true
25
- */
26
- closable?: boolean;
27
- /**
28
- * Position of a dropdown window
29
- * @default right
30
- */
31
- placement?: SidePanelPlacement;
32
- }
33
- export interface ISidePanelOverlayProps extends IFadeInOutProps {
34
- /**
35
- * @link ISidePanelProps.visible
36
- */
37
- visible?: ISidePanelProps['visible'];
38
- }
39
- export interface ISidePanelPanelProps extends ITransformProps {
40
- /**
41
- * @link ISidePanelProps.visible
42
- */
43
- visible?: ISidePanelProps['visible'];
44
- /**
45
- * @link ISidePanelProps.placement
46
- */
47
- placement?: ISidePanelProps['placement'];
48
- /**
49
- * @link ISidePanelProps.closable
50
- */
51
- closable?: ISidePanelProps['closable'];
52
- onOutsideClick?: (e?: React.SyntheticEvent) => void;
53
- }
54
- export interface ISidePanelContext extends ISidePanelProps {
55
- /**
56
- * The method that encapsulates the props needed to run SidePanel.Overlay
57
- */
58
- getOverlayProps?: PropGetter<RootSidePanel['getOverlayProps']>;
59
- /**
60
- * The method that encapsulates the props needed to run SidePanel.Panel
61
- */
62
- getPanelProps?: PropGetter<RootSidePanel['getPanelProps']>;
63
- /**
64
- * The method that encapsulates the props needed to run SidePanel.Close
65
- */
66
- getCloseProps?: PropGetter<RootSidePanel['getCloseProps']>;
67
- }
68
- declare class RootSidePanel extends Component<ISidePanelProps> {
69
- static displayName: string;
70
- static style: any;
71
- static defaultProps: {
72
- placement: string;
73
- duration: number;
74
- closable: boolean;
75
- };
76
- sidebarRef: React.RefObject<HTMLElement>;
77
- handleSidebarKeyDown: (e: any) => void;
78
- handleIconCloseClick: (e: any) => void;
79
- handleOutsideClick: (e: any) => void;
80
- isAdvanceMode(): boolean;
81
- isUsedOverlay(): boolean;
82
- calculateDelayAnimation(name: any): any;
83
- getOverlayProps(): {
84
- visible: boolean;
85
- duration: number;
86
- delay: any;
87
- };
88
- getPanelProps(): {
89
- visible: boolean;
90
- placement: SidePanelPlacement;
91
- closable: boolean;
92
- duration: number;
93
- disableEnforceFocus: boolean;
94
- delay: any;
95
- onOutsideClick: (e: any) => void;
96
- onKeyDown: (e: any) => void;
97
- };
98
- getCloseProps(): {
99
- onClick: (e: any) => void;
100
- };
101
- render(): JSX.Element;
102
- }
103
- declare const SidePanel: import("@semcore/core").ComponentType<ISidePanelProps, {
104
- Overlay: Merge<ISidePanelOverlayProps, ComponentProps<typeof Box>>;
105
- Panel: Merge<ISidePanelPanelProps, ComponentProps<typeof Box>>;
106
- Close: ComponentProps<typeof Box>;
107
- }, ISidePanelContext, {
108
- [key: string]: (arg: unknown) => void;
109
- }, null>;
110
- export default SidePanel;
package/src/SidePanel.tsx DELETED
@@ -1,271 +0,0 @@
1
- import React, { ComponentProps, useRef } from 'react';
2
- import FocusLock from 'react-focus-lock';
3
- import createComponent, {
4
- Component,
5
- IFunctionProps,
6
- Merge,
7
- PropGetter,
8
- styled,
9
- } from '@semcore/core';
10
- import { Box } from '@semcore/flex-box';
11
- import { FadeInOut, IFadeInOutProps, ITransformProps, Transform } from '@semcore/animation';
12
- import Portal, { IPortalProps, PortalProvider } from '@semcore/portal';
13
- import OutsideClick from '@semcore/outside-click';
14
- import CloseS from '@semcore/icon/lib/Close/s';
15
- import fire from '@semcore/utils/lib/fire';
16
- import findComponent from '@semcore/utils/lib/findComponent';
17
- import keyboardFocusEnhance from '@semcore/utils/lib/enhances/keyboardFocusEnhance';
18
- import usePreventScroll from '@semcore/utils/lib/use/usePreventScroll';
19
-
20
- import style from './style/side-panel.shadow.css';
21
-
22
- export type OnCloseTriggerType = 'onOutsideClick' | 'onEscape' | 'onCloseClick';
23
- export type OnCloseType = (
24
- trigger: OnCloseTriggerType,
25
- e?: React.MouseEvent | React.KeyboardEvent,
26
- ) => void;
27
-
28
- export type SidePanelPlacement = 'top' | 'left' | 'right' | 'bottom';
29
-
30
- export interface ISidePanelProps extends IPortalProps {
31
- /** Animation display duration in ms
32
- * @default 350
33
- */
34
- duration?: number;
35
- /**
36
- * The property responsible for the visibility of the component
37
- */
38
- visible?: boolean;
39
- /**
40
- * Callback for the component closure
41
- */
42
- onClose?: OnCloseType;
43
- /**
44
- * Property for displaying the close button
45
- * @default true
46
- */
47
- closable?: boolean;
48
- /**
49
- * Position of a dropdown window
50
- * @default right
51
- */
52
- placement?: SidePanelPlacement;
53
- }
54
-
55
- export interface ISidePanelOverlayProps extends IFadeInOutProps {
56
- /**
57
- * @link ISidePanelProps.visible
58
- */
59
- visible?: ISidePanelProps['visible'];
60
- }
61
-
62
- export interface ISidePanelPanelProps extends ITransformProps {
63
- /**
64
- * @link ISidePanelProps.visible
65
- */
66
- visible?: ISidePanelProps['visible'];
67
- /**
68
- * @link ISidePanelProps.placement
69
- */
70
- placement?: ISidePanelProps['placement'];
71
- /**
72
- * @link ISidePanelProps.closable
73
- */
74
- closable?: ISidePanelProps['closable'];
75
-
76
- onOutsideClick?: (e?: React.SyntheticEvent) => void;
77
- }
78
-
79
- export interface ISidePanelContext extends ISidePanelProps {
80
- /**
81
- * The method that encapsulates the props needed to run SidePanel.Overlay
82
- */
83
- getOverlayProps?: PropGetter<RootSidePanel['getOverlayProps']>;
84
- /**
85
- * The method that encapsulates the props needed to run SidePanel.Panel
86
- */
87
- getPanelProps?: PropGetter<RootSidePanel['getPanelProps']>;
88
- /**
89
- * The method that encapsulates the props needed to run SidePanel.Close
90
- */
91
- getCloseProps?: PropGetter<RootSidePanel['getCloseProps']>;
92
- }
93
-
94
- const placementTransformMap = {
95
- top: ['translate(0, -100%)', 'translate(0, 0)'],
96
- right: ['translate(100%, 0)', 'translate(0, 0)'],
97
- bottom: ['translate(0, 100%)', 'translate(0, 0)'],
98
- left: ['translate(-100%, 0)', 'translate(0, 0)'],
99
- };
100
-
101
- class RootSidePanel extends Component<ISidePanelProps> {
102
- static displayName = 'SidePanel';
103
- static style = style;
104
- static defaultProps = {
105
- placement: 'right',
106
- duration: 350,
107
- closable: true,
108
- };
109
-
110
- sidebarRef = React.createRef<HTMLElement>();
111
-
112
- handleSidebarKeyDown = (e) => {
113
- if (e.key === 'Escape') {
114
- e.stopPropagation();
115
- fire(this, 'onClose', 'onEscape', e);
116
- }
117
- };
118
-
119
- handleIconCloseClick = (e) => {
120
- fire(this, 'onClose', 'onCloseClick', e);
121
- };
122
-
123
- handleOutsideClick = (e) => {
124
- fire(this, 'onClose', 'onOutsideClick', e);
125
- };
126
-
127
- isAdvanceMode() {
128
- return !!findComponent(this.asProps.Children, [
129
- SidePanel.Overlay.displayName,
130
- SidePanel.Panel.displayName,
131
- ]);
132
- }
133
-
134
- isUsedOverlay() {
135
- return (
136
- !this.isAdvanceMode() ||
137
- !!findComponent(this.asProps.Children, [SidePanel.Overlay.displayName])
138
- );
139
- }
140
-
141
- calculateDelayAnimation(name) {
142
- const delay = this.asProps.duration / 2;
143
- const usedOverlay = this.isUsedOverlay();
144
-
145
- return {
146
- overlay: usedOverlay ? [0, delay] : 0,
147
- panel: usedOverlay ? [delay, 0] : 0,
148
- }[name];
149
- }
150
-
151
- getOverlayProps() {
152
- const { visible, duration } = this.asProps;
153
- return {
154
- visible,
155
- duration,
156
- delay: this.calculateDelayAnimation('overlay'),
157
- };
158
- }
159
-
160
- getPanelProps() {
161
- const { placement, visible, closable, duration } = this.asProps;
162
-
163
- return {
164
- visible,
165
- placement,
166
- closable,
167
- duration,
168
- disableEnforceFocus: !this.isUsedOverlay(),
169
- delay: this.calculateDelayAnimation('panel'),
170
- onOutsideClick: this.handleOutsideClick,
171
- onKeyDown: this.handleSidebarKeyDown,
172
- };
173
- }
174
-
175
- getCloseProps() {
176
- return {
177
- onClick: this.handleIconCloseClick,
178
- };
179
- }
180
-
181
- render() {
182
- const Root = this.Root;
183
- const { Children, disablePortal } = this.asProps;
184
-
185
- return (
186
- <Portal disablePortal={disablePortal}>
187
- {this.isAdvanceMode() ? (
188
- <Children />
189
- ) : (
190
- <SidePanel.Overlay>
191
- <Root render={SidePanel.Panel} />
192
- </SidePanel.Overlay>
193
- )}
194
- </Portal>
195
- );
196
- }
197
- }
198
-
199
- function Overlay(props: IFunctionProps<ISidePanelOverlayProps>) {
200
- const { Root: SOverlay, styles, visible } = props;
201
- usePreventScroll(visible);
202
-
203
- return styled(styles)(<SOverlay render={FadeInOut} visible={visible} />);
204
- }
205
-
206
- const FocusLockWrapper = React.forwardRef<HTMLElement, any>(function (
207
- { disableEnforceFocus, ...other },
208
- ref,
209
- ) {
210
- return <FocusLock ref={ref} lockProps={other} disabled={disableEnforceFocus} {...other} />;
211
- });
212
-
213
- const TransformWrapper = React.forwardRef<HTMLElement, any>(function ({ tag, ...other }, ref) {
214
- return <Transform tag={FocusLockWrapper} ref={ref} as={tag} {...other} />;
215
- });
216
-
217
- function Panel(props: IFunctionProps<ISidePanelPanelProps>) {
218
- const { Root: SPanel, Children, styles, visible, closable, placement, onOutsideClick } = props;
219
-
220
- const sidebarRef = useRef<HTMLElement>(null);
221
-
222
- return styled(styles)(
223
- <>
224
- {visible && <OutsideClick onOutsideClick={onOutsideClick} excludeRefs={[sidebarRef]} />}
225
- <SPanel
226
- render={TransformWrapper}
227
- tag={Box}
228
- ref={sidebarRef}
229
- placement={placement}
230
- visible={visible}
231
- tabIndex={-1}
232
- returnFocus={true}
233
- autoFocus={true}
234
- transform={placementTransformMap[placement]}
235
- >
236
- <PortalProvider value={sidebarRef}>
237
- {closable && <SidePanel.Close />}
238
- <Children />
239
- </PortalProvider>
240
- </SPanel>
241
- </>,
242
- );
243
- }
244
-
245
- function Close(props) {
246
- const { Root: SClose, styles, keyboardFocused } = props;
247
- return styled(styles)(
248
- <SClose render={Box} tag="button" tabIndex={0} keyboardFocused={keyboardFocused} />,
249
- );
250
- }
251
-
252
- Close.defaultProps = {
253
- children: <CloseS title="Close" />,
254
- };
255
- Close.enhance = [keyboardFocusEnhance()];
256
-
257
- const SidePanel = createComponent<
258
- ISidePanelProps,
259
- {
260
- Overlay: Merge<ISidePanelOverlayProps, ComponentProps<typeof Box>>;
261
- Panel: Merge<ISidePanelPanelProps, ComponentProps<typeof Box>>;
262
- Close: ComponentProps<typeof Box>;
263
- },
264
- ISidePanelContext
265
- >(RootSidePanel, {
266
- Overlay,
267
- Panel,
268
- Close,
269
- });
270
-
271
- export default SidePanel;