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="app"></div>

              
            
!

CSS

              
                @use postcss-cssnext;

:root {
  --font-family: "Lato", "Lucida Grande","Lucida Sans Unicode", Tahoma, sans-serif;
  --marginMenuToPopup: 10px;
}

html,
button {
  font-family: var(--font-family);
}

main {
  margin: 1em auto;
  min-height: 100vh;
  max-width: 30em;
  padding: 0 3em;
  line-height: 1.5;
}

h1 {
  font-size: 1.5em;
}

h2 {
  font-size: 1.3em;
}

.Menu {
  margin: 0;
  padding-left: 0;
  list-style: none;
  white-space: nowrap;
  text-align: center;
}

.Menu button {
  border: 0;
  background: none;
}

.Menu li + li {
  margin-top: 0.5em;
}

.PopupMenuWrapper.-under {
  padding-bottom: 8em;
}

.PopupMenuWrapper.-over {
  padding-top: 8em;
}

/*
 * Component Style
 */

.PopupMenu {
  position: relative;
  display: inline-block;
}

.PopupMenu__inner {
  display: flex;
  justify-content: center;
}

.PopupMenu__Button {
  padding: 0.5em 1.5em;
  border: 0;
  border-radius: 3em;
  background-color: midnightblue;
  color: #fff;
}

.PopupMenu__Menu {
  align-self: center;
  position: absolute;
  bottom: calc(2.5em + var(--marginMenuToPopup)); /* button height + margin */
  padding: 0.5em;
  border-radius: 2px;
  background-color: color(midnightblue a(25%));
}

.PopupMenu__Menu.-under {
  top: calc(2.5em + var(--marginMenuToPopup));
  bottom: auto;
}

.PopupMenu__Menu:not([aria-hidden="true"]) {
  transition: transform 0.15s ease-in-out,
              opacity 0.15s ease-in-out,
              visibility 0.15s;
}

.PopupMenu__Menu[aria-hidden="true"] {
  visibility: hidden;
  opacity: 0;
  transform: translateY(10px);
  transition: transform 0.1s ease-in-out,
              opacity 0.1s ease-in-out,
              visibility 0s ease 0.1s;
}

.PopupMenu__Menu.-under[aria-hidden="true"] {
  transform: translateY(-10px);
}

/* for hover area between button and menu */
.PopupMenu__Menu::before {
  content: "";
  position: absolute;
  display: block;
  bottom: -1em;
  width: 100%;
  height: 1em;
}

.PopupMenu__Menu.-under::before {
  top: -1em;
  bottom: auto;
}

.PopupMenu__Menu::after {
  content: "";
  position: absolute;
  right: 0;
  left: 0;
  bottom: calc(var(--marginMenuToPopup) * -2);
  display: block;
  height: 0;
  width: 0;
  margin: auto;
  border: var(--marginMenuToPopup) solid transparent;
  border-top-color: color(midnightblue a(25%));
}

.PopupMenu__Menu.-under::after {
  top: calc(var(--marginMenuToPopup) * -2);
  bottom: 0;
  bottom: auto;
  border-color: transparent;
  border-bottom-color: color(midnightblue a(25%));
}

              
            
!

JS

              
                class PopupMenu extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      isShownMenu : false
    }
    
    this.onClickButton = this.onClickButton.bind(this);
    this.onMouseEnter  = this.onMouseEnter.bind(this);
    this.onMouseLeave  = this.onMouseLeave.bind(this);
  }
  
  onClickButton() {
    this.setState({
      isShownMenu : !this.state.isShownMenu
    });
  }
  
  onMouseEnter() {
    this.setState({
      isShownMenu : true
    })
  }
  
  onMouseLeave() {
    this.setState({
      isShownMenu : false
    })
  }

  render() {
    return(
      <div
        className="PopupMenu"
        aria-expanded={this.state.isShownMenu}
        onMouseEnter={this.onMouseEnter}
        onMouseLeave={this.onMouseLeave}
      >
        <div className="PopupMenu__inner">
          <button
            type="button"
            aria-haspopup="true"
            aria-controls={this.props.id}
            className="PopupMenu__Button"
            onClick={this.onClickButton}
          >
            {this.props.title}
          </button>
          <div
            id={this.props.id}
            className={`PopupMenu__Menu -${this.props.position}`}
            aria-hidden={!this.state.isShownMenu}
            aria-label={this.props.menuLabel}
          >
            {this.props.children}
          </div>
        </div>
      </div>
    );
  }
}

PopupMenu.defaultProps = {
  position : 'over',
  title    : 'Menu'
};


class Container extends React.Component {
  render() {
    return (
      <main>
        <h1>Popup menu React component, hover and keyboard</h1>
        <section>
          <h2>Under Menu</h2>
          <div className="PopupMenuWrapper -under">
            <PopupMenu id="PopupMenuUnder" menuLabel="Menu Under" title="Menu" position="under">
              <ul className="Menu">
                <li><button type="button">MenuItem2</button></li>
                <li><button type="button">MenuItem</button></li>
                <li><button type="button">MenuItem</button></li>
              </ul>
            </PopupMenu>
          </div>
        </section>
        <section>
          <h2>Over Menu</h2>
          <div className="PopupMenuWrapper -over">
            <PopupMenu id="PopupMenuOver" menuLabel="Menu Over">
              <ul className="Menu">
                <li><button type="button">Awesome Menu Item</button></li>
                <li><button type="button">Cool Menu Item</button></li>
                <li><button type="button">Smart Menu Item</button></li>
              </ul>
            </PopupMenu>
          </div>
        </section>
      </main>
    );
  }
}

ReactDOM.render(
  <Container />,
  document.getElementById('app')
);
              
            
!
999px

Console