Skip to content

Commit a75738f

Browse files
author
Michael Jordan
authored
fix(#4313): ariaHideOutside/aria-modal-polyfill: use inert instead of aria-hidden where supported (#4773)
Speed up ariaHideOutside on large pages
1 parent 003f856 commit a75738f

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

packages/@react-aria/aria-modal-polyfill/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"dependencies": {
2525
"@react-types/shared": "^3.18.1",
2626
"@swc/helpers": "^0.5.0",
27-
"aria-hidden": "^1.1.1"
27+
"aria-hidden": "^1.2.3"
2828
},
2929
"peerDependencies": {
3030
"react": "^16.8.0 || ^17.0.0-rc.1 || ^18.0.0"

packages/@react-aria/aria-modal-polyfill/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* governing permissions and limitations under the License.
1111
*/
1212

13-
import {hideOthers} from 'aria-hidden';
13+
import {suppressOthers} from 'aria-hidden';
1414

1515
type Revert = () => void;
1616

@@ -49,7 +49,7 @@ export function watchModals(selector:string = 'body', {document = currentDocumen
4949
let modal = addNode.querySelector('[aria-modal="true"], [data-ismodal="true"]') as HTMLElement;
5050
undo?.();
5151
let others = [modal, ... liveAnnouncer ? [liveAnnouncer as HTMLElement] : []];
52-
undo = hideOthers(others);
52+
undo = suppressOthers(others);
5353
}
5454
} else if (mutation.type === 'childList' && mutation.removedNodes.length > 0) {
5555
let removedNodes = Array.from(mutation.removedNodes);
@@ -60,7 +60,7 @@ export function watchModals(selector:string = 'body', {document = currentDocumen
6060
if (modalContainers.length > 0) {
6161
let modal = modalContainers[modalContainers.length - 1].querySelector('[aria-modal="true"], [data-ismodal="true"]') as HTMLElement;
6262
let others = [modal, ... liveAnnouncer ? [liveAnnouncer as HTMLElement] : []];
63-
undo = hideOthers(others);
63+
undo = suppressOthers(others);
6464
} else {
6565
undo = undefined;
6666
}

packages/@react-aria/overlays/src/ariaHideOutside.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
let refCountMap = new WeakMap<Element, number>();
1616
let observerStack = [];
1717

18+
const supportsInert = typeof HTMLElement !== 'undefined' && Object.prototype.hasOwnProperty.call(HTMLElement.prototype, 'inert');
19+
20+
interface InertElement extends HTMLElement {
21+
inert: boolean
22+
}
23+
1824
/**
1925
* Hides all elements in the DOM outside the given targets from screen readers using aria-hidden,
2026
* and returns a function to revert these changes. In addition, changes to the DOM are watched
@@ -81,11 +87,14 @@ export function ariaHideOutside(targets: Element[], root = document.body) {
8187

8288
// If already aria-hidden, and the ref count is zero, then this element
8389
// was already hidden and there's nothing for us to do.
84-
if (node.getAttribute('aria-hidden') === 'true' && refCount === 0) {
90+
let alreadyHidden = supportsInert ? (node as InertElement).inert : node.getAttribute('aria-hidden') === 'true';
91+
if (alreadyHidden && refCount === 0) {
8592
return;
8693
}
8794

8895
if (refCount === 0) {
96+
supportsInert ?
97+
((node as InertElement).inert = true) :
8998
node.setAttribute('aria-hidden', 'true');
9099
}
91100

@@ -150,6 +159,8 @@ export function ariaHideOutside(targets: Element[], root = document.body) {
150159
for (let node of hiddenNodes) {
151160
let count = refCountMap.get(node);
152161
if (count === 1) {
162+
supportsInert ?
163+
((node as InertElement).inert = false) :
153164
node.removeAttribute('aria-hidden');
154165
refCountMap.delete(node);
155166
} else {

yarn.lock

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5831,12 +5831,12 @@ argparse@^2.0.1:
58315831
resolved "https://registry.yarnpkg.com/argparse/-/argparse-2.0.1.tgz#246f50f3ca78a3240f6c997e8a9bd1eac49e4b38"
58325832
integrity sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==
58335833

5834-
aria-hidden@^1.1.1:
5835-
version "1.1.1"
5836-
resolved "https://registry.yarnpkg.com/aria-hidden/-/aria-hidden-1.1.1.tgz#0c356026d3f65e2bd487a3adb73f0c586be2c37e"
5837-
integrity sha512-M7zYxCcOQPOaxGHoMTKUFD2UNcVFTp9ycrdStLcTPLf8zgTXC3+YcGe+UuzSh5X1BX/0/PtS8xTNy4xyH/6xtw==
5834+
aria-hidden@^1.2.3:
5835+
version "1.2.3"
5836+
resolved "https://registry.yarnpkg.com/aria-hidden/-/aria-hidden-1.2.3.tgz#14aeb7fb692bbb72d69bebfa47279c1fd725e954"
5837+
integrity sha512-xcLxITLe2HYa1cnYnwCjkOO1PqUHQpozB8x9AR0OgWN2woOBi5kSDVxKfd0b7sb1hw5qFeJhXm9H1nu3xSfLeQ==
58385838
dependencies:
5839-
tslib "^1.0.0"
5839+
tslib "^2.0.0"
58405840

58415841
aria-query@^5.0.0:
58425842
version "5.0.0"
@@ -21894,7 +21894,7 @@ [email protected], tslib@^2.0.0, tslib@^2.0.1, tslib@^2.1.0, tslib@^2.4.0:
2189421894
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.4.0.tgz#7cecaa7f073ce680a05847aa77be941098f36dc3"
2189521895
integrity sha512-d6xOpEDfsi2CZVlPQzGeux8XMwLT9hssAsaPYExaQMuYskwb+x1x7J371tWlbBdWHroy99KnVB6qIkUbs5X3UQ==
2189621896

21897-
tslib@^1.0.0, tslib@^1.10.0, tslib@^1.8.1, tslib@^1.9.0:
21897+
tslib@^1.10.0, tslib@^1.8.1, tslib@^1.9.0:
2189821898
version "1.13.0"
2189921899
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.13.0.tgz#c881e13cc7015894ed914862d276436fa9a47043"
2190021900
integrity sha512-i/6DQjL8Xf3be4K/E6Wgpekn5Qasl1usyw++dAA35Ue5orEn65VIxOA+YvNNl9HV3qv70T7CNwjODHZrLwvd1Q==

0 commit comments

Comments
 (0)