Skip to content

Commit 4b6692b

Browse files
authored
Merge pull request #10935 from marmelab/fix-custom-notification-requires-forward-ref
Fix passing an element to `notify` requires to wrap it in `forwardRef`
2 parents 4c2c2b3 + 0c716bb commit 4b6692b

File tree

2 files changed

+11
-18
lines changed

2 files changed

+11
-18
lines changed

packages/ra-ui-materialui/src/layout/Notification.stories.tsx

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,7 @@ import {
66
useNotify,
77
useCloseNotification,
88
} from 'ra-core';
9-
import {
10-
Alert,
11-
Button,
12-
SnackbarContent,
13-
SnackbarContentProps,
14-
Stack,
15-
} from '@mui/material';
9+
import { Alert, Button, SnackbarContent, Stack } from '@mui/material';
1610

1711
import { Notification } from './Notification';
1812

@@ -214,11 +208,7 @@ export const ConsecutiveUndoable = ({
214208
</CoreAdminContext>
215209
);
216210

217-
// forwardRef is required for Snackbar to work (transitions) in React 18
218-
const CustomNotificationWithActionContent = React.forwardRef<
219-
HTMLDivElement,
220-
SnackbarContentProps
221-
>((props, ref) => {
211+
const CustomNotificationWithActionContent = props => {
222212
const closeNotification = useCloseNotification();
223213
const handleClick = () => {
224214
console.log('Custom action');
@@ -229,15 +219,16 @@ const CustomNotificationWithActionContent = React.forwardRef<
229219
message="Applied automatic changes"
230220
action={<Button onClick={handleClick}>Cancel</Button>}
231221
{...props}
232-
ref={ref}
233222
/>
234223
);
235-
});
224+
};
236225

237226
const CustomNotificationElementWithAction = () => {
238227
const notify = useNotify();
239228
React.useEffect(() => {
240-
notify(<CustomNotificationWithActionContent />);
229+
notify(<CustomNotificationWithActionContent />, {
230+
autoHideDuration: null,
231+
});
241232
}, [notify]);
242233
return null;
243234
};

packages/ra-ui-materialui/src/layout/Notification.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,11 @@ export const Notification = (inProps: NotificationProps) => {
180180
>
181181
{message &&
182182
typeof message !== 'string' &&
183-
React.isValidElement(message)
184-
? message
185-
: undefined}
183+
React.isValidElement(message) ? (
184+
// Wrap the element in a div to avoid forcing our users to wrap their component with forwardRef
185+
// which is required for animating the Snackbar
186+
<div>{message}</div>
187+
) : undefined}
186188
</StyledSnackbar>
187189
</CloseNotificationContext.Provider>
188190
);

0 commit comments

Comments
 (0)