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 includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

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

              
                <h2>React Color Picker 3</h2>
<div id="color-picker-container"></div>
              
            
!

CSS

              
                @import url(https://fonts.googleapis.com/css?family=Open+Sans);
body {
  margin: 0;
  padding: 0;
  background-color: #e6e6e6;
}
h2 {
  background-color: #dbdbdb;
  margin: 0;
  margin-bottom: 15px;
  padding: 10px;
  font-family: 'Open Sans';
}
#color-label {
  margin-left: 15px;
  position: relative;
  height: 20px;
  width: 40px;
  border: none;
}
#color-select {
  position: relative;
  left: 60px; 
  top: -22px;
  background-color: white;
  border: solid 1px #ccc;
  padding: 5px;
  height: 200px;
  width: 175px;
  opacity: 0;
  transition: all 0.3s;
}
canvas:hover {
  cursor: crosshair;
}
#color-strip {
  margin-left: 5px;
}
p {
  margin: 20px 0 0 70px;
}
              
            
!

JS

              
                var contexts = [];
var wB = 150;
var hB = 150;
var wS = 20;
var hS = 150;
var drag = false;
var rgbColor = 'rgb(0,0,0)';

var ColorPickerContainer = React.createClass({
  getInitialState: function() {
    return {
      isPickerVisible: true,
      color: rgbColor
    };
  },
  togglePicker: function(id) {
    this.setState({isPickerVisible: !this.state.isPickerVisible});
  },
  blockFill: function() {
    this.ctxB.rect(0, 0, wB, hB);
    this.gradientBlock();
  },
  stripFill: function() {
    this.ctxS.rect(0, 0, wS, hS);
    var grd1 = this.ctxS.createLinearGradient(0, 0, 0, hS);
    grd1.addColorStop(0, 'rgb(255, 0, 0)'); // red
    grd1.addColorStop(0.17, 'rgb(255, 255, 0)'); // yellow
    grd1.addColorStop(0.34, 'rgb(0, 255, 0)'); // green
    grd1.addColorStop(0.51, 'rgb(0, 255, 255)'); // aqua
    grd1.addColorStop(0.68, 'rgb(0, 0, 255)'); // blue
    grd1.addColorStop(0.85, 'rgb(255, 0, 255)'); // magenta
    grd1.addColorStop(1, 'rgb(255, 0, 0)'); // red
    this.ctxS.fillStyle = grd1;
    this.ctxS.fill();
  },
  gradientBlock: function() {
    this.ctxB.fillStyle = rgbColor;
    this.ctxB.fillRect(0, 0, wB, hB);
    var grdWhite = this.ctxB.createLinearGradient(0, 0, wB, 0);
    grdWhite.addColorStop(0, 'rgb(255,255,255)');
    grdWhite.addColorStop(1, 'transparent');
    this.ctxB.fillStyle = grdWhite;
    this.ctxB.fillRect(0, 0, wB, hB);
    var grdBlack = this.ctxB.createLinearGradient(0, 0, 0, hB);
    grdBlack.addColorStop(0, 'transparent');
    grdBlack.addColorStop(1, 'rgb(0,0,0)');
    this.ctxB.fillStyle = grdBlack;
    this.ctxB.fillRect(0, 0, wB, hB);
  },
  selectColor: function(ctx, e, self) {
    var x = e.nativeEvent.offsetX;
    var y = e.nativeEvent.offsetY;
    var imageData = ctx.getImageData(x, y, 1, 1).data;
    rgbColor = 'rgb(' + imageData[0] + ',' + imageData[1] + ',' + imageData[2] + ')';
    self.setState({color: rgbColor});
  },
  selectColorSquare: function (color, self) {
    rgbColor = color;
    self.setState({color: rgbColor});
  },
  clickStrip: function (e) {
    var color = e.target.style.backgroundColor;
    color ? this.selectColorSquare(color, this) : this.selectColor(this.ctxS, e, this);
    this.gradientBlock(); 
  },
  mouseDownBlock: function(e) {
    drag = true;
    this.selectColor(this.ctxB, e, this);
  },
  mouseMoveBlock: function(e) {
    if (drag) {
      this.selectColor(this.ctxB, e, this);
    }
  },
  mouseUpBlock: function() {
    drag = false;
  },
  setContexts: function(ctxB, ctxS) {
    this.ctxB = ctxB;
    this.ctxS = ctxS;
  },
  render: function() {
    return (
      <div>
        <ColorLabel isChecked={this.state.isPickerVisible} 
                    color={this.state.color} 
                    handleClick={this.togglePicker}
                    id={this.props.id} />
        <ColorPicker isVisible={this.state.isPickerVisible} 
                     color={this.state.color} 
                     setContexts={this.setContexts}
                     mouseDownBlock={this.mouseDownBlock} 
                     mouseMoveBlock={this.mouseMoveBlock} 
                     mouseUpBlock={this.mouseUpBlock} 
                     clickStrip={this.clickStrip}
                     blockFill={this.blockFill}
                     stripFill={this.stripFill}
                     id={this.props.id} />
      </div>
    );
  }
});

var ColorPicker = React.createClass({
  getInitialState: function() {
    return {
      color: rgbColor
    }
  },
  componentDidMount: function() {
    var self = this;
    var canvasB = this.refs.canvasBlock;
    var canvasS = this.refs.canvasStrip;
    var ctxB = canvasB.getContext('2d');
    var ctxS = canvasS.getContext('2d');
    contexts.push(ctxB, ctxS);    
    self.props.setContexts(ctxB, ctxS);    
    contexts.forEach(function(item) {
      self.props.blockFill(item);
      self.props.stripFill(item);
    });
  },
  render: function(e) {
    var styles = {
      opacity: this.props.isVisible ? '1' : '0'
    };
    return (
      <div id="color-select" style={styles}>
        <div id="color-picker" className={this.props.id}>
          <canvas id="color-block" 
                  height={hB} 
                  width={wB} 
                  onMouseDown={this.props.mouseDownBlock}
                  onMouseMove={this.props.mouseMoveBlock}
                  onMouseUp={this.props.mouseUpBlock}
                  ref="canvasBlock"></canvas>
          <canvas id="color-strip" 
                  height={hS} 
                  width={wS} 
                  onClick={this.props.clickStrip}
                  ref="canvasStrip"></canvas>
        </div>
        <ColorTable handleClick={this.props.clickStrip} />
      </div>
    );
  }
});

var ColorTable = React.createClass({
  render: function () {
    var self = this;
    var colors = [
      'rgb(255, 65, 54)', // Red   
      'rgb(255, 133, 27)', // Orange 
      'rgb(255, 220, 0)', // Yellow  
      'rgb(1, 255, 112)', // Lime
      'rgb(46, 204, 64)', // Green
      'rgb(57, 204, 204)', // Teal
      'rgb(127, 219, 255)', // Aqua
      'rgb(0, 116, 217)', // Blue
      'rgb(177, 13, 201)', // Purple
      'rgb(240, 18, 190)', // Magenta
      'rgb(133, 20, 75)', // Maroon
      'rgb(17, 17, 17)', // Black
      'rgb(170, 170, 170)', // Gray
      'rgb(255, 255, 255)'  // White
    ];
    var styles = {
      position: 'relative',
      height: '50px',
      width: '175px',
      marginLeft: '2px'
    };
    return (
      <div id="color-table" style={styles}>
        {colors.map(function(color) {
          var style = {
            backgroundColor: color,
            border: 'solid 1px #ccc',
            height: '20px',
            width: '20px',
            marginRight: '3px',
            display: 'inline-block'
          };
          return <div style={style} onClick={self.props.handleClick}></div>
        })}
      </div>
    );
  }
});

var ColorLabel = React.createClass({
  handleClick: function() {
    this.props.handleClick();
  },
  render: function() {
    var styles = {
      backgroundColor: this.props.color
    };
    return(
      <div>
        <button type='button' id='color-label' className={this.props.id} style={styles} onClick={this.props.handleClick.bind(this, this.props.id)}  />
      </div>
    ); 
  }
});

var App = React.createClass({
  render: function() {
    return <ColorPickerContainer />
  }
});

React.render(
  <App />,
  document.getElementById('color-picker-container')
);

              
            
!
999px

Console