Navigating through pages in React using Pagination Animations

Attempting to style an active button with a small transition when it receives that attribute has been a challenge. The same issue occurs with the paragraph rendered from the data file.

function Pagination() {
    const [selected, setSelected] = useState(0);

    function TabButton({ children, handleClick, index }) {
        return (
            <li><button active={selected === index ? "true" : "false"} onClick={handleClick}>{children}</button></li>
        );
    }

    function handleClick(index) {
        setSelected(index);
    }

    return (
        <div className="pagination-container">
                <div className="pagination-right">
                    <h3>Discover New Horizon</h3>
                    <div className="headers">
                        <ul>
                            <TabButton index={0} handleClick={() => handleClick(0)}>about us</TabButton>
                            <TabButton index={1} handleClick={() => handleClick(1)}>why choose us</TabButton>
                            <TabButton index={2} handleClick={() => handleClick(2)}>our mission</TabButton>
                        </ul>
                    </div>

                    <div className='paragraph'>
                        <p>
                            {pagins[selected].content}
                        </p>
                    </div>
                </div>
            </div>
        </div>
    );
}
.headers button {
    border: 0;
    color: #9b9b9b;
    border-bottom: 3px solid #9b9b9b;
    background-color: transparent;
    transition: all 0.3s ease-in-out;
}

.headers button[active="true"] {
    color: var(--primary-color);
    border-color: var(--primary-color);
}

.headers button:hover {
    color: var(--primary-color);
}

.paragraph {
    transition: opacity 0.3s;
}

Changing the active state based on the match between index and selected triggers a color change with a border for the button, however, this occurs instantaneously without the intended transition. Hover transitions work as expected but not in this specific scenario.

Answer №1

Concern : The correct way to identify attributes is through the data-* pattern rather than using the active attribute in the <button>.

  • The active attribute is not valid for a button element.
  • For more information on
    data-*<a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/data-" rel="nofollow noreferrer">*</a>
    , refer to the documentation.

Resolution: It would be better to utilize ClassName instead. I have included the className utilizing the same logic below.

function TabButton({ children, handleClick, index }) {
    return (
        <li>
            <button 
                className={selected === index ? "active" : ""} 
                onClick={handleClick}
            >
                {children}
            </button>
        </li>
    );
}

CSS

.headers button {
    border: 0;
    color: #9b9b9b;
    border-bottom: 3px solid #9b9b9b;
    background-color: transparent;
    transition: all 0.3s ease-in-out;
}

.headers button.active {
    color: var(--primary-color);
    border-color: var(--primary-color);
}

.headers button:hover {
    color: var(--primary-color);
}

.paragraph {
    transition: opacity 0.3s;
}

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

What is the method for retrieving the index of an array from an HTML element?

Howdy! Within my Vue application, I have the ability to create multiple individuals: <div v-for="newContent in temp" :key="newContent.id" @click="showId(newContent.id)" v-show="true"> <!-- ...

What are the steps to implementing a JavaScript WebWorker in a webpack environment?

Struggling to implement web workers in my web app and encountering difficulties. The addition of a new entry to the webpack.config.js file is not producing desired results. Attempting to utilize the npm package named worker-loader, however, facing challen ...

Localization of text in jQuery timeago.js

I have implemented J Query time ago to display date and time on my website. I am currently working on a multilanguage website where I want the time ago message to show as "1 min ago" for English users and "1 دقیقه قبل" for Farsi users. Can I achi ...

Using only CSS to swap out periods in OL>LI elements for a different style

Is there a way to customize the punctuation used in lists in HTML and CSS? For instance, can the standard period at the end of list items be changed to a right parenthesis or removed altogether? Concept Below is an example of improper CSS attempting to a ...

How can I automatically center a Vimeo iframe vertically without having to manually adjust the position?

I'm trying to adjust a popular fluid jQuery script so that it can automatically center a vimeo video vertically, without any black bars. A little horizontal cropping is acceptable. Here is my current code: HTML <div class="container"> < ...

AngularJS modules are not loading dependencies such as constants or controllers

While searching for the reason why a properly defined factory is not loading, I came across this question. This led me to contemplate on the concept of services being "defined in an angular module". Consider this example: angular.module('d3', ...

How to troubleshoot when Reactjs doesn't render anything after successfully posting a new note using axios to the server?

I am currently working on updating data in the backend using a json server. I've noticed that when I refresh the server, the new data gets posted successfully; however, the render of the updated list disappears from the screen after submitting. Can an ...

Tips for aligning Material UI Dialog in the middle of its parent component

Looking to adjust the positioning of the Material UI Dialog according to its parent element. Specifically, aiming to center it within the second grid item. //parent element <Grid container> <Grid item xs={5} style={{ border: "1px solid ...

How about adjusting this RPG Battle Sequence?

Both the client and server sides of my game are built in JavaScript. I have opted not to implement a master game loop since combat is infrequent and nothing else in the game requires one. When two players engage in combat, they enter an auto-attack loop as ...

Issue encountered while attempting to parse JSON data

I've been dealing with JSON data in Django view and parsing it in JavaScript to extract the necessary information. This is a snippet from my view.py file (Django) ibms = [] for i in range(2, 5): ibm = Mapa(i, wsMapa) ibms.append(ibm.__dict__) ...

What is the best method for incorporating a custom font with @fontface in Storybook version 6.5.5?

I have attempted multiple online solutions to incorporate a custom font in Storybook, but unfortunately, none have been successful. Here are the files that I have added under the .storybook folder based on this guidance: webpack.config.js : const pat ...

The custom created THREE.Curve is not rendering correctly

Following advice from this solution, I have implemented a linearly interpolated curve in the code below: THREE.Linear3 = THREE.Curve.create( function ( points, label /* array of Vector3 */) { this.points = (points == undefined) ? [] : points; ...

I'm experiencing difficulties in installing dependencies for my project through npm, as it keeps showing an error

Recently, I decided to delve into the world of HTML5 and javascript games by running a browser game from Github. The game that caught my eye is called BrowserQuest, and you can find it on Github. A hiccup arose while using npm debug: 237 error 404 Not ...

Curved divs display outlines on more compact dimensions

Check out this relevant Codesandbox There seems to be a recurring issue in my application where rounded divs display edges when they are of smaller sizes. Refer to the image below for a visual representation. What could be causing this problem and how can ...

Is it possible to evaluate the complexity of automating the user interface of a web application?

What are some common indicators of the challenges involved in automating the frontend of a web application? One example could be ensuring that all “critical” elements have stable ID attributes that do not change frequently. Are there any other similar ...

Arrays in Javascript (correlating)

I'm having trouble figuring out what needs to be included in this code (I apologize that it's in German, it's an array corresponding to images for JavaScript.) function namenZuBildern(){ var bilder = new Array( "000227", "000228", " ...

Having trouble changing a state from a class component to a function component in React

I've been updating my React projects by converting all my classes to functions. However, I encountered an issue where ESLint is reporting a parsing error in my code. Oddly enough, if I remove just one line, the error disappears. Here's the snippe ...

Customize your select element's background using jQuery when the content changes

Here is a select element with different options to choose from: <select class="state"> <option value="1">Done</option> <option value="2">Closed</option> <option value="2">Open</option> <option v ...

What is the best way to arrange the JSON data in React Native using the map function

I have a JSON format like this: { "name":"xyz", "age-group":"bb" } How can I resolve the issue with this JSON? Here's the code I'm currently using: const array = [{ "name":"xyz", &q ...

Converting JSON into key-value pairs using ReactJS

Here is a JSON object that I have: [ { "crime": "LARCENY-NON_VEHICLE", "count": "23217" }, { "crime": "AUTO_THEFT", "count": "13675" ...