-
Notifications
You must be signed in to change notification settings - Fork 779
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
feat: support the dialog element #3902
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,149 @@ | ||
import memoize from '../../core/utils/memoize'; | ||
import { querySelectorAllFilter, contains } from '../../core/utils'; | ||
import isVisibleOnScreen from './is-visible-on-screen'; | ||
import createGrid from './create-grid'; | ||
|
||
/** | ||
* Determines if an element is inside an inert subtree. | ||
* @param {VirtualNode} vNode | ||
* @param {Boolean} [options.skipAncestors] If the ancestor tree should not be used | ||
* @return {Boolean} The element's inert state | ||
*/ | ||
export default function isInert(vNode, { skipAncestors } = {}) { | ||
export default function isInert(vNode, { skipAncestors, isAncestor } = {}) { | ||
if (skipAncestors) { | ||
return isInertSelf(vNode); | ||
return isInertSelf(vNode, isAncestor); | ||
} | ||
|
||
return isInertAncestors(vNode); | ||
return isInertAncestors(vNode, isAncestor); | ||
} | ||
|
||
/** | ||
* Check the element for inert | ||
*/ | ||
const isInertSelf = memoize(function isInertSelfMemoized(vNode) { | ||
return vNode.hasAttr('inert'); | ||
const isInertSelf = memoize(function isInertSelfMemoized(vNode, isAncestor) { | ||
if (vNode.hasAttr('inert')) { | ||
return true; | ||
} | ||
|
||
if (!isAncestor && vNode.actualNode) { | ||
// elements outside of an opened modal | ||
// dialog are treated as inert by the | ||
// browser | ||
const modalDialog = getModalDialog(); | ||
if (modalDialog && !contains(modalDialog, vNode)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
}); | ||
|
||
/** | ||
* Check the element and ancestors for inert | ||
*/ | ||
const isInertAncestors = memoize(function isInertAncestorsMemoized(vNode) { | ||
if (isInertSelf(vNode)) { | ||
const isInertAncestors = memoize(function isInertAncestorsMemoized( | ||
vNode, | ||
isAncestor | ||
) { | ||
if (isInertSelf(vNode, isAncestor)) { | ||
return true; | ||
} | ||
|
||
if (!vNode.parent) { | ||
return false; | ||
} | ||
|
||
return isInertAncestors(vNode.parent); | ||
return isInertAncestors(vNode.parent, true); | ||
}); | ||
|
||
/** | ||
straker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* Determine if a dialog element is opened as a modal. Currently there are no APIs to determine this so we'll use a bit of a hacky solution that has known issues. | ||
* This can tell us that a dialog element is open but it cannot tell us which one is the top layer, nor which one is visually on top. Nested dialogs that are opened using both `.show` and`.showModal` can cause issues as well. | ||
* @see https://github.com/dequelabs/axe-core/issues/3463 | ||
* @return {VirtualNode|Null} The modal dialog virtual node or null if none are found | ||
*/ | ||
const getModalDialog = memoize(function getModalDialogMemoized() { | ||
straker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// this is here for tests so we don't have | ||
// to set up the virtual tree when code | ||
// isn't testing this bit | ||
if (!axe._tree) { | ||
return; | ||
} | ||
|
||
const dialogs = querySelectorAllFilter( | ||
// TODO: es-module-_tree | ||
axe._tree[0], | ||
'dialog[open]', | ||
isVisibleOnScreen | ||
); | ||
|
||
if (!dialogs.length) { | ||
return; | ||
} | ||
|
||
// for Chrome and Firefox, look to see if | ||
// elementsFromPoint returns the dialog | ||
// when checking outside its bounds | ||
let modalDialog = dialogs.find(dialog => { | ||
const rect = dialog.boundingClientRect; | ||
const stack = document.elementsFromPoint(rect.left - 10, rect.top - 10); | ||
|
||
return stack.includes(dialog.actualNode); | ||
}); | ||
|
||
// fallback for Safari, look at the grid to | ||
// find a node to check as elementsFromPoint | ||
// does not return inert nodes | ||
if (!modalDialog) { | ||
modalDialog = dialogs.find(dialog => { | ||
const vNode = getNodeFromGrid(dialog); | ||
if (!vNode) { | ||
return false; | ||
} | ||
|
||
const rect = vNode.boundingClientRect; | ||
const stack = document.elementsFromPoint(rect.left + 1, rect.top + 1); | ||
straker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return !stack.includes(vNode.actualNode); | ||
}); | ||
} | ||
|
||
return modalDialog; | ||
}); | ||
|
||
/** | ||
* Find the first non-html from the grid to use as a test for elementsFromPoint | ||
* @return {VirtualNode|Null} | ||
*/ | ||
function getNodeFromGrid(dialog) { | ||
createGrid(); | ||
// TODO: es-module-_tree | ||
const grid = axe._tree[0]._grid; | ||
|
||
for (let row = 0; row < grid.cells.length; row++) { | ||
const cols = grid.cells[row]; | ||
if (!cols) { | ||
continue; | ||
} | ||
|
||
for (let col = 0; col < cols.length; col++) { | ||
const cells = cols[col]; | ||
if (!cells) { | ||
continue; | ||
} | ||
|
||
const vNode = cells.find(virtualNode => { | ||
return ( | ||
// html is always returned from elementsFromPoint | ||
virtualNode.props.nodeName !== 'html' && | ||
virtualNode !== dialog && | ||
virtualNode.getComputedStylePropertyValue('pointer-events') !== 'none' | ||
); | ||
}); | ||
|
||
if (vNode) { | ||
return vNode; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 does tell me that if we have nested open modals, we should grab the outer-most one. The way you implemented that should work though, since querySelectorAll is in that order already.