Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhancement(custom element): make tag optional in customElement component options #12751

Closed
Theo-Steiner opened this issue Aug 6, 2024 · 1 comment

Comments

@Theo-Steiner
Copy link
Contributor

Describe the problem

there are currently three possible options to set the tag name of a custom element in svelte

  1. As a string value set to customElement: <svelte:options customElement="my-element" />
  2. As a string value set to customElement.tag: <svelte:options customElement="{tag: 'my-element'}" />
  3. Not setting it at all using <svelte:options />, but instead using customElements.define('my-element', MyElement.element);

option 1 is for the most simple use cases, where you want svelte to handle everything for you, so that you just have to import the module and as a side effect your component is registered as a custom element with the tag of "my-element".


option 2 was introduced with svelte 4's custom elements rework (#8457) and is for more advanced use cases, where you perhaps might want to pass on shadow root creation or add type converters for props set via attributes

<svelte:options
	customElement={{
		tag: 'custom-element',
		shadow: 'none',
		props: {
			name: { reflect: true, type: 'Number', attribute: 'my-index' }
		},
	}}
/>

option 3 is used when you need more control over how your element is registered to the customElements registry. A common case for this is a scenario in which a website imports the svelte-built custom element from two sources. Since trying to register a custom element with a tag name that is already in the customElements registry fails (see: #7595), users have to check before defining the tag with the customElements registry.

if (!customElements.get(tag_name)) {
  customElements.define('my-element', MyElement.element);
}

given that some use cases require both advanced settings (as provided by option 2) and control over how customElements are registered (as provided option 3), it would be really nice if there were a way to combine the two options - allowing both fine grained control over the element that is being created and the way it is registered.

Describe the proposed solution

if tag were optional in the customElement config passed to svelte:options, the compiler could decide based on the existence of a tag name whether to add a call for registering the custom element.

<svelte:options
	customElement={{
		shadow: 'none',
		props: {
			name: { reflect: true, type: 'Number', attribute: 'my-index' }
		},
	}}
/>

↑ this svelte:option would create the custom element's class, but not call customElements.define

Importance

nice to have

Rich-Harris added a commit that referenced this issue Aug 11, 2024
… optional (#12751) (#12754)

* feat: make svelte:option customElement tag property optional (#12751)

* tweak comment

* tweak docs

* tweak some more wording

* Update .changeset/four-kids-flow.md

---------

Co-authored-by: Rich Harris <[email protected]>
Co-authored-by: Rich Harris <[email protected]>
@Theo-Steiner
Copy link
Contributor Author

Closed via #12754

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant