Skip to content

Allow to use scoped (':scope' and '&') selectors relative to controller's element in outlets selectors#888

Open
christophehenry wants to merge 1 commit into
hotwired:mainfrom
christophehenry:allow-use-scoped-selectors
Open

Allow to use scoped (':scope' and '&') selectors relative to controller's element in outlets selectors#888
christophehenry wants to merge 1 commit into
hotwired:mainfrom
christophehenry:allow-use-scoped-selectors

Conversation

@christophehenry

@christophehenry christophehenry commented Jun 18, 2026

Copy link
Copy Markdown

This allow to use the concept of outlets to express component composition. Take the following:

<CustomSelect id="products" data-controller="custom-select" data-custom-select-custom-item-outlet="d .items">
    <SearchBar role="searchbox"></SearchBar>
    <ol>
       <li>
           <CustomComponent data-controller="custom-item" class="items"></CustomComponent>
           <!-- Some more-->
       </li> 
    </ol>
</CustomSelect>

This allows to reference custom-item outlets only within the scope of custom-select controller's element. Of course you could solve this by setting an id on every custom-select controller's element and referencing it in outlet's selector but this is not always convenient since HTML code can be generated and not easy to customize. Plus, scope selectors (& and :scope) are a things in most browsers now.

What it does is, if element.matches(selector) returns false, the code will additionnaly perform a querySelectorAll(selector) on the controller's element nand tests if the element appears in the results. This is additionnal work but I expect performance impact to be very limited since this additionnal test will only be performed if the first match fails, I expect querySelectorAll to be highly optimized on modern browsers and I expect the controller's element to have a limited nuber of nodes mode of the time. The performance penalty could, however, be mitigated by pushing whatwg/dom#1081 into the standards.

@christophehenry christophehenry marked this pull request as ready for review June 18, 2026 11:10
@christophehenry christophehenry force-pushed the allow-use-scoped-selectors branch from 11f2382 to 4c9d41b Compare June 18, 2026 12:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant