Skip to content

Commit 23478d8

Browse files
author
Eric Olkowski
committed
Feedback from Austin
1 parent fda6f24 commit 23478d8

File tree

3 files changed

+32
-28
lines changed

3 files changed

+32
-28
lines changed

packages/react-core/src/components/Alert/AlertGroupInline.tsx

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,24 @@ export const AlertGroupInline: React.FunctionComponent<AlertGroupProps> = ({
1919
const updateTransitionEnd = (onTransitionEnd: () => void) => {
2020
setHandleTransitionEnd(() => onTransitionEnd);
2121
};
22+
23+
const onTransitionEnd = (event: React.TransitionEvent<HTMLLIElement>) => {
24+
if (!hasAnimations) {
25+
return;
26+
}
27+
28+
const prefersReducedMotion = !window.matchMedia('(prefers-reduced-motion: no-preference)')?.matches;
29+
if (
30+
// If a user has no motion preference, we want to target the grid template rows transition
31+
// so that the onClose is called after the "slide up" animation of other alerts finishes.
32+
// If they have motion preference, we don't need to check for a specific transition since only opacity should fire.
33+
(prefersReducedMotion || (!prefersReducedMotion && event.propertyName === 'grid-template-rows')) &&
34+
(event.target as HTMLElement).className.includes(styles.modifiers.offstageRight)
35+
) {
36+
handleTransitionEnd();
37+
}
38+
};
39+
2240
return (
2341
<AlertGroupContext.Provider value={{ hasAnimations, updateTransitionEnd }}>
2442
<ul
@@ -30,22 +48,7 @@ export const AlertGroupInline: React.FunctionComponent<AlertGroupProps> = ({
3048
>
3149
{React.Children.toArray(children).map((alert, index) => (
3250
<li
33-
onTransitionEnd={(event: React.TransitionEvent<HTMLLIElement>) => {
34-
if (!hasAnimations) {
35-
return;
36-
}
37-
38-
const prefersReducedMotion = !window.matchMedia('(prefers-reduced-motion: no-preference)')?.matches;
39-
if (
40-
// If a user has no motion preference, we want to target the grid template rows transition
41-
// so that the onClose is called after the "slide up" animation of other alerts finishes.
42-
// If they have motion preference, we don't need to check for a specific transition since only opacity should fire.
43-
(prefersReducedMotion || (!prefersReducedMotion && event.propertyName === 'grid-template-rows')) &&
44-
(event.target as HTMLElement).className.includes(styles.modifiers.offstageRight)
45-
) {
46-
handleTransitionEnd();
47-
}
48-
}}
51+
onTransitionEnd={onTransitionEnd}
4952
className={css(styles.alertGroupItem, hasAnimations && styles.modifiers.offstageTop)}
5053
key={
5154
(alert as React.ReactElement<AlertProps>).props?.id ||

packages/react-core/src/components/Alert/__tests__/AlertActionCloseButton.test.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ test('Does not call the callback provided via onClose when it is not clicked', (
6060
});
6161

6262
test('Calls the callback provided via onClose when clicked', async () => {
63-
const onCloseMock = jest.fn();
6463
const user = userEvent.setup();
64+
const onCloseMock = jest.fn();
6565

6666
render(
6767
<AlertContext.Provider value={{ title: 'title', variantLabel: 'variantLabel' }}>
@@ -75,8 +75,8 @@ test('Calls the callback provided via onClose when clicked', async () => {
7575
});
7676

7777
test('Calls updateTransitionEnd with onClose when animations are enabled', async () => {
78-
const onClose = jest.fn();
7978
const user = userEvent.setup();
79+
const onClose = jest.fn();
8080
const updateMock = jest.fn();
8181
render(
8282
<AlertGroupContext.Provider value={{ hasAnimations: true, updateTransitionEnd: updateMock }}>
@@ -86,6 +86,7 @@ test('Calls updateTransitionEnd with onClose when animations are enabled', async
8686
</AlertGroupContext.Provider>
8787
);
8888

89+
expect(updateMock).not.toHaveBeenCalled();
8990
await user.click(screen.getByRole('button'));
9091
expect(updateMock).toHaveBeenCalledWith(onClose);
9192
expect(updateMock).toHaveBeenCalledTimes(1);

packages/react-core/src/components/Alert/__tests__/AlertGroup.test.tsx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ test('Calls the callback set by updateTransitionEnd when transition ends and ani
7373
matches: false,
7474
media: query,
7575
onchange: null,
76-
addListener: jest.fn(), // deprecated
77-
removeListener: jest.fn(), // deprecated
78-
addEventListener: jest.fn(),
79-
removeEventListener: jest.fn(),
80-
dispatchEvent: jest.fn()
76+
addListener: () => {}, // deprecated
77+
removeListener: () => {}, // deprecated
78+
addEventListener: () => {},
79+
removeEventListener: () => {},
80+
dispatchEvent: () => true
8181
});
8282
const mockCallback = jest.fn();
8383
const user = userEvent.setup();
@@ -103,11 +103,11 @@ test('Does not call the callback set by updateTransitionEnd when transition ends
103103
matches: false,
104104
media: query,
105105
onchange: null,
106-
addListener: jest.fn(), // deprecated
107-
removeListener: jest.fn(), // deprecated
108-
addEventListener: jest.fn(),
109-
removeEventListener: jest.fn(),
110-
dispatchEvent: jest.fn()
106+
addListener: () => {}, // deprecated
107+
removeListener: () => {}, // deprecated
108+
addEventListener: () => {},
109+
removeEventListener: () => {},
110+
dispatchEvent: () => true
111111
});
112112
const mockCallback = jest.fn();
113113
const user = userEvent.setup();

0 commit comments

Comments
 (0)