React Base Fiddle (JSX)

Starting point for creating JSFiddles with React.

by rockyhuber

HTML

<script src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<div id="container"></div>

CSS

.price-slider { 
  position: relative; 
  width: 400px; 
  margin: 0 auto 20px; 
  height: 35px; 
  text-align: center; 
} 

.price-slider input { 
  pointer-events: 
  none; position: absolute; 
  left: 0; 
  top: 15px; 
  width: 100%; 
  outline: none; 
  height: 18px; 
  margin: 0; 
  padding: 0; 
  border-radius: 8px; 
} 
.price-slider input::-webkit-slider-thumb { 
  pointer-events: all; 
  position: relative; 
  z-index: 1; 
  outline: 0; 
  -webkit-appearance: none; 
  height: 24px; 
  width: 24px; 
  border-radius: 12px; 
  background-color: white; 
  border: 2px solid black; 
}

Babel + JSX

class Hello extends React.Component {

constructor() {
super();
	this.state = { 
		val1: 0,
    val2: 100
	}
}

componentDidMount() {
this.sliders = document.querySelectorAll(".price-slider input");
}

updatePriceLabels = (e, slider) => { 

	let val1, val2;
  
  if (slider === 'one') {
    val1 = e.target.value;
    val2 = this.state.val2;    
  } else if (slider === 'two') {
    val1 = this.state.val1;
    val2 = e.target.value;
  }

  if (val1 >= val2) { 
    this.setState({
      val1: val2 - 3,
      val2: val2
    });
    return;
  } else if (val2 <= val1) { 
    this.setState({
      val1: val1 + 3,
    	val2: val1
    });
    
    return;
    } else {
      this.setState({
				val1: val1,
        val2: val2
      });
    }
  console.log(this.sliders)
  if (val1 > 0 || val2 < 99) { 
    this.sliders[0].style.background = this.sliders[1].style.background = "-webkit-gradient(linear, 0 0,100% 0, color-stop(0, white), color-stop(" + (val1 / 100) + ", white),color-stop(" + (val1 / 100) + ", #f0f0f0), color-stop(" + (val2 / 100) + ", #f0f0f0), color-stop(" + (val2 / 100) + ", white))"; 
  } else { 
    this.sliders[0].style.background = this.sliders[1].style.background = ''; } 
}

  render() {
		return (
    <div className="price-slider">
      <input value={this.state.val1} min="0" max="100" step="0.5" type="range" onChange={(e) => this.updatePriceLabels(e, 'one')}></input>
      <input value={this.state.val2} min="0" max="100" step="0.5" type="range" onChange={(e) => this.updatePriceLabels(e, 'two')}></input>
    </div>
  
    )
  }
}

ReactDOM.render(
  <Hello name="World" />,
  document.getElementById('container')
);