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: prevent scrolling when focus trap cycles back into the modal #82

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
12 changes: 7 additions & 5 deletions src/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import activeElement from 'dom-helpers/activeElement';
import contains from 'dom-helpers/contains';
import canUseDOM from 'dom-helpers/canUseDOM';
import listen from 'dom-helpers/listen';

import {
useState,
useRef,
Expand Down Expand Up @@ -299,9 +300,7 @@ const Modal: React.ForwardRefExoticComponent<
removeFocusListenerRef.current = listen(
document as any,
'focus',
// the timeout is necessary b/c this will run before the new modal is mounted
Copy link
Collaborator

Choose a reason for hiding this comment

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

Looks like this was necessary to resolve the TypeError: Converting circular structure to JSON error in the tests

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah I'm confused why tho. It doesn't seem necessary in real life. Gotta dig in a bit

// and so steals focus from it
() => setTimeout(handleEnforceFocus),
handleEnforceFocus,
true,
);

Expand Down Expand Up @@ -363,7 +362,7 @@ const Modal: React.ForwardRefExoticComponent<

// --------------------------------

const handleEnforceFocus = useEventCallback(() => {
const handleEnforceFocus = useEventCallback((event: FocusEvent) => {
if (!enforceFocus || !isMounted() || !modal.isTopModal()) {
return;
}
Expand All @@ -375,7 +374,9 @@ const Modal: React.ForwardRefExoticComponent<
currentActiveElement &&
!contains(modal.dialog, currentActiveElement)
) {
event.preventDefault();
modal.dialog.focus();
manager.maybeResetScrollPosition();
}
});

Expand Down Expand Up @@ -417,7 +418,8 @@ const Modal: React.ForwardRefExoticComponent<
role,
ref: modal.setDialogRef,
// apparently only works on the dialog role element
'aria-modal': role === 'dialog' ? true : undefined,
'aria-modal':
role === 'dialog' || role === 'alertdialog' ? true : undefined,
...rest,
style,
className,
Expand Down
26 changes: 26 additions & 0 deletions src/ModalManager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import css from 'dom-helpers/css';
import getSetScrollTop from 'dom-helpers/scrollTop';
import getSetScrollLeft from 'dom-helpers/scrollLeft';
import getScrollParent from 'dom-helpers/scrollParent';
import isDocument from 'dom-helpers/isDocument';
import { dataAttr } from './DataKey';
import getBodyScrollbarWidth from './getScrollbarWidth';

Expand Down Expand Up @@ -51,6 +55,11 @@ class ModalManager {
return getBodyScrollbarWidth(this.ownerDocument);
}

protected getScollingElement() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Typo in "Scroll"

const element = getScrollParent(this.getElement());
return isDocument(element) ? element.defaultView! : element;
Copy link
Member Author

Choose a reason for hiding this comment

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

maybe this is over complicating things? people shouldn't actually be rendering modals into not-the-body

}

getElement() {
return (this.ownerDocument || document).body;
}
Expand Down Expand Up @@ -113,8 +122,12 @@ class ModalManager {
return modalIdx;
}

const scrollElement = this.getScollingElement() as Element;

this.state = {
scrollBarWidth: this.getScrollbarWidth(),
scrollTop: getSetScrollTop(scrollElement),
scrollLeft: getSetScrollLeft(scrollElement),
style: {},
};

Expand All @@ -125,6 +138,19 @@ class ModalManager {
return modalIdx;
}

maybeResetScrollPosition() {
const scrollElement = this.getScollingElement() as Element;

const { scrollTop, scrollLeft } = this.state;

if (getSetScrollTop(scrollElement) !== scrollTop) {
getSetScrollTop(scrollElement, scrollTop);
}
if (getSetScrollLeft(scrollElement) !== scrollLeft) {
getSetScrollLeft(scrollElement, scrollLeft);
}
}

remove(modal: ModalInstance) {
const modalIdx = this.modals.indexOf(modal);

Expand Down