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">
</div>
              
            
!

CSS

              
                * {
  margin: 0;
  padding: 0;
}

body {
  font-family: system-ui, sans-serif;
  font-size: 16px;
  background: #e7eef9;
}

code {
  font-size: 0.9em;
  font-family: ui-monospace, "Cascadia Code", "Source Code Pro", Menlo, Consolas,
    "DejaVu Sans Mono", monospace;
  font-weight: normal;
  color: rgb(50, 100, 255);
}

h1 {
  font-size: 24px;
  margin-bottom: 40px;
}

a,
a:hover,
a:focus, 
a:active {
  color: rgb(50, 100, 255);
}

button {
  font-family: inherit;
  border-radius: 100px;
  border: 1px solid rgba(0, 0, 0, 0.1);
  outline: none;
  padding: 10px 20px;
  background: white;
  transition: transform 200ms, background-color 200ms;
  will-change: transform, background-color;
  font-size: 16px;
  color: rgba(0, 0, 0, 0.75);
  margin-right: 10px;

  &:hover {
    cursor: pointer;
  }

  &:active {
    background-color: rgba(255, 255, 255, 0.9);
    transform: translateY(1px);
  }

  &:focus-visible {
    outline-offset: 3px;
    outline: 3px solid rgba(50, 100, 255, 0.3);
  }
}

input {
  accent-color: rgb(50, 100, 255);
  outline: none;
}

label {
  border-radius: 100px;
  margin-inline: -5px;
  padding-inline: 5px;
  display: inline-flex;
  align-items: center;
  gap: 5px;
}

label:focus-within {
  outline-offset: 3px;
  outline: 3px solid rgba(50, 100, 255, 0.3);
}

p {
  margin-bottom: 15px;
  opacity: 0.7;
  font-size: 16px;
}

main {
  padding: 20px;
  background: rgba(255, 255, 255, 0.3);
  border: 1px solid rgba(0, 0, 0, 0.1);
  border-radius: 20px;
  margin-block: 10px 40px;
}

main.disabled {
  opacity: 0.6;
}

.app {
  max-width: 500px;
  padding: 40px 20px;
  margin-inline: auto;
}


              
            
!

JS

              
                import React, {
  useRef,
  useEffect,
  useState
} from "https://esm.sh/react@18.2.0";
import { createRoot } from "https://esm.sh/react-dom@18.2.0/client";

// ----- Inert component ----- //

const focusableElementsSelector = [
  "a[href]",
  "input",
  "select",
  "textarea",
  "button",
  "audio[controls]",
  "video[controls]",
  "details > summary:first-of-type",
  "details",
  '[contenteditable]:not([contenteditable="false"])',
  '[tabindex]:not([tabindex="-1"])'
].join(", ");

const addTabIndex = ($wrapper) => {
  $wrapper.querySelectorAll(focusableElementsSelector).forEach(($element) => {
    $element.setAttribute("tabindex", -1);
  });
};
const removeTabIndex = ($wrapper) => {
  $wrapper.querySelectorAll(focusableElementsSelector).forEach(($element) => {
    $element.removeAttribute("tabindex");
  });
};

const Inert = ({ enabled, children, ...props }) => {
  const wrapperRef = useRef(null);

  const observerRef = useRef(
    new MutationObserver(() => addTabIndex(wrapper.current))
  );

  useEffect(() => {
    if (enabled) {
      if (wrapperRef.current) {
        // Add tabindex
        addTabIndex(wrapperRef.current);
        // Start observing
        observerRef.current.observe(wrapperRef.current, {
          childList: true,
          subtree: true
        });
      }
    } else {
      // Disconnect the mutation observer
      observerRef.current.disconnect();
      // And remove the attributes which we added
      removeTabIndex(wrapperRef.current);
    }
  }, [enabled, wrapperRef.current]);

  return (
    <div
      {...props}
      ref={wrapperRef}
      inert={enabled ? '' : null}
      tabindex={enabled ? -1 : null}
      aria-hidden={enabled}
      style={{
        pointerEvents: enabled ? "none" : null
      }}
    >
      {children}
    </div>
  );
};

// ----- Demo ----- //
const App = () => {
  const [enabled, setEnabled] = useState(true);

  return (
    <>
      <h1>
        React <code>Inert</code> component
      </h1>
      <label>
        <input
          type="checkbox"
          checked={enabled}
          onChange={() => setEnabled(!enabled)}
        />{" "}
        Enable inert
      </label>

      <main className={enabled ? "disabled" : ""}>
        <Inert enabled={enabled}>
          <p>
            When the checkbox above is checked, this content will be skipped by
            screen readers and you won't be able to click or tab into it.
          </p>
          <p>
            Native solution is to use{" "}
            <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/inert">
              inert
            </a>{" "}
            if your browser matrix allows it.
          </p>

          <div>
            <button>1</button>
            <button>2</button>
            <button>3</button>
          </div>
        </Inert>
      </main>

      <footer>
        <p>Some more content and a button to let you tab into it.</p>
        <button>Hello World!</button>
      </footer>
    </>
  );
};

// Render
const root = createRoot(document.querySelector(".app"));

root.render(<App />);

              
            
!
999px

Console