content logo

React Buttons:

Background Color Picker with Hidable Circular React Buttons

In this post, we have a button in the middle of the page and a circle. By clicking on this button, buttons with different colors and a circle are placed around it. Clicking on any of the buttons will change the background color to the color of that button. At the beginning of the page load, these buttons rotate around and then disappear. All buttons have a thin purple body.

#

Circular React Button Code

#

HTML Color Picker Code

#

React Background Color Picker

#

Hidable React Buttons

<!-- This script got from frontendfreecode.com -->
<div id="color-picker-root"></div><a style="font-size: 8pt; text-decoration: none" target="_blank" href="http://frontendfreecode.com">Free Frontend</a>
                                                                            
* {
  box-sizing: border-box;
}
.color-picker-container {
  position: absolute;
  width: 100%;
  height: 100%;
  background-color: #330f53;
}
.svg-sliders {
  display: block;
  position: absolute;
  width: 100%;
  height: 100%;
}
.svg-sliders .slider {
  transform-origin: center center;
  transform: translatey(-100%);
}
.btn-container {
  display: flex;
  justify-content: center;
  align-items: center;
  position: absolute;
  width: 100%;
  height: 100%;
}
.btn {
  position: relative;
  width: 60px;
  height: 60px;
  background-color: #8b1a89;
  border: 3px solid;
  border-color: #c58cc4;
  border-radius: 50%;
  z-index: 2;
  transform: scale(1.25);
  box-shadow: 0px 1px 3px 0px rgba(0, 0, 0, 0.5);
  transition: box-shadow 0.2s ease-in-out, transform 0.2s ease-in-out, border-color 0.2s ease-in-out;
}
.btn:hover {
  cursor: pointer;
}
.btn:active {
  box-shadow: 0px 0px 1px 0px rgba(0, 0, 0, 0.5);
  transition: box-shadow 0.1s ease-in-out;
}
.btn .small-dot {
  content: " ";
  position: absolute;
  width: 4px;
  height: 4px;
  background-color: #8b1a89;
  bottom: 4px;
  left: -2px;
  border-radius: 50%;
  transform: translate(0px, 0px);
  transition: transform 0.5s;
  transition-timing-function: cubic-bezier(0.68, -0.55, 0.265, 1.55);
  z-index: 1;
}
.btn .big-dot {
  content: " ";
  position: absolute;
  width: 6px;
  height: 6px;
  background-color: #8b1a89;
  bottom: -2px;
  right: 0px;
  border-radius: 50%;
  transform: translate(0px, 0px);
  transition: transform 0.5s;
  transition-timing-function: cubic-bezier(0.68, -0.55, 0.265, 1.55);
  transition-delay: 0.2s;
  z-index: 1;
}
.btn.active {
  border-color: #dd52c9;
  box-shadow: 0px 0px 1px 0px rgba(0, 0, 0, 0.5);
  transform: scale(1);
  transition: box-shadow 0.2s ease-in-out, transform 0.2s ease-in-out, border-color 0.2s ease-in-out;
}
.btn.active .small-dot {
  transform: translate(30px, -30px);
  transition: transform 0.5s;
  transition-timing-function: cubic-bezier(0.68, -0.55, 0.265, 1.55);
  z-index: 1;
}
.btn.active .big-dot {
  transform: translate(-30px, -30px);
  transition: transform 0.5s;
  transition-timing-function: cubic-bezier(0.68, -0.55, 0.265, 1.55);
  transition-delay: 0.2s;
  z-index: 1;
}
.selector-container {
  display: flex;
  justify-content: center;
  align-items: center;
  position: absolute;
  width: 100%;
  height: 100%;
}
.selector-container .selectors {
  width: 200px;
  height: 200px;
  position: relative;
}
.selector-container .selectors .selector {
  width: 40px;
  height: 40px;
  position: absolute;
  top: 80px;
  left: 80px;
  background-color: green;
  border-radius: 50%;
  border: 3px solid #c58cc4;
  z-index: 1;
  transition: transform 0.5s;
  transition-timing-function: cubic-bezier(0.68, -0.55, 0.265, 1.55);
}
.selector-container .selectors .selector:hover {
  cursor: pointer;
}
class ColorPicker extends React.Component {
  constructor(props) {
    super(props);
    this.togglePickers = this.togglePickers.bind(this);
    this.setBackgroundColor = this.setBackgroundColor.bind(this);
    this.state = {
      active: false,
      bgColor: "#f44195",
      pickerColors: ["#4286f4", "#f44195", "#330f53", "#f4be41", "#4545ad", "#f44141", "#82e276", "#ff8f2d"],
      timeout: null,
      sliders: [],
      slideDuration: 500 };
  }
  setBackgroundColor(bgColor) {
    var sliders = this.state.sliders;
    sliders.push( /*#__PURE__*/React.createElement(Slider, { bgColor: bgColor, transitionDuration: this.state.slideDuration }));
    var timeout = this.state.timeout;
    clearTimeout(timeout);
    timeout = setTimeout(() => {
      this.setState({ bgColor: bgColor, sliders: [] });
    }, this.state.slideDuration);
    this.setState({
      sliders: sliders,
      timeout: timeout });
  }
  componentDidMount() {
    window.addEventListener('load', () => {
      this.setState({ active: true });
      this.selectors.style.transform = "rotate(360deg)";
      const delay = 0.2;
      let duration = delay * this.state.pickerColors.length;
      this.selectors.style.transition = "transform " + duration + "s" + " ease-in-out";
      setTimeout(() => {
        this.setState({ active: false });
        this.selectors.style.transform = "none";
        this.selectors.style.transition = "none";
      }, duration * 1000);
    });
  }
  togglePickers() {
    this.setState({ active: !this.state.active });
  }
  render() {
    const pickerColors = this.state.pickerColors;
    const len = pickerColors.length;
    const degIncrement = 360 / len;
    const pickers = [];
    const radius = 100;
    const pickerAnimationDelay = 0.1;
    let i = 0;
    let delay;
    for (i = 0; i < len; i += 1) {
      const degrees = i * degIncrement;
      const rad = degrees * Math.PI / 180;
      delay = pickerAnimationDelay * i;
      let coordinateObject = {};
      let x = radius * Math.cos(rad);
      let y = radius * Math.sin(rad);
      coordinateObject.x = x;
      coordinateObject.y = y;
      pickers.push( /*#__PURE__*/
      React.createElement(Picker, {
        transformCoordinates: coordinateObject,
        pickerIsActive: this.state.active,
        pickerClicked: this.setBackgroundColor,
        pickerColor: pickerColors[i],
        delay: delay }));
    }
    return /*#__PURE__*/(
      React.createElement("div", {
        style: { backgroundColor: this.state.bgColor },
        className: "color-picker-container" }, /*#__PURE__*/
      React.createElement("svg", {
        viewBox: "0 0 100 100",
        preserveAspectRatio: "none",
        className: "svg-sliders" },
      this.state.sliders), /*#__PURE__*/
      React.createElement("div", { className: "btn-container" }, /*#__PURE__*/
      React.createElement("div", { className: "btn" + (this.state.active === true ? " active" : ""), onClick: this.togglePickers }, /*#__PURE__*/
      React.createElement("div", { className: "small-dot", style: { transitionDelay: this.state.active !== true ? delay + "s" : "0s" } }), /*#__PURE__*/
      React.createElement("div", { className: "big-dot", style: { transitionDelay: this.state.active !== true ? delay - 0.2 + "s" : "0.2s" } }))), /*#__PURE__*/
      React.createElement("div", { className: "selector-container" }, /*#__PURE__*/
      React.createElement("div", {
        ref: selectors => {this.selectors = selectors;},
        className: "selectors" },
      pickers))));
  }}
class Picker extends React.Component {
  constructor(props) {
    super(props);
    this.pickerClicked = this.pickerClicked.bind(this);
    this.state = {
      active: false };
  }
  pickerClicked() {
    this.props.pickerClicked(this.props.pickerColor);
  }
  render() {
    return /*#__PURE__*/(
      React.createElement("span", {
        style:
        this.props.pickerIsActive ?
        {
          backgroundColor: this.props.pickerColor,
          transform: "translate(" + this.props.transformCoordinates.x + "px, " + this.props.transformCoordinates.y + "px)",
          transitionDelay: this.props.delay + "s" } :
        {
          backgroundColor: this.props.pickerColor,
          transform: "translate(0px,0px)",
          transitionDelay: this.props.delay + "s" },
        className: "selector",
        onClick: this.pickerClicked }));
  }}
class Slider extends React.Component {
  constructor(props) {
    super(props);
    this.getRandomIntInclusive = this.getRandomIntInclusive.bind(this);
    this.bounceCurve = this.bounceCurve.bind(this);
    this.state = {
      curvePointCoordinates: {
        x: 0,
        y: 140 },
      startX: this.getRandomIntInclusive(35, 65),
      startY: 140,
      then: performance.now(),
      start: performance.now() };
  }
  componentDidMount() {
    const transitionDuration = this.props.transitionDuration;
    this.slider.style.transition = "none";
    this.slider.style.transform = "translatey(-100%)";
    this.slider.clientHeight;
    this.slider.style.transition = "transform " + transitionDuration + "ms ease-in-out";
    this.slider.style.fill = this.props.bgColor;
    this.slider.style.transform = "translatey(0%)";
    this.bounceCurve();
  }
  getRandomIntInclusive(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min + 1)) + min; //The maximum is inclusive and the minimum is inclusive 
  }
  bounceCurve() {
    const now = performance.now();
    const elapsedTime = now - this.state.start;
    const delta = now - this.state.then;
    let spacetoCover = this.state.startY - 100;
    let duration = this.props.transitionDuration;
    //calculate displacement for the time passed
    let currentDisplacement = spacetoCover / duration * delta;
    let coordinateY = this.state.curvePointCoordinates.y - currentDisplacement;
    let curvePointCoordinates = { x: this.state.startX, y: coordinateY };
    this.setState({ curvePointCoordinates: curvePointCoordinates, then: now });
    if (elapsedTime < this.props.transitionDuration) {
      requestAnimationFrame(this.bounceCurve);
    } else {
      const curvePointCoordinates = { x: 50, y: 100 };
      this.setState({ curvePointCoordinates: curvePointCoordinates });
    }
  }
  render() {
    return /*#__PURE__*/(
      React.createElement("path", {
        style: { fill: this.props.bgColor },
        ref: slider => {this.slider = slider;},
        d: "M0 0 H100 V100 Q" + this.state.curvePointCoordinates.x + " " + this.state.curvePointCoordinates.y + " 0 100 L 0 0" }));
  }}
ReactDOM.render( /*#__PURE__*/
React.createElement(ColorPicker, null),
document.getElementById('color-picker-root'));
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react-dom.min.js'></script>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- This script got from frontendfreecode.com -->

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react-dom.min.js'></script>
<style>
* {
  box-sizing: border-box;
}
.color-picker-container {
  position: absolute;
  width: 100%;
  height: 100%;
  background-color: #330f53;
}
.svg-sliders {
  display: block;
  position: absolute;
  width: 100%;
  height: 100%;
}
.svg-sliders .slider {
  transform-origin: center center;
  transform: translatey(-100%);
}
.btn-container {
  display: flex;
  justify-content: center;
  align-items: center;
  position: absolute;
  width: 100%;
  height: 100%;
}
.btn {
  position: relative;
  width: 60px;
  height: 60px;
  background-color: #8b1a89;
  border: 3px solid;
  border-color: #c58cc4;
  border-radius: 50%;
  z-index: 2;
  transform: scale(1.25);
  box-shadow: 0px 1px 3px 0px rgba(0, 0, 0, 0.5);
  transition: box-shadow 0.2s ease-in-out, transform 0.2s ease-in-out, border-color 0.2s ease-in-out;
}
.btn:hover {
  cursor: pointer;
}
.btn:active {
  box-shadow: 0px 0px 1px 0px rgba(0, 0, 0, 0.5);
  transition: box-shadow 0.1s ease-in-out;
}
.btn .small-dot {
  content: " ";
  position: absolute;
  width: 4px;
  height: 4px;
  background-color: #8b1a89;
  bottom: 4px;
  left: -2px;
  border-radius: 50%;
  transform: translate(0px, 0px);
  transition: transform 0.5s;
  transition-timing-function: cubic-bezier(0.68, -0.55, 0.265, 1.55);
  z-index: 1;
}
.btn .big-dot {
  content: " ";
  position: absolute;
  width: 6px;
  height: 6px;
  background-color: #8b1a89;
  bottom: -2px;
  right: 0px;
  border-radius: 50%;
  transform: translate(0px, 0px);
  transition: transform 0.5s;
  transition-timing-function: cubic-bezier(0.68, -0.55, 0.265, 1.55);
  transition-delay: 0.2s;
  z-index: 1;
}
.btn.active {
  border-color: #dd52c9;
  box-shadow: 0px 0px 1px 0px rgba(0, 0, 0, 0.5);
  transform: scale(1);
  transition: box-shadow 0.2s ease-in-out, transform 0.2s ease-in-out, border-color 0.2s ease-in-out;
}
.btn.active .small-dot {
  transform: translate(30px, -30px);
  transition: transform 0.5s;
  transition-timing-function: cubic-bezier(0.68, -0.55, 0.265, 1.55);
  z-index: 1;
}
.btn.active .big-dot {
  transform: translate(-30px, -30px);
  transition: transform 0.5s;
  transition-timing-function: cubic-bezier(0.68, -0.55, 0.265, 1.55);
  transition-delay: 0.2s;
  z-index: 1;
}
.selector-container {
  display: flex;
  justify-content: center;
  align-items: center;
  position: absolute;
  width: 100%;
  height: 100%;
}
.selector-container .selectors {
  width: 200px;
  height: 200px;
  position: relative;
}
.selector-container .selectors .selector {
  width: 40px;
  height: 40px;
  position: absolute;
  top: 80px;
  left: 80px;
  background-color: green;
  border-radius: 50%;
  border: 3px solid #c58cc4;
  z-index: 1;
  transition: transform 0.5s;
  transition-timing-function: cubic-bezier(0.68, -0.55, 0.265, 1.55);
}
.selector-container .selectors .selector:hover {
  cursor: pointer;
}
</style>

</head>
<body>
<div id="color-picker-root"></div><div id="bcl"><a style="font-size:8pt;text-decoration:none;" href="http://www.devanswer.com">Free Frontend</a></div>
<script>
class ColorPicker extends React.Component {
  constructor(props) {
    super(props);
    this.togglePickers = this.togglePickers.bind(this);
    this.setBackgroundColor = this.setBackgroundColor.bind(this);
    this.state = {
      active: false,
      bgColor: "#f44195",
      pickerColors: ["#4286f4", "#f44195", "#330f53", "#f4be41", "#4545ad", "#f44141", "#82e276", "#ff8f2d"],
      timeout: null,
      sliders: [],
      slideDuration: 500 };
  }
  setBackgroundColor(bgColor) {
    var sliders = this.state.sliders;
    sliders.push( /*#__PURE__*/React.createElement(Slider, { bgColor: bgColor, transitionDuration: this.state.slideDuration }));
    var timeout = this.state.timeout;
    clearTimeout(timeout);
    timeout = setTimeout(() => {
      this.setState({ bgColor: bgColor, sliders: [] });
    }, this.state.slideDuration);
    this.setState({
      sliders: sliders,
      timeout: timeout });
  }
  componentDidMount() {
    window.addEventListener('load', () => {
      this.setState({ active: true });
      this.selectors.style.transform = "rotate(360deg)";
      const delay = 0.2;
      let duration = delay * this.state.pickerColors.length;
      this.selectors.style.transition = "transform " + duration + "s" + " ease-in-out";
      setTimeout(() => {
        this.setState({ active: false });
        this.selectors.style.transform = "none";
        this.selectors.style.transition = "none";
      }, duration * 1000);
    });
  }
  togglePickers() {
    this.setState({ active: !this.state.active });
  }
  render() {
    const pickerColors = this.state.pickerColors;
    const len = pickerColors.length;
    const degIncrement = 360 / len;
    const pickers = [];
    const radius = 100;
    const pickerAnimationDelay = 0.1;
    let i = 0;
    let delay;
    for (i = 0; i < len; i += 1) {
      const degrees = i * degIncrement;
      const rad = degrees * Math.PI / 180;
      delay = pickerAnimationDelay * i;
      let coordinateObject = {};
      let x = radius * Math.cos(rad);
      let y = radius * Math.sin(rad);
      coordinateObject.x = x;
      coordinateObject.y = y;
      pickers.push( /*#__PURE__*/
      React.createElement(Picker, {
        transformCoordinates: coordinateObject,
        pickerIsActive: this.state.active,
        pickerClicked: this.setBackgroundColor,
        pickerColor: pickerColors[i],
        delay: delay }));
    }
    return /*#__PURE__*/(
      React.createElement("div", {
        style: { backgroundColor: this.state.bgColor },
        className: "color-picker-container" }, /*#__PURE__*/
      React.createElement("svg", {
        viewBox: "0 0 100 100",
        preserveAspectRatio: "none",
        className: "svg-sliders" },
      this.state.sliders), /*#__PURE__*/
      React.createElement("div", { className: "btn-container" }, /*#__PURE__*/
      React.createElement("div", { className: "btn" + (this.state.active === true ? " active" : ""), onClick: this.togglePickers }, /*#__PURE__*/
      React.createElement("div", { className: "small-dot", style: { transitionDelay: this.state.active !== true ? delay + "s" : "0s" } }), /*#__PURE__*/
      React.createElement("div", { className: "big-dot", style: { transitionDelay: this.state.active !== true ? delay - 0.2 + "s" : "0.2s" } }))), /*#__PURE__*/
      React.createElement("div", { className: "selector-container" }, /*#__PURE__*/
      React.createElement("div", {
        ref: selectors => {this.selectors = selectors;},
        className: "selectors" },
      pickers))));
  }}
class Picker extends React.Component {
  constructor(props) {
    super(props);
    this.pickerClicked = this.pickerClicked.bind(this);
    this.state = {
      active: false };
  }
  pickerClicked() {
    this.props.pickerClicked(this.props.pickerColor);
  }
  render() {
    return /*#__PURE__*/(
      React.createElement("span", {
        style:
        this.props.pickerIsActive ?
        {
          backgroundColor: this.props.pickerColor,
          transform: "translate(" + this.props.transformCoordinates.x + "px, " + this.props.transformCoordinates.y + "px)",
          transitionDelay: this.props.delay + "s" } :
        {
          backgroundColor: this.props.pickerColor,
          transform: "translate(0px,0px)",
          transitionDelay: this.props.delay + "s" },
        className: "selector",
        onClick: this.pickerClicked }));
  }}
class Slider extends React.Component {
  constructor(props) {
    super(props);
    this.getRandomIntInclusive = this.getRandomIntInclusive.bind(this);
    this.bounceCurve = this.bounceCurve.bind(this);
    this.state = {
      curvePointCoordinates: {
        x: 0,
        y: 140 },
      startX: this.getRandomIntInclusive(35, 65),
      startY: 140,
      then: performance.now(),
      start: performance.now() };
  }
  componentDidMount() {
    const transitionDuration = this.props.transitionDuration;
    this.slider.style.transition = "none";
    this.slider.style.transform = "translatey(-100%)";
    this.slider.clientHeight;
    this.slider.style.transition = "transform " + transitionDuration + "ms ease-in-out";
    this.slider.style.fill = this.props.bgColor;
    this.slider.style.transform = "translatey(0%)";
    this.bounceCurve();
  }
  getRandomIntInclusive(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min + 1)) + min; //The maximum is inclusive and the minimum is inclusive 
  }
  bounceCurve() {
    const now = performance.now();
    const elapsedTime = now - this.state.start;
    const delta = now - this.state.then;
    let spacetoCover = this.state.startY - 100;
    let duration = this.props.transitionDuration;
    //calculate displacement for the time passed
    let currentDisplacement = spacetoCover / duration * delta;
    let coordinateY = this.state.curvePointCoordinates.y - currentDisplacement;
    let curvePointCoordinates = { x: this.state.startX, y: coordinateY };
    this.setState({ curvePointCoordinates: curvePointCoordinates, then: now });
    if (elapsedTime < this.props.transitionDuration) {
      requestAnimationFrame(this.bounceCurve);
    } else {
      const curvePointCoordinates = { x: 50, y: 100 };
      this.setState({ curvePointCoordinates: curvePointCoordinates });
    }
  }
  render() {
    return /*#__PURE__*/(
      React.createElement("path", {
        style: { fill: this.props.bgColor },
        ref: slider => {this.slider = slider;},
        d: "M0 0 H100 V100 Q" + this.state.curvePointCoordinates.x + " " + this.state.curvePointCoordinates.y + " 0 100 L 0 0" }));
  }}
ReactDOM.render( /*#__PURE__*/
React.createElement(ColorPicker, null),
document.getElementById('color-picker-root'));
</script>

</body>
</html>
Preview