Choose the sequence in which CSS Transitions are applied

Currently facing an interesting challenge, but I'm confident there's a clever solution out there.

The issue at hand is with a table that has three different sorting states for its columns: unsorted, where no icon should be displayed, ascending, marked by an up arrow, and descending, indicated by a down arrow.

Clicking on the column header should cycle through these three states.

starts off => click => ascending => click => descending => click => back to off

All of this functionality works smoothly, except for one requirement - I need to use the same Caret element and apply CSS transitions to animate it from opacity: 0 to opacity: 1, then rotate it 180deg on click to show a downward arrow for descending sorting. However, when removing the sort, I want the Caret to fade out without rotating back to 0 degrees.

This specific aspect is posing a challenge for me.

EDIT

I have replicated the behavior in a sandbox, but in reality, I am utilizing react-table, which only offers three potential states being controlled by the package:

initial state: {showCaret: false, flipped: false}
first click: {showCaret: true, flipped: false}
second click: {showCaret: true, flipped: true}
third click, back to initial: {showCaret: false, flipped: false}

The transition between these states is managed by react-table, so setting a setTimeout on the flipped variable is not feasible.

I am seeking a purely CSS-based solution to achieve this goal without altering the state transitions, if possible.

END EDIT

I have included a codesandbox link for demonstration purposes. Click Show Caret first, then Flip Caret, followed by Hide Caret. The CSS setup here mirrors what I have implemented in my actual project as well.

https://codesandbox.io/embed/admiring-rain-svsc9?fontsize=14&hidenavigation=1&theme=dark

Answer №1

If you're looking to make the arrow vanish without returning to its original position, a simple solution is to adjust the state of visibility and rotation separately in React.

Considering that you're already managing everything with React state, you can set these two states independently, each with a delay of 0.3 seconds (your CSS transition time).

There are several ways to achieve this. To illustrate, I have modified your example in this fork where the visibility is turned off first, followed by monitoring when it's hidden in componentDidUpdate. Once it detects that the arrow has disappeared, it waits for 300ms (.3s) before resetting the rotation state back.

componentDidUpdate(oldProps, oldState) {
    if (oldState.showCaret && !this.state.showCaret) {
      //it was just hidden
      setTimeout(() => {
        this.setState({
          flipped: false
        });
      }, 300);
    }
}

Check out the modified example here.

UPDATE, using CSS only approach

Here's an alternative solution relying solely on CSS.

/* Handling the rotation of the img tag through 'flipped' */
.image {
  transition: transform 0.3s linear 2s;
}
.flipped {
  transform: rotate(180deg);
  transition: transform 0.3s;
}

Answer №2

Modify


    onPress={() => this.setState({ displayArrow: false, reversed: true })}

Into


    onPress={() => this.setState({ displayArrow: false })}

and your code will function properly.

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 toggling the visibility of a revolution slider based on current time using JavaScript

Currently, I am integrating the revolution slider into my WordPress website. My goal is to have the slider change according to the standard time of day. For instance, I want slider1 to display in the morning, slider2 at noon, slider3 in the evening, and sl ...

The chat server powered by Node.js for telnet communication

I'm currently working on developing a telnet chat server using node.js However, I'm facing an issue where the text I enter in multiple terminals is not being transmitted to other terminals. After opening several terminals, I enter the following ...

The getJSON function is showing [object Object] instead of the expected values

Having trouble displaying JSON data in an HTML list? When trying to loop through the JSON file, you might encounter issues such as displaying [object Object]. Check out the script below for assistance: Here is the original JSON: [ { "us":"USA ...

What is the best way to reset a form upon submission using Redux Form?

Currently, I am in the process of developing a basic portfolio website that includes a Contact form section. This section allows users to send me messages directly to my email using Formspree. To achieve this functionality, I have implemented Redux Forms. ...

I am in need of assistance with styling Material UI elements. Can anyone lend a hand with

https://i.stack.imgur.com/37daN.png The image above shows my current layout. I would like the icon to appear on the right-hand side instead. I am currently using material-ui design in my project. Is there a way to achieve this positioning? import React ...

Upon the initial page load, the create-react-app is rendered without any content

I created an app that initially shows a blank page upon startup, but strangely it loads correctly when the page is refreshed. Here is my code: index.js import React from 'react'; import ReactDOM from 'react-dom'; import './Asset ...

Leverage AJAX for real-time Django Model updates

Seeking insights on how to effortlessly update a Django model through AJAX without reloading the page or requiring user input for saving. Various tutorials address fetching data from Django models using AJAX, yet resources on updating models remain scarce. ...

React/Ionic: Avoiding SVG rendering using <img/> elements

I seem to be encountering an issue when trying to load SVG's in my React/Ionic App. I am fetching weather data from OpenWeatherMap and using the weather?.weather[0].icon property to determine which icon to display. I am utilizing icons from the follow ...

Make HTML design responsive to any screen size

After completing the design of a mobile app interface with dimensions 750w by 1334H, I encountered an issue where it appears too large on the app testing screen. I am seeking a way to automatically adjust the design to fit all screens without compromising ...

Tips for enabling Azure Blob Storage file uploads in a React app while staying compliant with Content Security Policies

I am using an Azure Storage Account and a container to upload files from my React application. Through an Azure Function, I receive SAS Keys with the necessary permissions for file uploading. My CORS settings in the storage account allow all connections. ...

Show the key and value of a JSON object in a React component

When attempting to parse a JSON data file, I encountered an error message stating: "Element implicitly has an 'any' type because expression of type 'string' can't be used to the index type." The JSON data is sourced locally from a ...

Issue with React.JS: Hash route navigation failing to refresh view

Recently, I started learning React and currently following a course on Pluralsight. The tutorial includes building a simple route. var App = React.createClass({ render: function () { var Child; switch (this.props.route) { ...

Issue with Axios response processing

I need to upload a new document into a database using Axios in React. I have successfully implemented the functionality, but I also want to display a message in the console saying "New post has been inserted". This is my front end code: newTodo(todo){ ax ...

How can we dynamically render a component in React using an object?

Hey everyone, I'm facing an issue. I would like to render a list that includes a title and an icon, and I want to do it dynamically using the map method. Here is the object from the backend API (there are more than 2 :D) // icons are Material UI Ic ...

Troubleshooting issue with changing class based on input values

It appears that there is an issue with the functionality when switching values on page load. Initially, I was able to make it work for a single switch, but now that there are multiple switches on the page, toggling affects all of them. How can I modify it ...

The checkbox labeled "Shipping Same as Billing" is not functioning correctly. I have included my JavaScript code below, can someone please help me identify what I am overlooking? (Only JavaScript code is provided; HTML is

I'm having trouble transferring data from a 'shipping' address to the 'billing' address section. I've included all my code, but nothing seems to copy over when the check box useShip is selected. All the HTML code is provided f ...

What is the best way to modify a constant array in Angular?

Hello team, I'm fresh to working with angular and I have a TypeScript file that contains a list of heroes: export const HEROES: Hero[] = [ { id: 11, name: 'Dr Nice' }, { id: 12, name: 'Narco' }, { id: 13, name: 'Bombas ...

app launching error encountered on react native android

V/SoLoader: libimagepipeline.so file not found at /data/data/com.learnapp/lib-main D/SoLoader: Found libimagepipeline.so at /data/app/com.learnapp-liVxYGD3IHkNE51rFYNd0g==/lib/x86, but not resolving dependencies D/EGL_emulation: eglMakeCurrent: 0xa9905ea0: ...

What is the best way to keep wp_nav_menu expanded while accessing submenu links?

Recently, I joined the Wordpress community and am currently in the process of converting a static HTML site to the WP platform. Almost everything is working smoothly except for the navigation menu. I am attempting to utilize the built-in navigation featur ...

Modifying the dimensions of a text input box within an HTML string using React

I'm currently utilizing a popup library called sweetalert2-react-content to generate a popup box with a textbox and a text area. Even though the elements are being created successfully, I am struggling to adjust the size of the text area in the popupb ...