Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                h1 React.js Star Rating
div#container
              
            
!

CSS

              
                $color-gray = #aaa
$color-dark-gray = #222
$color-yellow =  #ffd300
$color-white = #fff
$color-hover = #FFBF00

body 
  background $color-dark-gray
  text-align center
  font-family TrebuchetMS, Helvetica, sans-serif
  color $color-white
  
.container
  width 100%
  height 40px
  position relative
  color black
  background red  
  
.star-widget
  font-size 40px
  &:hover {
    .full {
      color $color-hover;
    }
  }
.star-item
  color $color-gray
  cursor pointer
  padding 1px
  box-sizing border-box
  &.full 
    transition 0.25s all
    color $color-yellow
              
            
!

JS

              
                var ReactCSSTransitionGroup = React.addons.CSSTransitionGroup;

class StarWidget extends React.Component {  
  constructor(props) {
    super(props);
    this.state = {
      localRating: props.rating
    }    
    this.reset = this.reset.bind(this);
    this.setRating = this.setRating.bind(this);
  }  
  
  setRating(i,type){ 
    this.setState({localRating:i+1});
  }
  
  reset() {
    this.setState({localRating: this.props.rating});
  }
  
  render() {
        const {totalStars, rating, updateRating} = this.props;
        const totalStarsNum = (totalStars | 5);
        const classes = 'star-item icon icon-star';
        let stars = _.fill(_.times(totalStarsNum, (i)=> 0), 1, 0, this.state.localRating).map((value, index)=> {
            return <span
                key={index}                   
                className={(value === 1) ? `full ${classes}`: `${classes}`}  
                onClick={updateRating.bind(null, index+1)}
                onMouseOver={this.setRating.bind(this, index, 'hover')}
                onMouseOut={this.reset}>
                </span>;
        });
        return (<div className='star-widget' ref={(w)=> this.stars = w}     >
              <ReactCSSTransitionGroup name="test">{stars}</ReactCSSTransitionGroup></div>
            );
    }
}

class App extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            rating:0
        }
    }

    updateRating(rating) {
        this.setState({rating});
    }

    render() {
        return (<div id="app">
                  <StarWidget 
                      rating={this.state.rating} 
                      updateRating={this.updateRating.bind(this)} 
                    />             
                </div>);
     }
}
        
ReactDOM.render(<App />, document.getElementById('container'));
              
            
!
999px

Console