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

              
                <header class='site-header' role='banner'>
  <div class='page-width'>
    <h1>Keyboard Accessible Modal Window Demo</h1>
  </div>
</header>
<main role='main'>
  <div aria-labelledby='KeyboardAccessibleModal' class='modal-container' role='dialog' aria-labelledby="KeyboardAccessibleModal">
      <div class='modal-content'>
        <div class='modal-content__header'>
          <button class='btn btn--close modal-trigger' type='button'>Return to Page</button>
        </div>
        <div class='modal-content__body'>
          <h2 id='KeyboardAccessibleModal'>Keyboard Modal Content</h2>
          <p>
            Close the window by tabbing to "Return to Page" or by hitting the escape key!
          </p>
          <a href='http://en.wikipedia.org/wiki/Potato_chip'>Tell me about Chips!</a>
        </div>
      </div>
    </div>
    
  <div class='page-container page-width'>
    <div class='flex-container'>
      <div class='layout-col-left'>
        <nav aria-label='demo links for tabbing' class='nav--vertical'>
          <ul>
            <li>
              <a href='#'>Link</a>
            </li>
            <li>
              <a href='#'>Link</a>
            </li>
            <li>
              <a href='#'>Link</a>
            </li>
            <li>
              <a href='#'>Link</a>
            </li>
            <li>
              <a href='#'>Link</a>
            </li>
          </ul>
        </nav>
      </div>
      <div class='layout-col-right'>
        <div class='content'>
          <h2>
            Modal Windows & Keyboard focus
          </h2>
          <p>
            Navigate with your keyboard and watch the keyboard focus states - tab through to modal, open, close, repeat
          </p>
          <p>
            Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dignissimos totam doloribus velit iusto blanditiis deserunt minima eveniet, expedita, dolor dolorum officiis veniam unde. Aliquam voluptatem obcaecati officia ducimus perspiciatis! Reprehenderit.
          </p>
          <button class='modal-trigger btn' type='button'>Trigger Modal Window</button>
        </div>
      </div>
    </div>
  </div>
</main>
<footer>
  <div class='page-width'>
    <div class='author'>
      By
      <a href='https://twitter.com/monsika'>Monika Piotrowicz</a>
    </div>
  </div>
</footer>


              
            
!

CSS

              
                @import "bourbon";

/*============================================================================
  Modal
==============================================================================*/
.modal-container {
  display: none;
  background: #000;
  height: 100%;
  left: 0;
  opacity: 0;
  overflow: scroll;
  position: fixed;
  top: 0;
  width: 100%;
  z-index: -1;
  color: #FFF;
  -webkit-transition: opacity 0.18s;
  -moz-transition: opacity 0.18s;
  transition: opacity 0.18s;
  -webkit-overflow-scrolling: touch;
  
  .modal-content {
    max-width: 900px;
    margin: 50px auto 0;
    padding: 50px;
    background: #fff;
    color: #000;
    border-radius: 5px;
  }
  
  .modal--open & {
    display: block;
    opacity: 1;
    visibility: visible;
    z-index: 1000;
  }
}

/*============================================================================
Demo  Layout
==============================================================================*/
*, *:before, *:after {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}

@media (min-width: 768px) {
  body {
    display: flex;
    min-height: 100vh;
    flex-direction: column;
  }

  main,
  .flex-container {
    display: flex;
    flex: 1;
  }

  .flex-container {
    flex-direction: row;
  }
  
  .layout-col-right {
    flex: 1;
  }
  
  .layout-col-left {
    flex: 0 0 12em;
  }
}

.page-width {
   margin: 0 auto;
   max-width: 960px;
}

header, 
footer {
  padding: 30px 0;
}

footer {
  margin-top: 30px;
  background: #ccc;
  text-align: right;
}

nav ul,
nav li {
  list-style: none;
  margin-bottom: 10px;
}

/*============================================================================
  Typography
==============================================================================*/
body {
  font-family: helvetica, arial, sans-serif;
  background: #fff;
  color: #000;
  line-height: 1.2;
}

a {
  color: #5C5252;
  &:hover,
  &:focus {
    color: #000;
  }
}

p {
  margin-bottom: 20px;
}

/*============================================================================
  Buttons
==============================================================================*/

.btn {
  padding: 8px;
  background: #666;
  color: #fff;
  border: #999;
  border-radius: 3px;
  line-height: 1.4;
  @include transition(background 150ms ease, color 150ms ease);
  &:hover,
  &:focus {
    background: #000;
  }
  &:focus {
    box-shadow: 0 0 0 1px #FFF, 0 0 0 3px rgba(0, 132, 180, 0.5);
  }
}

.btn--close {
  float: right;
  background: none;
  color: #174EDA;
  &:hover,
  &:focus {
    text-decoration: underline;
    background: none;
  }
}

              
            
!

JS

              
                var a11yModal = {
  init: function (options) {
    var defaults = {
      $body           : $('body'),
      $modalContainer : $('.modal-container'),
      classOpen       : 'modal--open',
      classTrigger    : 'modal-trigger',
      modalNamespace  : 'a11y_modal'
    };

    this.options = $.extend({}, defaults, options);
    this.bindEvents();
    this.options.$modalContainer.attr('tabindex', '-1');
    this.modalIsOpen = false;
  },

  bindEvents : function () {
    $('.' + this.options.classTrigger).on('click', $.proxy(this.handleClick, this));
    this.options.$modalContainer.on('keydown', $.proxy(this.handleKeyDown, this));
  },

  handleClick: function (evt) {
    evt.preventDefault();

    if (this.modalIsOpen) {
      this.modalClose();
    }
    else {
      this.$modalTrigger = $(evt.currentTarget);
      this.modalOpen();
    }
  },

  handleKeyDown : function (evt) {
    if (this.modalIsOpen) {
      switch (evt.keyCode) {
        // escape
        case 27:
          this.modalClose();
          break;
      }
    }
  },

  modalOpen: function() {
    this.options.$body.addClass(this.options.classOpen);
    this.trapFocus();
    this.options.$modalContainer.focus();
    this.modalIsOpen = true;
  },

  modalClose : function () {
    this.options.$body.removeClass(this.options.classOpen);

    // turn of the focus listener
    this.removeTrapFocus();

    this.$modalTrigger.focus();
    this.modalIsOpen = false;
  },

  trapFocus : function () {
    var $container = this.options.$modalContainer;
    $(document).on('focusin.' + this.options.modalNamespace, function (evt) {
      if ($container[0] !== evt.target && !$container.has(evt.target).length) {
        $container.focus();
      }
    });
  },

  removeTrapFocus : function () {
    $(document).off('focusin.' + this.options.modalNamespace);
  }
};

$(function() {
  a11yModal.init();
});

              
            
!
999px

Console