Skip to content

armgit / vaul

Public Imported from GitHub
0

A drawer component for React.

Add dismissable prop

emilkowalski1038d2fb3076
diff --git a/src/context.ts b/src/context.ts
index 41d0d63..5199cb5 100644
--- a/src/context.ts
+++ b/src/context.ts
@@ -7,6 +7,7 @@ interface DrawerContextValue {
onPress: (event: React.PointerEvent<HTMLDivElement>) => void;
onRelease: (event: React.PointerEvent<HTMLDivElement>) => void;
onMove: (event: React.PointerEvent<HTMLDivElement>) => void;
+ dismissible: boolean;
}
export const DrawerContext = React.createContext<DrawerContextValue>({
@@ -16,6 +17,7 @@ export const DrawerContext = React.createContext<DrawerContextValue>({
onPress: () => {},
onRelease: () => {},
onMove: () => {},
+ dismissible: true,
});
export const useDrawerContext = () => React.useContext(DrawerContext);
diff --git a/src/index.tsx b/src/index.tsx
index cec3ea3..4573eff 100644
--- a/src/index.tsx
+++ b/src/index.tsx
@@ -63,9 +63,17 @@ interface DialogProps {
defaultOpen?: boolean;
onOpenChange?(open: boolean): void;
shouldScaleBackground?: boolean;
+ dismissible?: boolean;
}
-function Root({ open: openProp, defaultOpen, onOpenChange, children, shouldScaleBackground }: DialogProps) {
+function Root({
+ open: openProp,
+ defaultOpen,
+ onOpenChange,
+ children,
+ shouldScaleBackground,
+ dismissible = true,
+}: DialogProps) {
const [isOpen = false, setIsOpen] = useControllableState({
prop: openProp,
defaultProp: defaultOpen,
@@ -89,6 +97,7 @@ function Root({ open: openProp, defaultOpen, onOpenChange, children, shouldScale
}
function onPress(event: React.PointerEvent<HTMLDivElement>) {
+ if (!dismissible) return;
setIsDragging(true);
dragStartTime.current = new Date();
@@ -228,6 +237,7 @@ function Root({ open: openProp, defaultOpen, onOpenChange, children, shouldScale
}, []);
function closeDrawer() {
+ if (!dismissible) return;
setIsOpen(false);
const drawerHeight = drawerRef.current?.getBoundingClientRect().height || 0;
@@ -373,6 +383,7 @@ function Root({ open: openProp, defaultOpen, onOpenChange, children, shouldScale
onPress,
onRelease,
onMove,
+ dismissible,
}}
>
{children}
@@ -391,8 +402,8 @@ const Overlay = React.forwardRef<HTMLDivElement, React.ComponentPropsWithoutRef<
);
const Content = React.forwardRef<HTMLDivElement, React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content>>(
- function ({ children, onOpenAutoFocus, ...rest }, ref) {
- const { drawerRef, onPress, onRelease, onAnimationStart, onMove } = useDrawerContext();
+ function ({ children, onOpenAutoFocus, onPointerDownOutside, ...rest }, ref) {
+ const { drawerRef, onPress, onRelease, onAnimationStart, onMove, dismissible } = useDrawerContext();
const composedRef = useComposedRefs(ref, drawerRef);
return (
@@ -408,6 +419,13 @@ const Content = React.forwardRef<HTMLDivElement, React.ComponentPropsWithoutRef<
e.preventDefault();
}
}}
+ onPointerDownOutside={(e) => {
+ if (!dismissible) {
+ e.preventDefault();
+ }
+
+ onPointerDownOutside?.(e);
+ }}
ref={composedRef}
{...rest}
vaul-drawer=""