diff --git a/.changeset/eight-goats-suffer.md b/.changeset/eight-goats-suffer.md new file mode 100644 index 00000000000..0da56a9ba50 --- /dev/null +++ b/.changeset/eight-goats-suffer.md @@ -0,0 +1,5 @@ +--- +"@clerk/clerk-js": patch +--- + +Fix issue where scroll lock was not restored correctly when multiple modals were opening diff --git a/packages/clerk-js/bundlewatch.config.json b/packages/clerk-js/bundlewatch.config.json index fd73a9957e9..ae744eadc31 100644 --- a/packages/clerk-js/bundlewatch.config.json +++ b/packages/clerk-js/bundlewatch.config.json @@ -4,7 +4,7 @@ { "path": "./dist/clerk.browser.js", "maxSize": "75kB" }, { "path": "./dist/clerk.headless.js", "maxSize": "48.3KB" }, { "path": "./dist/ui-common*.js", "maxSize": "89KB" }, - { "path": "./dist/vendors*.js", "maxSize": "25KB" }, + { "path": "./dist/vendors*.js", "maxSize": "25.1KB" }, { "path": "./dist/coinbase*.js", "maxSize": "35.5KB" }, { "path": "./dist/createorganization*.js", "maxSize": "5KB" }, { "path": "./dist/impersonationfab*.js", "maxSize": "5KB" }, diff --git a/packages/clerk-js/src/ui/elements/Modal.tsx b/packages/clerk-js/src/ui/elements/Modal.tsx index ef72f1bb833..f0524ab6740 100644 --- a/packages/clerk-js/src/ui/elements/Modal.tsx +++ b/packages/clerk-js/src/ui/elements/Modal.tsx @@ -1,8 +1,9 @@ -import { createContextAndHook, useSafeLayoutEffect } from '@clerk/shared/react'; +import { createContextAndHook } from '@clerk/shared/react'; +import { FloatingOverlay } from '@floating-ui/react'; import React, { useRef } from 'react'; import { descriptors, Flex } from '../customizables'; -import { usePopover, useScrollLock } from '../hooks'; +import { usePopover } from '../hooks'; import type { ThemableCssProp } from '../styledSystem'; import { animations, mqu } from '../styledSystem'; import { withFloatingTree } from './contexts'; @@ -22,7 +23,6 @@ type ModalProps = React.PropsWithChildren<{ export const Modal = withFloatingTree((props: ModalProps) => { const { handleClose, handleOpen, contentSx, containerSx, canCloseModal, id, style } = props; - const { disableScroll, enableScroll } = useScrollLock(document.body); const overlayRef = useRef(null); const { floating, isOpen, context, nodeId, toggle } = usePopover({ defaultOpen: true, @@ -39,11 +39,6 @@ export const Modal = withFloatingTree((props: ModalProps) => { } }, [isOpen]); - useSafeLayoutEffect(() => { - disableScroll(); - return () => enableScroll(); - }); - const modalCtx = React.useMemo(() => ({ value: canCloseModal === false ? {} : { toggle } }), [toggle, canCloseModal]); return ( @@ -52,51 +47,53 @@ export const Modal = withFloatingTree((props: ModalProps) => { context={context} isOpen={isOpen} > - - ({ - animation: `${animations.fadeIn} 150ms ${t.transitionTiming.$common}`, - zIndex: t.zIndices.$modal, - backgroundColor: t.colors.$modalBackdrop, - alignItems: 'flex-start', - justifyContent: 'center', - overflow: 'auto', - width: '100vw', - height: ['100vh', '-webkit-fill-available'], - position: 'fixed', - left: 0, - top: 0, - }), - containerSx, - ]} - > + + ({ - position: 'relative', - outline: 0, - animation: `${animations.modalSlideAndFade} 180ms ${t.transitionTiming.$easeOut}`, - margin: `${t.space.$16} 0`, - [mqu.sm]: { - margin: `${t.space.$10} 0`, - }, + animation: `${animations.fadeIn} 150ms ${t.transitionTiming.$common}`, + zIndex: t.zIndices.$modal, + backgroundColor: t.colors.$modalBackdrop, + alignItems: 'flex-start', + justifyContent: 'center', + overflow: 'auto', + width: '100vw', + height: ['100vh', '-webkit-fill-available'], + position: 'fixed', + left: 0, + top: 0, }), - contentSx, + containerSx, ]} > - {props.children} + ({ + position: 'relative', + outline: 0, + animation: `${animations.modalSlideAndFade} 180ms ${t.transitionTiming.$easeOut}`, + margin: `${t.space.$16} 0`, + [mqu.sm]: { + margin: `${t.space.$10} 0`, + }, + }), + contentSx, + ]} + > + {props.children} + - - + + ); }); diff --git a/packages/clerk-js/src/ui/hooks/index.ts b/packages/clerk-js/src/ui/hooks/index.ts index 263683924b4..58996c9c9fa 100644 --- a/packages/clerk-js/src/ui/hooks/index.ts +++ b/packages/clerk-js/src/ui/hooks/index.ts @@ -15,7 +15,6 @@ export * from './useResizeObserver'; export * from './useSafeState'; export * from './useSearchInput'; export * from './useDebounce'; -export * from './useScrollLock'; export * from './useClerkModalStateParams'; export * from './useNavigateToFlowStart'; export * from './useEnterpriseSSOLink'; diff --git a/packages/clerk-js/src/ui/hooks/useScrollLock.ts b/packages/clerk-js/src/ui/hooks/useScrollLock.ts deleted file mode 100644 index e41cbba3a1e..00000000000 --- a/packages/clerk-js/src/ui/hooks/useScrollLock.ts +++ /dev/null @@ -1,28 +0,0 @@ -/** - * Disables scroll for an element. - * Adds extra padding to prevent layout shifting - * caused by hiding the scrollbar. - */ -export const useScrollLock = (el: T) => { - let oldPaddingRightPx: string; - let oldOverflow: string; - - const disableScroll = () => { - oldPaddingRightPx = getComputedStyle(el).paddingRight; - oldOverflow = getComputedStyle(el).overflow; - const oldWidth = el.clientWidth; - el.style.overflow = 'hidden'; - const currentWidth = el.clientWidth; - const oldPaddingRight = Number.parseInt(oldPaddingRightPx.replace('px', '')); - el.style.paddingRight = `${currentWidth - oldWidth + oldPaddingRight}px`; - }; - - const enableScroll = () => { - el.style.overflow = oldOverflow; - if (oldPaddingRightPx) { - el.style.paddingRight = oldPaddingRightPx; - } - }; - - return { disableScroll, enableScroll }; -};