cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

            
              <div id="drawer-panel" class="offscreen-content drawer-panel">
  <h3>Navigation</h3>
  <div>
    <input type="search" name="search" /><button for="search">Search</button>
  </div>
  <ul>
    <li><a href="#">Ratione ab modi qui</a></li>
    <li><a href="#">consectetur adipisicing elit</a></li>
    <li><a href="#">illo distinctio nostrum</a></li>
    <li><a href="#">facere nobis blanditiis</a></li>
    <li><a href="#">voluptatem laboriosam</a></li>
  </ul> 
  <button id="btn-close" data-close="offscreen-content" class="btn-close--offscreen">
    <span>X</span>
    <span class="screen-readers-only">Close Modal</span>
  </button>
</div>




<div class="wrapper">

  <h1>Keyboard traps - Example of a modal</h1>
  <div style="padding-top: 10px; overflow: auto;">
    <button class="modal-toggle" style="float:left;"  data-open="modal">Open Modal</button>
    <button class="drawer-panel-toggle" data-open="drawer-panel" style="float:right;">Open Drawer Panel</button>
  </div>

<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quo deleniti aspernatur, illum rerum debitis molestiae a at corrupti. <a href="http://weblayouts.ca">Ratione ab modi qui</a>. Dolorem sit ipsum eos consequuntur nobis est laboriosam.</p>
<p><button>click here!</button></p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. <a href="#">Numquam, incidunt aspernatur</a> similique quo, esse quibusdam quam libero ullam iusto voluptatum eaque odio facere nobis blanditiis, ab expedita illo distinctio nostrum.</p>
<p><button>click also here!</button></p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aspernatur deleniti inventore est, iure maiores cumque vero voluptatum ratione quasi impedit reiciendis voluptatem laboriosam unde, accusamus mollitia minus, blanditiis facilis sed.</p>
</div>

    <div id="modal" class="offscreen-content modal">
      <h1>Hello! Join our newsletter!</h1>
      <p>
        Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
      </p>
      <div class="field">
        <label for="fullname">Full Name</label>
        <input id="fullname" type="text">
      </div>
      <div class="field">
        <label for="email">E-Mail Address</label>
        <input id="email" type="text">
      </div>
      <button id="signup" data-close="offscreen-content">Sign me up already!</button>
      <button id="btn-close" class="btn-close--offscreen"  data-close="offscreen-content">
        <span>X</span>
        <span class="screen-readers-only">Close Modal</span>
      </button>
    </div>



    <div class="offscreen-content-overlay" data-close="offscreen-content"></div>
            
          
!
            
              body {
  font-family: 'Georgia', sans-serif;
}

h1{
  text-align: center;
}
.wrapper{
  margin-left: auto;
  margin-right: auto;
  max-width: 600px;
  button, a{
    &:focus{
      background: blue;
      color: #fff;
    }
  }
}


.offscreen-content{
  display: none;
  position: fixed;
  background-color: #fff;
  z-index: 3; /* places the modal on top of everything */
}

.offscreen-content-overlay {
  position: fixed;
  width: 100%;
  height: 100%;
  z-index: 2; /* places the modalOverlay between the main page and the modal dialog */
  background-color: #000;
  opacity: 0.5;
  top: 0;
  left: 0;
  display: none;
  margin: 0;
  padding: 0;
}

.modal {
  width: 50%;
  border-radius: 4px;
  margin-left: auto;
  margin-right: auto;
  padding: 24px;
  top: 25%;
  left: 25%;
}


.drawer-panel{ 
  top:0;
  left:0;
  max-width: 300px; 
  height: 100%;
  padding: 20px; 
  ul{
    list-style: decimal;
    margin: 0;
    padding: 0 0 0 15px;
    line-height: 2em;
  }
  a{
    color: #999;
    text-decoration: none;
    &:hover, &:focus{
      color: blue;
      text-decoration: underline;
    }
  }
}

h1 {
  margin-top: 0;
}

.field {
  display: inline-block;
  vertical-align: bottom;
}

label {
  display: block;
  margin-bottom: 4px;
}


.btn-close--offscreen{
  position: absolute;
  top: 0;
  right: 0;
  padding: 10px;
  border: 0;
  background: #999;
  color: #fff;
  // border-top-right-radius: 3px;
  font-size: 1.3em;
  &:focus, &:hover{
    background: blue;
  }
}
.screen-readers-only{
  position: absolute !important;
  clip: rect(1px 1px 1px 1px); /* IE6, IE7 */
  clip: rect(1px, 1px, 1px, 1px);
}


            
          
!
            
              /**
 * Trapping the "keyboard focus" inside two off-screen contents (modal and drawer panel)
 * - Keyboard users won't be able to focus outside an opened 'modal' or 'drawer panel'
 * unless they first close it (excellent user experience)
 * Creator: Eric Njanga - December 2016
 * Improved version of : https://github.com/udacity/ud891/tree/gh-pages/lesson2-focus/07-modals-and-keyboard-traps
*/
var system = (function(){ 
  var _public = {}; 
  // Will hold previously focused element
  var focusedElementBeforeModal; 
  var offscreenContents = document.querySelectorAll('.offscreen-content'); 
  var overlay           = document.querySelector('.offscreen-content-overlay');
  var btnCloseOffscreen = document.querySelectorAll('[data-close]'); 
  var btnOpenOffscreen  = document.querySelectorAll('[data-open]'); 
  var signUpBtn         = modal.querySelector('#signup');// Sign-Up button
  // List of elements that can have the focus ...
  var focusableElementsString = 'a[href], area[href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), button:not([disabled]), iframe, object, embed, [tabindex="0"], [contenteditable]';
  // Will hold 'first' and 'last' focusable of the current active 'off-screen' content
  var firstTabStop  = '';
  var lastTabStop   = '';
  
  
  
  /**
   * Initializing the app, add event listeners ...
  */
  _public.initialize = function(){    
    // Listen to when any "off-screen" trigger buttons is clicked
    // and open the appropriate content in response ...
    for(var i=0, l=btnOpenOffscreen.length; i<l ; i++){
      (function(i) {
        this.addEventListener('click', function(e){ 
          var offscreenTarget = document.querySelector('#'+this.dataset.open );  
          openOffScreenContent(offscreenTarget);  
        }); 
      }).call(btnOpenOffscreen[i], i);
    }
    
    // Listen to when any "off-screen" close buttons is clicked
    // and close the appropriate content in response ...
    for(var i=0, l=btnCloseOffscreen.length; i<l ; i++){
      (function(i) {
        this.addEventListener('click', function(e){ 
          var offscreenTargets = document.querySelectorAll('.'+this.dataset.close );  
          for(var i=0, l=offscreenTargets.length; i<l ; i++){
            closeOffScreenContent(offscreenTargets[i]);   
          }
        }); 
      }).call(btnCloseOffscreen[i], i);
    }
    
    // Listen to when any key is pressed and "make sure"
    // the focus stays trapped inside the currently active
    // off-screen content
    for(var i=0, l=offscreenContents.length; i<l ; i++){
      (function(i) {
        this.addEventListener('keydown', trapTabKey);
      }).call(offscreenContents[i], i);
    }
  };


  
  /**
   * Analysing the keys pressed and:
   * - [TAB key] : Forcing the "focus" to remain within a select group of active elements
   * regardless of the tabbing direction (forward or backwards).
   * - [ESC key] : Closing the active off-screen content
  */
  function trapTabKey(e) {
    // TAB key had been pressed ...
    if (e.keyCode === 9) { //(tabbing backwards)
      // SHIFT key has also been pressed (so TAB + SHIFT) ... 
      // and the current active element is the first interractive 
      // element of the list. 
      // > We move the focus to the last interractive element of the list 
      // (focus is moving backwards and goes from the first to the last,
      // element of the off-screen content)
      if (e.shiftKey) { 
        if (document.activeElement === firstTabStop) {
          e.preventDefault();
          lastTabStop.focus();
        }
      } 
    // Otherwise ...
    // User has just tabbed forward (TAB)
    // and the current active element is last interractive 
    // element of the modal.
    // > We move the focus to the first interractive element of the list
    // (focus is moving forward and goes from the last to the first,
    // element of the off-screen content)
    else {
        if (document.activeElement === lastTabStop) {
          e.preventDefault();
          firstTabStop.focus();
        }
      }
    }//[end] TAB key had been pressed 

    // if "ESCAPE" has been pressed, so close the off-screen content
    // that is currently active
    if (e.keyCode === 27) {
      var offscreentarget = document.querySelector('#'+document.body.dataset.activeoffscreen);
      closeOffScreenContent(offscreentarget);
    }
  }
  
 
  
  /**
   * Hide off-screen content (and the overlay as well) from the DOM.
   * Return the focus to the element that had the focus before the modal was open.
  */
  function closeOffScreenContent(content){
    if(content!==undefined && content!==null){
      // Hide the modal and overlay
      content.style.display = 'none';
      overlay.style.display = 'none';
      // Set focus back to element that had it before the modal was opened
      focusedElementBeforeModal.focus();
    }
  }
  
   
  
  /**
   * Display off-screen content (and the overlay as well).
   * Calculate its focus boundaries.
  */
  function openOffScreenContent(content){
    if(content!==undefined && content!==null){
      document.body.dataset.activeoffscreen = content.id; 
      // 1) Deal with focusable elements ... 
      // Save current focus ...
      focusedElementBeforeModal = document.activeElement; 
      // Find all focusable children of this content and locate the 'first' and 'last' of them
      var focusableElements = content.querySelectorAll(focusableElementsString);
      // Convert NodeList to Array...
      focusableElements = Array.prototype.slice.call(focusableElements); 
      firstTabStop  = focusableElements[0]; //first
      lastTabStop   = focusableElements[focusableElements.length - 1]; //last

      // 2) Display offscreen element and overlay, then add focus on its first element ...
      // Hide the modal and overlay
      content.style.display = 'block';
      overlay.style.display = 'block';
      // Focus first child
      firstTabStop.focus();
    }
  }
  
  return _public;
})();


//Launch the app
system.initialize();
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console