mirror of
https://gitlab.nic.cz/turris/reforis/foris-js.git
synced 2024-11-13 17:25:34 +01:00
Refactor Alert component to include dismiss animation and timeout
This commit is contained in:
parent
81b71f8153
commit
87c81a2a2d
|
@ -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")}
|
||||
/>
|
||||
)}
|
||||
|
|
Loading…
Reference in New Issue
Block a user