Reusing a component in lit-element

833 Views Asked by At

Anyone that can point to any documentation on howto reuse code in lit-element.

The problem now is that if I declare an element, in my case a close-button and I want to reuse it by importing it into 2 or more lit-elements, there will be an error in the browser about the close-button being declared more than once.

Understandable enough, but how do I reuse a component, I could of course move the button to a separate file and add it to the document, but then there would be dependencies on that for other components to work.

Any suggestions

2

There are 2 best solutions below

1
On

After investigating a bit more, I concluded that sharing HTML templates might be the way to do it.

0
On

If close-button self-registers itself, with a call to customElements.define('close-button', ...), then you should be able to import its defining module and not have any errors due to the module caching behavior of JS.

You must have multiple customElements.define('close-button', ...) calls, so I'd make sure that 1) it's self-registering and you're not registering it again in each component that uses it, and 2) you're using standard JS modules.