CSS animations are not functioning properly on disabled Bootstrap buttons

One issue I'm facing is with a BootStrap button that I'm rendering in React. The button has a property or a condition that determines if it's disabled:

  <Button bsClass="fill"
        disabled={!props.purchaseble}
        onClick={console.log("clicked!")}
        >Order now</Button>

The button functions correctly without the CSS class, but there is an unwanted animation when hovering over it, even when it's disabled:

Below is the CSS code for the button:

  .fill:hover {
    color: whitesmoke;
    cursor: pointer;

  }

  .fill:before {
    cursor: pointer;

    content: "";
    position: absolute;
    background: #5a320b;
    bottom: 0;
    left: 0;
    right: 0;
    top: 100%;
    z-index: -1;
    -webkit-transition: top 0.09s ease-in;
  }

  .fill:hover:before {
    cursor: pointer;

    top: 0;
  }

My question is whether these animations still work even when the button is disabled, and if there is a way to prevent the animations from appearing when the button is disabled?

Answer №1

Check out this example that shows the animation not being active when the button is disabled.

To achieve this, you can modify bsClass="fill" to something like

bsClass={this.state.isDisabled ? '' : 'fill'}
, which eliminates the CSS class animation when the button is disabled.

// Here's an example of a class component
class Container extends React.Component {
  constructor(props) {
    super(props);
    this.toggleDisabled = this.toggleDisabled.bind(this);
    this.state = { isDisabled: false };
  }
  
  toggleDisabled() {
    this.setState({ isDisabled: !this.state.isDisabled });
  }

  render() {
    return (
        <div>
          <button className={this.state.isDisabled ? '' : 'fill'}
            disabled={this.state.isDisabled}
          >Order now</button>
          <button onClick={() => { this.toggleDisabled(); }}>Click to toggle disabled</button>
        </div>
    );
  }
}

// Render it
ReactDOM.render(
  <Container/>,
  document.getElementById("react")
);
.fill:hover {
    color: whitesmoke;
    cursor: pointer;

  }

  .fill:before {
    cursor: pointer;

    content: "";
    position: absolute;
    background: #5a320b;
    bottom: 0;
    left: 0;
    right: 0;
    top: 100%;
    z-index: -1;
    -webkit-transition: top 0.09s ease-in;
  }

  .fill:hover:before {
    cursor: pointer;

    top: 0;
  }
<div id="parent"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

Answer №2

Even with the button in a disabled state, the transition should still function properly. Feel free to refer to this example for guidance and don't hesitate to ask if you need clarification.


Note: Initially I misunderstood the question, but I have now adjusted the code to rectify that misunderstanding. By incorporating :not([disabled]), we can prevent the animation from executing when the element is designated as disabled.

.fill:not([disabled]):hover {
  color: whitesmoke;
  cursor: pointer;
}

.fill::before {
  cursor: pointer;
  content: "";
  position: absolute;
  background: #5a320b;
  bottom: 0;
  left: 0;
  right: 0;
  top: 100%;
  -webkit-transition: top 0.09s ease-in;
}

.fill:not([disabled]):hover::before {
  cursor: pointer;
  top: 0;
}

  /* Set the button to be relative so the absolute pseudo element stays inside */
.fill {
  position: relative;
}

/* Place the text inside of a span so that it can sit in front of the pseudo element */
.fill > span {
  position: relative;
}
<button class="fill" disabled>
  <span>Look at me, I'm a disabled example!</span>
</button>

<br/>

<button class="fill">
  <span>Look at me, I'm a not disabled example!</span>
</button>

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

Steps on removing a file type from a Material UI textfield

I've been struggling to figure out how to clear a Material UI textfield with type="file" even after trying multiple approaches. My issue arises when I set a file size limit and display an error message if a user tries to upload a file larg ...

What is the best way to apply styling to a kendo-grid-column?

Utilizing the kendo-grid in my project serves multiple purposes. Within one of the cells, I aim to incorporate an input TextBox like so: <kendo-grid-column field="value" title="{{l('Value')}}" width="200"></kendo-grid-column> It is ...

Exploring JQuery Mobile: A guide on looping through elements in a list and collecting their unique identifiers

I am facing a challenge with a list of li tags within an unordered list. Each tag is assigned a class=listitem and a data-clicked=true/false. My task is to extract only the id's of those with data-clicked=true, add them to a string named custidlist, a ...

When multiple files are used, .env does not return any values

Having trouble with two files utilizing .env, as both are returning undefined for variables. I attempted setting a root .env file for both the front-end and backend, but still facing issues with loading the .env variables. In the frontend, any .env variab ...

Securing routes with server-side validation

I've been experimenting with different methods to secure a route for unauthenticated users, but I'm facing difficulties in implementing it effectively. One common issue I encountered while attempting authentication from getServerSideProps was th ...

Delay the occurrence of a hover effect until the previous effect has finished executing

As I hover over an element, the desired animation is displayed while hiding other elements on the page. The challenge I'm encountering is that if I quickly hover over many divs, the animations queue up and hide the divs sequentially. I want only one ...

Discover the nodes with the highest connections in a D3 Force Graph

As I explore the functionalities of a D3 Force Directed Graph with zoom and pan features, I encounter an issue due to my limited knowledge of d3.js. Is there a way to estimate the number of links for each node in this scenario? I am currently at a loss on ...

Arrange a div element among the descendants of a neighboring div

I am working with a div that is generated by a library. This div contains 2 child divs with varying heights, which can change based on the API response and the width of the browser window. My goal is to display another div (my-div) between top-bar and bot ...

Maintaining the original layout upon refreshing the page

Incorporating two forms on my website, I added a button that transitions between the login and sign-up forms smoothly. The process is as follows: Initially, the login form is displayed; clicking the button switches to the sign-up form. Proceeding to subm ...

Error: Dockerizing a React application resulted in a syntax error due to an unexpected token '<'

I am working on dockerizing my React project with Webpack by following the steps outlined in this particular article. Dockerfile: FROM node:12.14.1 RUN npm install webpack -g WORKDIR /tmp COPY package.json /tmp/ RUN npm config set registry http://regis ...

Eliminate any undefined data in the popup map of Javascript

I am working with JSON data that is being displayed in a pop-up on a map. In cases where there is missing data (Visibility), the word undefined appears in the pop-up. Is there a way to remove the undefined text so that it does not show up in the pop-up? ...

Arranging the placement of list-group-item buttons

Looking for a way to create a list-group-item with two buttons that are equal in height and aligned to the right? Take a look at this example below: https://i.sstatic.net/II2nl.png While I have the basic structure in place, I'm having trouble gettin ...

jQuery's click event on dynamically generated AJAX elements sometimes fails to trigger randomly

When I use the getData() function to retrieve ajax content, it dynamically adds elements based on the JSON response from the server. The code snippet below shows how the elements are added: $("#list").append('<div class="ui card fluid" id="' ...

CSS will only be visible if it is enclosed within the <style> tag

While creating a view, I noticed that my CSS is not being displayed unless it's placed inside a <style> tag. I've tried using !important without success, as well as utilizing the selector .container-fluid > .row > .d-flex > .out_pr ...

What is the best way to assign a JavaScript return value to a PHP variable?

Is it possible to capture the return value of a JavaScript function that checks if text boxes are empty and assign it to a PHP variable? I have successfully created a JavaScript function that returns false if any of the text boxes are empty, but now I ne ...

The "Overall Quantity" of items will vary as it goes through different numerical values, despite the fact that I employed --

I am currently working on an e-commerce website with a shopping cart feature. The cart displays the number of items added to it, which increases by one when 'Add to Cart' is clicked and decreases by one when 'Remove' is clicked. However ...

How to best handle dispatching two async thunk actions in Redux Toolkit when using TypeScript?

A recent challenge arose when attempting to utilize two different versions of an API. The approach involved checking for a 404 error with version v2, and if found, falling back to version v1. The plan was to create separate async thunk actions for each ver ...

Utilizing Images as Backgrounds in JSP

Looking for assistance with adding a background image in a JSP file without overlapping the navigation bar? Check out my code below: <jsp:include page="index_header.jsp"></jsp:include> <div class="container"> <div style=&ap ...

Adjusting the height of a table column to accommodate lengthy text without disrupting the layout of adjacent columns

I am encountering an issue while creating an invoice using an HTML table. The problem arises when the customer address value is too long, causing the sold by column to shift downwards. I need a way to break the customer address value when it reaches the ma ...

Creating JavaScript functions that accept three inputs and perform an operation on them

Apologies in advance if there are any silly mistakes as I am new to Javascript. I want to add "salary", "pension", and "other" together, then display the result in an input field when a button is clicked. Not sure if I have used the correct tags in my cod ...