Skip to content
Open
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
59 changes: 48 additions & 11 deletions packages/react-aria-components/src/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
*/

import {AriaModalOverlayProps, useModalOverlay} from 'react-aria/useModalOverlay';

import {
ClassNameOrFunction,
ContextValue,
Expand All @@ -25,6 +24,7 @@ import {
import {DismissButton, Overlay} from 'react-aria/Overlay';
import {DOMAttributes, forwardRefType, GlobalDOMAttributes, RefObject} from '@react-types/shared';
import {filterDOMProps} from 'react-aria/filterDOMProps';
import {getActiveElement} from 'react-aria/private/utils/shadowdom/DOMFunctions';
import {isScrollable} from 'react-aria/private/utils/isScrollable';
import {mergeProps} from 'react-aria/mergeProps';
import {mergeRefs} from 'react-aria/mergeRefs';
Expand All @@ -34,11 +34,22 @@ import {
useOverlayTriggerState
} from 'react-stately/useOverlayTriggerState';
import {OverlayTriggerStateContext} from './Dialog';
import React, {createContext, ForwardedRef, forwardRef, useContext, useMemo, useRef} from 'react';
import React, {
createContext,
ForwardedRef,
forwardRef,
useContext,
useMemo,
useRef,
useState
} from 'react';
import {useEnterAnimation, useExitAnimation} from 'react-aria/private/utils/animation';
import {useIsSSR} from 'react-aria/SSRProvider';
import {useLayoutEffect} from 'react-aria/private/utils/useLayoutEffect';
import {useObjectRef} from 'react-aria/useObjectRef';
import {useViewportSize} from 'react-aria/private/utils/useViewportSize';
import {useVisuallyHidden} from 'react-aria/VisuallyHidden';
import {willOpenKeyboard} from 'react-aria/private/utils/keyboard';

export interface ModalOverlayProps
extends
Expand Down Expand Up @@ -300,11 +311,12 @@ interface ModalContentProps

function ModalContent(props: ModalContentProps) {
let {modalProps, modalRef, isExiting, isDismissable} = useContext(InternalModalContext)!;
let [isOpen, setOpen] = useState(false);
let state = useContext(OverlayTriggerStateContext)!;
let mergedRefs = useMemo(() => mergeRefs(props.modalRef, modalRef), [props.modalRef, modalRef]);

let ref = useObjectRef(mergedRefs);
let entering = useEnterAnimation(ref);
let entering = useEnterAnimation(ref, isOpen);
let renderProps = useRenderProps({
...props,
defaultClassName: 'react-aria-Modal',
Expand All @@ -315,15 +327,40 @@ function ModalContent(props: ModalContentProps) {
}
});

// Hide the modal initially, since an auto-focused input may cause a viewport resize in the next frame.
// If so, delay the reveal by another frame to avoid layout shift when the viewport settles.
Comment on lines +330 to +331
Copy link
Copy Markdown
Contributor Author

@nwidynski nwidynski May 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to squeeze this into the hook layer (e.g. useModalOverlay), but that was challenging, due to expectations around a single render pass. This is also pretty opinionated styling, so I ultimately decided to place it in RAC instead.

useLayoutEffect(() => {
let frame: number, frame2: number;

frame = requestAnimationFrame(() => {
let activeElement = getActiveElement();
if (activeElement && willOpenKeyboard(activeElement)) {
frame2 = requestAnimationFrame(() => setOpen(true));
} else {
setOpen(true);
}
});

return () => {
cancelAnimationFrame(frame);
cancelAnimationFrame(frame2);
};
}, []);

let {visuallyHiddenProps} = useVisuallyHidden();
let contentStyle = isOpen ? {display: 'contents'} : visuallyHiddenProps.style;

return (
<dom.div
{...mergeProps(filterDOMProps(props, {global: true}), modalProps)}
{...renderProps}
ref={ref}
data-entering={entering || undefined}
data-exiting={isExiting || undefined}>
{isDismissable && <DismissButton onDismiss={state.close} />}
{renderProps.children}
<dom.div style={contentStyle}>
<dom.div
{...mergeProps(filterDOMProps(props, {global: true}), modalProps)}
{...renderProps}
ref={ref}
data-entering={entering || undefined}
data-exiting={isExiting || undefined}>
{isDismissable && <DismissButton onDismiss={state.close} />}
{renderProps.children}
</dom.div>
</dom.div>
);
}
37 changes: 22 additions & 15 deletions packages/react-aria/src/overlays/usePreventScroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
*/

import {chain} from '../utils/chain';

import {getActiveElement, getEventTarget} from '../utils/shadowdom/DOMFunctions';
import {getNonce} from '../utils/getNonce';
import {getScrollParent} from '../utils/getScrollParent';
import {isIOS} from '../utils/platform';
import {isIOS, isWebKit} from '../utils/platform';
import {isScrollable} from '../utils/isScrollable';
import {useLayoutEffect} from '../utils/useLayoutEffect';
import {willOpenKeyboard} from '../utils/keyboard';
Expand Down Expand Up @@ -46,7 +45,7 @@ export function usePreventScroll(options: PreventScrollOptions = {}): void {

preventScrollCount++;
if (preventScrollCount === 1) {
if (isIOS()) {
if (isIOS() && isWebKit()) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regressed in #1514

restore = preventScrollMobileSafari();
} else {
restore = preventScrollStandard();
Expand Down Expand Up @@ -197,18 +196,22 @@ function preventScrollMobileSafari() {

// Override programmatic focus to scroll into view without scrolling the whole page.
let focus = HTMLElement.prototype.focus;
HTMLElement.prototype.focus = function (opts) {
// Track whether the keyboard was already visible before.
let activeElement = getActiveElement();
let wasKeyboardVisible = activeElement != null && willOpenKeyboard(activeElement);

// Focus the element without scrolling the page.
focus.call(this, {...opts, preventScroll: true});

if (!opts || !opts.preventScroll) {
scrollIntoViewWhenReady(this, wasKeyboardVisible);
Reflect.defineProperty(HTMLElement.prototype, 'focus', {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as #10041

configurable: true,
writable: true,
value: function (opts?: FocusOptions) {
// Track whether the keyboard was already visible before.
let activeElement = getActiveElement();
let wasKeyboardVisible = activeElement != null && willOpenKeyboard(activeElement);

// Focus the element without scrolling the page.
focus.call(this, {...opts, preventScroll: true});

if (!opts || !opts.preventScroll) {
scrollIntoViewWhenReady(this, wasKeyboardVisible);
}
}
};
});

let removeEvents = chain(
addEvent(document, 'touchstart', onTouchStart, {passive: false, capture: true}),
Expand All @@ -220,7 +223,11 @@ function preventScrollMobileSafari() {
restoreOverflow();
removeEvents();
style.remove();
HTMLElement.prototype.focus = focus;
Reflect.defineProperty(HTMLElement.prototype, 'focus', {
configurable: true,
writable: true,
value: focus
});
};
}

Expand Down