Bug 260480 - Custom element with delegatesFocus does not focus if inner element is not first child
Summary: Custom element with delegatesFocus does not focus if inner element is not fir...
Status: RESOLVED INVALID
Alias: None
Product: WebKit
Classification: Unclassified
Component: DOM (show other bugs)
Version: Safari 16
Hardware: Mac (Apple Silicon) macOS 13
: P2 Normal
Assignee: Nobody
URL: https://jsbin.com/ranuvuhiri/edit?htm...
Keywords: InRadar
Depends on:
Blocks: 148695
  Show dependency treegraph
 
Reported: 2023-08-21 12:46 PDT by Elizabeth Mitchell
Modified: 2024-03-28 15:28 PDT (History)
3 users (show)

See Also:


Attachments
Reduction (667 bytes, text/html)
2024-03-28 15:15 PDT, Ryosuke Niwa
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Elizabeth Mitchell 2023-08-21 12:46:24 PDT
A custom element with `delegatesFocus: true` does not correctly delegate focus if the first child of the custom element's shadow DOM is not focusable.

This is common with wrapper divs and styling.

```
<!-- Does not delegate focus correctly -->
<x-button>
  #shadow-root
    <div class="style">
      <button><slot></slot></button>
    </div>
</x-button>

<!-- Delegates focus correctly -->
<x-button>
  #shadow-root
    <button><slot></slot></button>
</x-button>
```

See https://jsbin.com/ranuvuhiri/edit?html,output and click on "Focus button"

Expected: custom element button is focused
Actual: button is not focused

Tested in Safari 16.6 on MacOS 13.5
Comment 1 Radar WebKit Bug Importer 2023-08-28 12:47:14 PDT
<rdar://problem/114580905>
Comment 2 Ryosuke Niwa 2024-03-28 15:10:54 PDT
Hm... the focus is set. We're somehow not applying :focus rule, however.
Comment 3 Ryosuke Niwa 2024-03-28 15:15:25 PDT
Created attachment 470649 [details]
Reduction
Comment 4 Ryosuke Niwa 2024-03-28 15:27:55 PDT
Oh, the issue is simply that x-button becomes width 0px because it's a display: inline. If you set display: block on x-button or replace div with span, the dotted red line renders correctly.

Note that buttons are not focusable by mouse clicks on Apple platforms by default. If you want to make a button focusable, set tabindex=0 on button elements.

As such, this is behaving correctly.