@alfalab/core-components-popup-sheet 1.0.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.
Files changed (67) hide show
  1. package/Component-9211a437.d.ts +38 -0
  2. package/Component.d.ts +47 -0
  3. package/Component.desktop-2e2b2125.d.ts +6 -0
  4. package/Component.js +139 -0
  5. package/components/backdrop/Component.d.ts +14 -0
  6. package/components/backdrop/Component.js +22 -0
  7. package/components/backdrop/index.css +36 -0
  8. package/components/backdrop/index.d.ts +1 -0
  9. package/components/backdrop/index.js +12 -0
  10. package/cssm/Component-9211a437.d.ts +38 -0
  11. package/cssm/Component.d.ts +47 -0
  12. package/cssm/Component.desktop-2e2b2125.d.ts +6 -0
  13. package/cssm/Component.js +139 -0
  14. package/cssm/components/backdrop/Component.d.ts +14 -0
  15. package/cssm/components/backdrop/Component.js +21 -0
  16. package/cssm/components/backdrop/index.d.ts +1 -0
  17. package/cssm/components/backdrop/index.js +13 -0
  18. package/cssm/components/backdrop/index.module.css +35 -0
  19. package/cssm/desktop-2e2b2125.d.ts +2 -0
  20. package/cssm/index-ebda875c.d.ts +1 -0
  21. package/cssm/index.d.ts +1 -0
  22. package/cssm/index.js +20 -0
  23. package/cssm/index.module.css +52 -0
  24. package/cssm/typings-9211a437.d.ts +95 -0
  25. package/desktop-2e2b2125.d.ts +2 -0
  26. package/esm/Component-9211a437.d.ts +38 -0
  27. package/esm/Component.d.ts +47 -0
  28. package/esm/Component.desktop-2e2b2125.d.ts +6 -0
  29. package/esm/Component.js +130 -0
  30. package/esm/components/backdrop/Component.d.ts +14 -0
  31. package/esm/components/backdrop/Component.js +14 -0
  32. package/esm/components/backdrop/index.css +36 -0
  33. package/esm/components/backdrop/index.d.ts +1 -0
  34. package/esm/components/backdrop/index.js +4 -0
  35. package/esm/desktop-2e2b2125.d.ts +2 -0
  36. package/esm/index-ebda875c.d.ts +1 -0
  37. package/esm/index.css +53 -0
  38. package/esm/index.d.ts +1 -0
  39. package/esm/index.js +10 -0
  40. package/esm/typings-9211a437.d.ts +95 -0
  41. package/index-ebda875c.d.ts +1 -0
  42. package/index.css +53 -0
  43. package/index.d.ts +1 -0
  44. package/index.js +18 -0
  45. package/modern/Component-9211a437.d.ts +38 -0
  46. package/modern/Component.d.ts +47 -0
  47. package/modern/Component.desktop-2e2b2125.d.ts +6 -0
  48. package/modern/Component.js +136 -0
  49. package/modern/components/backdrop/Component.d.ts +14 -0
  50. package/modern/components/backdrop/Component.js +15 -0
  51. package/modern/components/backdrop/index.css +36 -0
  52. package/modern/components/backdrop/index.d.ts +1 -0
  53. package/modern/components/backdrop/index.js +3 -0
  54. package/modern/desktop-2e2b2125.d.ts +2 -0
  55. package/modern/index-ebda875c.d.ts +1 -0
  56. package/modern/index.css +53 -0
  57. package/modern/index.d.ts +1 -0
  58. package/modern/index.js +9 -0
  59. package/modern/typings-9211a437.d.ts +95 -0
  60. package/package.json +26 -0
  61. package/src/Component.tsx +229 -0
  62. package/src/components/backdrop/Component.tsx +35 -0
  63. package/src/components/backdrop/index.module.css +25 -0
  64. package/src/components/backdrop/index.tsx +1 -0
  65. package/src/index.module.css +56 -0
  66. package/src/index.ts +1 -0
  67. package/typings-9211a437.d.ts +95 -0
@@ -0,0 +1,229 @@
1
+ import React, {
2
+ CSSProperties,
3
+ forwardRef,
4
+ KeyboardEvent,
5
+ MouseEvent,
6
+ useRef,
7
+ useState,
8
+ } from 'react';
9
+ import { SwipeCallback, useSwipeable } from 'react-swipeable';
10
+ import cn from 'classnames';
11
+
12
+ import { BaseModal, BaseModalProps } from '@alfalab/core-components-base-modal';
13
+ import { Closer } from '@alfalab/core-components-navigation-bar/shared';
14
+ import { createPaddingStyle, easingFns, getDataTestId } from '@alfalab/core-components-shared';
15
+
16
+ import { PaddingType } from '../../types';
17
+
18
+ import { PopupBackdrop } from './components/backdrop';
19
+
20
+ import styles from './index.module.css';
21
+
22
+ const SWIPE_VELOCITY = 0.3;
23
+ const CLOSE_OFFSET = 0.3;
24
+ const ANIMATION_DURATION = 350;
25
+
26
+ export type PopupSheetProps = Omit<BaseModalProps, 'onClose'> & {
27
+ /**
28
+ * Наличие кнопки закрытия
29
+ */
30
+ hasCloser?: boolean;
31
+
32
+ /**
33
+ * Будет ли свайпаться шторка
34
+ */
35
+ swipeable?: boolean;
36
+
37
+ /**
38
+ * Отступы
39
+ */
40
+ padding?: PaddingType;
41
+
42
+ /**
43
+ * Обработчик закрытия
44
+ */
45
+ onClose?: (
46
+ event: MouseEvent<HTMLElement> | KeyboardEvent<HTMLElement>,
47
+ reason?: 'backdropClick' | 'escapeKeyDown' | 'closerClick' | 'swipe',
48
+ ) => void;
49
+ };
50
+
51
+ const DEFAULT_PADDING = 32;
52
+
53
+ export const PopupSheet = forwardRef<HTMLDivElement, PopupSheetProps>(
54
+ (
55
+ {
56
+ children,
57
+ hasCloser,
58
+ className,
59
+ onClose,
60
+ transitionProps,
61
+ contentProps,
62
+ swipeable,
63
+ dataTestId,
64
+ backdropProps,
65
+ padding = DEFAULT_PADDING,
66
+ ...restProps
67
+ },
68
+ ref,
69
+ ) => {
70
+ const [sheetOffset, setSheetOffset] = useState(0);
71
+ const [backdropOpacity, setBackdropOpacity] = useState(1);
72
+
73
+ const sheetHeight = useRef(0);
74
+ const sheetRef = useRef<HTMLDivElement>(null);
75
+ const bySwipeCloseAnimation = useRef(false);
76
+
77
+ const getSheetOffset = (deltaY: number): number => Math.max(0, deltaY);
78
+
79
+ const getBackdropOpacity = (offset: number): number =>
80
+ Math.max(0, 1 - offset / sheetHeight.current);
81
+
82
+ const getSwipeStyles = (): CSSProperties =>
83
+ sheetOffset ? { transform: `translateY(${sheetOffset}px)` } : {};
84
+
85
+ const handleSwiping: SwipeCallback = ({ deltaY }) => {
86
+ const offset = getSheetOffset(deltaY);
87
+
88
+ setSheetOffset(offset);
89
+ setBackdropOpacity(getBackdropOpacity(offset));
90
+ };
91
+
92
+ const handleSwiped: SwipeCallback = ({ deltaY, velocity, event }) => {
93
+ const shouldCloseByVelocity = deltaY >= 0 && velocity >= SWIPE_VELOCITY;
94
+ const shouldCloseByOffset = deltaY >= sheetHeight.current * CLOSE_OFFSET;
95
+
96
+ const offset = sheetOffset;
97
+ const animDuration = ANIMATION_DURATION / 2;
98
+ let start: number;
99
+
100
+ if (shouldCloseByVelocity || shouldCloseByOffset) {
101
+ bySwipeCloseAnimation.current = true;
102
+ onClose?.(event as MouseEvent<HTMLElement>, 'swipe');
103
+
104
+ const runLoop = (timeStamp: number) => {
105
+ if (!start) start = timeStamp;
106
+ const elapsedTime = timeStamp - start;
107
+
108
+ if (elapsedTime <= animDuration) {
109
+ const nextOffset =
110
+ offset +
111
+ (sheetHeight.current - offset) *
112
+ easingFns.easeInOutQuad(elapsedTime / animDuration);
113
+
114
+ if (nextOffset > 0) {
115
+ setSheetOffset(nextOffset);
116
+ }
117
+ requestAnimationFrame(runLoop);
118
+ } else {
119
+ setSheetOffset(0);
120
+ bySwipeCloseAnimation.current = false;
121
+ }
122
+ };
123
+
124
+ requestAnimationFrame(runLoop);
125
+ } else {
126
+ const runLoop = (timeStamp: number) => {
127
+ if (!start) start = timeStamp;
128
+ const elapsedTime = timeStamp - start;
129
+
130
+ if (elapsedTime <= animDuration) {
131
+ const nextOffset =
132
+ offset - offset * easingFns.easeInOutQuad(elapsedTime / animDuration);
133
+
134
+ if (nextOffset > 0) {
135
+ setSheetOffset(nextOffset);
136
+ setBackdropOpacity(getBackdropOpacity(nextOffset));
137
+ }
138
+ requestAnimationFrame(runLoop);
139
+ } else {
140
+ setSheetOffset(0);
141
+ setBackdropOpacity(1);
142
+ }
143
+ };
144
+
145
+ requestAnimationFrame(runLoop);
146
+ }
147
+ };
148
+
149
+ const handleEntered = (node: HTMLElement, isAppearing: boolean) => {
150
+ bySwipeCloseAnimation.current = false;
151
+ if (sheetRef.current) {
152
+ sheetHeight.current = sheetRef.current.offsetHeight;
153
+ }
154
+
155
+ if (transitionProps?.onEntered) {
156
+ transitionProps.onEntered(node, isAppearing);
157
+ }
158
+ };
159
+
160
+ const handleExited = (node: HTMLElement) => {
161
+ bySwipeCloseAnimation.current = false;
162
+ setSheetOffset(0);
163
+ setBackdropOpacity(1);
164
+
165
+ if (transitionProps?.onExited) {
166
+ transitionProps.onExited(node);
167
+ }
168
+ };
169
+
170
+ const sheetSwipeableHandlers = useSwipeable({
171
+ onSwiping: handleSwiping,
172
+ onSwiped: handleSwiped,
173
+ trackMouse: swipeable,
174
+ trackTouch: swipeable,
175
+ delta: 5,
176
+ });
177
+
178
+ return (
179
+ <BaseModal
180
+ {...restProps}
181
+ onClose={onClose}
182
+ ref={ref}
183
+ className={cn(styles.component, className)}
184
+ dataTestId={dataTestId}
185
+ Backdrop={PopupBackdrop}
186
+ backdropProps={{
187
+ ...backdropProps,
188
+ opacity: backdropOpacity,
189
+ }}
190
+ transitionProps={{
191
+ classNames: {
192
+ enter: styles.enter,
193
+ appear: styles.appear,
194
+ enterActive: styles.enterActive,
195
+ appearActive: styles.appearActive,
196
+ exit: bySwipeCloseAnimation.current ? styles.exitBySwipe : styles.exit,
197
+ exitActive: bySwipeCloseAnimation.current
198
+ ? styles.exitActiveBySwipe
199
+ : styles.exitActive,
200
+ },
201
+ timeout: ANIMATION_DURATION,
202
+ ...transitionProps,
203
+ onEntered: handleEntered,
204
+ onExited: handleExited,
205
+ }}
206
+ componentDivProps={{
207
+ ref: sheetRef,
208
+ style: getSwipeStyles(),
209
+ }}
210
+ contentProps={{
211
+ style: createPaddingStyle(padding),
212
+ ...contentProps,
213
+ ...sheetSwipeableHandlers,
214
+ className: cn(styles.content, contentProps?.className),
215
+ }}
216
+ >
217
+ {hasCloser && (
218
+ <Closer
219
+ view='mobile'
220
+ className={styles.closer}
221
+ onClick={onClose}
222
+ dataTestId={getDataTestId(dataTestId, 'closer')}
223
+ />
224
+ )}
225
+ {children}
226
+ </BaseModal>
227
+ );
228
+ },
229
+ );
@@ -0,0 +1,35 @@
1
+ import React, { FC } from 'react';
2
+
3
+ import { Backdrop, BackdropProps } from '@alfalab/core-components-backdrop';
4
+
5
+ import styles from './index.module.css';
6
+
7
+ export type PopupBackdropProps = BackdropProps & {
8
+ /**
9
+ * Прозрачность бэкдропа
10
+ */
11
+ opacity?: number;
12
+
13
+ /**
14
+ * Время анимации opacity
15
+ */
16
+ opacityTimeout?: number;
17
+ };
18
+
19
+ export const PopupBackdrop: FC<BackdropProps> = ({
20
+ opacity,
21
+ opacityTimeout,
22
+ style,
23
+ ...backdropProps
24
+ }) => (
25
+ <div
26
+ style={{
27
+ opacity,
28
+ transition: opacity === 1 ? `opacity ${opacityTimeout}ms ease-in-out` : '',
29
+ position: 'relative',
30
+ ...style,
31
+ }}
32
+ >
33
+ <Backdrop transitionClassNames={styles} {...backdropProps} />
34
+ </div>
35
+ );
@@ -0,0 +1,25 @@
1
+ @import '@alfalab/core-components-themes/src/default.css';
2
+ @import '@alfalab/core-components-backdrop/src/vars.css';
3
+
4
+ .appear,
5
+ .enter {
6
+ background-color: var(--backdrop-hidden-background);
7
+ }
8
+
9
+ .appearActive,
10
+ .enterActive,
11
+ .appearDone,
12
+ .enterDone {
13
+ background-color: var(--backdrop-visible-background);
14
+ transition: background-color 300ms cubic-bezier(0.65, 0, 0.35, 1);
15
+ }
16
+
17
+ .exit {
18
+ background-color: var(--backdrop-visible-background);
19
+ }
20
+
21
+ .exitActive,
22
+ .exitDone {
23
+ background-color: var(--backdrop-hidden-background);
24
+ transition: background-color 300ms cubic-bezier(0.65, 0, 0.35, 1);
25
+ }
@@ -0,0 +1 @@
1
+ export { PopupBackdrop } from './Component';
@@ -0,0 +1,56 @@
1
+ @import '@alfalab/core-components-themes/src/default.css';
2
+
3
+ .component {
4
+ position: fixed;
5
+ bottom: 0;
6
+ margin: auto var(--gap-xs) var(--gap-xs);
7
+ width: calc(100% - 2 * var(--gap-xs));
8
+ max-width: 600px;
9
+ border-radius: 36px;
10
+ overflow: hidden;
11
+ }
12
+
13
+ .closer {
14
+ position: absolute;
15
+ top: 8px;
16
+ right: 8px;
17
+
18
+ & > button {
19
+ backdrop-filter: none;
20
+ }
21
+ }
22
+
23
+ .content {
24
+ box-sizing: border-box;
25
+ }
26
+
27
+ /* 100px нужны для того, чтобы фон успевал немного затемниться перед тем, как начнет выезжать шторка */
28
+ .appear,
29
+ .enter {
30
+ transform: translateY(calc(100% + 100px));
31
+ }
32
+
33
+ .appearActive,
34
+ .enterActive {
35
+ transform: translateY(0);
36
+ transition: transform 300ms cubic-bezier(0.65, 0, 0.35, 1);
37
+ transition-delay: 50ms;
38
+ }
39
+
40
+ .exit {
41
+ transform: translateY(0);
42
+ }
43
+
44
+ .exitBySwipe {
45
+ transform: translateY(calc(100% + 100px));
46
+ }
47
+
48
+ .exitActiveBySwipe {
49
+ transition: none;
50
+ }
51
+
52
+ .exitActive,
53
+ .exitDone {
54
+ transform: translateY(calc(100% + 100px));
55
+ transition: transform 300ms cubic-bezier(0.65, 0, 0.35, 1);
56
+ }
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export { PopupSheet, PopupSheetProps } from './Component';
@@ -0,0 +1,95 @@
1
+ import { AnchorHTMLAttributes, ButtonHTMLAttributes, ElementType, ReactNode } from 'react';
2
+ type StyleColors = {
3
+ default: {
4
+ [key: string]: string;
5
+ };
6
+ inverted: {
7
+ [key: string]: string;
8
+ };
9
+ };
10
+ type ComponentProps = {
11
+ /**
12
+ * Тип кнопки
13
+ * @default secondary
14
+ */
15
+ view?: 'accent' | 'primary' | 'secondary' | 'tertiary' | 'outlined' | 'filled' | 'transparent' | 'link' | 'ghost';
16
+ /**
17
+ * Слот слева
18
+ */
19
+ leftAddons?: ReactNode;
20
+ /**
21
+ * Слот справа
22
+ */
23
+ rightAddons?: ReactNode;
24
+ /**
25
+ * Размер компонента
26
+ * @default m
27
+ */
28
+ size?: 'xxs' | 'xs' | 's' | 'm' | 'l' | 'xl';
29
+ /**
30
+ * Растягивает компонент на ширину контейнера
31
+ * @default false
32
+ */
33
+ block?: boolean;
34
+ /**
35
+ * Дополнительный класс
36
+ */
37
+ className?: string;
38
+ /**
39
+ * Дополнительный класс для спиннера
40
+ */
41
+ spinnerClassName?: string;
42
+ /**
43
+ * Выводит ссылку в виде кнопки
44
+ */
45
+ href?: string;
46
+ /**
47
+ * Позволяет использовать кастомный компонент для кнопки (например Link из роутера)
48
+ */
49
+ Component?: ElementType;
50
+ /**
51
+ * Идентификатор для систем автоматизированного тестирования
52
+ */
53
+ dataTestId?: string;
54
+ /**
55
+ * Показать лоадер
56
+ * @default false
57
+ */
58
+ loading?: boolean;
59
+ /**
60
+ * Не переносить текст кнопки на новую строку
61
+ * @default false
62
+ */
63
+ nowrap?: boolean;
64
+ /**
65
+ * Набор цветов для компонента
66
+ */
67
+ colors?: 'default' | 'inverted';
68
+ /**
69
+ * Дочерние элементы.
70
+ */
71
+ children?: ReactNode;
72
+ /**
73
+ * Основные стили компонента.
74
+ */
75
+ styles: {
76
+ [key: string]: string;
77
+ };
78
+ /**
79
+ * Стили компонента для default и inverted режима.
80
+ */
81
+ colorStylesMap: StyleColors;
82
+ };
83
+ type AnchorBaseButtonProps = ComponentProps & AnchorHTMLAttributes<HTMLAnchorElement>;
84
+ type NativeBaseButtonProps = ComponentProps & ButtonHTMLAttributes<HTMLButtonElement>;
85
+ type BaseButtonProps = Partial<AnchorBaseButtonProps | NativeBaseButtonProps>;
86
+ type AnchorButtonProps = Omit<BaseButtonProps, 'styles' | 'colorStylesMap'> & AnchorHTMLAttributes<HTMLAnchorElement>;
87
+ type NativeButtonProps = Omit<BaseButtonProps, 'styles' | 'colorStylesMap'> & ButtonHTMLAttributes<HTMLButtonElement>;
88
+ type ButtonProps = Partial<AnchorButtonProps | NativeButtonProps> & {
89
+ /**
90
+ * Контрольная точка, с нее начинается desktop версия
91
+ * @default 1024
92
+ */
93
+ breakpoint?: number;
94
+ };
95
+ export { StyleColors, ComponentProps, AnchorBaseButtonProps, NativeBaseButtonProps, BaseButtonProps, AnchorButtonProps, NativeButtonProps, ButtonProps };