Re: [WICG/webcomponents] Anonymous Custom Elements (Issue #1074)

> If the tag name doesn't matter, then we should be able to use a generated tag name to fit in with all the current custom element APIs.
> 
> Is there problem with userland generated APIs? Is it still the possibility of name collisions?

I guess that is one approach that still allows for a DOM-only API. Is finding an appropriate tag name non-trivial? In particular if it supposed to be compatible scoped registries too. Maybe a helper on `customElements` that does some time-based randomization?

```js
// my-example.js
customElements.define(customElements.randomName(), MyExampleElement);

export class MyExampleElement extends HTMLElement {
  constructor() {
    super()
    const root = this.attachShadow({ mode: 'open' })
    root.innerHTML = '<span>example</span>'
  }
}

// index.js
import { MyExampleElement } from './my-example.js' 
document.body.appendChild(new MyExampleElement ())
```

It also has the major drawback that we don't get a way to access this element via HTML.  We could do

```js
customElements.define(customElements.randomName(), MyExampleElement);
customElements.define('my-example', class extends MyExampleElement {});
```

But as you note that opens up the door to naming collisions. The idea of creating a dependency to a specific ES module was meant to sidestep any issues with naming. 

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

Message ID: <WICG/webcomponents/issues/1074/2384044112@github.com>

Received on Monday, 30 September 2024 20:01:44 UTC