0

How I can trigger the onClick and onHover CSS animations for the bellow ReactJS button component?

I've tried using ref = {input => (this.inputElement = input)}, but only clicked the button, no animation.

          <td>
            {this.state.buttons.slice(0, 4).map(btn => (
              <button
                onClick={() => this.handleClick(btn)}
                key={btn}
                className={this.getBadgeClasses(btn)}
              >
                {btn}
              </button>
            ))}
          </td>

I need this so that I can trigger both animations from a keyboard event.

1

1 Answer 1

3

Basically, onClick works when you press the button with either mouse or keyboard (Enter or Space key).

When you move cursor over the button, the browser dispatches onMouseOver and if you focus the button using a keyboard, the browser dispatches onFocus.

It's very common practice to style :hover and :focus styles together. To add CSS animation to buttons simply pass :focus to it like below:

.btn-classes:hover,
.btn-classes:focus {

}

However, if you want to add JS animation to the component, pass onFocus prop to button. The following may be helpful to you:

          <td>
            {this.state.buttons.slice(0, 4).map(btn => (
              <button
                onClick={() => this.handleClick(btn)}
                onFocus={() => this.handleFocus(btn)}
                key={btn}
                className={this.getBadgeClasses(btn)}
              >
                {btn}
              </button>
            ))}
          </td>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.