Clicking on a React component will result in highlighting

What's the best way to display the selected item after a user clicks on it?

   <Box py={2}>
      <Grid container width="330px">
        <Grid container direction="column" item xs align="left">
          <Grid item>
            <Typography
              variant="h6"
              className="locationTitle"
              display="block"
            >
              {' '}
              {location.name}{' '}
            </Typography>
          </Grid>
        </Grid>
      </Grid>
      <Typography variant="body" display="block">
        {' '}
        {location.address.street1}
      </Typography>
    </Box>

css

.css-1yjo05o:hover{
  background-color: lightgray;
}

I managed to get the hover effect working, but I ran into issues with the :focus and :target pseudo-classes.

https://i.sstatic.net/RxdcM.png

Answer №1

To maintain consistent styling, avoid directly styling the hashes created by the Mui styling engine as they are not static. Instead, opt for an onClick listener that can toggle a className (if CSS is being used) or an SX prop (if @mui/material is being used).

const [highlight, setHighlight] = useState(false)

<Box 
  py={2} 
  onClick={() => setHighlight(!highlight)} 
  sx={{ bgcolor: highlight ? 'lightgrey' : 'unset' }}
>

It's important to note that this approach may result in multiple boxes being highlighted simultaneously. If the goal is to highlight only one box at a time, the state should be managed in the parent component and the child's index should be used to toggle the highlight. View a demo here.

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

changing size when hovered over with the mouse is not consistent between entering and exiting

Hi there, I'm currently utilizing the transform: scale(); property on a website and could use some assistance with a particular issue I haven't been able to find an answer for online. Here is the code snippet I'm working with: HTML: <d ...

What is the mechanism behind the functioning of "3D Meninas" (CSS/Animation)?

Recently stumbled upon a fascinating website called "3D Meninas", showcasing an impressive 3D animation effect. Upon examining the HTML code, I noticed there was no mention of any <script> tags or events, leading me to believe that it operates solely ...

The loader image does not appear on mobile screens during an AJAX call, but it functions correctly on desktop screens

While I have successfully implemented a loader (.gif) using a div tag in an AJAX call for desktop screens, the same code is not functioning properly on mobile devices. Instead of displaying the loading animation, it simply shows a white screen. <div ...

Experiencing difficulty sending Redux state to child component

Being new to Redux, I decided to delve into the code of the initial example project provided in the Redux documentation. The example did not incorporate containers or separate action files, so my aim was to abstract it a bit. The absence of an App compone ...

Monitor changes in the visible price range in lightweight-chart

Is there a way to detect when the visible price range changes in lightweight-chart, similar to how the timeScale's visible time range change can be detected through subscribeVisibleTimeRangeChange? I couldn't find anything related in the document ...

A step-by-step guide to uploading MP3 files to GitHub and streaming them from a web browser

I have recently created a repository and uploaded some audio files into it. However, when I try to access the audio files directly by clicking on the link such as https://github.com/cre8ture/audioFilesForBL/blob/main/1.mp3, it takes me to the Github page i ...

Send the Children prop to the React Memo component

Currently, I am in the stage of enhancing a set of React SFC components by utilizing React.memo. The majority of these components have children and the project incorporates TypeScript. I had a notion that memo components do not support children when I en ...

Encountering a roadblock when attempting to redirect to a different page within a

I am facing an issue with my React page. When I click a button, the method studentApproval() is called successfully. However, after this method runs, I am trying to redirect to another page but it doesn't seem to be working. How can I resolve this red ...

Achieve text length that does not exceed the specified limit dynamically

Is it possible to determine the length of the visible portion of text that overflows (or calculate the size of the overflow for further processing) using CSS or JavaScript? If so, can this calculation be done dynamically (such as on window resize)? The g ...

I am working with three columns and I am looking to keep the center column fixed in place while scrolling

Check out this CSS snippet: #container { margin: 0 auto; width: 1200px; } #col1 { float: left; width: 700px; } #col2 { float: left; width: 100px; padding: 0 17px; } #col3 { float: left; width: 400px; } I am trying t ...

Exporting Next.js sitemap statically

Can anyone assist me in creating a Sitemap for my NextJs project? I have developed a headless CMS using GraphQL and Next, with everything being statically generated. However, I am facing challenges when it comes to generating a sitemap. I attempted to use ...

Replace background image using styled-components

My goal is to dynamically change the background image, but I'm encountering some issues. When I try to do it using the code snippet below, it doesn't work as expected. However, when I open the devtools in Chrome, I can see that the background cha ...

Importing CSS conditionally in NextJS allows for better organization

Is there a way to conditionally import a CSS stylesheet for the entire application? {process.env.PARAMETER ? <link href="/localfonts.css" rel="stylesheet" /> : ''} I tried adding this code snippet to document.tsx in the ...

The transparency of my navbar renders it elegant, yet I desire to imbue the toggle background with a vibrant hue

I'm facing an issue with the transparent Navbar I built in Bootstrap 5. On mobile toggle view, the navigation links are getting lost within the text of the hero image. To resolve this, I want to keep the main navigation bar transparent and change the ...

Tips on incorporating CKEditor4 wiris MathML formulas into react JS

I am having trouble displaying MathML in the DOM. When I try to render it, the output is not showing correctly in the Editor. I am utilizing CKEditor4 Let me share the code below to provide more context on what I have attempted so far App.js file: impo ...

How many logical lines of code are in the Ubuntu operating system?

As I develop my web application, it is crucial for me to track the lines of code written in languages such as php, css, html, and JavaScript specific to the /var/www directory. However, when using the command line code counter tool, I find myself tempted ...

How to Align Video Alongside Image in HTML without Using CSS

Is there a way to place a video on the left and an image on the right of the same line in HTML without relying on CSS? ...

The specified version for scheduler@^0.13.4 could not be located

There seems to be a problem with installing the scheduler package in React, as mentioned in this Github issue: https://github.com/facebook/react/issues/13693 Oddly enough, I was able to run 'npm install' yesterday. However, today, without any ch ...

Error: Attempting to access the 'map' property of an undefined variable, cart

My project consists of two main components, App.js and Cart.js. I am utilizing material-ui and commerce.js API for this application. import React , {useState , useEffect} from 'react' import Products from './Components/Products/Products&apos ...

Tips for validating material-ui-phone-number using Yup

I am currently facing an issue with validating a material-ui-phone-number field using YUP. The problem arises when I add the inputRef prop to the component, as YUP throws an error stating TypeError: e is undefined. It appears that the material-ui-phone-num ...