Steps for modifying the look of a button to display an arrow upon being clicked with CSS

Looking to enhance the visual appearance of a button by having an arrow emerge from it upon clicking, all done through CSS. Currently developing a React application utilizing TypeScript.

Upon clicking the next button, the arrow should transition from the current button to the next one in line.

A visual representation of the desired outcome can be found here: https://i.sstatic.net/OIdZL.png

This snippet provides an example featuring two buttons:

 
<div className="button-container">
    <Button
        className="text-white font-nunito text active"
        onClick={() => onFieldAdd('textField')}
    >
        <TextFieldsIcon />
        <p>&nbsp;&nbsp;&nbsp;Text Box</p>
    </Button>

    <Button
        className="text-white font-nunito text mx-2 pr-15"
        onClick={() => onFieldAdd('imageField')}
        disabled={!!formId}
    >
        <AddPhotoAlternateIcon />
        <p>&nbsp;&nbsp;&nbsp;Image</p>
    </Button>
</div>

Answer №1

If you're searching for a solution, this might be what you need:

import { useState } from "react";

const CustomComponent = () => {
    const [selectedButton, setSelectedButton] = useState(null);

    const handleClick = (e) => setSelectedButton(e.target.id);

    return (
        <div>
            <button id="btn1" onClick={handleClick} className={selectedButton === "btn1" ? "hasArrow" : ""}>
                Button 1
            </button>

            <button id="btn2" onClick={handleClick} className={selectedButton === "btn2" ? "hasArrow" : ""}>
                Button 2
            </button>

            // REPEAT THE PROCESS FOR OTHER BUTTONS
        </div>
    )
}

export default CustomComponent;

Feel free to modify it according to your specific requirements.

In this code snippet, I set up a state named selectedButton with a default value of null, and I'm monitoring onClick events on all buttons to update the state with the clicked button's id. When the state changes, the component will refresh, and the CSS class hasArrow will be assigned to the appropriate button based on whether the selectedButton state matches the button's id, using the ternary conditional operator ?:.

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

ReactJS component update issueUpdate function malfunctioning in ReactJs

Hey there, I could really use some assistance with a React component that I'm working on. I'm fairly new to React and what I'm trying to do is retrieve a list of available languages from the backend and display them in a dropdown. Once the u ...

Highchart displays text centrally on legend hover

I am struggling with the code provided here. My goal is to make text appear in the center of the donut chart when hovering over the legend. Similar to how it works when hovering over a single piece of the donut chart. var chart = new Highcharts.Chart ...

What is the reason behind causing the overflow to become visible on the parent anchor when a child anchor is added?

I've been facing a puzzling issue for the past day. I am working on creating a website that resembles Windows 8, where each box represents a list item. The code is more complex than what I have shared here, but the problem seems to be isolated... &l ...

Creating a hover effect for displaying image masks

My website features a profile page displaying the user's photo. I am attempting to create a functionality where when the user hovers over their photo, a transparent mask with text appears allowing them to update their profile picture via a modal. How ...

Feeling lost when it comes to forms and hitting that submit button?

Below is a sample form structure: <html> <head> <title>My Page</title> </head> <body> <form name="myform" action="http://www.abcdefg.com/my.cgi" method="POST"> <div align="center"> <br><br; <br& ...

Error encountered when compiling SCSS with the '@use' statement

Currently, I am utilizing Gulp for the purpose of compiling scss into css. However, whenever I include a @use statement in my code, it does not compile the css correctly into the css file; instead, it simply duplicates the @use statement. What could be cau ...

Steps for achieving uniform positioning of a div element that dynamically appears within a table with two rows and four table data cells

If I want to display the same div in different positions dynamically on my page, how can I achieve that? My page has a table with two rows and two columns, each containing an Embed button. When these buttons are clicked, a div is shown with a list of site ...

Internet Explorer experiences performance issues when a table containing over 500 rows is being utilized

Can anyone offer advice on speeding up the display of large tables with 500+ rows in Internet Explorer? Here is my current approach: The MySQL query result includes 500+ rows, which I then loop through using a while loop to display: echo "<tr class=& ...

Exploring firestore information using react hooks

I am currently exploring how to retrieve data from Firestore using React hooks. To achieve this, I followed the example provided in this blog post. A record was created in the "impact_metrics" collection in Firestore. My objective is to display this dat ...

The second entry is not being styled by CSS, is there an issue with the code in this section?

I am facing a challenge while trying to set the same background image for two div elements: .abc { background: url('images/autogen.png') no-repeat 0px -133px; width: 256px; height: 256px; }; .def { background: url('images/autogen.png' ...

After launching a fresh API on AWS Api-Gateway, I attempted to send a request only to receive an error indicating that I do not have the proper authorization to access

I've implemented a request to another API owned by Timetastic, a platform that helps manage work holidays. Here is the snippet of code from my React app: const token = process.env.TIMETASTIC_TOKEN; get timetasticAPI() { return new Api(token); } ge ...

Typescript's interface for key-value pairing with generic types

Consider the example Object below: let obj1: Obj = { 'key1': { default: 'hello', fn: (val:string) => val }, 'key2': { default: 123, fn: (val:number) => val }, // this should throw an error, because the types of d ...

What is the reason for the presence of "e034" in CSS not resulting in the creation of a tick icon?

Here is the CSS code I am using: <style> .icons:before { content: "\e034"; color: #00bc9b; font-size: 16px; margin-top: -1px; margin-left: -1px } </style& ...

What is the best way to revert the Highcharts bar color back to its default color?

I am currently working with Highcharts version 4.1.10 In my bar chart, I have implemented a feature where the color of the bar changes when its value exceeds a certain threshold. //This function is called at regular intervals by a timeout function myTime ...

Is it possible to execute TestCafe tests using TypeScript page objects that have not been utilized?

While working with TestCafe, I am implementing tests using the Page Objects pattern. I have already written some page objects in advance, even before their actual usage, as I am familiar with the page and know what to anticipate. However, when attempting ...

Substitute data types for certain keys within a Typescript interface

Within this code snippet, the goal is to create a new type from an existing one by iterating through the keys and only replacing those that meet a specific condition. Additionally, union types are being utilized here. class A {} class B { constructor ...

Navigating through nested JSON Objects for dropdown functionality in Angular 6 - a step-by-step guide

Currently, I am facing a challenge in Angular 6.0 where I am trying to use HttpClient to iterate through JSON data retrieved from a local file within my assets folder. Below is the sample JSON Data: [{ "configKey": [{ "user1": [{ ...

Troubleshooting the NullInjectorError in Angular - Service Provider Missing?

I'm facing an issue in my code where I have buttons that should trigger pop-ups displaying details as a list when clicked. However, every time I click the buttons, I encounter the error mentioned below. It seems like I am unable to access the desired ...

AngularJS and CSS: A Guide to Effortlessly Toggle Sliding List Elements

I am in the process of developing a drop-down menu that can be clicked. Using my custom AngularJS directive, I have successfully implemented functionality to load menu items dynamically. While I have made significant progress, I have encountered a small i ...

Bizarre Drop-down Peculiarities

Having a perplexing issue where the 'search' list item is oddly shifted below the dropdown box only when it is displayed. The other list items remain in their correct positions. Any advice on how to resolve this? The appearance and functionality ...