-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGenericSnackbar.tsx
More file actions
128 lines (116 loc) · 4.08 KB
/
Copy pathGenericSnackbar.tsx
File metadata and controls
128 lines (116 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import * as React from "react";
import { animated, useSpring, config } from "@react-spring/web";
const { useState, useEffect, useMemo } = React;
export interface SnackbarButtonProps {
text: string | React.ReactNode;
onClick: () => void;
className?: string;
disabled?: boolean; // Added disabled prop
}
export interface GenericSnackbarProps {
id: string;
message: string | React.ReactNode;
buttons?: SnackbarButtonProps[];
isVisible: boolean;
onDismiss: (id: string) => void; // Called for explicit dismiss actions like timeout
onAnimatedOut?: (id: string) => void; // Called after exit animation completes
kind?: string;
autoHideDuration?: number | null; // Duration in ms, null to disable auto-hide
}
export function GenericSnackbar({
id,
message,
buttons,
isVisible,
onDismiss,
onAnimatedOut,
kind,
autoHideDuration = 6000, // Default to 6 seconds
}: GenericSnackbarProps): React.ReactElement | null {
const [isShown, setIsShown] = useState(false); // Internal state to help drive animation
// Animation for the snackbar
const animationProps = useSpring({
from: { opacity: 0, transform: "translateY(100%)" },
to: {
opacity: isVisible ? 1 : 0, // Drive directly by isVisible for opacity
transform: isVisible ? "translateY(0%)" : "translateY(100%)", // And transform
},
config: config.gentle,
onRest: (result) => {
// onRest can be called for both entry and exit animations.
// We are interested when the snackbar has animated out.
if (!isVisible && result.finished) { // Check if animation finished and snackbar is not visible
onAnimatedOut?.(id);
}
},
});
useEffect(() => {
// This effect manages the isShown state, primarily for initial mount.
// The animation itself is now more directly driven by the isVisible prop.
if (isVisible) {
setIsShown(true);
} else {
// If isVisible is false from the start, or becomes false
setIsShown(false);
}
}, [isVisible]);
useEffect(() => {
let timerId: NodeJS.Timeout | null = null;
if (isVisible && autoHideDuration) {
timerId = setTimeout(() => {
onDismiss(id);
}, autoHideDuration);
}
return () => {
if (timerId) {
clearTimeout(timerId);
}
};
}, [id, isVisible, autoHideDuration, onDismiss]);
// Memoize buttons to prevent re-renders if props haven't changed
const memoizedButtons = useMemo(() => {
return buttons?.map((buttonProps, index) => (
<button
key={index}
onClick={() => {
if (buttonProps.disabled) return;
buttonProps.onClick();
}}
className={`mdl-button mdl-js-button mdl-button--colored ${buttonProps.className || ""} ${buttonProps.disabled ? "mdl-button--disabled" : ""}`}
disabled={buttonProps.disabled}
>
{buttonProps.text}
</button>
));
}, [buttons]);
// Determine CSS classes
const snackbarClasses = ["generic-snackbar"];
if (kind) {
snackbarClasses.push(`snackbar-kind-${kind}`);
}
// Add basic styling for visibility and layout
// More specific styling should be handled by CSS files.
const direction = localStorage.languageOption === "hebrew" ? "rtl" : "ltr";
// Do not render if it's not supposed to be visible and not currently shown (e.g. already dismissed and animated out)
// This is a slight adjustment to ensure that if isVisible is false from the start, nothing is rendered.
// The main purpose of this component is to animate in/out based on isVisible.
// If isVisible is false and isShown is also false (meaning it's fully dismissed or was never meant to show), return null.
if (!isVisible && !isShown) {
return null;
}
return (
<animated.div
style={animationProps}
className={snackbarClasses.join(" ")}
dir={direction}
role="alert"
aria-live="assertive"
aria-atomic="true"
>
<div className="snackbar-text">{message}</div>
{memoizedButtons && memoizedButtons.length > 0 && (
<div className="snackbar-buttons">{memoizedButtons}</div>
)}
</animated.div>
);
}