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

Ability to optionally define a custom element #275

Open
Hyzual opened this issue Oct 10, 2024 · 2 comments
Open

Ability to optionally define a custom element #275

Hyzual opened this issue Oct 10, 2024 · 2 comments
Labels

Comments

@Hyzual
Copy link

Hyzual commented Oct 10, 2024

Hi, again thank you for sharing with us and maintaining hybrids, it's been a great help for us!

We are in a situation where we can sometimes load the code to define the same custom element (for example: tuleap-lazybox-dropdown) twice on the same page. Since the element is defined with the define() function from hybrids, when this happens, it raises the following error:

Uncaught TypeError: Custom element with 'tuleap-lazybox-dropdown' tag name already defined outside of the hybrids context

The error is definitely useful, but in our case the element is really the same one, so it is okay to just "not re-define it". We can use the following code to achieve that:

const TAG = "tuleap-lazybox-dropdown";
const MyElement = define.compile({ <element definition object...> });

if (!window.customElements.get(TAG)) {
    window.customElements.define(TAG, MyElement);
}

Do you think, as a feature request, we could maybe use another function or option from hybrids to have this behavior, to define an element unless its tag name is already in the customElements registry ? Maybe something like define.optional({ <element definition object...> }). What do you think about it ?

@smalluban
Copy link
Contributor

Is it the use case where you import files that define elements more than once on the same page?

@Hyzual
Copy link
Author

Hyzual commented Oct 10, 2024

Yes, we have "internal libraries" that define elements, and they can end up being used in several bundled JS files that are loaded on the same page.

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

No branches or pull requests

2 participants