Holding off on routing the pathName through the router until the next-page-transitions have been completed

In my current setup, I have two distinct elements at play. Using the code snippet below:

className={router.pathname.startsWith("/pagename") ? "menu-active" : ""}

I am able to apply the menu-active class to the pagename navigation link when we are on the pagename page.

Additionally, I have implemented the following code block which mirrors the functionality described in the next-page-transitions documentation:

<PageTransition timeout={450} classNames="page-transition">
      <Component {...pageProps} key={router.route} />
        </PageTransition>
        <style jsx global>{`
          .page-transition-enter {
            opacity: 0;
          }
          .page-transition-enter-active {
            opacity: 1;
            transition: opacity 450ms;
          }
          .page-transition-exit {
            opacity: 1;
          }
          .page-transition-exit-active {
            opacity: 0;
            transition: opacity 450ms;
          }
        `}</style>

This achieves a smooth 450ms fade-out effect between pages as intended.

The issue arises when transitioning between pages while using the menu-active class to update the font style of the active page link to italics and change its color to orange. It appears that the class is applied before the actual transition occurs; causing the menu change to happen prior to the white fade-out effect.

I attempted to rectify this by introducing a delay of 450ms for the transition, allowing the change to occur just as the fade completes. This solution worked well for adjusting the color but proved insufficient for modifying the font style due to the limitations of transition-delay.

As an alternative approach, I am exploring different methods to postpone the application of this class/style effectively. Any suggestions or recommendations to address this challenge?

UPDATE: Please note that I am open to alternatives beyond utilizing next-page-transitions if there are better solutions available for achieving the desired outcome.

Answer №1

To achieve the desired solution, consider creating two components that are identical except for their font family. By implementing a setTimeout function of 450ms, you can alternate between these components to change the font style effectively.

This approach may seem unconventional, but it addresses the challenge of applying transitions to fonts in a straightforward manner.

I will continue to explore alternative ideas and provide updates if any new insights arise.

Edit:

Here is a simple code example using styled-components and react-hooks. Styled-components offer a CSS-like syntax for styling, which can be easily translated into regular CSS. React-hooks such as useState and useEffect are essential concepts to understand for coding in React.


const FontX = styled.p`
  font-family: 'Andale Mono';
`;

const FontY = styled.p`
  font-family: Arial;
`;

const Container = () => {
  const [Component, setComponent] = useState(FontX);

  useEffect(() => {
    setTimeout(() => {
      setComponent(FontY);
    }, 450);
  }, []);

  return <Component>Look here! Font will change after 450ms</Component>;
};

Explore the live demo on CodeSandbox:

Live Demo Link

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

Adding an array of objects to a specific key using JavaScript

I'm facing a challenge with adding an array of objects to an existing array of objects using Vue.js. What I'm attempting to accomplish is creating a form repeater within another form repeater. While I was able to successfully implement the first ...

The Google timezone API is displaying an inaccurate daylight saving offset

London now has an additional +1 hour of daylight saving time. I made a request to the Google timezone API for the London timezone, but unfortunately, it is not displaying the correct time. https://maps.googleapis.com/maps/api/timezone/json?location=51.507 ...

Load Form with Json Data from LocalStorage Key-ID to Auto-Populate Fields

After successfully setting the ID in localStorage, I now need to retrieve and display only that specific record from my JSON data. The content of my localStorage is: This information is obtained from my JSON data after a button click. The challenge is us ...

The computer system encountered an issue in computing the values of the text

Possible Duplicate: Unable to get textfield value using jQuery My current project involves updating input fields based on user changes in quantity for items. Each item is retrieved from a database and I am generating invoices for customers. However, ...

Mastering Flowtype: Enhancing your understanding of arrays

Currently working with React and facing an issue with the following code: type Props = { children: SuperOption | HTMLHrElement | Array<SuperOption | HtmlHrElement>, } Further down in the code, there is: let arr: Array<SuperOption | HtmlHrEle ...

The Auth0 getTokenSilently function is not returning any value

I am currently working with the react-auth-spa.jsx file, /* eslint-disable react/require-default-props */ // src/react-auth0-spa.js import React, { useState, useEffect, useContext } from 'react'; import PropTypes from 'prop-types'; imp ...

Getting the value of "Page=?" from the href attribute in an HTML tag can be done using Selenium Webdriver and Java

I am looking to extract the value "page = ?" from a specific "href" tag in the HTML code below. I need this value for my Selenium WebDriver script so that my loop can iterate up to page 53. Can someone guide me on how to retrieve the "page =" value mentio ...

Obtain a fresh SQL identifier post submission through Ajax

When a comment is submitted on a post using ajax, the comment.php script will display the new comment with its own auto-incremented SQL id. It will look something like this: // The $id variable contains the SQL id after data submission <div class="comm ...

What is the best way to send parameters to an angular service or factory?

This Plunker demonstration is a great visual guide: http://plnkr.co/edit/y3uacaQSc1MbrWKfb0At?p=preview Here's the code snippet for reference: var app = angular.module('plunker', []); app.controller('MainCtrl', function($scope,t ...

Is it possible to utilize LESS to dynamically read the width?

My LESS code looks like this: &.settings-prompt .panel { margin-left: -190px; width: 380px; height: 200px; } &.create-playlist-prompt .panel { margin-left: -165px; width: 330px; height: 180px; } I want the .panel to have ...

What is the method for including a placeholder (instead of a label) in the MUI 5 DatePicker component?

I'm looking to customize the placeholder text in MUI 5's date picker. You can find the MUI 5 datepickerlink here: https://mui.com/x/react-date-pickers/date-picker/ The desired outcome:: I've tried referring to this chat, but it hasn't ...

Media queries for aspect ratios across all browsers

I have been exploring ways to modify background-size from 'contain' to 'cover' using media queries when the browser width results in an aspect ratio of less than 4:3. This adjustment creates a background image that may not be fully vis ...

Encountered an issue with setting the property of "something" to undefined when attempting to generate an array sorted by dates

After seeking help from @Kato on Stack Overflow regarding sorting a Firebase AngularFire array into weeks and days, I encountered an error where I cannot set a property of "something" that is undefined. To provide more context, I created a detailed Plunke ...

Tips on enlarging a webpage without showing the scroll bar on the main component

My goal is to create a webpage that resembles a window application. However, I've encountered an issue where zooming in on the page results in a scroll bar appearing on the main page. The parameters for the main page (the parent) are set as follows: w ...

Issues with Node AssertionErrors cause failures to be silent and prevent proper error output

I am facing an issue with a particular method in my code. The code snippet is as follows: console.log('Trouble spot here') assert(false) console.log('Will this show up?') Upon running this code within my application, the followi ...

Logging out of Laravel after sending a POST request

I'm developing a laravel application that heavily relies on POST requests. One common type of request in my app looks like this: var classElements = document.querySelectorAll("tr.ui-selected td.filename"); var csrf = $('input[name=_token]') ...

Angular II slash avoiding Pipe

I am working on developing a customized pipe in Angular 2 that will handle the replacement of the backslash ('\') character in a given string. This backslash is commonly used to escape special characters. What I have accomplished so far: T ...

The validation process encountered an error as it was unable to access the 'value' property of an undefined object, resulting

Greetings all, I am encountering an issue while attempting to validate the input component on React/NextJS. Unhandled Runtime Error TypeError: Cannot read property 'value' of undefined Source: .next\static\chunks\pages\index ...

What methods are recommended for expanding the size of a select/dropdown menu?

Managing a long list of values can be challenging, especially when it continues to grow. Consider the following example: <select> <option value="1">1, some test</option> <option value="2">2, some text</option> <optio ...

I'm struggling with developing react applications due to problems with canvas elements

I am currently using an M1 MacBook Pro, with Node version 15.4.1 and npm version 7.0.15. When I ran the command npx create-react-app my-app, it gave me this output: https://i.sstatic.net/OKKnA.jpg I have attempted various solutions but keep encountering ...