首页 > 其他 > 详细

[React] Radium: Updating Button Styles via Props

时间:2016-04-18 06:32:09      阅读:282      评论:0      收藏:0      [点我收藏+]

In a CSS library like Bootstrap we can set a button‘s style to be "primary" or "secondary" by appending classes. For React components we want to be able to do this via props. Radium enables this by composing styles via an array. This mimicks the cascade of CSS.

 

Radiumn allows ‘style‘ attr accepts array. Inside array, the larger index style will overwrite the pervious index‘s style.

  <button style={[
    styles.base,
    type===‘primary‘ && styles.primary
  ]}>
    {children}
  </button>

So in the code, styles.primary will overwrite the styles.base:

const styles = {
  base: {
    backgroundColor: ‘#aaa‘,
    border: ‘none‘,
    borderRadius: 4,
    color: ‘#fff‘,
    padding: ‘5px 20px‘,
    ‘:hover‘: {
      backgroundColor: ‘#08f‘
    }
  },
  primary: {
    backgroundColor: ‘#07d‘
  }
}

 

We can pass a props to the component to tell when should apply styles.primary style:

const { render } = ReactDOM
const rootEl = document.getElementById(‘root‘)

const Button = Radium(({ children, kind }) => (
  <button style={[
    styles.base,
    kind === ‘primary‘ && styles.primary
  ]}>
    {children}
  </button>
))

const styles = {
  base: {
    backgroundColor: ‘#aaa‘,
    border: ‘none‘,
    borderRadius: 4,
    color: ‘#fff‘,
    padding: ‘5px 20px‘,
    ‘:hover‘: {
      backgroundColor: ‘#08f‘
    }
  },
  primary: {
    backgroundColor: ‘#07d‘
  }
}

render(
  <Button kind="primary">
       OK
  </Button>,
rootEl)

 

[React] Radium: Updating Button Styles via Props

原文:http://www.cnblogs.com/Answer1215/p/5402996.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!