Skip to content

armgit / vaul

Public Imported from GitHub
0

A drawer component for React.

Close menu properly when controlled (#90)

Emil Kowalskia87182a5bdf9
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index aa021ad..e58fac7 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -58,6 +58,7 @@ importers:
'@types/react-dom': 18.2.7
'@vercel/analytics': ^1.0.1
autoprefixer: 10.4.14
+ clsx: ^2.0.0
eslint: 8.45.0
eslint-config-next: 13.4.10
next: 13.4.10
@@ -74,6 +75,7 @@ importers:
'@types/react-dom': 18.2.7
'@vercel/analytics': 1.0.1
autoprefixer: [email protected]
+ clsx: 2.0.0
eslint: 8.45.0
diff --git a/src/index.tsx b/src/index.tsx
index d54489d..d72f1be 100644
--- a/src/index.tsx
+++ b/src/index.tsx
@@ -74,15 +74,11 @@ function Root({
modal = true,
onClose,
}: DialogProps) {
- const [isOpen = false, setIsOpen] = useControllableState({
- prop: openProp,
- defaultProp: defaultOpen,
- onChange: onOpenChange,
- });
+ const [isOpen = false, setIsOpen] = React.useState<boolean>(false);
// Not visible = translateY(100%)
- const [visible, setVisible] = React.useState(false);
- const [isDragging, setIsDragging] = React.useState(false);
- const [justReleased, setJustReleased] = React.useState(false);
+ const [visible, setVisible] = React.useState<boolean>(false);
+ const [isDragging, setIsDragging] = React.useState<boolean>(false);
+ const [justReleased, setJustReleased] = React.useState<boolean>(false);
const overlayRef = React.useRef<HTMLDivElement>(null);
const dragStartTime = React.useRef<Date | null>(null);
const dragEndTime = React.useRef<Date | null>(null);
@@ -372,6 +368,20 @@ function Root({
}
}, [isOpen, shouldScaleBackground]);
+ // This can be done much better
+ React.useEffect(() => {
+ if (openProp) {
+ setIsOpen(true);
+ } else {
+ closeDrawer();
+ }
+ }, [openProp]);
+
+ // This can be done much better
+ React.useEffect(() => {
+ onOpenChange?.(isOpen);
+ }, [isOpen]);
+
function resetDrawer() {
if (!drawerRef.current) return;
const wrapper = document.querySelector('[vaul-drawer-wrapper]');
diff --git a/website/package.json b/website/package.json
index 9eb3a24..022c78c 100644
--- a/website/package.json
+++ b/website/package.json
@@ -14,6 +14,7 @@
"@types/react-dom": "18.2.7",
"@vercel/analytics": "^1.0.1",
"autoprefixer": "10.4.14",
+ "clsx": "^2.0.0",
"eslint": "8.45.0",
"eslint-config-next": "13.4.10",
"next": "13.4.10",