Skip to content

armgit / vaul

Public Imported from GitHub
0

A drawer component for React.

Fix controlled state closing instantly (#94)

Emil Kowalski45dc6183b083
diff --git a/src/index.tsx b/src/index.tsx
index 0c443c0..d3d3245 100644
--- a/src/index.tsx
+++ b/src/index.tsx
@@ -76,6 +76,7 @@ function Root({
const [isOpen = false, setIsOpen] = React.useState<boolean>(false);
// Not visible = translateY(100%)
const [visible, setVisible] = React.useState<boolean>(false);
+ const [mounted, setMounted] = React.useState<boolean>(false);
const [isDragging, setIsDragging] = React.useState<boolean>(false);
const [justReleased, setJustReleased] = React.useState<boolean>(false);
const overlayRef = React.useRef<HTMLDivElement>(null);
@@ -382,9 +383,15 @@ function Root({
// This can be done much better
React.useEffect(() => {
- onOpenChange?.(isOpen);
+ if (mounted) {
+ onOpenChange?.(isOpen);
+ }
}, [isOpen]);
+ React.useEffect(() => {
+ setMounted(true);
+ }, []);
+
function resetDrawer() {
if (!drawerRef.current) return;
const wrapper = document.querySelector('[vaul-drawer-wrapper]');
diff --git a/website/src/app/components/hero.tsx b/website/src/app/components/hero.tsx
index 77342c8..732f540 100644
--- a/website/src/app/components/hero.tsx
+++ b/website/src/app/components/hero.tsx
@@ -49,8 +49,8 @@ export function Hero() {
Open Drawer
</button>
</Drawer.Trigger>
- <Drawer.Overlay className="fixed inset-0 bg-black/40" />
<Drawer.Portal>
+ <Drawer.Overlay className="fixed inset-0 bg-black/40" />
<Drawer.Content className="bg-gray-100 flex flex-col rounded-t-[10px] h-full mt-24 max-h-[96%] fixed bottom-0 left-0 right-0">
<div className="p-4 bg-white rounded-t-[10px] flex-1">
<div className="mx-auto w-12 h-1.5 flex-shrink-0 rounded-full bg-gray-300 mb-8" />