Skip to content

Commit

Permalink
fix(Toast): fix fade animation (#6004)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyletsang committed Sep 2, 2021
1 parent 91e2bcd commit 0237a14
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 14 deletions.
10 changes: 9 additions & 1 deletion src/Fade.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface FadeProps extends TransitionCallbacks {
appear?: boolean;
timeout?: number;
children: React.ReactElement;
transitionClasses?: Record<string, string>;
}

const propTypes = {
Expand Down Expand Up @@ -80,6 +81,12 @@ const propTypes = {
* You must provide a single JSX child element to this component and that element cannot be a \<React.Fragment\>
*/
children: PropTypes.element.isRequired,

/**
* Applies additional specified classes during the transition. Takes an object
* where the keys correspond to the Transition status
*/
transitionClasses: PropTypes.object,
};

const defaultProps = {
Expand All @@ -96,7 +103,7 @@ const fadeStyles = {
};

const Fade = React.forwardRef<Transition<any>, FadeProps>(
({ className, children, ...props }, ref) => {
({ className, children, transitionClasses = {}, ...props }, ref) => {
const handleEnter = useCallback(
(node, isAppearing) => {
triggerBrowserReflow(node);
Expand All @@ -121,6 +128,7 @@ const Fade = React.forwardRef<Transition<any>, FadeProps>(
className,
children.props.className,
fadeStyles[status],
transitionClasses[status],
),
})
}
Expand Down
4 changes: 2 additions & 2 deletions src/Toast.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import classNames from 'classnames';

import useTimeout from '@restart/hooks/useTimeout';
import { TransitionComponent } from '@restart/ui/types';
import Fade from './Fade';
import ToastFade from './ToastFade';
import ToastHeader from './ToastHeader';
import ToastBody from './ToastBody';
import { useBootstrapPrefix } from './ThemeProvider';
Expand Down Expand Up @@ -75,7 +75,7 @@ const Toast: BsPrefixRefForwardingComponent<'div', ToastProps> =
{
bsPrefix,
className,
transition: Transition = Fade,
transition: Transition = ToastFade,
show = true,
animation = true,
delay = 5000,
Expand Down
19 changes: 19 additions & 0 deletions src/ToastFade.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as React from 'react';
import Transition, {
ENTERING,
EXITING,
} from 'react-transition-group/Transition';
import Fade, { FadeProps } from './Fade';

const fadeStyles = {
[ENTERING]: 'showing',
[EXITING]: 'showing show',
};

const ToastFade = React.forwardRef<Transition<any>, FadeProps>((props, ref) => (
<Fade {...props} ref={ref} transitionClasses={fadeStyles} />
));

ToastFade.displayName = 'ToastFade';

export default ToastFade;
18 changes: 7 additions & 11 deletions www/src/examples/Toast/Dismissible.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ function Example() {

return (
<Row>
<Col xs={6}>
<Col md={6} className="mb-2">
<Button onClick={toggleShowA} className="mb-2">
Toggle Toast <strong>with</strong> Animation
</Button>
<Toast show={showA} onClose={toggleShowA}>
<Toast.Header>
<img
Expand All @@ -21,12 +24,10 @@ function Example() {
<Toast.Body>Woohoo, you're reading this text in a Toast!</Toast.Body>
</Toast>
</Col>
<Col xs={6}>
<Button onClick={toggleShowA}>
Toggle Toast <strong>with</strong> Animation
<Col md={6} className="mb-2">
<Button onClick={toggleShowB} className="mb-2">
Toggle Toast <strong>without</strong> Animation
</Button>
</Col>
<Col xs={6} className="my-1">
<Toast onClose={toggleShowB} show={showB} animation={false}>
<Toast.Header>
<img
Expand All @@ -40,11 +41,6 @@ function Example() {
<Toast.Body>Woohoo, you're reading this text in a Toast!</Toast.Body>
</Toast>
</Col>
<Col xs={6}>
<Button onClick={toggleShowB}>
Toggle Toast <strong>without</strong> Animation
</Button>
</Col>
</Row>
);
}
Expand Down

0 comments on commit 0237a14

Please sign in to comment.