Skip to content
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
30 changes: 28 additions & 2 deletions packages/react/src/internal/__tests__/usePresence-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ const TestComponent = () => {

const createDeferred = () => {
let resolve;
const promise = new Promise((res) => {
let reject;
const promise = new Promise((res, rej) => {
resolve = res;
reject = rej;
});

return { promise, resolve };
return { promise, resolve, reject };
};

const mockPresenceAnimations = () => {
Expand Down Expand Up @@ -130,4 +132,28 @@ describe('usePresence', () => {
cleanup();
}
});

it('should finish exiting without an unhandled rejection when the exit animation is canceled', async () => {
const { deferredAnimations, cleanup } = mockPresenceAnimations();

try {
render(<TestComponent />);

await userEvent.click(screen.getByRole('button', { name: 'Open' }));
await userEvent.click(screen.getByRole('button', { name: 'Close' }));
await waitFor(() => expect(deferredAnimations.length).toBe(1));

// Browsers reject `Animation.finished` with an `AbortError` when an
// animation is canceled, e.g. when the element unmounts mid-exit.
deferredAnimations[0].reject(
new DOMException('The animation was aborted.', 'AbortError')
);

await waitFor(() =>
expect(screen.queryByRole('dialog')).not.toBeInTheDocument()
);
} finally {
cleanup();
}
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* Copyright IBM Corp. 2026
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

import { render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import React, { useRef, useState } from 'react';
import { useSidePanelPresence } from '../useSidePanelPresence';

const EXIT_ANIMATION = 'cds--side-panel-exit';

const TestComponent = () => {
const ref = useRef(null);
const [open, setOpen] = useState(true);
const { shouldRender } = useSidePanelPresence(open, ref, EXIT_ANIMATION);

return shouldRender ? (
<div ref={ref} role="dialog">
<button onClick={() => setOpen(false)}>Close</button>
</div>
) : null;
};

describe('useSidePanelPresence', () => {
let originalGetAnimations;
let originalCSSAnimation;
let exitAnimation;

beforeEach(() => {
originalGetAnimations = Element.prototype.getAnimations;
originalCSSAnimation = global.CSSAnimation;

function MockCSSAnimation(animationName) {
this.animationName = animationName;
this.finished = new Promise((resolve, reject) => {
exitAnimation = { resolve, reject };
});
}

global.CSSAnimation = MockCSSAnimation;
Element.prototype.getAnimations = jest.fn(() => [
new MockCSSAnimation(EXIT_ANIMATION),
]);
});

afterEach(() => {
if (originalGetAnimations) {
Element.prototype.getAnimations = originalGetAnimations;
} else {
delete Element.prototype.getAnimations;
}

if (originalCSSAnimation) {
global.CSSAnimation = originalCSSAnimation;
} else {
delete global.CSSAnimation;
}
});

it('should keep the panel mounted until the exit animation finishes', async () => {
render(<TestComponent />);

await userEvent.click(screen.getByRole('button', { name: 'Close' }));
expect(screen.getByRole('dialog')).toBeInTheDocument();

exitAnimation.resolve();

await waitFor(() =>
expect(screen.queryByRole('dialog')).not.toBeInTheDocument()
);
});

it('should unmount without an unhandled rejection when the exit animation is canceled', async () => {
render(<TestComponent />);

await userEvent.click(screen.getByRole('button', { name: 'Close' }));
expect(screen.getByRole('dialog')).toBeInTheDocument();

// Browsers reject `Animation.finished` with an `AbortError` when an
// animation is canceled, e.g. when the panel unmounts mid-exit.
exitAnimation.reject(
new DOMException('The animation was aborted.', 'AbortError')
);

await waitFor(() =>
expect(screen.queryByRole('dialog')).not.toBeInTheDocument()
);
});
});
12 changes: 8 additions & 4 deletions packages/react/src/internal/usePresence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,16 @@ export const usePresence = (

let cancelled = false;

Promise.all(animations.map((animation) => animation.finished)).finally(
() => {
Promise.all(animations.map((animation) => animation.finished))
.finally(() => {
if (cancelled) return;
setExitState('finished');
}
);
})
.catch(() => {
// `finished` rejects with an `AbortError` when an animation is
// canceled, e.g. when the element unmounts mid-exit. The exit is over
// either way, so the rejection is expected and safe to ignore.
});

return () => {
cancelled = true;
Expand Down
12 changes: 9 additions & 3 deletions packages/react/src/internal/useSidePanelPresence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,15 @@ export const useSidePanelPresence = (
}

let cancelled = false;
Promise.all(animations.map((a) => a.finished)).finally(() => {
if (!cancelled) setShouldRender(false);
});
Promise.all(animations.map((a) => a.finished))
.finally(() => {
if (!cancelled) setShouldRender(false);
})
.catch(() => {
// `finished` rejects with an `AbortError` when an animation is
// canceled, e.g. when the panel unmounts mid-exit. The exit is over
// either way, so the rejection is expected and safe to ignore.
});

return () => {
cancelled = true;
Expand Down
Loading