Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add preventClose argument to onClose callback #350

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
48 changes: 30 additions & 18 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type DialogProps = {
onRelease?: (event: React.PointerEvent<HTMLDivElement>, open: boolean) => void;
modal?: boolean;
nested?: boolean;
onClose?: () => void;
onClose?: (preventClose?: () => void) => void;
direction?: 'top' | 'bottom' | 'left' | 'right';
preventScrollRestoration?: boolean;
disablePreventScroll?: boolean;
Expand Down Expand Up @@ -101,6 +101,7 @@ function Root({
const keyboardIsOpen = React.useRef(false);
const previousDiffFromInitial = React.useRef(0);
const drawerRef = React.useRef<HTMLDivElement>(null);
const shouldClose = React.useRef(true);
const drawerHeightRef = React.useRef(drawerRef.current?.getBoundingClientRect().height || 0);
const initialDrawerHeight = React.useRef(0);

Expand Down Expand Up @@ -139,7 +140,7 @@ function Root({
nested,
hasBeenOpened,
preventScrollRestoration,
noBodyStyles
noBodyStyles,
});

function getScale() {
Expand Down Expand Up @@ -408,30 +409,41 @@ function Root({
return () => window.visualViewport?.removeEventListener('resize', onVisualViewportChange);
}, [activeSnapPointIndex, snapPoints, snapPointsOffset]);

function preventClose() {
shouldClose.current = false;
}

function closeDrawer() {
if (!drawerRef.current) return;

cancelDrag();

onClose?.();
set(drawerRef.current, {
transform: isVertical(direction)
? `translate3d(0, ${direction === 'bottom' ? '100%' : '-100%'}, 0)`
: `translate3d(${direction === 'right' ? '100%' : '-100%'}, 0, 0)`,
transition: `transform ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(',')})`,
});
onClose?.(preventClose);

set(overlayRef.current, {
opacity: '0',
transition: `opacity ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(',')})`,
});
if (shouldClose.current === true) {
set(drawerRef.current, {
transform: isVertical(direction)
? `translate3d(0, ${direction === 'bottom' ? '100%' : '-100%'}, 0)`
: `translate3d(${direction === 'right' ? '100%' : '-100%'}, 0, 0)`,
transition: `transform ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(',')})`,
});

set(overlayRef.current, {
opacity: '0',
transition: `opacity ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(',')})`,
});

scaleBackground(false);
scaleBackground(false);

setTimeout(() => {
setVisible(false);
setIsOpen(false);
}, 300);
setTimeout(() => {
setVisible(false);
setIsOpen(false);
}, 300);
} else {
resetDrawer();
}

shouldClose.current = true;

setTimeout(() => {
// reset(document.documentElement, 'scrollBehavior');
Expand Down
Loading