0

I have my custom button with my background and a :hover color, on the web it works fine, but on mobile devices, after clicking the button, it keeps the color of the :hover even when the mouse is not over that button .

I want to find a way to correct this error because the button keeps :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>
  );
};
Pablo Alejandro
  • 591
  • 2
  • 10
  • 19

1 Answers1

0

I added this code to fix the problem on mobile devices. I get the original color of the button and if it is mobile I will replace the color with the original.

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

  /**
   * This function fix the color bug with :hover and :focus 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);
  }
Pablo Alejandro
  • 591
  • 2
  • 10
  • 19