Stylish hover effects displayed on disabled button using Styled Components

I am currently working on a button using Styled Components. However, even when the button is in a disabled state (:disabled), it still exhibits hover behavior that should not be present. I am wondering if there is a proper way to prevent hover effects when the button is disabled.

Here is the style definition:

import styled from "styled-components";

const Button = styled.button`
background-color: #023047;
border: 1px solid #023047;
color: white;
padding: 4px 16px;
width:100%;
border-radius: 4px;
outline: 0;
text-transform: uppercase;
margin: 10px 0px;
cursor: pointer;
box-shadow: 0px 2px 2px #023047;
transition: ease background-color 250ms;
:disabled {
    cursor: default;
    opacity: 0.7;
}
:hover {
    background-color: #ffb703;
    box-shadow: 0px 2px 2px #ffb703;
}
:active {
    background-color: #023047;
    box-shadow: 2px 2px 0 #023047;
}
`;

const MyButton = ({ content, disabled = false}) => {
    return <Button disabled={disabled}>{content}</Button>;
}

This is how it is being used:

<MyButton content="I am active" /> // displays active/hover effect correctly

<MyButton content="I am disabled" disabled={true}  /> // appears disabled but still reacts to hover

Any suggestions on how to disable the hover effect when the button is disabled?

Answer №1

Check out this code snippet:

const CustomButton = styled.button`
  background-color: #023047;
  border: 1px solid #023047;
  color: white;
  padding: 4px 16px;
  width: 100%;
  border-radius: 4px;
  outline: 0;
  text-transform: uppercase;
  margin: 10px 0px;
  cursor: pointer;
  box-shadow: 0px 2px 2px #023047;
  transition: ease background-color 250ms;
  :disabled {
    cursor: default;
    opacity: 0.7;
  }
  ${(props) =>
    !props.disabled &&
    `:hover {
    background-color: #ffb703;
    box-shadow: 0px 2px 2px #ffb703;
  }`}
  :active {
    background-color: #023047;
    box-shadow: 2px 2px 0 #023047;
  }
`;

Answer №2

To pinpoint that element, utilize this selector

:hover:not([disabled])

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

React, React Router, and Flux are all tangled up in a confusing web of "dispatch in the middle of a dispatch" whenever I attempt to clear the messages

Utilizing React, React Router, and Flux, I have a MessageStore that maintains messages triggered by components using MessagesAction.addError(), MessagesAction.addWarning(), and MessagesAction.addSuccess(). Upon dispatching the message, the MessageStore is ...

Navigating Unknown Properties in Angular: A Guide to Iterating Through Arrays

I'm having trouble coming up with a title for my question. I want to loop through an array of dynamic objects coming from a database, but I don't know the properties of the objects. Here's an example of the array object: [{ "id": 9, ...

After a span of two minutes, the Node.js and Express server terminates the connection

I am currently working with Express 4.X and Node.js 0.12. One of the routes in my application is responsible for uploading and processing files. However, I have encountered an issue where some file uploads are taking longer than the default 2-minute timeo ...

ng-options encounters duplicate data when using group by in Internet Explorer

The dropdown menu that lists states works fine in Google Chrome, but duplicates groups in Internet Explorer. In Chrome, there are 2 groups, but in IE there are 4. How can I fix this issue in IE as well? Thank you. Here is the code snippet: <!DOCTYPE h ...

I'm looking to find the Angular version of "event.target.value" - can you help me out?

https://stackblitz.com/edit/angular-ivy-s2ujmr?file=src/app/pages/home/home.component.html I am currently working on getting the dropdown menu to function properly for filtering the flags displayed below it. My initial thought was to replicate the search ...

Using 'settingsToggleClassName' in Griddle: A Step-by-Step Guide

When using the settingsToggleClassName, it's important to provide a string input. To implement this feature, I have created the following style: let griddleBtnStyle = { background: 'red' }; I am then using this style as follows: <Gri ...

Having trouble with JQuery click function not executing on initial click?

As I scoured through various resources looking for solutions to issues similar to mine, I stumbled upon numerous helpful insights. However, after going through approximately 15 of them, I hit a roadblock in finding someone who has encountered the exact sam ...

Stop React Form from automatically submitting by preventing automatic submission

I am currently facing an issue with a form that includes a rich text editor and a submit button. The problem arises when the style buttons in the rich text editor are clicked, causing the form inputs to submit unintentionally. My goal is to prevent this b ...

Node.js's module-only scope concept allows variables and functions defined within

Currently, I am attempting to create a function that can set a variable at the top-level scope of a module without it leaking into the global scope. Initially, I believed that implicit variable declarations within a module would remain confined to the modu ...

What exactly are AngularJS module dependencies and how do they work together?

After exploring the tutorial example provided on AngularJs's site ( here) (The main HTML appears to be quite minimal with only ng-view and ng-app=phonecatApp included) Within the app.js file, we find: var phonecatApp = angular.module('phoneca ...

The native styled component does not function properly with Material-UI media query

Is it feasible to utilize [props => props.theme.breakpoints.up('sm')] in this context? import React from 'react'; import { styled, withTheme } from '@material-ui/core'; export const DefaultContent = withTheme( styled(({ th ...

backbone.js router failing to recognize URL fragments

I have set up a basic router in my code and initialized it. Issue: I encountered an unexpected behavior when visiting the URL http://localhost/backbone1/#photos/5. Despite expecting an output from console.log() in the JavaScript console, nothing appears. ...

Creating a JSON object from an array of data using TypeScript

This might not be the most popular question, but I'm asking for educational purposes... Here is my current setup: data = {COLUMN1: "DATA1", COLUMN2: "DATA2", COLUMN3: "DATA3", ..., COLUMNn: "DATAn"}; keyColumns = ["COLUMN2", "COLUMN5", "COLUMN9"]; ...

Experiencing the recurring issue of "TypeError: Unable to access property 'map' of undefined" while implementing the map function in ReactJS

I can't seem to figure out what's causing the issue I'm facing with my code. Let me provide some context for the code snippets below. Within the Slider component, I'm receiving the storeList from the parent component as props and passi ...

Determining the specific condition that failed in a series of condition checks within a TypeScript script

I am currently trying to determine which specific condition has failed in a set of multiple conditions. If one does fail, I want to identify it. What would be the best solution for achieving this? Here is the code snippet that I am using: const multiCondi ...

JavaScript on Ruby on Rails stops functioning in js.erb file

I have encountered an issue with pagination using AJAX in a view. Initially, I had two paginations working perfectly fine with their respective AJAX calls. However, when I tried to add a third pagination (following the same method as the previous two), it ...

What is the best way to utilize the .done() callback in order to execute a function when new data is loaded upon request?

I have a web page that pulls data from a JSON feed, and there's also a button to load more content from the feed when clicked. I want to add additional elements inside the page for each item in the feed. I've managed to create a function that doe ...

Looking for a jQuery Gantt Chart that includes a Treeview, Zoom functionality, and editing capabilities?

I am in need of integrating a dynamic Gantt Chart tool into a room booking solution using jQuery. The Gantt chart should be highly interactive, allowing for drag-and-drop functionality, a tree view on the left to group tasks, and a zoom feature for adjusti ...

React - click event to switch between two components

I encountered an issue and need assistance with it - Error: TypeError: Cannot read properties of null (reading 'useState') Menu.tsx export const MainMenu: FC = () => { const [toggle, setToggle] = useState(false); return( <& ...

Issue with jquery curvy corners not functioning properly on Internet Explorer 8

Check out my website at If you view the page in IE8 and then in IE7 compatibility mode, you'll notice a strange issue. The box on the right disappears in IE8 but displays perfectly rounded corners in IE7. I am currently using the jQuery Curvy Corner ...