diff --git a/src/components/Navigation.js b/src/components/Navigation.js index 3cd70d1..571de40 100644 --- a/src/components/Navigation.js +++ b/src/components/Navigation.js @@ -274,9 +274,12 @@ function Navigation() { setShowSidebar(false)} open={showSidebar} /> -
- -
+ ); }; diff --git a/src/components/NewHathorAlert.js b/src/components/NewHathorAlert.js index a22d227..061f39c 100644 --- a/src/components/NewHathorAlert.js +++ b/src/components/NewHathorAlert.js @@ -5,7 +5,7 @@ * LICENSE file in the root directory of this source tree. */ -import React, { useRef, forwardRef, useImperativeHandle, useEffect } from 'react'; +import React, { useRef, forwardRef, useImperativeHandle } from 'react'; import { ReactComponent as SuccessIcon } from '../assets/images/success-icon.svg'; /** @@ -15,7 +15,7 @@ import { ReactComponent as SuccessIcon } from '../assets/images/success-icon.svg * @param {Object} props - Component properties. * @param {string} props.type - Defines the alert type for styling (e.g., "success", "error"). * @param {string} props.text - The message text displayed in the alert. - * @param {boolean} [props.fixedPosition=false] - If true, the alert will be fixed at the bottom right of the screen + * @param {boolean} [props.fixedPosition=false] - If true, the alert will be fixed at the bottom center of the screen * @param {React.Ref} ref - A reference to call the `show` method from parent components. * * @example: @@ -25,7 +25,8 @@ import { ReactComponent as SuccessIcon } from '../assets/images/success-icon.svg * alertRef.current.show(2000); // Show the alert for 2 seconds * ``` */ -const NewHathorAlert = forwardRef(({ type, text, showAlert, fixedPosition }, ref) => { +const NewHathorAlert = forwardRef(({ type, text, fixedPosition }, ref) => { + const containerDiv = useRef(null); const alertDiv = useRef(null); /** @@ -34,41 +35,80 @@ const NewHathorAlert = forwardRef(({ type, text, showAlert, fixedPosition }, ref * @param {number} duration - The display duration for the alert in milliseconds. */ const show = duration => { - if (alertDiv.current) { - alertDiv.current.classList.add('show'); - setTimeout(() => { - alertDiv.current.classList.remove('show'); - }, duration); + // If the component is not in a fixed position, only handle the alert component + if (!fixedPosition) { + showAlertComponent(); + return; } - }; - useEffect(() => { - if (showAlert === undefined) { + // No-op if the container is unavailable + if (!containerDiv?.current) { return; } - if (showAlert) { + + // The component is in a fixed position: managing its parent container to allow for animations + // and precise positioning + containerDiv.current.classList.add('show'); + setTimeout(showAlertComponent, 100); // Delay the alert display to accomodate animations + setTimeout(() => { + // By the time the timeout is called, the container may have been unmounted + if (containerDiv?.current) { + containerDiv.current.classList.remove('show'); + } + }, duration + 500); + + /** + * Handles the Alert component display and removal + */ + function showAlertComponent() { + // No-op if the ref is unavailable + if (!alertDiv?.current) { + return; + } + alertDiv.current.classList.add('show'); - } else { - alertDiv.current.classList.remove('show'); + setTimeout(() => { + // By the time the timeout is called, the component may have been unmounted + if (alertDiv?.current) { + alertDiv.current.classList.remove('show'); + } + }, duration); } - }, [showAlert]); + }; useImperativeHandle(ref, () => ({ show, })); - const fixedPositionClass = fixedPosition ? 'fixed-position' : ''; - return ( -
-
{type === 'success' ? : null}
-

{text}

-
- ); + /** + * Renders the alert component + * @returns {Element} + */ + function renderAlertDiv() { + return ( +
+
{type === 'success' ? : null}
+

{text}

+
+ ); + } + + // If the component is in a fixed position, also render its parent container on demand + if (fixedPosition) { + return ( +
+ {renderAlertDiv()} +
+ ); + } + + // Render only the alert component + return renderAlertDiv(); }); NewHathorAlert.displayName = 'NewHathorAlert'; diff --git a/src/newUi.scss b/src/newUi.scss index e9354e6..34e03de 100644 --- a/src/newUi.scss +++ b/src/newUi.scss @@ -952,7 +952,7 @@ nav { } .new-hathor-alert-container { - display: flex; + display: none; position: fixed; justify-content: center; align-items: center; @@ -960,6 +960,10 @@ nav { bottom: 220px; width: 100%; left: 0; + + &.show { + display: flex; + } } .new-hathor-alert { @@ -975,12 +979,6 @@ nav { justify-content: space-between; z-index: 9999; margin: 0; - - &.fixed-position { - position: fixed !important; - right: 15%; - bottom: 50%; - } } .alert-success-container {