beautiful-snackbar 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.
- package/README.md +233 -0
- package/example/.claude/settings.json +5 -0
- package/example/.vscode/extensions.json +1 -0
- package/example/.vscode/settings.json +7 -0
- package/example/AGENTS.md +3 -0
- package/example/CLAUDE.md +1 -0
- package/example/app.json +44 -0
- package/example/assets/expo.icon/Assets/expo-symbol 2.svg +3 -0
- package/example/assets/expo.icon/Assets/grid.png +0 -0
- package/example/assets/expo.icon/icon.json +40 -0
- package/example/assets/images/android-icon-background.png +0 -0
- package/example/assets/images/android-icon-foreground.png +0 -0
- package/example/assets/images/android-icon-monochrome.png +0 -0
- package/example/assets/images/expo-badge-white.png +0 -0
- package/example/assets/images/expo-badge.png +0 -0
- package/example/assets/images/expo-logo.png +0 -0
- package/example/assets/images/favicon.png +0 -0
- package/example/assets/images/icon.png +0 -0
- package/example/assets/images/logo-glow.png +0 -0
- package/example/assets/images/react-logo.png +0 -0
- package/example/assets/images/react-logo@2x.png +0 -0
- package/example/assets/images/react-logo@3x.png +0 -0
- package/example/assets/images/splash-icon.png +0 -0
- package/example/assets/images/tabIcons/explore.png +0 -0
- package/example/assets/images/tabIcons/explore@2x.png +0 -0
- package/example/assets/images/tabIcons/explore@3x.png +0 -0
- package/example/assets/images/tabIcons/home.png +0 -0
- package/example/assets/images/tabIcons/home@2x.png +0 -0
- package/example/assets/images/tabIcons/home@3x.png +0 -0
- package/example/assets/images/tutorial-web.png +0 -0
- package/example/metro.config.js +24 -0
- package/example/package.json +46 -0
- package/example/scripts/reset-project.js +114 -0
- package/example/src/app/_layout.tsx +63 -0
- package/example/src/app/explore.tsx +181 -0
- package/example/src/app/index.tsx +641 -0
- package/example/src/components/animated-icon.module.css +6 -0
- package/example/src/components/animated-icon.tsx +132 -0
- package/example/src/components/animated-icon.web.tsx +108 -0
- package/example/src/components/app-tabs.tsx +33 -0
- package/example/src/components/app-tabs.web.tsx +116 -0
- package/example/src/components/external-link.tsx +25 -0
- package/example/src/components/hint-row.tsx +35 -0
- package/example/src/components/themed-text.tsx +73 -0
- package/example/src/components/themed-view.tsx +16 -0
- package/example/src/components/ui/collapsible.tsx +65 -0
- package/example/src/components/web-badge.tsx +44 -0
- package/example/src/constants/theme.ts +66 -0
- package/example/src/global.css +9 -0
- package/example/src/hooks/use-color-scheme.ts +1 -0
- package/example/src/hooks/use-color-scheme.web.ts +21 -0
- package/example/src/hooks/use-theme.ts +14 -0
- package/example/tsconfig.json +35 -0
- package/lib/components/ActionableSnackbar.d.ts +7 -0
- package/lib/components/ActionableSnackbar.js +96 -0
- package/lib/components/BeautifulSnackbar.d.ts +11 -0
- package/lib/components/BeautifulSnackbar.js +189 -0
- package/lib/components/StandardSnackbar.d.ts +7 -0
- package/lib/components/StandardSnackbar.js +65 -0
- package/lib/constants.d.ts +7 -0
- package/lib/constants.js +20 -0
- package/lib/index.d.ts +5 -0
- package/lib/index.js +11 -0
- package/lib/manager.d.ts +58 -0
- package/lib/manager.js +107 -0
- package/lib/types.d.ts +25 -0
- package/lib/types.js +2 -0
- package/lib/useSnackbarAnimation.d.ts +14 -0
- package/lib/useSnackbarAnimation.js +118 -0
- package/package.json +33 -0
- package/src/components/ActionableSnackbar.tsx +109 -0
- package/src/components/BeautifulSnackbar.tsx +203 -0
- package/src/components/StandardSnackbar.tsx +70 -0
- package/src/constants.ts +20 -0
- package/src/index.ts +5 -0
- package/src/manager.ts +151 -0
- package/src/types.ts +27 -0
- package/src/useSnackbarAnimation.ts +145 -0
- package/tsconfig.json +23 -0
package/src/constants.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { SnackbarDuration } from './types';
|
|
2
|
+
|
|
3
|
+
export const DURATION_SHORT = 1500;
|
|
4
|
+
export const DURATION_MEDIUM = 2200;
|
|
5
|
+
export const DURATION_LONG = 3200;
|
|
6
|
+
|
|
7
|
+
export const DEFAULT_HEIGHT_LIMIT = 90;
|
|
8
|
+
export const TRANSITION_SPEED = 200;
|
|
9
|
+
|
|
10
|
+
export const getDurationMs = (duration?: SnackbarDuration): number => {
|
|
11
|
+
switch (duration) {
|
|
12
|
+
case 'long':
|
|
13
|
+
return DURATION_LONG;
|
|
14
|
+
case 'medium':
|
|
15
|
+
return DURATION_MEDIUM;
|
|
16
|
+
case 'short':
|
|
17
|
+
default:
|
|
18
|
+
return DURATION_SHORT;
|
|
19
|
+
}
|
|
20
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { BeautifulSnackbar } from './components/BeautifulSnackbar';
|
|
2
|
+
export { snackbar, SnackbarManager, SnackbarItem } from './manager';
|
|
3
|
+
export { useSnackbarAnimation } from './useSnackbarAnimation';
|
|
4
|
+
export type { SnackbarOptions, SnackbarDuration, SnackbarPosition, SnackbarAnimationType } from './types';
|
|
5
|
+
export type { BeautifulSnackbarProps } from './components/BeautifulSnackbar';
|
package/src/manager.ts
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { ViewStyle, TextStyle, Platform } from 'react-native';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { SnackbarOptions, SnackbarDuration, SnackbarPosition, SnackbarAnimationType } from './types';
|
|
4
|
+
|
|
5
|
+
export interface SnackbarConfig {
|
|
6
|
+
avoidKeyboard: boolean;
|
|
7
|
+
position: SnackbarPosition;
|
|
8
|
+
animationType: SnackbarAnimationType;
|
|
9
|
+
bottomOffset: number;
|
|
10
|
+
topOffset: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export class SnackbarItem {
|
|
14
|
+
id: string;
|
|
15
|
+
message: string;
|
|
16
|
+
messageStyle?: TextStyle;
|
|
17
|
+
containerStyle?: ViewStyle;
|
|
18
|
+
actionLabel?: string;
|
|
19
|
+
actionStyle?: TextStyle;
|
|
20
|
+
onActionPress?: () => void;
|
|
21
|
+
icon?: React.ReactNode;
|
|
22
|
+
duration: SnackbarDuration;
|
|
23
|
+
position?: SnackbarPosition;
|
|
24
|
+
animationType?: SnackbarAnimationType;
|
|
25
|
+
bottomOffset?: number;
|
|
26
|
+
topOffset?: number;
|
|
27
|
+
dismissOnNavigation: boolean;
|
|
28
|
+
backgroundColor?: string;
|
|
29
|
+
textColor?: string;
|
|
30
|
+
actionColor?: string;
|
|
31
|
+
type?: string;
|
|
32
|
+
data?: any;
|
|
33
|
+
|
|
34
|
+
onShowRequested = () => {};
|
|
35
|
+
onHideRequested = () => {};
|
|
36
|
+
onDismissRequested?: () => void;
|
|
37
|
+
|
|
38
|
+
constructor(options: SnackbarOptions, id: string) {
|
|
39
|
+
this.id = id;
|
|
40
|
+
this.message = options.message;
|
|
41
|
+
this.messageStyle = options.messageStyle;
|
|
42
|
+
this.containerStyle = options.containerStyle;
|
|
43
|
+
this.actionLabel = options.actionLabel;
|
|
44
|
+
this.actionStyle = options.actionStyle;
|
|
45
|
+
this.onActionPress = options.onActionPress;
|
|
46
|
+
this.icon = options.icon;
|
|
47
|
+
this.duration = options.duration ?? 'short';
|
|
48
|
+
this.position = options.position;
|
|
49
|
+
this.animationType = options.animationType;
|
|
50
|
+
this.bottomOffset = options.bottomOffset;
|
|
51
|
+
this.topOffset = options.topOffset;
|
|
52
|
+
this.dismissOnNavigation = Boolean(options.dismissOnNavigation);
|
|
53
|
+
this.backgroundColor = options.backgroundColor;
|
|
54
|
+
this.textColor = options.textColor;
|
|
55
|
+
this.actionColor = options.actionColor;
|
|
56
|
+
this.type = options.type;
|
|
57
|
+
this.data = options.data;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export class SnackbarManager {
|
|
62
|
+
private stateListener?: (items: SnackbarItem[]) => void;
|
|
63
|
+
private configListeners: ((config: SnackbarConfig) => void)[] = [];
|
|
64
|
+
private activeItems: SnackbarItem[] = [];
|
|
65
|
+
private counter = 0;
|
|
66
|
+
private config: SnackbarConfig = {
|
|
67
|
+
avoidKeyboard: true,
|
|
68
|
+
position: 'bottom',
|
|
69
|
+
animationType: 'slide',
|
|
70
|
+
bottomOffset: 24,
|
|
71
|
+
topOffset: Platform.OS === 'ios' ? 50 : 24,
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
registerListener(listener: (items: SnackbarItem[]) => void) {
|
|
75
|
+
this.stateListener = listener;
|
|
76
|
+
listener([...this.activeItems]);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
unregisterListener() {
|
|
80
|
+
this.stateListener = undefined;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
registerConfigListener(listener: (config: SnackbarConfig) => void) {
|
|
84
|
+
this.configListeners.push(listener);
|
|
85
|
+
listener({ ...this.config });
|
|
86
|
+
return () => {
|
|
87
|
+
this.configListeners = this.configListeners.filter((l) => l !== listener);
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
setAvoidKeyboard(val: boolean) {
|
|
92
|
+
this.config.avoidKeyboard = val;
|
|
93
|
+
this.configListeners.forEach((listener) => listener({ ...this.config }));
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
getAvoidKeyboard(): boolean {
|
|
97
|
+
return this.config.avoidKeyboard;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
setPosition(val: SnackbarPosition) {
|
|
101
|
+
this.config.position = val;
|
|
102
|
+
this.configListeners.forEach((listener) => listener({ ...this.config }));
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
getPosition(): SnackbarPosition {
|
|
106
|
+
return this.config.position;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
setAnimationType(val: SnackbarAnimationType) {
|
|
110
|
+
this.config.animationType = val;
|
|
111
|
+
this.configListeners.forEach((listener) => listener({ ...this.config }));
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
getAnimationType(): SnackbarAnimationType {
|
|
115
|
+
return this.config.animationType;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
setBottomOffset(val: number) {
|
|
119
|
+
this.config.bottomOffset = val;
|
|
120
|
+
this.configListeners.forEach((listener) => listener({ ...this.config }));
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
getBottomOffset(): number {
|
|
124
|
+
return this.config.bottomOffset;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
setTopOffset(val: number) {
|
|
128
|
+
this.config.topOffset = val;
|
|
129
|
+
this.configListeners.forEach((listener) => listener({ ...this.config }));
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
getTopOffset(): number {
|
|
133
|
+
return this.config.topOffset;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
show(options: SnackbarOptions): SnackbarItem {
|
|
137
|
+
this.counter++;
|
|
138
|
+
const id = `sb_${Date.now()}_${this.counter}`;
|
|
139
|
+
const item = new SnackbarItem(options, id);
|
|
140
|
+
this.activeItems.push(item);
|
|
141
|
+
this.stateListener?.([...this.activeItems]);
|
|
142
|
+
return item;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
dismiss(item: SnackbarItem) {
|
|
146
|
+
this.activeItems = this.activeItems.filter(x => x.id !== item.id);
|
|
147
|
+
this.stateListener?.([...this.activeItems]);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export const snackbar = new SnackbarManager();
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { ViewStyle, TextStyle } from 'react-native';
|
|
3
|
+
|
|
4
|
+
export type SnackbarDuration = 'short' | 'medium' | 'long' | 'infinite';
|
|
5
|
+
export type SnackbarPosition = 'top' | 'bottom';
|
|
6
|
+
export type SnackbarAnimationType = 'slide' | 'fade' | 'scale';
|
|
7
|
+
|
|
8
|
+
export interface SnackbarOptions {
|
|
9
|
+
message: string;
|
|
10
|
+
messageStyle?: TextStyle;
|
|
11
|
+
containerStyle?: ViewStyle;
|
|
12
|
+
actionLabel?: string;
|
|
13
|
+
actionStyle?: TextStyle;
|
|
14
|
+
onActionPress?: () => void;
|
|
15
|
+
icon?: React.ReactNode;
|
|
16
|
+
duration?: SnackbarDuration;
|
|
17
|
+
position?: SnackbarPosition;
|
|
18
|
+
animationType?: SnackbarAnimationType;
|
|
19
|
+
bottomOffset?: number;
|
|
20
|
+
topOffset?: number;
|
|
21
|
+
dismissOnNavigation?: boolean;
|
|
22
|
+
backgroundColor?: string;
|
|
23
|
+
textColor?: string;
|
|
24
|
+
actionColor?: string;
|
|
25
|
+
type?: string; // Key for registered custom template UI
|
|
26
|
+
data?: any; // Custom metadata context passed to custom templates
|
|
27
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef } from 'react';
|
|
2
|
+
import { Animated } from "react-native";
|
|
3
|
+
import { SnackbarItem, snackbar } from './manager';
|
|
4
|
+
import { TRANSITION_SPEED, getDurationMs } from "./constants";
|
|
5
|
+
|
|
6
|
+
export const useSnackbarAnimation = (item: SnackbarItem) => {
|
|
7
|
+
const durationMs = getDurationMs(item.duration);
|
|
8
|
+
const position = item.position || snackbar.getPosition();
|
|
9
|
+
const animType = item.animationType || snackbar.getAnimationType();
|
|
10
|
+
const isTop = position === "top";
|
|
11
|
+
|
|
12
|
+
const offset = isTop
|
|
13
|
+
? item.topOffset !== undefined
|
|
14
|
+
? item.topOffset
|
|
15
|
+
: snackbar.getTopOffset()
|
|
16
|
+
: item.bottomOffset !== undefined
|
|
17
|
+
? item.bottomOffset
|
|
18
|
+
: snackbar.getBottomOffset();
|
|
19
|
+
|
|
20
|
+
const startVal = isTop ? -120 : 120;
|
|
21
|
+
const endVal = isTop ? offset : -offset;
|
|
22
|
+
|
|
23
|
+
const opacity = useRef(new Animated.Value(0)).current;
|
|
24
|
+
const scale = useRef(
|
|
25
|
+
new Animated.Value(animType === "scale" ? 0.8 : 1),
|
|
26
|
+
).current;
|
|
27
|
+
const translateY = useRef(
|
|
28
|
+
new Animated.Value(animType === "slide" ? startVal : endVal),
|
|
29
|
+
).current;
|
|
30
|
+
|
|
31
|
+
const animStyle = {
|
|
32
|
+
opacity,
|
|
33
|
+
transform: [{ translateY }, { scale }],
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const removeSelf = useCallback(() => {
|
|
37
|
+
snackbar.dismiss(item);
|
|
38
|
+
}, [item]);
|
|
39
|
+
|
|
40
|
+
const animateIntro = useCallback(() => {
|
|
41
|
+
const animations = [
|
|
42
|
+
Animated.timing(opacity, {
|
|
43
|
+
toValue: 1,
|
|
44
|
+
duration: TRANSITION_SPEED,
|
|
45
|
+
useNativeDriver: true,
|
|
46
|
+
}),
|
|
47
|
+
];
|
|
48
|
+
|
|
49
|
+
if (animType === "slide") {
|
|
50
|
+
animations.push(
|
|
51
|
+
Animated.timing(translateY, {
|
|
52
|
+
toValue: endVal,
|
|
53
|
+
duration: TRANSITION_SPEED,
|
|
54
|
+
useNativeDriver: true,
|
|
55
|
+
}),
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (animType === "scale") {
|
|
60
|
+
animations.push(
|
|
61
|
+
Animated.spring(scale, {
|
|
62
|
+
toValue: 1,
|
|
63
|
+
tension: 40,
|
|
64
|
+
friction: 6,
|
|
65
|
+
useNativeDriver: true,
|
|
66
|
+
}),
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return Animated.parallel(animations);
|
|
71
|
+
}, [opacity, translateY, scale, animType, endVal]);
|
|
72
|
+
|
|
73
|
+
const animateOutro = useCallback(() => {
|
|
74
|
+
const animations = [
|
|
75
|
+
Animated.timing(opacity, {
|
|
76
|
+
toValue: 0,
|
|
77
|
+
duration: TRANSITION_SPEED,
|
|
78
|
+
useNativeDriver: true,
|
|
79
|
+
}),
|
|
80
|
+
];
|
|
81
|
+
|
|
82
|
+
if (animType === "slide") {
|
|
83
|
+
animations.push(
|
|
84
|
+
Animated.timing(translateY, {
|
|
85
|
+
toValue: startVal,
|
|
86
|
+
duration: TRANSITION_SPEED,
|
|
87
|
+
useNativeDriver: true,
|
|
88
|
+
}),
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (animType === "scale") {
|
|
93
|
+
animations.push(
|
|
94
|
+
Animated.timing(scale, {
|
|
95
|
+
toValue: 0.8,
|
|
96
|
+
duration: TRANSITION_SPEED,
|
|
97
|
+
useNativeDriver: true,
|
|
98
|
+
}),
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return Animated.parallel(animations);
|
|
103
|
+
}, [opacity, translateY, scale, animType, startVal]);
|
|
104
|
+
|
|
105
|
+
const animateIntroAndOutro = useCallback(() => {
|
|
106
|
+
Animated.sequence([
|
|
107
|
+
animateIntro(),
|
|
108
|
+
Animated.delay(durationMs),
|
|
109
|
+
animateOutro(),
|
|
110
|
+
]).start(({ finished }: { finished: boolean }) => {
|
|
111
|
+
if (finished) {
|
|
112
|
+
removeSelf();
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
}, [animateIntro, animateOutro, durationMs, removeSelf]);
|
|
116
|
+
|
|
117
|
+
const animateShow = useCallback(() => {
|
|
118
|
+
animateIntro().start();
|
|
119
|
+
}, [animateIntro]);
|
|
120
|
+
|
|
121
|
+
const animateHide = useCallback(() => {
|
|
122
|
+
animateOutro().start();
|
|
123
|
+
}, [animateOutro]);
|
|
124
|
+
|
|
125
|
+
const animateDismiss = useCallback(() => {
|
|
126
|
+
animateOutro().start(({ finished }: { finished: boolean }) => {
|
|
127
|
+
if (finished) {
|
|
128
|
+
removeSelf();
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
}, [animateOutro, removeSelf]);
|
|
132
|
+
|
|
133
|
+
useEffect(() => {
|
|
134
|
+
if (item.duration !== 'infinite') {
|
|
135
|
+
animateIntroAndOutro();
|
|
136
|
+
} else {
|
|
137
|
+
item.onShowRequested = animateShow;
|
|
138
|
+
item.onHideRequested = animateHide;
|
|
139
|
+
animateShow();
|
|
140
|
+
}
|
|
141
|
+
item.onDismissRequested = animateDismiss;
|
|
142
|
+
}, [animateDismiss, animateHide, item, animateShow, animateIntroAndOutro]);
|
|
143
|
+
|
|
144
|
+
return { animStyle };
|
|
145
|
+
};
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "es2020",
|
|
4
|
+
"module": "commonjs",
|
|
5
|
+
"lib": ["esnext", "dom"],
|
|
6
|
+
"allowJs": true,
|
|
7
|
+
"jsx": "react-native",
|
|
8
|
+
"declaration": true,
|
|
9
|
+
"outDir": "./lib",
|
|
10
|
+
"strict": true,
|
|
11
|
+
"moduleResolution": "node",
|
|
12
|
+
"esModuleInterop": true,
|
|
13
|
+
"skipLibCheck": true,
|
|
14
|
+
"resolveJsonModule": true,
|
|
15
|
+
"baseUrl": ".",
|
|
16
|
+
"paths": {
|
|
17
|
+
"react": ["example/node_modules/react"],
|
|
18
|
+
"react-native": ["example/node_modules/react-native"],
|
|
19
|
+
"react-native-safe-area-context": ["example/node_modules/react-native-safe-area-context"]
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"include": ["src"]
|
|
23
|
+
}
|