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

Check registered customElements before register it #107

Open
mnogono opened this issue Aug 14, 2024 · 1 comment
Open

Check registered customElements before register it #107

mnogono opened this issue Aug 14, 2024 · 1 comment

Comments

@mnogono
Copy link

mnogono commented Aug 14, 2024

I suggest to add follow code snipped
customElements.get("xxx") || customElements.define("xxx")
This allow to prevent issue
Failed to execute 'define' on 'CustomElementRegistry': the name "hsva-color-picker" has already been used with this registry

@SalahAdDin
Copy link

Yeah, we have the same issue, I would open a new related issue but it is better to improve this one.

We have the following component on React:

import React, { useState } from '[email protected]';
import '[email protected]';
import nativeEvents from 'jsx-native-events';
import 'vanilla-colorful';

const styles = `
output {
  display: block;
  margin-top: 10px;
  font-size: 1.25rem;
  text-align: center;
}`;

export default function ColorExample() {
  const [color, setColor] = useState('#1e88e5');

  const handleColorChanged = (event: CustomEvent<{ value: string }>) => {
    setColor(event.detail.value);
  };

  return (
    <div>
      <style>{styles}</style>
      <hex-color-picker
        color={color}
        onEventColorChanged={handleColorChanged}
      ></hex-color-picker>
      <output>{color}</output>
    </div>
  );
}

From time to time we get the error: CustomElementRegistry.define: 'hex-color-picker' has already been defined as a custom element.

To fix it we opted to use @lit/react:

import React, { useState } from '[email protected]';
import '[email protected]';
import 'vanilla-colorful';
import { HexColorPicker } from "vanilla-colorful/hex-color-picker.js";

const styles = `
output {
  display: block;
  margin-top: 10px;
  font-size: 1.25rem;
  text-align: center;
}`;

const ColorPicker = createComponent({
  react: React,
  tagName: "hex-color-picker",
  elementClass: HexColorPicker,
  events: {
    onEventColorChanged: "color-changed" as EventName<
      CustomEvent<{ value: string }>
    >,
  },
});

export default function ColorExample() {
  const [color, setColor] = useState('#1e88e5');

  const handleColorChanged = (event: CustomEvent<{ value: string }>) => {
    setColor(event.detail.value);
  };

  return (
    <div>
      <style>{styles}</style>
      <ColorPicker
              color={color}
              onEventColorChanged={(event) => setColor(event.detail.value)}
            />
      <output>{color}</output>
    </div>
  );
}

But, again, but less common than before, from time to time we have the same issue!

@mnogono Could you open a PR with the fix?

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

2 participants