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

Add ability to attach ElementInternals to Svelte Web Components #8472

Closed
tronicboy1 opened this issue Apr 11, 2023 · 1 comment · Fixed by #8991
Closed

Add ability to attach ElementInternals to Svelte Web Components #8472

tronicboy1 opened this issue Apr 11, 2023 · 1 comment · Fixed by #8991

Comments

@tronicboy1
Copy link

Describe the problem

Currently, there is no way to add a static tag to Svelte Web Components.

This poses a problem when you are trying to make custom elements with form interoperability using ElementInternals.

This could easily be fixed by adding logic in the constructor template to add the static value and internals setup logic.

https://developer.mozilla.org/en-US/docs/Web/API/ElementInternals#examples

Describe the proposed solution

I propose adding a new setting to the <svelte:options /> tag; elementInternals="true".

We could then add the static field necessary and have a separate function for adding internals in the lifecycle hook.

Alternatives considered

I thought it would be possible to adjust the constructor of the element's class. This would not work as we cannot redefine custom elements.

Importance

i cannot use svelte without it

dummdidumm added a commit that referenced this issue Jul 18, 2023
This should help everyone who has special needs and use cases around custom elements. Since Svelte components are wrapped and only run on connectedCallback, it makes sense to expose the custom element class for modification before that.
- fixes #8954 - use extend to attach the function manually and save possible values to a prop
- closes #8473 / closes #4168 - use extend to set the proper static attribute and then call attachInternals in the constructor
closes #8472 - use extend to attach anything custom you need
closes #3091 - pass `this` to a prop of your choice and use it inside your component
@dummdidumm
Copy link
Member

You can now do this using the new extend option:

<svelte:options
  customElement={{
    tag: 'custom-element',
    extend: (customElementConstructor) => {
      // Extend the class so we can let it participate in HTML forms
      return class extends customElementConstructor {
        static formAssociated = true;

        constructor() {
          super();
          this.attachedInternals = this.attachInternals();
        }
      };
    }
  }}
/>

<script>
  export let attachedInternals;
  // ...
  function check() {
    attachedInternals.checkValidity();
  }
</script>

...

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

Successfully merging a pull request may close this issue.

2 participants