Skip to content

Commit

Permalink
fix(Carousel): fix crossfade animation (#5671)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyletsang committed Mar 11, 2021
1 parent a4d6f98 commit a24ab44
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/Carousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import useUpdateEffect from '@restart/hooks/useUpdateEffect';
import useCommittedRef from '@restart/hooks/useCommittedRef';
import useTimeout from '@restart/hooks/useTimeout';
import classNames from 'classnames';
import transitionEnd from 'dom-helpers/transitionEnd';
import Transition from 'react-transition-group/Transition';
import PropTypes from 'prop-types';
import React, {
Expand All @@ -20,6 +19,7 @@ import CarouselItem from './CarouselItem';
import { map, forEach } from './ElementChildren';
import SafeAnchor from './SafeAnchor';
import { useBootstrapPrefix } from './ThemeProvider';
import transitionEndListener from './transitionEndListener';
import triggerBrowserReflow from './triggerBrowserReflow';
import {
BsPrefixPropsWithChildren,
Expand Down Expand Up @@ -567,7 +567,7 @@ function CarouselFunc(uncontrolledProps: CarouselProps, ref) {
in={isActive}
onEnter={isActive ? handleEnter : undefined}
onEntered={isActive ? handleEntered : undefined}
addEndListener={transitionEnd}
addEndListener={transitionEndListener}
>
{(status) =>
React.cloneElement(child, {
Expand Down
28 changes: 22 additions & 6 deletions src/transitionEndListener.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
import css from 'dom-helpers/css';
import transitionEnd from 'dom-helpers/transitionEnd';

function parseDuration(
node: HTMLElement,
property: 'transitionDuration' | 'transitionDelay',
) {
const str = css(node, property) || '';
const mult = str.indexOf('ms') === -1 ? 1000 : 1;
return parseFloat(str) * mult;
}

export default function transitionEndListener(
element: HTMLElement,
handler: (e: TransitionEvent) => void,
) {
const remove = transitionEnd(element, (e) => {
if (e.target === element) {
remove();
handler(e);
}
});
const duration = parseDuration(element, 'transitionDuration');
const delay = parseDuration(element, 'transitionDelay');
const remove = transitionEnd(
element,
(e) => {
if (e.target === element) {
remove();
handler(e);
}
},
duration + delay,
);
}

0 comments on commit a24ab44

Please sign in to comment.