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

              
                <div id="main"></div>
              
            
!

CSS

              
                @import url(https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.min.css);
html {
  height: 100%;
}
body {
  background: #DCDCDC;
  height: 100%;
}

#main {
  height: 100%;
  display: flex;
  justify-content: space-around;
  align-items: space-around;
}

#app {
  flex-basis: 90%;
  display: flex;
  flex-direction: column;
}

#headline {
  align-self: center;
}

#content {
  display: flex;
  justify-content: space-around;
  align-items: center;
  flex-grow: 1;
}



.button-container {
  position: relative;
  display: flex;
}

.button-menu {
  background: #72B0EC;
  border-radius: 1000px;
  box-shadow: 0px 1px 5px 0px rgba(0,0,0,0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
}

.button-child {
  position: absolute;
  background: white;
  border-radius: 1000px;
  box-shadow: 0px 1px 5px 0px rgba(0,0,0,0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
}

.child-button-icon {
  color: grey;
}

.main-button-icon {
  color: white;
}
              
            
!

JS

              
                // React Motion
let Motion = ReactMotion.Motion;
let spring = ReactMotion.spring;

// CONSTANTS
// Value of 1 degree in radians
const DEG_TO_RAD = 0.0174533;
const ELEMENTS = [
  {
    icon: "home",
    onClick: () => alert("clicked home")
  },{
    icon: "clock-o",
    onClick: () => alert("clicked clock")
  },{
    icon: "lock",
    onClick: () => alert("clicked lock")
  },{
    icon: "globe",
    onClick: () => alert("clicked globe")
  },{
    icon: "asterisk",
    onClick: () => alert("clicked asterisk")
  },{
    icon: "fighter-jet",
    onClick: () => alert("clicked fighter-jet")
  },{
    icon: "clipboard",
    onClick: () => alert("clicked clipboard")
  },{
    icon: "industry",
    onClick: () => alert("clicked industry")
  },{
    icon: "eye",
    onClick: () => alert("clicked eye")
  }
  
];


// UTILITY FUNCTIONS
function toRadians(degrees) {
	return degrees * DEG_TO_RAD;
}

// -------------------------------------------------------
// ---------------   COMPONENT START   -------------------
// -------------------------------------------------------
class MenuButton extends React.Component {
  
  constructor(props){
    super(props);
    
    this.state = {
      isOpen: true
    };
  }
  
  toggleMenu(){
    let { isOpen } = this.state;
    this.setState({
      isOpen: !isOpen
    });
  }
  
  getMainButtonStyle(){
    let { mainButtonDiam } = this.props;
    return {
      width: mainButtonDiam,
      height: mainButtonDiam
    }
  }
  
  getInitalChildButtonStyle(){
    let { childButtonDiam, mainButtonDiam, stiffness, damping } = this.props;
    return {
      width: childButtonDiam,
      height: childButtonDiam,
      zIndex: -1,
      top: spring(mainButtonDiam/2 - childButtonDiam/2, {stiffness, damping}),
      left: spring(mainButtonDiam/2 - childButtonDiam/2, {stiffness, damping})
    }
  }
  
  getFinalChildButtonStyle(index){
    let { childButtonDiam, mainButtonDiam, stiffness, damping } = this.props;
    let { deltaX, deltaY } = this.getFinalDeltaPositions(index);
    return {
      width: childButtonDiam,
      height: childButtonDiam,
      zIndex: spring(0),
      top: spring(mainButtonDiam/2 + deltaX, {stiffness, damping}),
      left: spring(mainButtonDiam/2 - deltaY, {stiffness, damping})
    }
  }
  
  getFinalDeltaPositions(index) {
    let NUM_CHILDREN = this.props.elements.length;
    let CHILD_BUTTON_DIAM = this.props.childButtonDiam;
    let FLY_OUT_RADIUS = this.props.flyOutRadius;
    let SEPARATION_ANGLE = this.props.seperationAngle;
    let ROTATION = this.props.rotation;
    let FAN_ANGLE = (NUM_CHILDREN - 1) * SEPARATION_ANGLE;
    let BASE_ANGLE = ((180 - FAN_ANGLE)/2)+90+ROTATION;
    
    let TARGET_ANGLE = BASE_ANGLE + ( index * SEPARATION_ANGLE );
    return {
      deltaX: FLY_OUT_RADIUS * Math.cos(toRadians(TARGET_ANGLE)) - (CHILD_BUTTON_DIAM/2),
      deltaY: FLY_OUT_RADIUS * Math.sin(toRadians(TARGET_ANGLE)) + (CHILD_BUTTON_DIAM/2)
    };
  }
  
  getCProps(){
    return {
      mainButtonProps: () => ({
        className: "button-menu",
        style: this.getMainButtonStyle(),
        onClick: ::this.toggleMenu
      }),
      childButtonProps: (style, onClick) => ({
        className: "button-child",
        style,
        onClick
      }),
      childButtonMotionProps: (index, isOpen) => ({
        key: index,
        style: isOpen ? ::this.getFinalChildButtonStyle(index)
                      : ::this.getInitalChildButtonStyle()
      }),
      // handle Icons
      childButtonIconProps: (name) => ({
        className: "child-button-icon fa fa-"+name+" fa-"+this.props.childButtonIconSize
      }),
      mainButtonIconProps: (name) => ({
        className: "main-button-icon fa fa-"+name+" fa-"+this.props.mainButtonIconSize
      })
    }
  }
  
  renderChildButton(item, index){
    let { isOpen } = this.state;
    let cp = this.getCProps();
    
    //return <div {...cp.childButtonProps(index, isOpen)}/>;
    
    return <Motion {...cp.childButtonMotionProps(index, isOpen)}>
      {
        (style) => <div {...cp.childButtonProps(style, item.onClick)}>
          <i {...cp.childButtonIconProps(item.icon)}/>
        </div>
      }
    </Motion>;
  }
  
  render(){
    let cp = this.getCProps();
    let { elements, mainButtonIcon } = this.props;
    let { isOpen } = this.state;
    
    return <div className="button-container">
      { elements.map((item, i) => this.renderChildButton(item, i)) }
      <div {...cp.mainButtonProps()}>
        <i {...cp.mainButtonIconProps(mainButtonIcon)}/>
      </div>
    </div>;
  }
}
// -------------------------------------------------------
// ----------------   COMPONENT END   --------------------
// -------------------------------------------------------


// APP
class App extends React.Component {
  constructor(props){
    super(props);
    
    this.state = {
      flyOutRadius: 120,
      seperationAngle: 40,
      mainButtonDiam: 90,
      childButtonDiam: 50,
      numElements: 4,
      stiffness: 320,
      damping: 17,
      rotation: 0,
      mainButtonIcon: "bars",
      mainButtonIconSize: "2x",
      childButtonIconSize: "lg"
    }
  }
  
  getInputProps(type, title){
    return {
      type: type,
      value: this.state[title],
      onChange: (e) => type === "number" 
        ? this.setState({[title]: parseInt(e.target.value || 0)})
        : this.setState({[title]: e.target.value })
    }
  }
  
  render(){
    const NUM = "number";
    const TEX = "text";
    return <div id="app">
      <div id="headline"><h1>React Menu Button</h1></div>
      <div id="content">
        <div id="component">
          <MenuButton {...this.state} elements={ELEMENTS.slice(0, this.state.numElements)}/>
        </div>
        <div id="config">
          <h2>Props</h2>
          <table>
            <tbody>
              <tr>
                <td>fly out radius:</td>
                <td><input {...this.getInputProps(NUM, "flyOutRadius")}/></td>
              </tr>
              <tr>
                <td>seperation angle:</td>
                <td><input {...this.getInputProps(NUM, "seperationAngle")}/></td>
              </tr>
              <tr>
                <td>main button diam:</td>
                <td><input {...this.getInputProps(NUM, "mainButtonDiam")}/></td>
              </tr>
              <tr>
                <td>child button diam:</td>
                <td><input {...this.getInputProps(NUM, "childButtonDiam")}/></td>
              </tr>
              <tr>
                <td>num elements:</td>
                <td><input {...this.getInputProps(NUM, "numElements")}/></td>
                <td><i className="fa fa-info" onClick={() => 
                  alert("normaly no number, but an array of obj {icon, onClick}")}/></td>
              </tr>
              <tr>
                <td>stiffness:</td>
                <td><input {...this.getInputProps(NUM, "stiffness")}/></td>
              </tr>
              <tr>
                <td>damping:</td>
                <td><input {...this.getInputProps(NUM, "damping")}/></td>
              </tr>
              <tr>
                <td>rotation:</td>
                <td><input {...this.getInputProps(NUM, "rotation")}/></td>
              </tr>
              <tr>
                <td>main button icon:</td>
                <td><input {...this.getInputProps(TEX, "mainButtonIcon")}/></td>
                <td><i className="fa fa-info" onClick={() => 
                  alert("font awesome icon")}/></td>
              </tr>
              <tr>
                <td>main button icon size:</td>
                <td><input {...this.getInputProps(TEX, "mainButtonIconSize")}/></td>
                <td><i className="fa fa-info" onClick={() => 
                  alert("none | lg | 2x | 3x | 4x | 5x")}/></td>
              </tr>
              <tr>
                <td>child button icon size:</td>
                <td><input {...this.getInputProps(TEX, "childButtonIconSize")}/></td>
                <td><i className="fa fa-info" onClick={() => 
                  alert("none | lg | 2x | 3x | 4x | 5x")}/></td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>
    </div>;
  }
}

// render ToggleButton to DOM
ReactDOM.render(
  <App/>,
  document.getElementById("main")
);
              
            
!
999px

Console