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
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "react-native-dan-forden",
"description": "Super simple react native toast that's just gonna work. (web, native, expo)",
"version": "0.0.8",
"version": "0.0.9",
"main": "src/index.ts",
"files": [
"src"
Expand Down Expand Up @@ -31,6 +31,8 @@
},
"devDependencies": {
"@types/react": "^18.2.75",
"@types/react-dom": "^18.2.25",
"react-dom": "18.2.0",
"react": "^18.2.0",
"react-native": "^0.73.6",
"react-native-gesture-handler": "^2.16.0",
Expand Down
22 changes: 21 additions & 1 deletion src/ToastContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useEffect, useState } from "react";
import { createPortal } from "react-dom";
import { View, Pressable, StyleSheet } from "react-native";

import { ToastMessage } from "./types";
Expand All @@ -17,6 +18,19 @@ export default function ToastContainer({
children,
}: Props) {
const [slide, setSlide] = useState(false);
const [portalEl, setPortalEl] = useState<HTMLDivElement | null>(null);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be a ref, not state


useEffect(() => {
const el = document.createElement("div");
el.style.zIndex = "10000";
el.style.position = "relative";
Comment on lines +25 to +26
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are these necessary? they might be, look down at my styles you'll notice similar zIndex stuff, but this needs to be as vanilla as possible so I don't want to add these unless we need to

document.body.appendChild(el);
setPortalEl(el);

return () => {
document.body.removeChild(el);
};
}, []);

function onDismiss() {
setSlide(false);
Expand All @@ -34,7 +48,7 @@ export default function ToastContainer({
setSlide(true);
}, []);

return (
const content = (
<View
pointerEvents="none"
style={[styles.container, position === "bottom" && styles.positionBottom]}
Expand Down Expand Up @@ -64,6 +78,12 @@ export default function ToastContainer({
</Pressable>
</View>
);

if (portalEl) {
return createPortal(content, portalEl);
}

return content;
Comment on lines +82 to +86
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we don't have the portal, we should return null.

}

const styles = StyleSheet.create({
Expand Down
Loading