Learn how to implement a rotation effect on a pseudo-element in ReactJS with clickable functionality, specifically designed for SVG

[images for my project]

[1]: https://i.sstatic.net/a6EFH.png

[2]: https://i.sstatic.net/lMhNu.png

Clicking on the orange arrow should trigger a 180-degree rotation.

Can this be achieved using CSS or do I need to utilize JavaScript?

Also, how can I declare a variable and assign a pseudo-element as its value in ReactJS?

<div className="container">
        <div className="imgBox">
          <img src={img_1} alt="img_1" className="img_1" />
          <img src={img_2} alt="img_2" className="img_2" />
        </div>

        <div className="textBox">
          <h1>FAQ</h1>
          <div className="questions">
            <div>
              <button
                onClick={() => {
                  hideText(p1);
                }}
              >
                How many team members can I invite?
              </button>
              <p ref={p1} className="p1 not-active">
                Lorem ipsum dolor sit amet consectetur, adipisicing elit.
                Temporibus obcaecati, quae, corporis.
              </p>
            </div>

            <hr />

            <div>
              <button
                onClick={() => {
                  hideText(p2);
                }}
              >
                How many team members can I invite?
              </button>
              <p ref={p2} className="p2 not-active">
                Lorem ipsum dolor sit amet consectetur, adipisicing elit.
                Temporibus obcaecati, quae, corporis.
              </p>
            </div>

            <hr />

            <div>
              <button
                onClick={() => {
                  hideText(p3);
                }}
              >
                How many team members can I invite?
              </button>
              <p ref={p3} className="p3 not-active">
                Lorem ipsum dolor sit amet consectetur, adipisicing elit.
                Temporibus obcaecati, quae, corporis.
              </p>
            </div>

            <hr />

            <div>
              <button
                onClick={() => {
                  hideText(p4);
                }}
              >
                How many team members can I invite?
              </button>
              <p ref={p4} className="p4 not-active">
                Lorem ipsum dolor sit amet consectetur, adipisicing elit.
                Temporibus obcaecati, quae, corporis.
              </p>
            </div>

            <hr />

            <div>
              <button
                onClick={() => {
                  hideText(p5);
                }}
              >
                How many team members can I invite?
              </button>
              <p ref={p5} className="p5 not-active">
                Lorem ipsum dolor sit amet consectetur, adipisicing elit.
                Temporibus obcaecati, quae, corporis.
              </p>
            </div>

            <hr />
          </div>
        </div>
      </div>

Styling

.container>.textBox>.questions>div>button {
  width: 100%;
  height: 30px;
  background: transparent;
  color: var(--Very-dark-grayish-blue);
  font-size: 15px;
  text-align: left;
  border: 0;
  cursor: pointer;
  outline: none;
}

.container>.textBox>.questions>div>button:hover {
  color: var(--Soft-red);
}

.container>.textBox>.questions>div>button:focus {
  font-weight: bold;
}

.container>.textBox>.questions>div>button::after {
  content: url(./images/icon-arrow.svg);
  float: right;
}

.container>.textBox>.questions>div>button::after:focus {
  transform: rotate(180deg);
}

Answer №1

Without seeing your markup, you can achieve a cleaner design using just html/css with the <details> disclosure element. Take a look at the MDN documentation for more information: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/details

You have the ability to fully customize its style, including hiding the marker pseudo-element. Check out a specific solution in this thread that may be helpful: HTML Details/Summary Element Marker Styling

Below is a code snippet to help you get started. I've created the arrow using CSS since I don't have your SVG. You'll need to adjust the :after selector with your background-image and do the same for the [open] selector.

details {
  width: 400px;
  border: 1px solid #ccc;
  padding: 10px;
}

details summary:after {
  border-bottom: 2px solid orange;
  border-right: 2px solid orange;
  content: "";
  display: inline-block;
  height: 7px;
  margin: 2px 0.5rem 0 0;
  float: right;
  transform: rotate(45deg);
  width: 7px;
}

details summary::-webkit-details-marker,
details summary::marker {
  display: none;
  content: "";
}

details[open]>summary:after {
  transform: rotate(-135deg) translate(-4px, -3px);
}
<details>
  <summary>Section title</summary>
  <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
    has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
    publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
</details>

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

Increasing counter on the backend using PHP or JavaScript

Having done my research, I am struggling to find a suitable solution for my project. I need a server-side incremental counter that resets daily. The goal is for every website visitor to see the same number on the counter. Is there a PHP script or JavaScr ...

Launching ReactJS Loading Screen

I'm currently facing an issue while trying to set up a loading screen using an animation sourced from LottieFiles. It appears that I am encountering some errors, possibly due to syntax issues which I am unable to troubleshoot on my own. Any assistance ...

Cease the execution of a task in Express.js once the request timeout has been

Suppose we have the following code snippet with a timeout of 5 seconds: router.get('/timeout', async (req, res, next) => { req.setTimeout(5000, () => { res.status(503) res.send() }) while (true) { ...

What is the most effective way to transfer visitor hits information from an Express.js server to Next.js?

I've developed a basic next.js application with an express.js backend. What I'm aiming for is to have the server track hits every time a user visits the site and then send this hit count back to the next.js application. Check out my server.js cod ...

jQuery - How come my string values are getting cut off?

It's been a while since I last worked with jQuery, and I seem to be missing something obvious that's affecting my calculations. I have various text boxes for Weight, Moisture %, and Number of Filled Squares, as well as a multi-select option for ...

Content not refreshing when closing and reopening Highslide iframe

I am encountering an issue with Highslide where I am unable to reopen a popup with new content. Even though the contentId remains the same, the content is not being updated when the popup is reopened. Below is the code snippet that showcases the problem: ...

Extracting information from JSON and presenting it in a structured table format

I've hit a wall with this JavaScript issue on my website. I'm trying to convert API JSON data into a table, and it's working fine when the data is on separate lines. However, when I introduce nested arrays in the JSON data, everything ends u ...

Differences between Array and Database Search

Currently, I have implemented a system where I store a refresh token in a JavaScript array as well as in each user's information table. When a user requests data, I first check the token in the array. If the token matches one in the array, I loop thro ...

Utilize JavaScript's Map function to employ generalized keys in an array of objects

I am dealing with an array of objects that looks like this: [ { "job_id": 1, "job_name": "Engineer" }, { "job_id": 2, "job_name": "Scientist" }, ...

The Bootstrap flex-grow-0 class did not work as intended in my navigation bar

My webpage has a navbar that currently looks like this Click here for an image example of the code output This is the code I have written for the navbar: <nav class="navbar navbar-expand-sm fixed-top bg-white"> <div class="container my-2" ...

Leetcode Algorithm for Adding Two Numbers

Recently, I attempted the following leetCode Problem: However, I encountered an issue where one of my test cases failed and I'm unsure why. Here is the scenario: You are provided with two non-empty linked lists that represent two non-negative integ ...

Exploring dynamic patterns in react-three-fibre with animated textures

Currently, I am integrating the three-plain-animator package into my project to implement animated textures in my mesh. However, I am encountering an issue where the texture is loaded successfully but fails to animate when using React Three Fiber instead o ...

Using REST APIs in React programming

While working on an existing code base for a React application, I found myself in a situation where the original developer had left and I am still quite new to React. In the current code base, the library "request-promise-native" is being passed from one c ...

Fixing a Dropdown Problem in Codeigniter

I have encountered a problem with the Dropdown pop-up feature. Specifically, when I click on the "Add New" option in the Dropdown, the pop-up window fails to open. Here is the code for the Dropdown: echo form_dropdown('Birth_Certificate_Storage_id[& ...

When useEffect continually updates its own dependency, it results in an endless loop

Within my component, I have a feature that showcases a table containing user information. The users are fetched from the redux state using getOverviewCitizensToday() and then passed to the component as a prop using mapStateToProps. In order to retrieve the ...

You can use the following code to retrieve the value of the checked radio button with the name "nameOfradio", but make sure that the radio button

I am attempting to extract the value from a radio button using the following code: document.querySelector('input[name=nameOfradio]:checked').value; However, I would also like to retrieve a value even if none of the radio buttons are checked. Fo ...

Can divs be arranged in a similar fashion as images?

My goal is to create a navigation bar (#nav) with each nav item being a div (#nav div). However, my current approach isn't working as expected. Here is the code I'm using: #nav { background: url("navbg.png"); /* navbg.png is 1x40 pixels */ ...

Guide on pushing a nested array of objects into another object as a nested array of objects using MongoDB and NodeJS

I am working with a "cart" object and an "order" object. The "cart" object has the following structure: { "_id" : ObjectId("cart123"), "userId" : "user123", "email" : "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" ...

Tips on incorporating animations into a class design?

I'm trying to figure out how to make an image disappear by adding a class However, when I try this, the element vanishes without any animation I'd like it to fade away slowly instead I've heard there are CSS3 properties that can help achi ...

The legend color in a JavaFX StackedBarChart does not adhere to the chart color CSS styling

Within my application, using JDK version 1.8u51, I need to assign specific colors to different data categories in a StackedBarChart. To accomplish this, I have created the following CSS: .root{ -fx-ok-color: darkgreen; -fx-critical-color: darkblue ...