Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
arnnis committed Jun 11, 2022
2 parents 1b941aa + 5987981 commit 3c47e53
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 12 deletions.
33 changes: 25 additions & 8 deletions example/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,16 @@ const Home = () => {
</Text>
<Text
onPress={() =>
toast.show("This is a customized toast! you can implement your own", {
type: "custom_toast",
animationDuration: 100,
data: {
title: "Customized toast",
},
})
toast.show(
"This is a customized toast! you can implement your own",
{
type: "custom_toast",
animationDuration: 100,
data: {
title: "Customized toast",
},
}
)
}
style={styles.test}
>
Expand All @@ -87,6 +90,16 @@ const Home = () => {
>
Placement top
</Text>
<Text
onPress={() => {
toast.show("This toast should render on center", {
placement: "center",
});
}}
style={[styles.test]}
>
Placement center
</Text>
<Text
onPress={() => {
toast?.show("This toast have zoom-in animation", {
Expand Down Expand Up @@ -155,7 +168,11 @@ const Home = () => {
>
Swipe to close disabled
</Text>
<TextInput ref={inputRef} style={{ height: 50 }} placeholder="Input"></TextInput>
<TextInput
ref={inputRef}
style={{ height: 50 }}
placeholder="Input"
></TextInput>

<Text
onPress={() => {
Expand Down
39 changes: 36 additions & 3 deletions src/toast-container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import {
ViewStyle,
KeyboardAvoidingView,
Platform,
Dimensions,
} from "react-native";
import Toast, { ToastOptions, ToastProps } from "./toast";

const { height, width } = Dimensions.get("window");

export interface Props extends ToastOptions {
renderToast?(toast: ToastProps): JSX.Element;
renderType?: { [type: string]: (toast: ToastProps) => JSX.Element };
Expand Down Expand Up @@ -145,11 +148,43 @@ class ToastContainer extends Component<Props, State> {
);
}

renderCenterToasts() {
const { toasts } = this.state;
let { offset, offsetTop } = this.props;
let style: ViewStyle = {
top: offsetTop || offset,
height: height,
width: width,
justifyContent: "center",
flexDirection: "column-reverse",
};

const data = toasts.filter((t) => t.placement === "center");
const foundToast = data.length > 0;

if (!foundToast) return null;

return (
<KeyboardAvoidingView
behavior={Platform.OS === "ios" ? "position" : undefined}
style={[styles.container, style]}
pointerEvents="box-none"
>
{toasts
.filter((t) => t.placement === "center")
.map((toast) => (
<Toast key={toast.id} {...toast} />
))}
</KeyboardAvoidingView>
);
}

render() {
return (
<>
{this.renderTopToasts()}
{this.renderBottomToasts()}
{this.renderCenterToasts()}
</>
);
}
Expand All @@ -159,11 +194,9 @@ const styles = StyleSheet.create({
container: {
flex: 0,
position: "absolute",
width: "100%",
maxWidth: "100%",
zIndex: 999999,
left: 0,
right: 0,
alignSelf: 'center',
...(Platform.OS === "web" ? { overflow: "hidden" } : null),
},
message: {
Expand Down
2 changes: 1 addition & 1 deletion src/toast.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export interface ToastOptions {
/**
* Customize when toast should be placed
*/
placement?: "top" | "bottom";
placement?: "top" | "bottom" | "center";

/**
* Customize style of toast
Expand Down

0 comments on commit 3c47e53

Please sign in to comment.