mirror of
https://gitlab.nic.cz/turris/reforis/foris-js.git
synced 2024-11-14 17:35:35 +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.
|
* See /LICENSE for more information.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import React, { useRef } from "react";
|
import React, { useRef, useEffect, useState } from "react";
|
||||||
|
|
||||||
import PropTypes from "prop-types";
|
import PropTypes from "prop-types";
|
||||||
|
|
||||||
|
@ -40,20 +40,36 @@ Alert.defaultProps = {
|
||||||
|
|
||||||
function Alert({ type, onDismiss, children }) {
|
function Alert({ type, onDismiss, children }) {
|
||||||
const alertRef = useRef();
|
const alertRef = useRef();
|
||||||
|
const [isVisible, setIsVisible] = useState(true);
|
||||||
useFocusTrap(alertRef, !!onDismiss);
|
useFocusTrap(alertRef, !!onDismiss);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (onDismiss) {
|
||||||
|
const timeout = setTimeout(() => setIsVisible(false), 7000);
|
||||||
|
return () => clearTimeout(timeout);
|
||||||
|
}
|
||||||
|
}, [onDismiss]);
|
||||||
|
|
||||||
|
const handleAnimationEnd = () => {
|
||||||
|
if (!isVisible && onDismiss) {
|
||||||
|
onDismiss();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
ref={alertRef}
|
ref={alertRef}
|
||||||
className={`alert alert-${type} ${
|
className={`alert alert-${type} ${isVisible ? "alert-fade-in" : "alert-slide-out-top"} ${
|
||||||
onDismiss ? "alert-dismissible" : ""
|
onDismiss ? "alert-dismissible" : ""
|
||||||
}`.trim()}
|
}`.trim()}
|
||||||
role="alert"
|
role="alert"
|
||||||
|
onAnimationEnd={handleAnimationEnd}
|
||||||
>
|
>
|
||||||
{onDismiss && (
|
{onDismiss && (
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
className="btn-close"
|
className="btn-close"
|
||||||
onClick={onDismiss}
|
onClick={() => setIsVisible(false)}
|
||||||
aria-label={_("Close")}
|
aria-label={_("Close")}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user