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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

              
                <main>
<h1>Set focus context for accessibility</h1>

<p>When opening a dialog or off-canvas menu - the rest of the page should be <i>disabled</i> in behaviour to match it's visual appearance.</p>

<p>There could be lots of <a href="#">Focusable elements</a>, some event due to <a href="#">tabindex</a> within a page which should be ignored in the dom and should not be focusable when dialog or fullscreen menu is active. </a></p>

<div class="launch-modal-wrapper">
  <button data-launch-modal>Launch a modal</button>
  </main>
</div>

<dialog>
  <div class="dialog__inner">
    <p>When active this dialog should be our focus content - focusable elements outside of this should be <a href="#">inert</a>.</p>
  <p>Tabbing items within should cycle through these and exit to the browser chrome after the last item.</p>
    <p>If a browser had full support for the <code>inert</code> attribute - we could just apply this to the parent element and the parent and it's contents would be considered inert without the need to remove <a href="#">href's</a> etc as we need to now.</p>
    <p><button data-exit-dialog>Close dialog</button></p>
  </div>
</dialog>
<div class="overlay"></div>


<form>
<label>
  Email address
  <input type="email" /></label>
  <button type="submit">Sign up to mailing list</button>
</form>

<div class="instructions">
  <p>Use your keyboard to tab through elements on the page - select the launch a modal button, observe that focus context is set. Focusable elements outside of the dialog context are disabled and the outermost elements of the dom are marked inert. Tabbing past the last element in the dialog will exit from the page.</p>
  
<p>This pen was created as an example for the article <a href="https://kahoot.com/tech-blog/focus-on-accessi…menus-and-modals">Focus on accessibility – accessible menus and modals on kahoot.com</a></p>
</div>
              
            
!

CSS

              
                body {
  font-family: sans-serif;
  color: #333;
  line-height: 1.3;
}
.overlay {
  position: absolute;
  top: 0; bottom: 0; left: 0; width:100%;
  z-index: 1;
  background-color: rgba(0,0,0,0.5);
  display: none;
}
dialog[open] + .overlay {
  display: block;
}
dialog {
  z-index:2;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translateX(-50%) translateY(-50%);
  max-width: calc(100% - 40px);
}
.launch-modal-wrapper {
  border: 2px dashed #ccc;
  padding: 20px;
  margin-bottom:20px;
  border-radius: 8px;
  display: flex;
  justify-content: center;
}

.instructions {
  font-size: 80%;
  padding: 10px;
  background-color: #eee;
  margin-top: 20px;
}
              
            
!

JS

              
                const launchDialog = document.querySelector('[data-launch-modal]'),
      dialog = document.querySelector('dialog'),
      exitDialog = document.querySelector('[data-exit-dialog]');


launchDialog.addEventListener('click', (e)=>{
  dialog.setAttribute('open', ' ');
  setFocusContext(dialog); // set focus context to element                              
  e.preventDefault();
});

exitDialog.addEventListener('click', (e)=>{
  dialog.removeAttribute('open');
  setFocusContext(false); // pass empty or false to reset
  e.preventDefault();
});

const setFocusContext = (focusContext) => {
	
    // focusable elements within document
		var focusableElements = document.querySelectorAll(`
			a, 
			input,
			button, 
			select, 
			textarea,
			[tabindex]
		`);
    
    var parentElements = document.querySelectorAll(`
      body > div, 
			body > header,
			body > aside, 
			body > section, 
			body > main,
			body > footer,
      body > form,
      body > article
		`);
    
    const formElements = ['BUTTON','INPUT','SELECT','TEXTAREA'];
  
  
    if ( focusContext ) {

      focusableElements.forEach(function(el){
        if ( !focusContext.contains(el) ) {  // disable focusable elements outside our context
          el.setAttribute('data-focus-context', false);
          el.setAttribute('aria-hidden', true);
          if ( el.hasAttribute('tabindex') ) {
            el.setAttribute('data-context-inert-tabindex', el.getAttribute('tabindex'));
            el.removeAttribute('tabindex');
          }
          if ( el.hasAttribute('href') ) {
            el.setAttribute('data-context-inert-href', el.getAttribute('href'));
            el.removeAttribute('href');
          }
          if ( formElements.indexOf(el.tagName) != -1 ) {
            if ( el.hasAttribute('disabled') ) {
              el.setAttribute('data-context-inert-disabled', el.getAttribute('disabled') );
            }
            el.setAttribute('disabled', true);
          }
        }
      });

      focusContext.focus();

      // set parents in dom inert
      parentElements.forEach( (parentEl)=> {
        if ( !parentEl.contains(focusContext) ) {
          parentEl.setAttribute('inert', ' ');
          parentEl.setAttribute('aria-hidden', true);
        } 
      });

    } else {
      var inertElements = document.querySelectorAll('[data-focus-context=false]');
      // restore 
      inertElements.forEach( (el) => {
        if ( el.hasAttribute('data-context-inert-tabindex') ) {
          el.setAttribute('tabindex', el.getAttribute('data-context-inert-tabindex'));
          el.removeAttribute('data-context-inert-tabindex');
        }
        if ( el.hasAttribute('data-context-inert-href') ) {
          el.setAttribute('href', el.getAttribute('data-context-inert-href'));
          el.removeAttribute('data-context-inert-href');
        }
        if ( formElements.indexOf(el.tagName) != -1 ) {
          el.removeAttribute('disabled', true);
          if ( el.hasAttribute('data-context-inert-disabled') ) {
            el.setAttribute('disabled', el.getAttribute('data-context-inert-disabled') );
            el.removeAttribute('data-context-inert-disabled')
          }
        }
        el.removeAttribute('data-context');
      });
      // restore parents in dom 
      parentElements.forEach( (parentEl)=>{
        parentEl.removeAttribute('inert');
        parentEl.removeAttribute('aria-hidden');
      });
  }
}
              
            
!
999px

Console