Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

Auto Save

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 class="App">
      <h1>Trap Focus Function Example</h1>
      <button onclick="toggleModal()">Open modal</button>
      <div tabIndex="0"></div>
      <div id="modalContainer" style="display:none;">
      <div class="overlay"></div>
        <div
           id="modal"
           role="dialog"
           aria-labelledby="modalTitle"
           aria-modal="true"
           class="modal"
           >
          <h2 id="modalTitle">Free Gift Form</h2>
          <form>
            <div class="inputItem">
              <label for="name" class="label">
                Name
              </label>
              <input
                id="name"
                type="text"
                placeholder="Name"
                class="input"
               />
            </div>
            <div class="input-item">
              <label htmlFor="surname" class="label">
                Surname
              </label>
              <input
                 id="surname"
                 type="text"
                 placeholder="Surname"
                 class="input"
               />
            </div>
          </form>
          <button onClick="toggleModal()">Close Modal</button>
        </div>
       </div>
      <div tabIndex="0"></div>
    </div>
              
            
!

CSS

              
                .overlay {
  position: fixed;
  background: grey;
  opacity: 0.7;
  left: 0;
  right: 0;
  bottom: 0;
  top: 0;
}

form {
  margin-bottom: 2rem;
}

.modal {
  position: absolute;
  z-index: 2;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  height: 80vh;
  width: 50vw;
  background-color: #fff;
  border-radius: 4px;
  padding: 1em;
  overflow:auto;
}

.inputItem {
  margin-bottom: 16px;
}

.label,
.input {
  display: flex;
  width: 100%;
}

.label {
  margin-bottom: 4px;
}

.App {
  text-align: center;
}

              
            
!

JS

              
                const trapFocus = (element, prevFocusableElement = document.activeElement) => {
    const focusableEls = Array.from(
      element.querySelectorAll(
        'a[href]:not([disabled]), button:not([disabled]), textarea:not([disabled]), input[type="text"]:not([disabled]), input[type="radio"]:not([disabled]), input[type="checkbox"]:not([disabled]), select:not([disabled])'
      )
    );
    const firstFocusableEl = focusableEls[0];
    const lastFocusableEl = focusableEls[focusableEls.length - 1];
    let currentFocus = null;

    firstFocusableEl.focus();
    currentFocus = firstFocusableEl;

    const handleFocus = e => {
      e.preventDefault();
      // if the focused element "lives" in your modal container then just focus it
      if (focusableEls.includes(e.target)) {
        currentFocus = e.target;
      } else {
        // you're out of the container
        // if previously the focused element was the first element then focus the last 
        // element - means you were using the shift key
        if (currentFocus === firstFocusableEl) {
          lastFocusableEl.focus();
        } else {
          // you previously were focused on the last element so just focus the first one
          firstFocusableEl.focus();
        }
        // update the current focus var
        currentFocus = document.activeElement;
      }
    };

    document.addEventListener("focus", handleFocus, true);

    return {
      onClose: () => {
        document.removeEventListener("focus", handleFocus, true);
        prevFocusableElement.focus();
      }
    };
  };

const toggleModal = ((e) => {
  const modal = document.getElementById("modalContainer");
  if (modal.style.display === "none") {
    modal.style.display = "block";
    trapped = trapFocus(modal);
  } else {
    modal.style.display = "none";
    trapped.onClose();
  } 
})
              
            
!
999px

Console