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:

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

I'm having trouble figuring out how to remove an unexpected blank space that appears after the <li> element in a basic drop-down menu code. Can

Why is the browser (Mac Chrome + Firefox) adding extra space after my simple CSS drop-down menu where I have a hidden sub-menu? Check out the code below: <ul> <li>Option Zero</li> <li>Option One Is Longer</li> &l ...

Building collapsible child rows using Material UI Grid in React

In my React application, I am utilizing the Material UI Grid component to display grids based on data retrieved from an external API. One of my requirements is to incorporate a feature that allows users to expand rows to reveal additional child content ben ...

IE11 Error: Script1003 expected but not found

I'm in the process of adding IE11 support, but encountering the following errors: SCRIPT1003: Expected ':' File: vendor.bundle.js, Line: 8699, Column: 8 SCRIPT5009: 'webpackJsonp' is undefined File: app.bundle.js, Line: 1, Colum ...

encountering issues with configuring TypeScript LSP in NeoVim with the use of the lazy package manager

Encountered an error in nvim when opening a .ts file. Using mason, mason-lspconfig, and nvim-lspconfig for lsp setup. Lua language lsp is functioning properly, but facing errors with ts files as shown in the screenshot below: https://i.stack.imgur.com/gYM ...

My element is not being animated by Elementbyclass

Without Using JQUERY The animation I'm trying to create isn't functioning properly. I attempted to utilize document.getElementsByClassName, but it's not working as expected. There are no errors, but the element is not animating correctly. ...

Apple StoreKit API Integration in React Native (ExternalLinkAccount)

Recently, I submitted my "reader" app to the app store and encountered an issue with ExternalLinkAccount after the IOS 16 update. Apple now mandates the use of the `open()` method instead of creating a custom modal component. Is there a way to call the `op ...

What is the best way to implement a CSS Style specifically for a glyphicon icon when it is in keyboard focus?

I have a particular icon representing a checkbox that appears as a glyphicon-star. It is designed to display in yellow when focused on by the keyboard navigation. This feature is intended to help users easily identify their location on a page. However, an ...

Material-UI: Avoid onClick event firing when clicking on an element that is overlapped by another in a sticky Table

I have a unique setup in my table where each row, including the header row, begins with a checkbox. This header row has a sticky property. As I scroll through the table, rows start to move behind the header row. If I try to click the checkbox in the heade ...

When words are enclosed in a table cell, the fixed table layout ensures that all columns are the same size

There are times when long words in my HTML table cells need to be broken if they overflow their designated area. To learn how to break table cell words, check out this question: Word-wrap in an HTML table The recommended CSS style is: style="word-wrap: ...

What is the solution to the question: "Hey there, time traveler. We are currently in an era where CSS prefixes are no longer necessary, thanks to the advances in prefix-less CSS

I'm having an issue in my Next.JS app with importing my stylesheet in _app.js. Here is how I currently import it: import '../public/css/Index.css'; The content of Index.css looks like this: .index-container { margin: 20px auto 0; } Wha ...

Utilize React to iterate through a dictionary and display each entry

Essentially, I am pulling data from my API and the structure of the data is as follows: { "comments": [ { "user": "user1" "text": "this is a sample text1" }, { "user": "user2" "text": "This is a simple text2" }, } ...

A React child error has occurred in Next.js due to invalid objects being used

Within my project, I have integrated the latest version of next.js and encountered an issue where objects are not valid as a React.js child. https://i.stack.imgur.com/MCO7z.png The problem arises after importing the Head component from Next.js and implem ...

Tips for Sending Props While Utilizing CSS Modules

I've been working with a button component that utilizes the Tailwindcss framework and css modules for some extra styling. It currently looks like this, incorporating template literal to integrate the red background styling. CSS Module: .red { back ...

What are some methods for utilizing the "name" attribute within React components?

About My Coding Environment Utilizing TypeScript and ReactJS The Issue with Using name as an Attribute Encountering the following error: Type '{ name: string; "data-id": string; "data-type": string; }' is not assignable to ...

Utilizing TypeScript to export a class constructor as a named function

Imagine you have this custom class: export class PerformActionClass<TEntity> { constructor(entity: TEntity) { } } You can use it in your code like this: new PerformActionClass<Person>(myPersonObject); However, you may want a more co ...

Using TypeScript to retrieve a strongly typed promiseValue from an angular $modalInstanceA

New to TypeScript Question: I'm working on returning a strongly typed promise from angular's $modalInstance. Here is an example of what I have: this.$modal.open(options).result.then(result => { At the moment, 'result' is of typ ...

Enhance the appearance of specific wordpress usernames by adding a touch of "flair" next to them

I'm looking to add a special "flair" next to specific users on my WordPress website, similar to how YouTube distinguishes verified users. I have the CSS for changing the color on hover, but I need help keeping it positioned correctly. Examples from Y ...

The issue arises when Jest fails to align with a custom error type while utilizing dynamic imports

In my project, I have defined a custom error in a file named 'errors.ts': export class CustomError extends Error { constructor(message?: string) { super(message); Object.setPrototypeOf(this, Error.prototype); this.nam ...

Div text with superfluous line breaks

Whenever a user clicks the sign-up button ("Cadastrar" in Portuguese) on my website, the newsletter form successfully hides. However, there seems to be unnecessary line breaks in the success message. Why is this happening? I am trying to make sure that th ...

incorrect request body in axios post

I am currently facing an issue while connecting my react app to my node api. I have successfully retrieved data, however, the format of the body is incorrect when attempting to post data. I referred to the axios documentation for guidance. Below is a snip ...