• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

fkhadra / react-toastify / 12362857581

16 Dec 2024 11:01PM UTC coverage: 81.398% (-7.6%) from 88.998%
12362857581

push

github

fkhadra
chore: bump dependencies

320 of 423 branches covered (75.65%)

Branch coverage included in aggregate %.

437 of 507 relevant lines covered (86.19%)

580.66 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.48
/src/utils/cssTransition.tsx
1
import React, { useEffect, useLayoutEffect, useRef } from 'react';
2
import { collapseToast } from './collapseToast';
3
import { Default } from './constant';
4

5
import { ToastTransitionProps } from '../types';
6

7
export interface CSSTransitionProps {
8
  /**
9
   * Css class to apply when toast enter
10
   */
11
  enter: string;
12

13
  /**
14
   * Css class to apply when toast leave
15
   */
16
  exit: string;
17

18
  /**
19
   * Append current toast position to the classname.
20
   * If multiple classes are provided, only the last one will get the position
21
   * For instance `myclass--top-center`...
22
   * `Default: false`
23
   */
24
  appendPosition?: boolean;
25

26
  /**
27
   * Collapse toast smoothly when exit animation end
28
   * `Default: true`
29
   */
30
  collapse?: boolean;
31

32
  /**
33
   * Collapse transition duration
34
   * `Default: 300`
35
   */
36
  collapseDuration?: number;
37
}
38

39
const enum AnimationStep {
126✔
40
  Enter,
126✔
41
  Exit
126✔
42
}
43

44
/**
45
 * Css animation that just work.
46
 * You could use animate.css for instance
47
 *
48
 *
49
 * ```
50
 * cssTransition({
51
 *   enter: "animate__animated animate__bounceIn",
52
 *   exit: "animate__animated animate__bounceOut"
53
 * })
54
 * ```
55
 *
56
 */
57
export function cssTransition({
58
  enter,
59
  exit,
60
  appendPosition = false,
×
61
  collapse = true,
188✔
62
  collapseDuration = Default.COLLAPSE_DURATION
188✔
63
}: CSSTransitionProps) {
64
  return function ToastTransition({
376✔
65
    children,
66
    position,
67
    preventExitTransition,
68
    done,
69
    nodeRef,
70
    isIn,
71
    playToast
72
  }: ToastTransitionProps) {
73
    const enterClassName = appendPosition ? `${enter}--${position}` : enter;
2,310!
74
    const exitClassName = appendPosition ? `${exit}--${position}` : exit;
2,310!
75
    const animationStep = useRef(AnimationStep.Enter);
2,310✔
76

77
    useLayoutEffect(() => {
2,310✔
78
      const node = nodeRef.current!;
1,258✔
79
      const classToToken = enterClassName.split(' ');
1,258✔
80

81
      const onEntered = (e: AnimationEvent) => {
1,258✔
82
        if (e.target !== nodeRef.current) return;
1,246✔
83

84
        playToast();
684✔
85
        node.removeEventListener('animationend', onEntered);
684✔
86
        node.removeEventListener('animationcancel', onEntered);
684✔
87
        if (animationStep.current === AnimationStep.Enter && e.type !== 'animationcancel') {
684✔
88
          node.classList.remove(...classToToken);
662✔
89
        }
90
      };
91

92
      const onEnter = () => {
1,258✔
93
        node.classList.add(...classToToken);
1,258✔
94
        node.addEventListener('animationend', onEntered);
1,258✔
95
        node.addEventListener('animationcancel', onEntered);
1,258✔
96
      };
97

98
      onEnter();
1,258✔
99
    }, []);
100

101
    useEffect(() => {
2,310✔
102
      const node = nodeRef.current!;
1,280✔
103

104
      const onExited = () => {
1,280✔
105
        node.removeEventListener('animationend', onExited);
×
106
        collapse ? collapseToast(node, done, collapseDuration) : done();
×
107
      };
108

109
      const onExit = () => {
1,280✔
110
        animationStep.current = AnimationStep.Exit;
22✔
111
        node.className += ` ${exitClassName}`;
22✔
112
        node.addEventListener('animationend', onExited);
22✔
113
      };
114

115
      if (!isIn) preventExitTransition ? onExited() : onExit();
1,280!
116
    }, [isIn]);
117

118
    return <>{children}</>;
2,310✔
119
  };
120
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc