Creating smooth transitions with CSS in React by fading text in when a user clicks a button

Within this presentational component:

const HowToControls = props => {
  return (
    <div className="col-md-6 how-to">
      {props.isOpen ?
        <p className={`text ${props.isOpen ? 'visible' : ''}`}>
         lorem ipsum............
        </p>/>
        : null}
    </div>
  );
}

The paragraph holds a class named "text" and another class is dynamically added depending on an action in its parent component. Despite both classes ("text" and "visible") being applied to the element, there seems to be no transition effect when toggling. Below is my CSS:

.text {
  opacity: 0.01;
  transition: opacity 600ms;
}

.text.visible {
  opacity: 1;
}

It appears that both classes are applied upon page load (prior to full component mounting). Is this assumption correct? Your input is greatly appreciated.

Answer №1

The if statement props.isOpen is placed before the paragraph, making the class change irrelevant. Depending on the value of props.isOpen, the paragraph will either display with the visible class applied or show as null. Consider this alternative approach:

<div className="col-md-6 how-to">
    <p className={`text ${props.isOpen ? 'visible' : ''}`}>
     lorem ipsum............
    </p>/>
</div>

In order for transitions to be effective, the element must first be present in the DOM with an initial CSS property. When this property is modified or overridden, it triggers the transition effect. In your current setup, the paragraph is not even rendered due to the conditional statement preceding it.

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

Tips for updating the left positioning of a Div element on the fly

Although my title may not fully explain my goal, I am dealing with dynamically created div elements. When a user triggers an ajax request to delete one of the divs, I want to remove it from the DOM upon success. The issue arises in adjusting the layout aft ...

The CSS transition for a DIV on an iOS Safari browser is experiencing a lag

In an attempt to optimize CSS transitions in the IOS Safari browser, I decided to use the transform: translate() property instead of changing the left property like other suggestions. Unfortunately, this approach did not yield the expected results as the ...

The function "classList.add" does not successfully add a class to both a div and form elements

I've encountered an issue where I am unable to add a class to div/form elements using the classList.add method. Interestingly, this method works perfectly fine for other elements like input or button. However, when it comes to the div and form element ...

Use webpack to selectively compile only CSS modules, excluding JavaScript files

Is it possible to include css modules in a webpack build without compiling the javascript code? I am creating a react component that relies on css-modules, and I want consumers to be able to use my un-transpiled source files as the entry point. However, ...

To prevent the default alignment in case the text exceeds the input length, stop the automatic alignment

I have a query regarding an input field with a maximum length of 4 characters. The appearance is such that these 4 characters seem to be delineated by borders which are actually 3 lines displayed above the input field. When I enter the fourth character, a ...

What are some effective methods for eliminating npm warnings?

Every time I execute npm install, I encounter the following issue: npm WARN 5e876469cab87cfb6b2f9a8cde1fa" data-cfemail="3451474740475756627b54574c4051575550120d020512"><i>Email address protected by JavaScript. Activate javascript to see the emai ...

Accessing a specific data point from a Rest Api with React JS

How can I extract the value '00000000000000000000000000000000' that comes after clusters in the links->href in a Rest Api response? [{ "analysisUnits": [ { "links": [ { "href": "http://127 ...

Open the overflow div within a table data cell

My goal is to create a drag-and-drop schedule tool similar to Fullcalendar. I have decided to use a table layout with a rectangular div inside each TD cell to represent tasks. <table onDragEnd={dragend}> <tr> <th>0</th> ...

Containers do not line up properly with each other. Adjusting the width leads to unexpected consequences

As someone who is new to HTML and CSS, I am facing a challenge with aligning the items within a navigation bar on my website. The list serves as a navigation bar and is contained inside the "inner header" div. While I was able to align the entire "nav" con ...

"Although the NextJS client-side data is present, it seems to be struggling to

I am facing an issue while trying to display fetched data on a blog page. Although the data is successfully retrieved client-side and I can view it using console.log(), it does not appear on the page and the elements remain empty. I am completely puzzled. ...

Encountered an "next: not found" error when attempting to launch nextjs standalone within Azure Web App

I am having issues deploying my application built with Next.js on an Azure Web App. Despite following the guidelines provided by Microsoft, I encountered a problem while trying to deploy. After compiling the application with the "standalone" flag as instru ...

Handling onMouseLeave event for Popover component in material ui library with hiderBackdrop parameter specified

<Popover key={element.id} className={classes.popoverItem} classes={{ paper: classes.paperElement }} open={isOpen} anchorEl={this.myRef.current} anchorOrigin={{ vertical: 'bottom&apo ...

How to Enforce Rate Limiting in NextJS

After picking up ReactJS and NextJS, I find myself struggling to set up rate-limiting/throttling in my NextJS project. My goal is to restrict the number of times a user can make certain requests within a specific timeframe. ...

Avoiding the insertion of styles into the HEAD section when using Webpack MiniCssExtractPlugin in combination with Create React

Currently, I am utilizing create-react-app to develop a component library with Storybook JS. The ultimate goal is to release an NPM package containing these components for use in various projects. Within this library, SASS is employed, complete with global ...

What prevents me from displaying the image in the Bootstrap tooltip?

I am currently utilizing the Bootstrap framework v3.3.0 for my website. I'm trying to implement an image as a tool-tip when the user hovers their mouse pointer over an icon. Here is the HTML code I have: <div class="col-sm-5"> <div class= ...

Transforming a React application from ES6 hooks to Class-based components

Hello, I am new to React and currently facing a challenge in converting the following code into a Class Based Component. Despite knowing that I am going in the opposite direction, I am unable to figure out how to proceed without encountering errors. Any ...

The scroll feature in JavaScript is malfunctioning

After countless hours of troubleshooting, I still can't figure out why the code snippet below is not working properly on my website at : <script> $(window).scroll(function () { if ($(window).scrollTop() > 400) { ...

The data that was successfully edited in the express server and saved in Mongo DB is not being saved in the data state on the React client when sent back

This server api for the express saves edited data from the React client to Mongo DB. Take a look at the code snippet of the server api: app.put( '/car/update/:id', requireLogin, upload.array('imageFiles'), ...

Seamless flow from one image to the next

I'm working on a project with a slideshow, but I've noticed that when it transitions between images, it can be quite abrupt. I'd like to find a way for it to change smoothly from one image to the next. <div> <img class="mySli ...

What is the best way to transfer information from a server running Express.js to a client using React.js?

When using Express.js, I have the ability to send an HTML file that includes a React component. app.get('/index', function(req, res) { res.sendFile(path.join(__dirname + '/src/index.html')); }); Suppose I want to send a uniqu ...