Skip to content

Commit 0237a14

Browse files
authored
fix(Toast): fix fade animation (react-bootstrap#6004)
1 parent 91e2bcd commit 0237a14

File tree

4 files changed

+37
-14
lines changed

4 files changed

+37
-14
lines changed

src/Fade.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export interface FadeProps extends TransitionCallbacks {
2020
appear?: boolean;
2121
timeout?: number;
2222
children: React.ReactElement;
23+
transitionClasses?: Record<string, string>;
2324
}
2425

2526
const propTypes = {
@@ -80,6 +81,12 @@ const propTypes = {
8081
* You must provide a single JSX child element to this component and that element cannot be a \<React.Fragment\>
8182
*/
8283
children: PropTypes.element.isRequired,
84+
85+
/**
86+
* Applies additional specified classes during the transition. Takes an object
87+
* where the keys correspond to the Transition status
88+
*/
89+
transitionClasses: PropTypes.object,
8390
};
8491

8592
const defaultProps = {
@@ -96,7 +103,7 @@ const fadeStyles = {
96103
};
97104

98105
const Fade = React.forwardRef<Transition<any>, FadeProps>(
99-
({ className, children, ...props }, ref) => {
106+
({ className, children, transitionClasses = {}, ...props }, ref) => {
100107
const handleEnter = useCallback(
101108
(node, isAppearing) => {
102109
triggerBrowserReflow(node);
@@ -121,6 +128,7 @@ const Fade = React.forwardRef<Transition<any>, FadeProps>(
121128
className,
122129
children.props.className,
123130
fadeStyles[status],
131+
transitionClasses[status],
124132
),
125133
})
126134
}

src/Toast.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import classNames from 'classnames';
55

66
import useTimeout from '@restart/hooks/useTimeout';
77
import { TransitionComponent } from '@restart/ui/types';
8-
import Fade from './Fade';
8+
import ToastFade from './ToastFade';
99
import ToastHeader from './ToastHeader';
1010
import ToastBody from './ToastBody';
1111
import { useBootstrapPrefix } from './ThemeProvider';
@@ -75,7 +75,7 @@ const Toast: BsPrefixRefForwardingComponent<'div', ToastProps> =
7575
{
7676
bsPrefix,
7777
className,
78-
transition: Transition = Fade,
78+
transition: Transition = ToastFade,
7979
show = true,
8080
animation = true,
8181
delay = 5000,

src/ToastFade.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import * as React from 'react';
2+
import Transition, {
3+
ENTERING,
4+
EXITING,
5+
} from 'react-transition-group/Transition';
6+
import Fade, { FadeProps } from './Fade';
7+
8+
const fadeStyles = {
9+
[ENTERING]: 'showing',
10+
[EXITING]: 'showing show',
11+
};
12+
13+
const ToastFade = React.forwardRef<Transition<any>, FadeProps>((props, ref) => (
14+
<Fade {...props} ref={ref} transitionClasses={fadeStyles} />
15+
));
16+
17+
ToastFade.displayName = 'ToastFade';
18+
19+
export default ToastFade;

www/src/examples/Toast/Dismissible.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ function Example() {
77

88
return (
99
<Row>
10-
<Col xs={6}>
10+
<Col md={6} className="mb-2">
11+
<Button onClick={toggleShowA} className="mb-2">
12+
Toggle Toast <strong>with</strong> Animation
13+
</Button>
1114
<Toast show={showA} onClose={toggleShowA}>
1215
<Toast.Header>
1316
<img
@@ -21,12 +24,10 @@ function Example() {
2124
<Toast.Body>Woohoo, you're reading this text in a Toast!</Toast.Body>
2225
</Toast>
2326
</Col>
24-
<Col xs={6}>
25-
<Button onClick={toggleShowA}>
26-
Toggle Toast <strong>with</strong> Animation
27+
<Col md={6} className="mb-2">
28+
<Button onClick={toggleShowB} className="mb-2">
29+
Toggle Toast <strong>without</strong> Animation
2730
</Button>
28-
</Col>
29-
<Col xs={6} className="my-1">
3031
<Toast onClose={toggleShowB} show={showB} animation={false}>
3132
<Toast.Header>
3233
<img
@@ -40,11 +41,6 @@ function Example() {
4041
<Toast.Body>Woohoo, you're reading this text in a Toast!</Toast.Body>
4142
</Toast>
4243
</Col>
43-
<Col xs={6}>
44-
<Button onClick={toggleShowB}>
45-
Toggle Toast <strong>without</strong> Animation
46-
</Button>
47-
</Col>
4844
</Row>
4945
);
5046
}

0 commit comments

Comments
 (0)