11import classNames from "classnames" ;
2- import React from "react" ;
2+ import React , { useEffect , useState } from "react" ;
33
44import styles from "./Toast.module.scss" ;
55import { Message , MessageProps } from "../Message" ;
@@ -9,12 +9,28 @@ export interface ToastProps extends MessageProps {
99}
1010
1111export const Toast : React . FC < ToastProps > = ( { timeout, ...props } ) => {
12+ // Delay the timeout animation until the user has focused the current tab
13+ const [ isFocused , setIsFocused ] = useState ( document . hasFocus ( ) ) ;
14+ useEffect ( ( ) => {
15+ const onFocus = ( ) => setIsFocused ( true ) ;
16+ const onBlur = ( ) => setIsFocused ( false ) ;
17+
18+ window . addEventListener ( "focus" , onFocus ) ;
19+ window . addEventListener ( "blur" , onBlur ) ;
20+
21+ return ( ) => {
22+ window . removeEventListener ( "focus" , onFocus ) ;
23+ window . removeEventListener ( "blur" , onBlur ) ;
24+ } ;
25+ } , [ ] ) ;
26+
1227 return (
1328 < div onAnimationEnd = { props . onClose } >
1429 < Message
1530 { ...props }
1631 className = { classNames ( props . className , styles . toastContainer , {
1732 [ styles [ "toastContainer--timeout" ] ] : timeout ,
33+ [ styles [ "toastContainer--focused" ] ] : isFocused ,
1834 } ) }
1935 textClassName = { styles . toastText }
2036 />
0 commit comments