-
Notifications
You must be signed in to change notification settings - Fork 110
fix(ui-modal): skip Modal.Body tab stop when it has focusable children #2584
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,7 +26,7 @@ import { Component } from 'react' | |
|
|
||
| import { View } from '@instructure/ui-view/v11_6' | ||
| import { omitProps } from '@instructure/ui-react-utils' | ||
| import { getCSSStyleDeclaration } from '@instructure/ui-dom-utils' | ||
| import { getCSSStyleDeclaration, findTabbable } from '@instructure/ui-dom-utils' | ||
|
|
||
| import { withStyle } from '@instructure/emotion' | ||
| import generateStyle from './styles' | ||
|
|
@@ -55,6 +55,8 @@ class ModalBody extends Component<ModalBodyProps> { | |
| } | ||
|
|
||
| ref: UIElement | null = null | ||
| private resizeObserver?: ResizeObserver | ||
| private mutationObserver?: MutationObserver | ||
|
|
||
| handleRef = (el: UIElement | null) => { | ||
| const { elementRef } = this.props | ||
|
|
@@ -86,12 +88,39 @@ class ModalBody extends Component<ModalBodyProps> { | |
| if (isFirefox) { | ||
| this.setState({ isFirefox }) | ||
| } | ||
|
|
||
| const finalRef = this.getFinalRef(this.ref) | ||
| if (finalRef && typeof ResizeObserver !== 'undefined') { | ||
| this.resizeObserver = new ResizeObserver(() => this.forceUpdate()) | ||
| this.resizeObserver.observe(finalRef) | ||
| this.mutationObserver = new MutationObserver(() => this.forceUpdate()) | ||
| this.mutationObserver.observe(finalRef, { | ||
| childList: true, | ||
| subtree: true, | ||
| attributes: true, | ||
| attributeFilter: [ | ||
| 'disabled', | ||
| 'tabindex', | ||
| 'hidden', | ||
| 'href', | ||
| 'contenteditable', | ||
| 'type', | ||
| 'class', | ||
| 'style' | ||
| ] | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. based on the set of attributes that
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you put this as a comment to |
||
| }) | ||
| } | ||
| } | ||
|
|
||
| componentDidUpdate() { | ||
| this.props.makeStyles?.() | ||
| } | ||
|
|
||
| componentWillUnmount() { | ||
| this.resizeObserver?.disconnect() | ||
| this.mutationObserver?.disconnect() | ||
| } | ||
|
|
||
| getFinalRef(el: UIElement): Element | undefined { | ||
| if (!el) { | ||
| return undefined | ||
|
|
@@ -124,6 +153,8 @@ class ModalBody extends Component<ModalBodyProps> { | |
| (finalRef.scrollHeight ?? 0) - | ||
| (finalRef.getBoundingClientRect()?.height ?? 0) | ||
| ) > 1 | ||
| const hasTabbableChildren = !!finalRef && findTabbable(finalRef).length > 0 | ||
| const needsTabIndex = hasScrollbar && !hasTabbableChildren | ||
| return ( | ||
| <ModalContext.Consumer> | ||
| {(value) => ( | ||
|
|
@@ -137,8 +168,9 @@ class ModalBody extends Component<ModalBodyProps> { | |
| as={as} | ||
| css={this.props.styles?.modalBody} | ||
| padding={padding} | ||
| // check if there is a scrollbar, if so, the element has to be tabbable | ||
| {...(hasScrollbar | ||
| // check if there is a scrollbar and no focusable children, if so, the | ||
| // element has to be tabbable | ||
| {...(needsTabIndex | ||
| ? { | ||
| tabIndex: 0, | ||
| 'aria-label': value.bodyScrollAriaLabel | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was needed to handle the the case when the modal body content changes dinamically (a scrollbar or a focusable element appears or disappears)