Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width,initial-scale=1">
  <title>Can a click on a light DOM node focus on a shadow element?</title>
  <script>
    class TestElement extends HTMLElement {
      constructor() {
        super();
        const root = this.attachShadow({ mode: 'open' });
        root.innerHTML = `
          <style>
            div {
              border: 1px solid gray;
            }
            div:focus {
              outline: 2px solid red;
            }
          </style>
          <div tabindex="0">
            <slot></slot>
          </div>
        `;
      }
    }
    customElements.define('test-element', TestElement);
  </script>
  <style>
    div {
      border: 1px solid gray;
    }
    div:focus {
      outline: 2px solid red;
    }
    span {
      border: 2px dotted gray;
      font-size: 36px;
    }
  </style>
</head>
<body>
  <p>
    Clicking anywhere in this focusable div gives it focus:
  </p>
  <div tabindex="0"><span>Click me</span></div>
  <p>
    Clicking inside this custom element has inconsistent behavior. Firefox gives
    the custom element focus as expected, but in Chrome/Safari, clicks inside
    the dotted lines do <em>not</em> give the custom element focus.
  </p>
  <test-element><span>Click me</span></test-element>
</body>
</html>
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
JanMiksovskypro
0viewers