CSS transitions in React fail to function when the state changes to true

A challenge arises with the error bar, which should ease-in when error = true.

Within the render() method:

render() {
    const { errors } = this.props;

    return (
        <div id='messages'>
            <div className={ errors ? 'show-messages' : 'hidden-messages' }>
                <Container>{ this.renderMessage() }</Container>
            </div>
        </div>
    )
}

If a user attempts to log in and receives invalid credentials, the server will respond by setting this.props.errors to true and displaying show-messages.

The desired behavior is for the bar to ease-in.

The latest CSS attempt is shown below:

.hidden-messages {
    visibility: hidden;
    transition: width 2s, height 2s;
    -webkit-transition: width 2s, height 4s;
    transition-delay: 5s;
}

.show-messages {
    visibility: visible;
    background-color: #FFF;
    transition: width 2s, height 2s;
    -webkit-transition: width 2s, height 4s;
    transition-delay: 5s;
}

Initially tried solely using transition:, then added transition-delay as suggested. Still encountered issues.

It seems that the ternary operator acts as an on/off switch without establishing a clear link between .hidden-messages and .show-messages. How can this issue be addressed?


UPDATE: Attempts to implement suggestions from Jason McFarlane's CodePen example failed to yield results.

With reference to a specific statement, modifications were made:

To toggle between hide/show states, apply an additional class along with the default state.

For instance, if default is to show messages, maintain that class on the respective div.

Since the default state is hide-messages, adjustments were made accordingly. However, changing the ternary from show-messages.hide-messages to show-messages hide-messages` did not achieve the intended outcome despite aligning with the DOM structure observed in the CodePen example.

import React, { Component } from 'react';
// rest of the code...

@import 'declarations';

#messages {
    position: relative;
    top: 105px;

    .hide-messages {
        visibility: hidden;
        height: 0;
        width: 0;
    }

    .hide-messages.show-messages {
        visibility: visible;
        background-color: $red;
        transition: height 5s ease-in !important;
        height: 100%;
        width: 100%;
    }

    .alert-danger {
        background-color: $red;
        margin-bottom: 0;
        border: none;
        padding-left: 0;
        padding-right: 0;
        color: white;

        i {
            cursor: pointer;
        }
    }
}

Shown above is the current configuration. The objective is to have a red banner easing in as the default behavior.

https://i.sstatic.net/LEdx5.png

Answer №1

The concept of using a ternary for an on/off switch is correct, but the issue lies in how it is implemented. When utilizing a CSS transition, it's essential to have a default state and then toggle an additional state on or off from there.

For instance, if you want to show/hide content, you should toggle a class that hides or shows the content on top of the default state.

For example: if the default state is to display messages, ensure that the class for showing messages is always present on the div.

<div className={ errors ? 'show-messages' : 'show-messages.hide-messages' }>

Remember to adjust your CSS accordingly:

.show-messages {
  visibility: visible;
  transition: height 0.3s ease-in;
  height:200px;
  width: 200px;
  background: #d8d8d8;
}

.show-messages.hide-messages {
  visibility: hidden;
  height: 0;
  width: 0;
}

I've provided a codepen https://codepen.io/anon/pen/gKmpgw where you can experiment with various CSS attributes to animate.

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

You are unable to apply multiple customizations to Bootstrap in a React application

I'm currently working on a React application that consists of two distinct sections, referred to as Part A and Part B. Each part requires its own set of SCSS rules and Bootstrap Grid System Customization. Specifically, I need Part A to have a 24-colu ...

Mastering the art of using div boxes in boxing form

When I box in information using divs, I've noticed that sometimes the wrapping boxes don't fill out the space completely. This can result in elements from other wrappers encroaching into the box area. For example: <div> <div style="le ...

Container with Two Columns Extending to Full Height of Body

I'm currently referencing this example for reference: . In this layout, the left column has a fixed width in pixels while the right column is resizable. My goal is to set the height of the container to 100% of the body height. For instance, focusing ...

Position a div in the center both horizontally and vertically within a section, ensuring that each section expands to fill the entire page when scrolling

I'm currently working on coding a page layout where each section takes up the entire screen. My goal is to have the content within each section centered both vertically and horizontally. I'm unsure if this is achievable, so any guidance or advice ...

I am interested in displaying a nested array of objects in a parent-child relationship as a tree structure using React Js

I am seeking a way to display this array of objects as a collapsible tree structure in reactJS, where clicking on nodes expands them to reveal their children. The data provided below is just a glimpse into a deeply nested array. const family =[ { // & ...

What is the best way to create individual clickable images that open up a modal window for each one?

Currently, I have created a function that is able to map images from an array in Next.js using react-bootstrap. The function is functioning as expected, however, my goal is to add an onClick function to each image so that when clicked, it opens up in a new ...

What could be causing the IDs to fail in my PHP file?

I'm attempting to create an animated logo for my PHP website by breaking the two words of the logo into separate spans. Despite trying to remove the whitespace between the span tags in order to style them independently, I encountered a puzzling issue. ...

Maintain uniform URL structure across various pages with React Redux

Our project utilizes React in combination with Redux and redux-simple-router for navigation purposes. I am currently working on a two-page form and aiming to maintain the same URL between both pages. Despite this, pressing the back button on the browser ca ...

Issue with react-native: borderRadius not properly framing component dimensions

The issue with the borderRadius style attribute not displaying correctly on a component. Ideally, one would expect to see a green circle on the red background seamlessly without any white space. Unfortunately, this is what is currently being displayed: h ...

Error Message: React encountered an issue when trying to access the 'theme' property of an undefined object

Currently developing a web application using React, I encountered an issue when trying to implement the react-datepicker component in my code. Upon adding the following lines of code, my web application breaks and an error is thrown: import {SingleDatePic ...

Is there a way to apply a custom CSS to Bootstrap without altering its appearance?

Check out this code snippet <link rel="stylesheet" href="css/bootstrap.css"> <link rel="stylesheet" href="css/sub-cat.css"> I am having trouble with my CSS. I tried to modify the "Caption" class in Bootstrap by adding some new styles in "sub- ...

Circular loading bar in React with no duplicate animations

I am trying to figure out a way to make the loader for React Circular Progressbar load only once without repeating the sequence. After reviewing all the documentation, I could not find any information on how to achieve this. Additionally, other components ...

Develop a custom input field feature that utilizes both JavaScript and CSS

I really appreciate the feature that allows me to resize the textarea by simply clicking and dragging on the slanted lines in the lower right hand corner. However, I am looking for a way to apply CSS styles to text which is not possible with a textarea. ...

Trouble with Varnish Cache

We are currently in the process of developing a website using React and Redux. Recently, we implemented a Varnish cache on our API. While testing the API on Postman, everything appears to be functioning properly. However, when accessing our website, we are ...

In TypeScript, there is a mismatch between the function return type

I am new to TypeScript and trying to follow its recommendations, but I am having trouble understanding this particular issue. https://i.stack.imgur.com/fYQmQ.png After reading the definition of type EffectCallback, which is a function returning void, I t ...

Does react-tap-event-plugin serve a purpose in the current year of 2018?

Does React still require the use of zilverline's tap event plugin or has it been integrated directly into React's source code, eliminating the need for a tap delay? ...

What is the best way to create a single title that is expressed in two distinct languages

My website features content in two local languages, but all the titles are currently only in one language. I would like to have titles in both languages alternating, with title1 displaying for 3 seconds before automatically switching to title2. This mean ...

JQuery fails to retrieve accurate width measurements

Utilizing this code snippet, I have been able to obtain the width of an element and then set it as its height: $(document).ready(function(){ $(".equal-height").each(function(){ var itemSize = $(this).outerWidth(); cons ...

Steps to execute scripts within a React application without the presence of an index.html file

I'm fairly new to React and I have a React web app where I need to incorporate multiple scripts. The challenge is that these scripts should be placed within the body tag in the index.html file, like this: <!DOCTYPE html> <html lang="en&q ...