Steps for designing an animated css notification message that slides up

I'm looking to implement a dynamic CSS animation for a slide-up notification or confirmation message in my ReactJS project. The goal is for the message to smoothly slide up from the bottom of the page, linger briefly, and then gracefully slide back down out of view. However, I've encountered an issue where the message slides way too far down and appears out of sight, though it can still be seen at the bottom when scrolling. It seems like this problem might relate to the display properties in the CSS.

edit

Below is the code snippet for the notification component I am utilizing. I found it online and made a few modifications:

import React, { useState, useEffect } from 'react';
import styled from 'styled-components';

const Container = styled.div`
    background-color: ${props => props.backgroundColor};
    color: ${props => props.color};
    padding: 16px;
    position: absolute;
    bottom: ${props => props.shift}px;
    left: ${props => props.left};
    margin: 0 auto;
    z-index: 999;
    transition: bottom 1.0s ease;
    border-radius: 5px;
`;

export default function Notification({ msg, showNotification, set_showNotification, type = 'info', left = '35%', duration = 5000 }) {  // 'type' can also be 'error'
    const distance = 500;
    const [shift, set_shift] = useState(-distance);
    const [showing, set_showing] = useState(false);
    const [timeout, set_timeout] = useState(-1);

    const color = type === 'error' ? 'white' : 'black';
    const backgroundColor = type === 'error' ? 'darkred' : 'whitesmoke';

    useEffect(() => {
        return function cleanup() {
            if (timeout !== -1) {
                clearTimeout(timeout);
            }
        }
    }, [timeout]);

    if (showNotification && !showing) {
        set_shift(distance);
        set_showNotification(false);
        set_showing(true);

        let t = setTimeout(() => {
            set_shift(-distance);
            set_showing(false);
        }, duration);

        set_timeout(t);
    }

    return (
        <Container shift={shift} color={color} backgroundColor={backgroundColor} left={left}>{msg}</Container>
    );
}

Answer №1

Utilize the power of Google for more information.

Click here to learn about CSS animations

Add some setTimeout() functions to your code to ensure the class is removed once the animation is complete.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"Error in sorting the new order: unable to retrieve value for tkEmail

I am struggling to sort items within div elements that have the same id and class. Specifically, I am having trouble retrieving the value of tkEmail tag for each item. For example: item 1 Order: 0 Value: 1 Here is the HTML structure: <div id="sortable ...

I am attempting to apply a class to #cart-total-remove using the location function, but despite trying various methods, I have been unable to make it work

This function is responsible for updating the cart successfully success: function(json) { // Setting a timeout to ensure total update setTimeout(function () { $('#cart > button').html('& ...

Sass/SCSS is failing to compile on my local environment

As a beginner in SASS, I'm currently attempting to compile my SASS file on my local machine. Unfortunately, I've hit a roadblock and despite trying all the methods mentioned, I continue to encounter the same or similar errors. It's frustrati ...

Create another div for the remaining vertical space occupied by the sibling div?

I currently have 3 different divs nested within each other: <div class="a"> <div class="b"> </div> <div class="c"> </div> </div> Here is the current CSS styling applied to these divs: .a { hei ...

Filtering a list with Vue.js using an array of checkboxes

Currently, I am exploring ways to filter a v-for list using a checkbox model array with multiple checkboxes selected. Most examples I have come across only demonstrate filtering one checkbox at a time. In the demo here, you can see checkboxes 1-3 and a lis ...

Enhance the appearance of your responsive embedded video with a stylish border

I'm attempting to give a decorative border to a flexible embedded video. I've experimented with two different techniques to display the video: 1. <div class="embed-responsive embed-responsive-16by9"> <iframe class="embed-responsiv ...

JSX Bootstrap unresolved arror

I have been using Bootstrap with React, but the compiler keeps failing due to unterminated JSX contents. I have checked my code numerous times and can't seem to find any unclosed tags no matter how the ternary operator evaluates. As a beginner in Reac ...

The initial storage of a variable results in it being Undefined, but subsequent storage operations are successful. This process involves utilizing jQuery, Ajax, and

This is a snippet from a function: var editid; $("div.editable").click(function(e) { if ($currentInput == null) return; var css = GetCssProperties(); $(this).css(css); editid = $(this).attr("id"); $currentInpu ...

Checkbox offering a tri-state option

Seeking help on creating a checkbox with three states. I have implemented a method to toggle between these states upon clicking. However, the issue is that this method is only triggered after the HTML changes. As a result, the checkbox's navigation be ...

What is the best way to conceal a Boostrap Navbar using jQuery?

Attempting to create a hidden side navbar using Jquery and Bootstrap 4. Struggling with the .on('click', ...) event handler for a button not hiding the sidebar as expected despite all files loading correctly according to the developer tools. Atte ...

Setting up and launching an Angular (HotTowel) application on Apache HTTP Server

After creating a project using John Papa's HotTowel, I ran the command: gulp serve-build This command is functioning properly. However, when I transfer the build folder to my Apache server, the application seems to be broken. Even though the files ...

Internet Explorer failing to trigger Ajax requests

I'm encountering an issue with my script on my webpage - it works perfectly in Chrome, but Internet Explorer is not entering the success{} function of Ajax. It does go into the Complete{} function without any problems. I attempted to pass the data var ...

Styling your div elements in CSS to shrink in width relative to their parent container

.container { max-width: 750px; height: 100px; background-color: red; margin: 0; padding: 0; } .box1 { width: 100px; height: 100%; float: left; background-color: black; } .box2 { width: 650px; height: 100%; fl ...

Ensure that the user-entered text input is validated within a table using Angular 5

HTML file <mat-table #table [dataSource]="CMDataSource"> <ng-container matColumnDef="mQues"> <mat-header-cell *matHeaderCellDef> Ticket Volume </mat-header-cell> <mat-cel ...

Style the nth child of a particular type in CSS with a unique color scheme

Is there a way to select the nth child of type and apply different colors without assigning IDs because they are dynamically generated in a loop from a template engine? I found some useful information on this topic here: https://www.w3schools.com/cssref/ ...

Tips for retrieving multiple independent response data in Express js app.get callback

What is the optimal way to send two independent MongoDB results in an Express application via HTTP Method? Check out this concise example to clarify: //app.js var express = require('express'); var app = express(); var testController = require(& ...

Converting data from an HTML file to JSON with the help of <input file> feature in Javascript (Vue)

Currently, I am attempting to upload an HTM file using in Vue. Within this file, there is a styled table utilizing , and other HTML elements... I am curious, is it feasible to extract the tabulated data from the HTM file and convert it into another forma ...

How can I delete a pathway in paper.js?

Is it possible to create a new path using the canvas globalCompositeOperation set to 'destination-out'? If so, how would I go about doing this? I have observed that Item has a blendMode property which can be found at . However, experimenting wit ...

Adjusting the font color when hovering over multiline text

I am currently in the process of coding a website for my job, and I am working on changing the text color when it is hovered over. However, there seems to be a break in my code causing the text not to highlight all at once. Any guidance or suggestions on h ...

Differences between Chrome, IE8, and other web browsers

I am currently working on a website where I need to replace one <span> element with another. The original <span> looks something like this: <span class="green_link">;Abbreviation;The definition</span> The format of the input str ...