question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support custom element with nested components without errors

See original GitHub issue

I have only 1 customElement and inside it there are several internal components of the Svelte. These internal/nested components cannot be 1 web component.

By setting the customElement option in the rollup, he forced me to put <svelte:options tag={null} /> on all internal components, but that seems to be wrong, as they should not be 1 customElement.

After setting up, I’m getting the following errors for internal components marked as tag={null}:

image

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:30
  • Comments:18 (1 by maintainers)

github_iconTop GitHub Comments

32reactions
pbastowskicommented, Oct 27, 2019

Same problem here. It seems that tag={null} does not work as documented in Svelte components imported into a parent component that has a tag="parent-component".

The below does not work

ParentComponent.svelte

<svelte:options tag="parent-component" />
<h1>Parent Component</h1>
<ImportedComponent />

ImportedComponent.svelte

<svelte:options tag={null} />
<h1>Imported Component</h1>
<slot />

However, if I give the imported component a tag name tag="imported-component" and then use the new custom element inside the parent component <imported-component></imported-component> then everything works.

This works, but it’s not what I want

ParentComponent.svelte

<svelte:options tag="parent-component" />
<h1>Parent</h1>
<imported-component></imported-component>

ImportedComponent.svelte

<svelte:options tag="imported-component" />
<h1>Important Component</h1>
<slot />

But, I don’t want to generate all my components as custom elements. I just want the top level element to be generated as a custom element and the rest of my Svelte components to be used within the app as normal Svelte components (not custom elements).

25reactions
jawishcommented, May 10, 2020

I solved this by using a combination of Rollup and file extensions.

Custom Element components get the extension .wc.svelte. Rollup then sets the customElement config depending on the file extension.

svelte({ customElement: true, include: /\.wc\.svelte$/ }),
svelte({ customElement: false, exclude: /\.wc\.svelte$/ }),

This works smoothly with local and NPM imported components.

Demo: https://jawish.github.io/svelte-customelement-rollup/

Read more comments on GitHub >

github_iconTop Results From Across the Web

Custom Element Best Practices - web.dev
Custom elements let you construct your own HTML tags. This checklist covers best practices to help you build high quality elements.
Read more >
How to create and style svelte 3 custom elements with nested ...
My goal is to create web component with svelte and then use it outside of svelte as native web-component. Here is REPL. Custom...
Read more >
Custom elements - The Modern JavaScript Tutorial
We can create custom HTML elements, described by our class, with its own methods and properties, events and so on. Once a custom...
Read more >
Using custom elements - Web Components | MDN
The controller of custom elements on a web document is the CustomElementRegistry object — this object allows you to register a custom element...
Read more >
Development: Scoped Elements - Open Web Components
Version 2 of Scoped Elements only supports lit with lit-element 3.x . ... Scoped Custom Element Registries feature and, by the moment, there...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found