``Fixing the focus background color for mobile devices and iPads using React and Material io: A Step-by-Step

My custom button has a background and a :hover color. It works fine on the web, but on mobile devices, the color of the :hover persists even after clicking the button when the mouse is not over it.

I am looking for a solution to fix this issue because the button retains the :focus CSS media with the same attributes as :hover

import MaterialButton from '@material-ui/core/Button';

export const Button = ({ title, className, style, onClick, disabled, type }: Props) => {
  return (
    <MaterialButton
      ref={buttonRef}
      type={type}
      className={className}
      style={style}
      onClick={onClick}
      disabled={disabled}
    >
      {title}
    </MaterialButton>
  );
};

Answer №1

After some testing, I discovered a solution to the issue that was occurring specifically on mobile devices. By identifying the original color of the button, I am now able to replace it with the correct color when accessed through a mobile device.

useEffect(() => {
    const originalClass = document.querySelector<any>(`.${className}`);
    if (originalClass) {
      const originalStyle = getComputedStyle(originalClass);
      setOriginalBackground(originalStyle.backgroundColor);
    }
  }, [className]);

  /**
   * This particular function addresses a bug in relation to :hover and :focus states on mobile devices.
   */
  const updateBackgroundColor = () => {
    if (!isMobile) return;

    try {
      document.querySelector<any>(`.${className}:hover`).style.backgroundColor = originalBackground;
    } catch (error) {
      console.error(error);
    }
  }

  const onClickEvent = (event: any) => {
    updateBackgroundColor();
    onClick(event);
  }

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

How can I position two bootstrap tables side by side based on their columns

Currently, I am utilizing bootstrap and Laravel form control to generate the table structures below. The one issue I am encountering is figuring out how to align the delete and edit buttons from two separate tables. Here is a snapshot of the current layout ...

Stepping into the colorful world of Material UI, exploring ways to extract vibrant

1) How can I extract a color from the palette within the Typography component? 2) Is it possible to access a specific color from the palette using an argument? Code:- 1) <Box color='common.white'> <Typography color='(I ...

What is the best way to use a CSS selector to target multiple divs such as block-11, block-12, and so

Here are several block IDs I have: <div id="block-11">content Here</div> <div id="block-12">content Here</div> <div id="block-13">content Here</div> <div id="block-14">conten ...

Establishing the state in a separate React component

I've tried various solutions found in other posts, but I still can't seem to resolve this issue. My main goal is to update the state of one component from another component. Below is a simplified version of my code: function updateOtherState(n ...

What is the best way to store types in a TypeScript-based React/Next application?

I'm currently in the process of setting up a Next.js page in the following manner const Index: NextPage<PageProps> = (props) => { // additional code... Prior to this, I had defined my PageProps as follows: type PageProps = { pictures: pi ...

What could be the reason my CSS styles are not taking effect when trying to flip an image?

I am facing an issue with an app that has a two-sided HTML element. The goal is to flip the element when a user clicks a button. I have been experimenting with different approaches without success. Here is the code snippet I have been working on: HTML < ...

The issue of border-radius and shadow box inconsistency

I'm trying to style a div with the following properties: .example { margin: 200px; width: 200px; height: 200px; border-radius: 20px; box-shadow: white 0px 0px 0pt 8pt, #033354 0px 0px 0pt 11pt; } <div class="example"> </div> ...

The height attribute in HTML tables fails to restrict height in Firefox

Is there a way to restrict the height of a table while still being able to scroll through the hidden elements? I've tried a few methods but none seem to work as intended: http://www.example.com/code/example table.ex1 { table-layout: auto; h ...

Developing a React class by integrating multiple components

As I dive into learning React, I've discovered that it's possible to create components in separate JavaScript files. However, I'm curious if there is a way to structure my code so that I have a Main class with nested sub1 and sub2 components ...

Easiest method to globally disable form autocompletion in an application without manually setting autocomplete off for every individual form

Is there a quick way to disable autocomplete in all forms within the application without individually adding "autocomplete=off" to each form? ...

There was a Runtime Error that occurred, stating a TypeError: It is not possible to access properties of an undefined value (specifically

I've encountered an issue with a donut chart implemented from react-apex charts. Every time I try to render the page containing the chart, an error occurs. However, if I make changes to a property of the chart, it renders without any errors on the fro ...

What is the best way to refresh tokens with apollo client and rerender my component?

I am currently using Apollo Client to handle authentication with JWT. When I encounter an error due to an expired token, I refresh the token and resend the request. While the new request returns data successfully, my component fails to rerender and still d ...

Adjust the Font Size of Bootstrap 3 across different breakpoints

I am currently using Bootstrap 3 and I have a requirement to adjust the font-size at different breakpoints across my website. My goal is to easily modify the font size in one place and have it automatically apply to all Bootstrap components. The desired b ...

React - How to properly pass a reference to a React portal

I have a Card component that needs to trigger a Modal component. Additionally, there is a versatile Overlay component used to display content above the application. Displayed here is the App component: class App extends Component { /* Some Code */ ...

Immediately encountered Next.js error upon setting up new Next app

Every time I attempt to run the server for my newly created Next.js app, I encounter this persistent error. ./node_modules/next/dist/client/dev/amp-dev.js Module not found: Can't resolve 'C:\Usersuddin\Desktop\asiqur\asp\ ...

The issue with EJS Header and Footer not showing up on the

I am currently building a basic todo list application using EJS. However, I am encountering an issue when running it. I would like to run app.js with the header and footer in EJS, but it seems that it is not recognizing the header despite using what I beli ...

Tips for wrapping a div around its floated children

I'm currently developing a website for an online shopping cart and working on making the search results responsive. I am aiming to display as many items as possible across the screen (usually 3) when viewed on any device with a resolution lower than t ...

What causes my animation to “reset” upon adding a new element using innerHTML?

One of the functionalities on my webpage involves a script that inserts a div called "doge" using innerHTML when a button is clicked. Additionally, there is a CSS keyframes animation applied to another div on the same page. However, whenever the button is ...

Motion effects within the page slide animation provide a dynamic backdrop in the Framer interface

I have created a multi-page display where users can navigate between pages by clicking on the buttons. The transition from one page to another is directional, with each page sliding in from the appropriate direction based on the user's action. For ins ...

Make the div stretch across the entire width of the page, excluding the width of a fixed div located on the right side, without needing to set a margin-right property

I've been struggling to find a solution for my wiki development project. My challenge involves designing a page layout with a fixed-width sidebar on the right, while ensuring that content to the left of the sidebar adjusts its width accordingly. I wan ...