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

fix: don't animate initially if defaultOpen is set to true #488

Merged
merged 1 commit into from
Oct 13, 2024
Merged
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
2 changes: 2 additions & 0 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ interface DrawerContextValue {
handleOnly?: boolean;
container?: HTMLElement | null;
autoFocus?: boolean;
shouldAnimate?: React.RefObject<boolean>;
}

export const DrawerContext = React.createContext<DrawerContextValue>({
Expand All @@ -57,6 +58,7 @@ export const DrawerContext = React.createContext<DrawerContextValue>({
setActiveSnapPoint: () => {},
closeDrawer: () => {},
direction: 'bottom',
shouldAnimate: { current: true },
shouldScaleBackground: false,
setBackgroundColorOnScale: true,
noBodyStyles: false,
Expand Down
15 changes: 13 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export type DialogProps = {
*/
direction?: 'top' | 'bottom' | 'left' | 'right';
/**
* Opened by default, still reacts to `open` state changes
* Opened by default, skips initial enter animation. Still reacts to `open` state changes
* @default false
*/
defaultOpen?: boolean;
Expand Down Expand Up @@ -206,6 +206,7 @@ export function Root({
const nestedOpenChangeTimer = React.useRef<NodeJS.Timeout | null>(null);
const pointerStart = React.useRef(0);
const keyboardIsOpen = React.useRef(false);
const shouldAnimate = React.useRef(!defaultOpen);
const previousDiffFromInitial = React.useRef(0);
const drawerRef = React.useRef<HTMLDivElement>(null);
const drawerHeightRef = React.useRef(drawerRef.current?.getBoundingClientRect().height || 0);
Expand Down Expand Up @@ -465,6 +466,12 @@ export function Root({
}
}

React.useEffect(() => {
window.requestAnimationFrame(() => {
shouldAnimate.current = true;
});
}, []);

React.useEffect(() => {
function onVisualViewportChange() {
if (!drawerRef.current || !repositionInputs) return;
Expand Down Expand Up @@ -763,6 +770,7 @@ export function Root({
onRelease,
onDrag,
dismissible,
shouldAnimate,
handleOnly,
isOpen,
isDragging,
Expand Down Expand Up @@ -791,7 +799,7 @@ export function Root({

export const Overlay = React.forwardRef<HTMLDivElement, React.ComponentPropsWithoutRef<typeof DialogPrimitive.Overlay>>(
function ({ ...rest }, ref) {
const { overlayRef, snapPoints, onRelease, shouldFade, isOpen, modal } = useDrawerContext();
const { overlayRef, snapPoints, onRelease, shouldFade, isOpen, modal, shouldAnimate } = useDrawerContext();
const composedRef = useComposedRefs(ref, overlayRef);
const hasSnapPoints = snapPoints && snapPoints.length > 0;

Expand All @@ -809,6 +817,7 @@ export const Overlay = React.forwardRef<HTMLDivElement, React.ComponentPropsWith
data-vaul-overlay=""
data-vaul-snap-points={isOpen && hasSnapPoints ? 'true' : 'false'}
data-vaul-snap-points-overlay={isOpen && shouldFade ? 'true' : 'false'}
data-vaul-animate={shouldAnimate?.current ? 'true' : 'false'}
{...rest}
/>
);
Expand Down Expand Up @@ -837,6 +846,7 @@ export const Content = React.forwardRef<HTMLDivElement, ContentProps>(function (
snapPoints,
container,
handleOnly,
shouldAnimate,
autoFocus,
} = useDrawerContext();
// Needed to use transition instead of animations
Expand Down Expand Up @@ -893,6 +903,7 @@ export const Content = React.forwardRef<HTMLDivElement, ContentProps>(function (
data-vaul-delayed-snap-points={delayedSnapPoints ? 'true' : 'false'}
data-vaul-snap-points={isOpen && hasSnapPoints ? 'true' : 'false'}
data-vaul-custom-container={container ? 'true' : 'false'}
data-vaul-animate={shouldAnimate?.current ? 'true' : 'false'}
{...rest}
ref={composedRef}
style={
Expand Down
6 changes: 5 additions & 1 deletion src/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
}

[data-vaul-drawer][data-vaul-snap-points='true'][data-vaul-drawer-direction='right'] {
transform: translate3d(var(--initial-transform, 100%) %, 0, 0);
transform: translate3d(var(--initial-transform, 100%), 0, 0);
}

[data-vaul-drawer][data-vaul-delayed-snap-points='true'][data-vaul-drawer-direction='top'] {
Expand Down Expand Up @@ -77,6 +77,10 @@
animation-name: fadeOut;
}

[data-vaul-animate='false'] {
animation: none !important;
}

[data-vaul-overlay][data-vaul-snap-points='true'] {
opacity: 0;
transition: opacity 0.5s cubic-bezier(0.32, 0.72, 0, 1);
Expand Down
Loading