Skip to content

Commit

Permalink
fix #476
Browse files Browse the repository at this point in the history
  • Loading branch information
fkhadra committed May 19, 2020
1 parent ce818bc commit 4209f0b
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 58 deletions.
71 changes: 34 additions & 37 deletions src/components/ToastContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as React from 'react';
import PropTypes from 'prop-types';
import cx from 'classnames';
import { TransitionGroup } from 'react-transition-group';

import { Toast } from './Toast';
import { CloseButton } from './CloseButton';
Expand All @@ -23,43 +22,41 @@ export const ToastContainer: React.FC<ToastContainerProps> = props => {
className={DEFAULT.CSS_NAMESPACE as string}
id={containerId as string}
>
<TransitionGroup component={null}>
{getToastToRender((position, toastList) => {
const swag = {
className: cx(
`${DEFAULT.CSS_NAMESPACE}__toast-container`,
`${DEFAULT.CSS_NAMESPACE}__toast-container--${position}`,
{ [`${DEFAULT.CSS_NAMESPACE}__toast-container--rtl`]: rtl },
parseClassName(className)
),
style:
toastList.length === 0
? { ...style, pointerEvents: 'none' }
: { ...style }
} as any;
{getToastToRender((position, toastList) => {
const swag = {
className: cx(
`${DEFAULT.CSS_NAMESPACE}__toast-container`,
`${DEFAULT.CSS_NAMESPACE}__toast-container--${position}`,
{ [`${DEFAULT.CSS_NAMESPACE}__toast-container--rtl`]: rtl },
parseClassName(className)
),
style:
toastList.length === 0
? { ...style, pointerEvents: 'none' }
: { ...style }
} as any;

return (
<ToastPositioner {...swag} key={`container-${position}`}>
{toastList.map(({ content, props: toastProps }) => {
return (
<Toast
{...toastProps}
in={isToastActive(toastProps.toastId)}
key={`toast-${toastProps.key}`}
closeButton={
toastProps.closeButton === true
? CloseButton
: toastProps.closeButton
}
>
{content}
</Toast>
);
})}
</ToastPositioner>
);
})}
</TransitionGroup>
return (
<ToastPositioner {...swag} key={`container-${position}`}>
{toastList.map(({ content, props: toastProps }) => {
return (
<Toast
{...toastProps}
in={isToastActive(toastProps.toastId)}
key={`toast-${toastProps.key}`}
closeButton={
toastProps.closeButton === true
? CloseButton
: toastProps.closeButton
}
>
{content}
</Toast>
);
})}
</ToastPositioner>
);
})}
</div>
);
};
Expand Down
2 changes: 2 additions & 0 deletions src/hooks/useToastContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export interface ContainerInstance {
}

export function useToastContainer(props: ToastContainerProps) {
const [, forceUpdate] = useReducer(x => x + 1, 0);
const [toast, dispatch] = useReducer(reducer, []);
const containerRef = useRef(null);
let toastCount = useKeeper(0);
Expand Down Expand Up @@ -274,6 +275,7 @@ export function useToastContainer(props: ToastContainerProps) {

function removeFromCollection(toastId: Id) {
delete collection[toastId];
forceUpdate();
}

function getToastToRender<T>(
Expand Down
7 changes: 1 addition & 6 deletions src/utils/collapseToast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ export function collapseToast(
const height = node.scrollHeight;
const style = node.style;

function onCollapseEnd() {
node.removeEventListener('transitionend', onCollapseEnd);
done();
}

requestAnimationFrame(() => {
style.minHeight = 'initial';
style.height = height + 'px';
Expand All @@ -25,7 +20,7 @@ export function collapseToast(
style.height = '0';
style.padding = '0';
style.margin = '0';
setTimeout(() => done(), duration as number);
});
node.addEventListener('transitionend', onCollapseEnd);
});
}
1 change: 1 addition & 0 deletions src/utils/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ export const TYPE = {

export const enum DEFAULT {
COLLAPSE_DURATION = 300,
DEBOUNCE_DURATION = 50,
CSS_NAMESPACE = 'Toastify'
}
15 changes: 8 additions & 7 deletions src/utils/cssTransition.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,12 @@ export function cssTransition({
}
};

const onCollapseStart = () => {
const onExited = () => {
const node = props.nodeRef.current;

if (node) {
collapseToast(node, done, collapseDuration);
node.removeEventListener('animationend', onCollapseStart);
node.removeEventListener('animationend', onExited);
collapse ? collapseToast(node, done, collapseDuration) : done();
}
};

Expand All @@ -100,9 +101,7 @@ export function cssTransition({
node.classList.add(exitClassName);
node.style.animationFillMode = 'forwards';
node.style.animationDuration = `${exitDuration}ms`;
collapse
? node.addEventListener('animationend', onCollapseStart)
: done();
node.addEventListener('animationend', onExited);
}
};

Expand All @@ -114,7 +113,9 @@ export function cssTransition({
? 0
: {
enter: enterDuration,
exit: collapse ? exitDuration + collapseDuration : exitDuration
exit: collapse
? exitDuration + collapseDuration
: exitDuration + DEFAULT.DEBOUNCE_DURATION
}
}
onEnter={onEnter}
Expand Down
11 changes: 3 additions & 8 deletions test/utils/collapseToast.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,17 @@ afterEach(() => {
window.requestAnimationFrame.mockRestore();
});

jest.useFakeTimers();

describe('collapseToast function', () => {
it('Should handle collapse function', () => {
const done = jest.fn();
const node = document.createElement('div');
const addEvent = jest.fn((_, fn) => {
fn();
});
const removeEvent = jest.fn();

node.addEventListener = addEvent;
node.removeEventListener = removeEvent;

collapseToast(node, done);
jest.runAllTimers();

expect(addEvent).toHaveBeenCalled();
expect(removeEvent).toHaveBeenCalled();
expect(done).toHaveBeenCalled();
});
});

0 comments on commit 4209f0b

Please sign in to comment.