@bacon-pos/ui 0.2.0 → 0.3.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/lib/commonjs/components/Dialog/Dialog.js +215 -0
- package/lib/commonjs/components/Dialog/Dialog.js.map +1 -0
- package/lib/commonjs/components/Dialog/Dialog.stories.js +169 -0
- package/lib/commonjs/components/Dialog/Dialog.stories.js.map +1 -0
- package/lib/commonjs/components/Dialog/Dialog.test.js +71 -0
- package/lib/commonjs/components/Dialog/Dialog.test.js.map +1 -0
- package/lib/commonjs/components/Dialog/index.js +13 -0
- package/lib/commonjs/components/Dialog/index.js.map +1 -0
- package/lib/commonjs/components/_internal/ScrollArea.js +96 -0
- package/lib/commonjs/components/_internal/ScrollArea.js.map +1 -0
- package/lib/commonjs/components/index.js +32 -10
- package/lib/commonjs/components/index.js.map +1 -1
- package/lib/module/components/Dialog/Dialog.js +211 -0
- package/lib/module/components/Dialog/Dialog.js.map +1 -0
- package/lib/module/components/Dialog/Dialog.stories.js +165 -0
- package/lib/module/components/Dialog/Dialog.stories.js.map +1 -0
- package/lib/module/components/Dialog/Dialog.test.js +70 -0
- package/lib/module/components/Dialog/Dialog.test.js.map +1 -0
- package/lib/module/components/Dialog/index.js +4 -0
- package/lib/module/components/Dialog/index.js.map +1 -0
- package/lib/module/components/_internal/ScrollArea.js +92 -0
- package/lib/module/components/_internal/ScrollArea.js.map +1 -0
- package/lib/module/components/index.js +2 -0
- package/lib/module/components/index.js.map +1 -1
- package/lib/typescript/commonjs/components/Dialog/Dialog.d.ts +75 -0
- package/lib/typescript/commonjs/components/Dialog/Dialog.d.ts.map +1 -0
- package/lib/typescript/commonjs/components/Dialog/index.d.ts +2 -0
- package/lib/typescript/commonjs/components/Dialog/index.d.ts.map +1 -0
- package/lib/typescript/commonjs/components/_internal/ScrollArea.d.ts +20 -0
- package/lib/typescript/commonjs/components/_internal/ScrollArea.d.ts.map +1 -0
- package/lib/typescript/commonjs/components/index.d.ts +2 -0
- package/lib/typescript/commonjs/components/index.d.ts.map +1 -1
- package/lib/typescript/module/components/Dialog/Dialog.d.ts +75 -0
- package/lib/typescript/module/components/Dialog/Dialog.d.ts.map +1 -0
- package/lib/typescript/module/components/Dialog/index.d.ts +2 -0
- package/lib/typescript/module/components/Dialog/index.d.ts.map +1 -0
- package/lib/typescript/module/components/_internal/ScrollArea.d.ts +20 -0
- package/lib/typescript/module/components/_internal/ScrollArea.d.ts.map +1 -0
- package/lib/typescript/module/components/index.d.ts +2 -0
- package/lib/typescript/module/components/index.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/components/Dialog/Dialog.tsx +269 -0
- package/src/components/Dialog/index.ts +6 -0
- package/src/components/_internal/ScrollArea.tsx +95 -0
- package/src/components/index.ts +2 -0
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
import { type ReactNode } from 'react';
|
|
2
|
+
import {
|
|
3
|
+
KeyboardAvoidingView,
|
|
4
|
+
Modal,
|
|
5
|
+
Platform,
|
|
6
|
+
Pressable,
|
|
7
|
+
ScrollView,
|
|
8
|
+
StyleSheet,
|
|
9
|
+
useWindowDimensions,
|
|
10
|
+
View,
|
|
11
|
+
type StyleProp,
|
|
12
|
+
type ViewStyle,
|
|
13
|
+
} from 'react-native';
|
|
14
|
+
import { useTheme } from '../../theme';
|
|
15
|
+
import { Button, type ButtonVariant } from '../Button/Button';
|
|
16
|
+
import { Icon } from '../Icon/Icon';
|
|
17
|
+
import { Text } from '../Text/Text';
|
|
18
|
+
import { ScrollArea } from '../_internal/ScrollArea';
|
|
19
|
+
|
|
20
|
+
export interface DialogAction {
|
|
21
|
+
label: string;
|
|
22
|
+
onPress?: () => void;
|
|
23
|
+
/** Button variant. Defaults to `default` (lime). Pass another for other colors. */
|
|
24
|
+
variant?: ButtonVariant;
|
|
25
|
+
loading?: boolean;
|
|
26
|
+
disabled?: boolean;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface DialogCancelConfig {
|
|
30
|
+
/** Cancel label. Defaults to "Cancel". */
|
|
31
|
+
label?: string;
|
|
32
|
+
/** Handler. Defaults to `onClose`. */
|
|
33
|
+
onPress?: () => void;
|
|
34
|
+
/** Use the destructive style (subtle red) instead of the neutral secondary. */
|
|
35
|
+
destructive?: boolean;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface DialogProps {
|
|
39
|
+
/** Whether the dialog is shown. */
|
|
40
|
+
visible: boolean;
|
|
41
|
+
/** Called on backdrop press, close button, or Android back. */
|
|
42
|
+
onClose: () => void;
|
|
43
|
+
/** Required title. */
|
|
44
|
+
title: string;
|
|
45
|
+
/** Optional subtitle. */
|
|
46
|
+
subtitle?: string;
|
|
47
|
+
/** Any content — from text to a full form. */
|
|
48
|
+
children?: ReactNode;
|
|
49
|
+
/** Primary/secondary action buttons (right side). Keep total buttons ≤ 3. */
|
|
50
|
+
actions?: DialogAction[];
|
|
51
|
+
/** Optional cancel button (left side). */
|
|
52
|
+
cancel?: DialogCancelConfig;
|
|
53
|
+
/** Show the top-right close (×). Defaults to true. */
|
|
54
|
+
showClose?: boolean;
|
|
55
|
+
/** Render the close (×) in the destructive color. */
|
|
56
|
+
closeDestructive?: boolean;
|
|
57
|
+
/** Dismiss when the backdrop (overlay) is pressed. Defaults to false. */
|
|
58
|
+
dismissOnBackdrop?: boolean;
|
|
59
|
+
/**
|
|
60
|
+
* `inner` (default): the card caps at the screen height and its content
|
|
61
|
+
* scrolls inside. `page`: the card grows to its content and the whole modal
|
|
62
|
+
* scrolls (nothing behind moves) — better for short screens.
|
|
63
|
+
*/
|
|
64
|
+
scrollMode?: 'inner' | 'page';
|
|
65
|
+
/** Keep the header pinned while content scrolls (inner mode). Defaults to true. */
|
|
66
|
+
stickyHeader?: boolean;
|
|
67
|
+
/** Keep the footer pinned while content scrolls (inner mode). Defaults to true. */
|
|
68
|
+
stickyFooter?: boolean;
|
|
69
|
+
/** Inner padding. Defaults to 24. */
|
|
70
|
+
padding?: number;
|
|
71
|
+
/** Vertical space between header↔content and content↔footer. Defaults to 24. */
|
|
72
|
+
contentSpacing?: number;
|
|
73
|
+
/** Corner radius. Defaults to 24. */
|
|
74
|
+
radius?: number;
|
|
75
|
+
/** Max width of the dialog card. Defaults to 560. */
|
|
76
|
+
maxWidth?: number;
|
|
77
|
+
/** Fraction of screen height the card may occupy (inner mode). Defaults to 0.9. */
|
|
78
|
+
maxHeightRatio?: number;
|
|
79
|
+
/** Scroll indicator color (a design-system color). Defaults to a subtle neutral. */
|
|
80
|
+
scrollbarColor?: string;
|
|
81
|
+
/** Override the card style. */
|
|
82
|
+
style?: StyleProp<ViewStyle>;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Dialog — an accessible, keyboard-aware modal that can hold anything from a
|
|
87
|
+
* short message to a full form (selects, date pickers, etc. keep working thanks
|
|
88
|
+
* to `keyboardShouldPersistTaps`). Content scrolls with an elegant thin,
|
|
89
|
+
* auto-hiding indicator. `scrollMode="page"` scrolls the whole modal on short
|
|
90
|
+
* screens instead of scrolling inside the card. Footer composes the
|
|
91
|
+
* design-system `Button`.
|
|
92
|
+
*/
|
|
93
|
+
export function Dialog({
|
|
94
|
+
visible,
|
|
95
|
+
onClose,
|
|
96
|
+
title,
|
|
97
|
+
subtitle,
|
|
98
|
+
children,
|
|
99
|
+
actions = [],
|
|
100
|
+
cancel,
|
|
101
|
+
showClose = true,
|
|
102
|
+
closeDestructive = false,
|
|
103
|
+
dismissOnBackdrop = false,
|
|
104
|
+
scrollMode = 'inner',
|
|
105
|
+
stickyHeader = true,
|
|
106
|
+
stickyFooter = true,
|
|
107
|
+
padding = 24,
|
|
108
|
+
contentSpacing = 24,
|
|
109
|
+
radius = 24,
|
|
110
|
+
maxWidth = 560,
|
|
111
|
+
maxHeightRatio = 0.9,
|
|
112
|
+
scrollbarColor,
|
|
113
|
+
style,
|
|
114
|
+
}: DialogProps) {
|
|
115
|
+
const theme = useTheme();
|
|
116
|
+
const { height } = useWindowDimensions();
|
|
117
|
+
const indicatorColor = scrollbarColor ?? theme.colors.text.tertiary;
|
|
118
|
+
|
|
119
|
+
const closeBtn = showClose ? (
|
|
120
|
+
<Pressable
|
|
121
|
+
onPress={onClose}
|
|
122
|
+
hitSlop={8}
|
|
123
|
+
accessibilityRole="button"
|
|
124
|
+
accessibilityLabel="Close"
|
|
125
|
+
style={{ position: 'absolute', top: padding, right: padding, padding: 4, zIndex: 1 }}
|
|
126
|
+
>
|
|
127
|
+
<Icon
|
|
128
|
+
name="close"
|
|
129
|
+
size={24}
|
|
130
|
+
color={closeDestructive ? theme.colors.feedback.error : theme.colors.text.secondary}
|
|
131
|
+
/>
|
|
132
|
+
</Pressable>
|
|
133
|
+
) : null;
|
|
134
|
+
|
|
135
|
+
const header = (
|
|
136
|
+
<View style={{ paddingHorizontal: padding, paddingTop: padding, paddingBottom: contentSpacing }}>
|
|
137
|
+
<View style={{ paddingRight: showClose ? 40 : 0, gap: 4 }}>
|
|
138
|
+
<Text variant="h3" weight="semibold" color="primary">
|
|
139
|
+
{title}
|
|
140
|
+
</Text>
|
|
141
|
+
{subtitle ? (
|
|
142
|
+
<Text variant="bodyLarge" color="secondary">
|
|
143
|
+
{subtitle}
|
|
144
|
+
</Text>
|
|
145
|
+
) : null}
|
|
146
|
+
</View>
|
|
147
|
+
</View>
|
|
148
|
+
);
|
|
149
|
+
|
|
150
|
+
const contentBody = <View style={{ paddingHorizontal: padding }}>{children}</View>;
|
|
151
|
+
|
|
152
|
+
const hasFooter = cancel != null || actions.length > 0;
|
|
153
|
+
const footer = hasFooter ? (
|
|
154
|
+
<View
|
|
155
|
+
style={{
|
|
156
|
+
flexDirection: 'row',
|
|
157
|
+
gap: 12,
|
|
158
|
+
paddingHorizontal: padding,
|
|
159
|
+
paddingBottom: padding,
|
|
160
|
+
paddingTop: contentSpacing,
|
|
161
|
+
}}
|
|
162
|
+
>
|
|
163
|
+
{cancel ? (
|
|
164
|
+
<View style={{ flex: 1 }}>
|
|
165
|
+
<Button
|
|
166
|
+
fullWidth
|
|
167
|
+
variant={cancel.destructive ? 'destructive' : 'secondary'}
|
|
168
|
+
label={cancel.label ?? 'Cancel'}
|
|
169
|
+
onPress={cancel.onPress ?? onClose}
|
|
170
|
+
/>
|
|
171
|
+
</View>
|
|
172
|
+
) : null}
|
|
173
|
+
{actions.map((action, i) => (
|
|
174
|
+
<View key={i} style={{ flex: 1 }}>
|
|
175
|
+
<Button
|
|
176
|
+
fullWidth
|
|
177
|
+
variant={action.variant ?? 'default'}
|
|
178
|
+
label={action.label}
|
|
179
|
+
onPress={action.onPress}
|
|
180
|
+
loading={action.loading}
|
|
181
|
+
disabled={action.disabled}
|
|
182
|
+
/>
|
|
183
|
+
</View>
|
|
184
|
+
))}
|
|
185
|
+
</View>
|
|
186
|
+
) : null;
|
|
187
|
+
|
|
188
|
+
const cardBase: ViewStyle = {
|
|
189
|
+
width: '100%',
|
|
190
|
+
maxWidth,
|
|
191
|
+
backgroundColor: theme.colors.background.default,
|
|
192
|
+
borderRadius: radius,
|
|
193
|
+
overflow: 'hidden',
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
const card =
|
|
197
|
+
scrollMode === 'page' ? (
|
|
198
|
+
<View accessibilityViewIsModal style={[cardBase, style]}>
|
|
199
|
+
{closeBtn}
|
|
200
|
+
{header}
|
|
201
|
+
{contentBody}
|
|
202
|
+
{footer}
|
|
203
|
+
</View>
|
|
204
|
+
) : (
|
|
205
|
+
<View
|
|
206
|
+
accessibilityViewIsModal
|
|
207
|
+
style={[cardBase, { maxHeight: height * maxHeightRatio }, style]}
|
|
208
|
+
>
|
|
209
|
+
{closeBtn}
|
|
210
|
+
{stickyHeader ? header : null}
|
|
211
|
+
<ScrollArea
|
|
212
|
+
indicatorColor={indicatorColor}
|
|
213
|
+
style={{ flexShrink: 1 }}
|
|
214
|
+
keyboardShouldPersistTaps="handled"
|
|
215
|
+
keyboardDismissMode="interactive"
|
|
216
|
+
>
|
|
217
|
+
{!stickyHeader ? header : null}
|
|
218
|
+
{contentBody}
|
|
219
|
+
{!stickyFooter ? footer : null}
|
|
220
|
+
</ScrollArea>
|
|
221
|
+
{stickyFooter ? footer : null}
|
|
222
|
+
</View>
|
|
223
|
+
);
|
|
224
|
+
|
|
225
|
+
return (
|
|
226
|
+
<Modal
|
|
227
|
+
visible={visible}
|
|
228
|
+
transparent
|
|
229
|
+
animationType="fade"
|
|
230
|
+
statusBarTranslucent
|
|
231
|
+
onRequestClose={onClose}
|
|
232
|
+
>
|
|
233
|
+
<KeyboardAvoidingView
|
|
234
|
+
behavior={Platform.OS === 'ios' ? 'padding' : undefined}
|
|
235
|
+
style={{ flex: 1 }}
|
|
236
|
+
>
|
|
237
|
+
<View style={{ flex: 1, backgroundColor: 'rgba(16, 18, 20, 0.55)' }}>
|
|
238
|
+
{scrollMode === 'page' ? (
|
|
239
|
+
<ScrollView
|
|
240
|
+
style={{ flex: 1 }}
|
|
241
|
+
contentContainerStyle={{ flexGrow: 1, padding: 16 }}
|
|
242
|
+
keyboardShouldPersistTaps="handled"
|
|
243
|
+
showsVerticalScrollIndicator={false}
|
|
244
|
+
>
|
|
245
|
+
<Pressable
|
|
246
|
+
style={{ flexGrow: 1, justifyContent: 'center', alignItems: 'center' }}
|
|
247
|
+
onPress={dismissOnBackdrop ? onClose : undefined}
|
|
248
|
+
>
|
|
249
|
+
<Pressable style={{ width: '100%', alignItems: 'center' }} onPress={() => {}}>
|
|
250
|
+
{card}
|
|
251
|
+
</Pressable>
|
|
252
|
+
</Pressable>
|
|
253
|
+
</ScrollView>
|
|
254
|
+
) : (
|
|
255
|
+
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center', padding: 16 }}>
|
|
256
|
+
<Pressable
|
|
257
|
+
style={StyleSheet.absoluteFill}
|
|
258
|
+
onPress={dismissOnBackdrop ? onClose : undefined}
|
|
259
|
+
accessibilityElementsHidden
|
|
260
|
+
importantForAccessibility="no-hide-descendants"
|
|
261
|
+
/>
|
|
262
|
+
{card}
|
|
263
|
+
</View>
|
|
264
|
+
)}
|
|
265
|
+
</View>
|
|
266
|
+
</KeyboardAvoidingView>
|
|
267
|
+
</Modal>
|
|
268
|
+
);
|
|
269
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { useRef, useState, type ReactNode } from 'react';
|
|
2
|
+
import {
|
|
3
|
+
Animated,
|
|
4
|
+
type NativeScrollEvent,
|
|
5
|
+
type NativeSyntheticEvent,
|
|
6
|
+
type StyleProp,
|
|
7
|
+
type ViewStyle,
|
|
8
|
+
} from 'react-native';
|
|
9
|
+
|
|
10
|
+
export interface ScrollAreaProps {
|
|
11
|
+
children: ReactNode;
|
|
12
|
+
/** Thin indicator color (a design-system color). */
|
|
13
|
+
indicatorColor: string;
|
|
14
|
+
/** Indicator thickness. Defaults to 4. */
|
|
15
|
+
indicatorWidth?: number;
|
|
16
|
+
keyboardShouldPersistTaps?: 'always' | 'never' | 'handled';
|
|
17
|
+
keyboardDismissMode?: 'none' | 'on-drag' | 'interactive';
|
|
18
|
+
style?: StyleProp<ViewStyle>;
|
|
19
|
+
contentContainerStyle?: StyleProp<ViewStyle>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* ScrollView with an elegant custom vertical indicator: thin, rounded, colored,
|
|
24
|
+
* and only visible while scrolling (fades out when idle). The native indicator
|
|
25
|
+
* is hidden so it looks the same on iOS, Android and web.
|
|
26
|
+
*/
|
|
27
|
+
export function ScrollArea({
|
|
28
|
+
children,
|
|
29
|
+
indicatorColor,
|
|
30
|
+
indicatorWidth = 4,
|
|
31
|
+
keyboardShouldPersistTaps,
|
|
32
|
+
keyboardDismissMode,
|
|
33
|
+
style,
|
|
34
|
+
contentContainerStyle,
|
|
35
|
+
}: ScrollAreaProps) {
|
|
36
|
+
const [viewportH, setViewportH] = useState(0);
|
|
37
|
+
const [contentH, setContentH] = useState(0);
|
|
38
|
+
const scrollY = useRef(new Animated.Value(0)).current;
|
|
39
|
+
const opacity = useRef(new Animated.Value(0)).current;
|
|
40
|
+
const hideTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
41
|
+
|
|
42
|
+
const scrollable = contentH > viewportH + 1;
|
|
43
|
+
const thumbH = scrollable ? Math.max(24, (viewportH / contentH) * viewportH) : 0;
|
|
44
|
+
const maxScroll = Math.max(1, contentH - viewportH);
|
|
45
|
+
const translateY = scrollY.interpolate({
|
|
46
|
+
inputRange: [0, maxScroll],
|
|
47
|
+
outputRange: [0, viewportH - thumbH],
|
|
48
|
+
extrapolate: 'clamp',
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
const reveal = () => {
|
|
52
|
+
Animated.timing(opacity, { toValue: 1, duration: 120, useNativeDriver: true }).start();
|
|
53
|
+
if (hideTimer.current) clearTimeout(hideTimer.current);
|
|
54
|
+
hideTimer.current = setTimeout(() => {
|
|
55
|
+
Animated.timing(opacity, { toValue: 0, duration: 400, useNativeDriver: true }).start();
|
|
56
|
+
}, 900);
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
return (
|
|
60
|
+
<Animated.View style={[{ position: 'relative' }, style]}>
|
|
61
|
+
<Animated.ScrollView
|
|
62
|
+
style={{ flexShrink: 1 }}
|
|
63
|
+
contentContainerStyle={contentContainerStyle}
|
|
64
|
+
showsVerticalScrollIndicator={false}
|
|
65
|
+
scrollEventThrottle={16}
|
|
66
|
+
keyboardShouldPersistTaps={keyboardShouldPersistTaps}
|
|
67
|
+
keyboardDismissMode={keyboardDismissMode}
|
|
68
|
+
onLayout={(e) => setViewportH(e.nativeEvent.layout.height)}
|
|
69
|
+
onContentSizeChange={(_w: number, h: number) => setContentH(h)}
|
|
70
|
+
onScroll={Animated.event([{ nativeEvent: { contentOffset: { y: scrollY } } }], {
|
|
71
|
+
useNativeDriver: true,
|
|
72
|
+
listener: (_e: NativeSyntheticEvent<NativeScrollEvent>) => reveal(),
|
|
73
|
+
})}
|
|
74
|
+
>
|
|
75
|
+
{children}
|
|
76
|
+
</Animated.ScrollView>
|
|
77
|
+
{scrollable ? (
|
|
78
|
+
<Animated.View
|
|
79
|
+
pointerEvents="none"
|
|
80
|
+
style={{
|
|
81
|
+
position: 'absolute',
|
|
82
|
+
top: 0,
|
|
83
|
+
right: 2,
|
|
84
|
+
width: indicatorWidth,
|
|
85
|
+
height: thumbH,
|
|
86
|
+
borderRadius: indicatorWidth / 2,
|
|
87
|
+
backgroundColor: indicatorColor,
|
|
88
|
+
opacity,
|
|
89
|
+
transform: [{ translateY }],
|
|
90
|
+
}}
|
|
91
|
+
/>
|
|
92
|
+
) : null}
|
|
93
|
+
</Animated.View>
|
|
94
|
+
);
|
|
95
|
+
}
|
package/src/components/index.ts
CHANGED
|
@@ -6,6 +6,7 @@ export * from './Checkbox';
|
|
|
6
6
|
export * from './Chip';
|
|
7
7
|
export * from './CodeField';
|
|
8
8
|
export * from './CodeInput';
|
|
9
|
+
export * from './Dialog';
|
|
9
10
|
export * from './Icon';
|
|
10
11
|
export * from './IconButton';
|
|
11
12
|
export * from './Input';
|
|
@@ -13,6 +14,7 @@ export * from './LinkButton';
|
|
|
13
14
|
export * from './ProductCard';
|
|
14
15
|
export * from './QuantityStepper';
|
|
15
16
|
export * from './Radio';
|
|
17
|
+
export * from './Spinner';
|
|
16
18
|
export * from './Surface';
|
|
17
19
|
export * from './Switch';
|
|
18
20
|
export * from './Text';
|