Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve styling of flat buttons #643

Merged
merged 1 commit into from
May 15, 2015
Merged

Conversation

troutowicz
Copy link
Contributor

  • hoverColor prop
  • rippleColor prop
  • persist hover state through ripple
  • miscellaneous refinements

If this gets a thumbs up, I will do the same for raised buttons.

@troutowicz troutowicz force-pushed the button_styling branch 4 times, most recently from a3569c9 to 7c20de6 Compare May 14, 2015 15:16
@pomerantsev
Copy link
Contributor

@troutowicz I did some research, and I have feedback on the PR. Hope it will be valuable to you.
The code has become somewhat brittle, because in _handleKeyboardFocus and _handleTouchTap we manipulate styles directly, and the whole purpose of _handleTouchTap is to cancel the change that has been done in _handleKeyboardFocus (we're relying on the fact that _handleKeyboardFocus fires first).
What I propose is getting rid of _handleTouchTap altogether and instead just using state to indicate whether a button is focused with a keyboard. Then in styles we would apply rootWhenHovered only when this.state.hovered || this.state.isKeyboardFocused.
So the style for enhancedButton would look like (you could still make it less repetitive, I guess):

style={this.mergeAndPrefix(
                               styles.root,
                               ((this.state.hovered || this.state.isKeyboardFocused) && !this.props.disabled) && styles.rootWhenHovered,
                               this.props.style,
                               ((this.state.hovered || this.state.isKeyboardFocused) && !this.props.disabled) && this.props.hoverStyle
                             )}

And these will be the event handlers:

  _handleMouseOver (e) {
    this.setState({hovered: true});
    if (this.props.onMouseOver) {
      this.props.onMouseOver(e);
    }
  },

  _handleMouseOut (e) {
    this.setState({hovered: false});
    if (this.props.onMouseOut) {
      this.props.onMouseOut(e);
    }
  },

  _handleKeyboardFocus (e, isKeyboardFocused) {
    this.setState({isKeyboardFocused});
  }

Notice that we no longer check if the enhanced button is focused with a keyboard since we always want the hovered state to be applied when the mouse is actually over the button.

I would also add an onBlur handler that would as well set hovered state to false - this way we cover all bases on mobile as well, since onMouseOver fires there, but not onMouseOut, so a button can stay in a hovered state after we press another button.

@troutowicz
Copy link
Contributor Author

@pomerantsev Thanks for the critique. Your suggestions make sense, I will make the changes later today... I wasn't happy with the handleTouchTap hack and the handleKeyboardFocus didn't sit well with me either.

Also, I recently updated this PR to instead have hoverColor and rippleColor props instead of a hoverStyle prop.

@pomerantsev
Copy link
Contributor

Great.

@troutowicz troutowicz force-pushed the button_styling branch 2 times, most recently from 4949d2e to cd8794a Compare May 14, 2015 16:45
@troutowicz
Copy link
Contributor Author

@pomerantsev Had some free time on lunch, I have updated the PR.

@pomerantsev
Copy link
Contributor

@troutowicz Thanks. Tested it quickly, looks good.
This project would benefit greatly from a test suite - I wonder how guys make sure nothing's broken with every new PR.

@troutowicz
Copy link
Contributor Author

The project definitely needs a test suite. I personally have been testing my changes inside a personal project that uses the various components that I submit a PR for.

Side note: My project uses mochify, and has worked well for my component tests thus far. (my project's tests)

@pomerantsev
Copy link
Contributor

Same with me - I've also been testing stuff inside a different project.

@hai-cea
Copy link
Member

hai-cea commented May 15, 2015

@troutowicz @pomerantsev There is an issue created for adding a test suite #396 - maybe we can have a discussion there and come up with a decision on which test suite to use?

hai-cea pushed a commit that referenced this pull request May 15, 2015
Improve styling of flat buttons
@hai-cea hai-cea merged commit 92bae5d into mui:master May 15, 2015
@hai-cea
Copy link
Member

hai-cea commented May 15, 2015

Looks good - Thanks @troutowicz & @pomerantsev 👍

@zannager zannager added the component: button This is the name of the generic UI component, not the React module! label Mar 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: button This is the name of the generic UI component, not the React module!
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants