An open source design system that's fully customizable and agent ready
TypeScript
fix(Dialog): lock body scroll on iOS Safari when modal is open (#948)
Cindy Zhangcc23f87d5498
diff --git a/packages/core/src/Dialog/XDSDialog.tsx b/packages/core/src/Dialog/XDSDialog.tsxindex 9c0d2f22c0..aae58b8f2b 100644--- a/packages/core/src/Dialog/XDSDialog.tsx+++ b/packages/core/src/Dialog/XDSDialog.tsx@@ -16,6 +16,7 @@import {useEffect, useRef, type ReactNode} from 'react';import type {XDSBaseProps} from '../XDSBaseProps';import * as stylex from '@stylexjs/stylex';+import {useScrollLock} from '../hooks/useScrollLock';import {colorVars,radiusVars,@@ -314,6 +315,9 @@ export function XDSDialog({}}, [isOpen]);+ // Lock body scroll when dialog is open (iOS Safari workaround)+ useScrollLock(isOpen);+// Handle Escape keyuseEffect(() => {const dialog = dialogRef.current;diff --git a/packages/core/src/hooks/index.ts b/packages/core/src/hooks/index.tsindex 4ea6721a2e..d66fe989d3 100644--- a/packages/core/src/hooks/index.ts+++ b/packages/core/src/hooks/index.ts@@ -22,3 +22,5 @@ export {useMediaQuery} from './useMediaQuery';export {useOverflow} from './useOverflow';export type {UseOverflowOptions, UseOverflowReturn} from './useOverflow';++export {useScrollLock} from './useScrollLock';diff --git a/packages/core/src/hooks/useScrollLock.ts b/packages/core/src/hooks/useScrollLock.tsnew file mode 100644index 0000000000..aec67582db--- /dev/null+++ b/packages/core/src/hooks/useScrollLock.ts@@ -0,0 +1,59 @@+'use client';++/**+ * @file useScrollLock.ts+ * @input Uses React useEffect+ * @output Exports useScrollLock hook for locking body scroll+ * @position Core hook; used by Dialog to prevent background scrolling+ *+ * Locks body scroll when active by pinning the body with position:fixed.+ * This is needed because overscroll-behavior:contain doesn't work on iOS Safari.+ *+ * SYNC: When modified, update:+ * - /packages/core/src/hooks/index.ts+ */++import {useEffect} from 'react';++/**+ * Locks body scroll when `isLocked` is true.+ *+ * Pins the body with `position: fixed` to prevent background scrolling,+ * which is necessary for iOS Safari where `overscroll-behavior: contain`+ * does not prevent body scroll behind modals. Restores scroll position+ * on unlock.+ *+ * @example+ * ```+ * useScrollLock(isOpen);+ * ```+ */+export function useScrollLock(isLocked: boolean): void {+ useEffect(() => {+ if (!isLocked) return;++ const scrollX = window.scrollX;+ const scrollY = window.scrollY;+ const {body} = document;+ const prevOverflow = body.style.overflow;+ const prevPosition = body.style.position;+ const prevTop = body.style.top;+ const prevLeft = body.style.left;+ const prevRight = body.style.right;++ body.style.overflow = 'hidden';+ body.style.position = 'fixed';+ body.style.top = `-${scrollY}px`;+ body.style.left = '0';+ body.style.right = '0';++ return () => {+ body.style.overflow = prevOverflow;+ body.style.position = prevPosition;+ body.style.top = prevTop;+ body.style.left = prevLeft;+ body.style.right = prevRight;+ window.scrollTo(scrollX, scrollY);+ };+ }, [isLocked]);+}