mirror of
				https://gitlab.nic.cz/turris/reforis/foris-js.git
				synced 2025-11-03 23:00:31 +01:00 
			
		
		
		
	Refactor Alert component to include dismiss animation and timeout
This commit is contained in:
		@@ -5,7 +5,7 @@
 | 
			
		||||
 * See /LICENSE for more information.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
import React, { useRef } from "react";
 | 
			
		||||
import React, { useRef, useEffect, useState } from "react";
 | 
			
		||||
 | 
			
		||||
import PropTypes from "prop-types";
 | 
			
		||||
 | 
			
		||||
@@ -40,20 +40,36 @@ Alert.defaultProps = {
 | 
			
		||||
 | 
			
		||||
function Alert({ type, onDismiss, children }) {
 | 
			
		||||
    const alertRef = useRef();
 | 
			
		||||
    const [isVisible, setIsVisible] = useState(true);
 | 
			
		||||
    useFocusTrap(alertRef, !!onDismiss);
 | 
			
		||||
 | 
			
		||||
    useEffect(() => {
 | 
			
		||||
        if (onDismiss) {
 | 
			
		||||
            const timeout = setTimeout(() => setIsVisible(false), 7000);
 | 
			
		||||
            return () => clearTimeout(timeout);
 | 
			
		||||
        }
 | 
			
		||||
    }, [onDismiss]);
 | 
			
		||||
 | 
			
		||||
    const handleAnimationEnd = () => {
 | 
			
		||||
        if (!isVisible && onDismiss) {
 | 
			
		||||
            onDismiss();
 | 
			
		||||
        }
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    return (
 | 
			
		||||
        <div
 | 
			
		||||
            ref={alertRef}
 | 
			
		||||
            className={`alert alert-${type} ${
 | 
			
		||||
            className={`alert alert-${type} ${isVisible ? "alert-fade-in" : "alert-slide-out-top"} ${
 | 
			
		||||
                onDismiss ? "alert-dismissible" : ""
 | 
			
		||||
            }`.trim()}
 | 
			
		||||
            role="alert"
 | 
			
		||||
            onAnimationEnd={handleAnimationEnd}
 | 
			
		||||
        >
 | 
			
		||||
            {onDismiss && (
 | 
			
		||||
                <button
 | 
			
		||||
                    type="button"
 | 
			
		||||
                    className="btn-close"
 | 
			
		||||
                    onClick={onDismiss}
 | 
			
		||||
                    onClick={() => setIsVisible(false)}
 | 
			
		||||
                    aria-label={_("Close")}
 | 
			
		||||
                />
 | 
			
		||||
            )}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user