Skip to content
Merged
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
7 changes: 6 additions & 1 deletion packages/@react-aria/overlays/src/ariaHideOutside.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,12 @@ export function ariaHideOutside(targets: Element[], options?: AriaHideOutsideOpt

// If the parent element of the added nodes is not within one of the targets,
// and not already inside a hidden node, hide all of the new children.
if (![...visibleNodes, ...hiddenNodes].some(node => node.contains(change.target))) {
if (
change.target.isConnected &&
![...visibleNodes, ...hiddenNodes].some((node) =>
node.contains(change.target)
)
) {
for (let node of change.addedNodes) {
if (
(node instanceof HTMLElement || node instanceof SVGElement) &&
Expand Down
72 changes: 71 additions & 1 deletion packages/@react-aria/overlays/test/ariaHideOutside.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import {act, render, waitFor} from '@react-spectrum/test-utils-internal';
import {ariaHideOutside} from '../src';
import React, {useState} from 'react';
import React, {useRef, useState} from 'react';

describe('ariaHideOutside', function () {
it('should hide everything except the provided element [button]', function () {
Expand Down Expand Up @@ -275,6 +275,76 @@ describe('ariaHideOutside', function () {
expect(() => getByTestId('test')).not.toThrow();
});

it('should handle when a new element is added and then reparented', async function () {

let Test = () => {
const ref = useRef(null);
const mutate = () => {
let parent = document.createElement('ul');
let child = document.createElement('li');
ref.current.append(parent);
parent.appendChild(child);
parent.remove(); // this results in a mutation record for a disconnected ul with a connected li (through the new ul parent) in `addedNodes`
let newParent = document.createElement('ul');
newParent.appendChild(child);
ref.current.append(newParent);
};

return (
<>
<div data-testid="test" ref={ref}>
<button onClick={mutate}>Mutate</button>
</div>
</>
);
};

let {queryByRole, getAllByRole, getByTestId} = render(<Test />);

ariaHideOutside([getByTestId('test')]);

queryByRole('button').click();
await Promise.resolve(); // Wait for mutation observer tick

expect(getAllByRole('listitem')).toHaveLength(1);
});

it('should handle when a new element is added and then reparented to a hidden container', async function () {

let Test = () => {
const ref = useRef(null);
const mutate = () => {
let parent = document.createElement('ul');
let child = document.createElement('li');
ref.current.append(parent);
parent.appendChild(child);
parent.remove(); // this results in a mutation record for a disconnected ul with a connected li (through the new ul parent) in `addedNodes`
let newParent = document.createElement('ul');
newParent.appendChild(child);
ref.current.append(newParent);
};

return (
<>
<div data-testid="test">
<button onClick={mutate}>Mutate</button>
</div>
<div data-testid="sibling" ref={ref} />
</>
);
};

let {queryByRole, queryAllByRole, getByTestId} = render(<Test />);

ariaHideOutside([getByTestId('test')]);

queryByRole('button').click();
await Promise.resolve(); // Wait for mutation observer tick

expect(queryAllByRole('listitem')).toHaveLength(0);
});


it('work when called multiple times', function () {
let {getByRole, getAllByRole} = render(
<>
Expand Down