Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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,8 @@
*/

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

import {useLayoutEffect} from 'react-aria/private/utils/useLayoutEffect';
import {willOpenKeyboard} from 'react-aria/private/utils/keyboard';
import {
ClassNameOrFunction,
ContextValue,
Expand All @@ -34,11 +35,21 @@ 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 {useObjectRef} from 'react-aria/useObjectRef';
import {useViewportSize} from 'react-aria/private/utils/useViewportSize';
import {getActiveElement} from 'react-aria/private/utils/shadowdom/DOMFunctions';
import {useVisuallyHidden} from 'react-aria/VisuallyHidden';

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 thread
nwidynski marked this conversation as resolved.
Outdated
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()) {

@nwidynski nwidynski May 25, 2026

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. Not sure if lucky accident though? Will need testing.

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', {
Comment thread
nwidynski marked this conversation as resolved.
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