Bug 260480

Summary: Custom element with delegatesFocus does not focus if inner element is not first child
Product: WebKit Reporter: Elizabeth Mitchell <lizmitchell>
Component: DOMAssignee: Nobody <webkit-unassigned>
Status: RESOLVED INVALID    
Severity: Normal CC: mike, rniwa, webkit-bug-importer
Priority: P2 Keywords: InRadar
Version: Safari 16   
Hardware: Mac (Apple Silicon)   
OS: macOS 13   
URL: https://jsbin.com/ranuvuhiri/edit?html,output
Bug Depends on:    
Bug Blocks: 148695    
Attachments:
Description Flags
Reduction none

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.