Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ There are lots of props to customize your toast or your can use renderToast to i
offsetTop={30}
offsetBottom={40}
swipeEnabled={true}
dismissAllPreviousToast={true} // false by default enable this to show one toast at a time
renderToast={(toastOptions) => JSX.Element} implement custom toast component.
>
...
Expand Down
44 changes: 28 additions & 16 deletions src/toast-container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
ViewStyle,
KeyboardAvoidingView,
Platform,
Dimensions, SafeAreaView,
Dimensions,
SafeAreaView,
} from "react-native";
import Toast, { ToastOptions, ToastProps } from "./toast";

Expand Down Expand Up @@ -42,25 +43,34 @@ class ToastContainer extends Component<Props, State> {
*/
show = (message: string | JSX.Element, toastOptions?: ToastOptions) => {
let id = toastOptions?.id || Math.random().toString();

const onDestroy = () => {
toastOptions?.onClose && toastOptions?.onClose();
this.setState({ toasts: this.state.toasts.filter((t) => t.id !== id) });
};

const createdToast = {
id,
onDestroy,
message,
open: true,
onHide: () => this.hide(id),
...this.props,
...toastOptions,
};

const dismissAllPreviousToast =
createdToast.dismissAllPreviousToast || false;

const openToasts = dismissAllPreviousToast
? []
: this.state.toasts.filter((t) => t.open);

const constructedToastState = [createdToast, ...openToasts];

requestAnimationFrame(() => {
this.setState({
toasts: [
{
id,
onDestroy,
message,
open: true,
onHide: () => this.hide(id),
...this.props,
...toastOptions,
},
...this.state.toasts.filter((t) => t.open),
],
toasts: constructedToastState,
});
});

Expand Down Expand Up @@ -107,7 +117,7 @@ class ToastContainer extends Component<Props, State> {
*/
isOpen = (id: string) => {
return this.state.toasts.some((t) => t.id === id && t.open);
}
};

renderBottomToasts() {
const { toasts } = this.state;
Expand Down Expand Up @@ -211,8 +221,10 @@ const styles = StyleSheet.create({
maxWidth: "100%",
zIndex: 999999,
elevation: 999999,
alignSelf: 'center',
...(Platform.OS === "web" ? { overflow: "hidden", userSelect: 'none' } : null),
alignSelf: "center",
...(Platform.OS === "web"
? { overflow: "hidden", userSelect: "none" }
: null),
},
message: {
color: "#333",
Expand Down
6 changes: 6 additions & 0 deletions src/toast.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ export interface ToastOptions {
*/
data?: any;

/**
* Dismiss previous toast
*/

dismissAllPreviousToast?: boolean;

swipeEnabled?: boolean;
}

Expand Down