Re: [WICG/webcomponents] Reference Target: what type should the `referenceTarget` attribute be? (Issue #1093)

> It's tempting to treat the `referenceTarget` property as kind of equivalent to setting an ID elsewhere

I think of `referenceTarget` less like an ID, and more like an ID reference - it's not identifying the shadow root, but referring to another element's ID.

Most attributes on Elements which use ID references (and don't use Element reflection) are non-nullable `DOMString`s, I think for the reason that nullable `DOMString`s are supposed to be limited to enumerated values (which we [messed up with `ARIAMixin`](https://github.com/whatwg/html/issues/10037), unfortunately). So, I think you could still make a similar analogy.

However, I think it depends on how we want to think about un-setting a `referenceTarget` property, which is what the original comment was in reference to. I was surprised by this example code:

```js
document.querySelector("#fancy-listbox").shadowRoot.referenceTarget = "";
console.log(input.ariaControlsElements);  // logs `listbox` since there is no reference target; aria-controls now applies to the shadow host
```

The original issue was concerning invalid values for `referenceTarget`; we determined that an invalid value should work as though any reference to the host was an invalid ID reference. Meanwhile, a _lack of_ a `referenceTarget` property means that any reference to the host is a valid reference to the host. My surprise resulted from `referenceTarget` being set to an empty string being considered synonymous with the `referenceTarget` property not being present at all; I had expected that it would be similar to setting any ID reference property to an empty string (i.e., straightforwardly an invalid reference, since no element can have an empty string as an ID).

I can see three options here:

1. `referenceTarget` is a nullable `DOMString`, and setting it to `null` is equivalent to not having a `referenceTarget` property
2. `referenceTarget` is a non-nullable `DOMString`, and setting it to empty string is equivalent to not having a `referenceTarget` property
3. `referenceTarget` is a non-nullable `DOMString`, setting it to empty string is equivalent to setting it to any other invalid ID value, and it can be un-set using `delete`.

My preference would be (1), but I'd honestly be ok with any of these.

-- 
Reply to this email directly or view it on GitHub:
https://github.com/WICG/webcomponents/issues/1093#issuecomment-2623224590
You are receiving this because you are subscribed to this thread.

Message ID: <WICG/webcomponents/issues/1093/2623224590@github.com>

Received on Thursday, 30 January 2025 00:17:03 UTC