Skip to content

Commit

Permalink
Update Reference Target Explainer to add support for nesting inside l…
Browse files Browse the repository at this point in the history
…abels (#210)
  • Loading branch information
behowell authored Jun 14, 2024
1 parent 751bd5e commit 83e5908
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions reference-target-explainer.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,9 @@ A [form-associated custom element](https://html.spec.whatwg.org/dev/custom-eleme

#### Nesting inside `<label>`

There is no special support for an element nested inside a label. The label must use the `for` attribute to work with `referenceTarget`. It is still ok to nest the custom element inside the label, but it must also use `for`.
Reference Target allows for labels to be implicitly associated with the target element when the host is nested inside a `<label>` element. The shadow tree's reference target will be associated with the label that contains the element. If the shadow tree is using `referenceTargetMap`, this uses the `for` attribute from the map.
In the following example, the label of the `<input id="real-input">` is "Fancy input".
```html
<script>
Expand All @@ -262,24 +264,17 @@ There is no special support for an element nested inside a label. The label must
this.shadowRoot_ = this.attachShadow({ mode: "closed" });
this.shadowRoot_.innerHTML = `<input id="real-input" />`;
this.shadowRoot_.referenceTarget = "real-input";
// Alternatively, set the referenceTargetMap with the `for` attribute:
// this.shadowRoot_.referenceTargetMap.htmlFor = "real-input";
}
}
);
</script>
<!-- ❌ This label isn't associated with anything because it doesn't use `for`,
and fancy-input is not form-associated. -->
<label>
Fancy Input
Fancy input
<fancy-input></fancy-input>
</label>
<!-- ✅ This label is applied to the inner `<input id="real-input" />`, which is
fancy-input's referenceTarget. -->
<label for="fancy-input">
Fancy Input
<fancy-input id="fancy-input"></fancy-input>
</label>
```
#### Nesting inside `<form>`
Expand Down

0 comments on commit 83e5908

Please sign in to comment.