Material-UI [v0.x] RaisedButton on hover styles

强颜欢笑 提交于 2019-12-05 02:18:43

You'll have to use CSS and some hackery, but below is a little component that wraps it up.

https://jsfiddle.net/0gjs8bh3/3/

const InvertHoverButton = ({ id, backgroundColor, labelColor }) => {
  // cubic-bezier(0.23, 1, 0.32, 1)
  const transition = `background-color 450ms ${MaterialUI.transitions.easeOutFunction} 0ms`;

    return (
    <div style={{ display: 'inline-block' }}>
        <style>
          {`
          #${id}:hover {
            transition: ${transition} !important;
            background-color: ${labelColor} !important;
            color: ${backgroundColor} !important;
          }

          #${id} {
            transition: ${transition} !important;
            background-color: ${backgroundColor} !important;
            color: ${labelColor} !important;
          }      
          `}
        </style>
        <RaisedButton id={id} backgroundColor={labelColor} labelColor={backgroundColor} className="test">Fiddle!</RaisedButton>
      </div>  
  );
};

Example:

// Must set id, backgroundColor and labelColor
<InvertHoverButton id="myButton" backgroundColor="red" labelColor="white">Fiddle!</InvertHoverButton>

You could choose to do the CSS differently here, but I am just trying to encapsulate it for you here on SO to make it more easily testable. If you choose to implement this differently, the key points are:

1) You need to use CSS :hover on the <button> element that RaisedButton renders

2) You must initialize the RaisedButton's props with the colors inverted already (this is because material-ui uses the label color to determine hover overlay and ripple color). Use your CSS to 'fix' the swapped colors when not :hover'ing.

3) You need match the transition that material-ui uses on the button's label, or it won't animate correctly

NOTE: If you are using material-ui's custom theming, you could modify this component to use the raisedButton theme's primary/secondary colors rather than accepting custom colors.

Sorry for a late answer, but maybe this will help someone else. Material-ui supports media queries like this (at least with 1.0):

const styles = {
  button: {
    padding: '10px',
    '&:hover': {
      background: 'blue'
    }
  },
  '@media (min-width: 1024px)': {
    button: {
      padding: '20px'
    }
  }
}

This example and more can be found from Mark Dalgleish's post about unified styling language.

GURU PRASAD
 linkLabel:{
    fontSize: 14,
    color: '#2bd5c6',
    '&:hover': {
      color: '#2bd5c6 !important',
    }
  }

sometimes 'hover' styles not applying even though you applied. U can achieve using '!important'.

<a href="#" className={classes.linkLabel}> {'Link content'}</a>

Just try this, you can create className in reactjs, so you can do hover style..

import React from 'react';
import Style from 'style-it';

class Intro extends React.Component {
  render() {
    return (
      <Style>
      {`
        .intro:hover {
          color: red;
        }
      `}
      <button className="intro">Save</button>
    </Style>
  }
}

export default Intro;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!