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

Custom element dispatch event #4142

Closed

Conversation

hontas
Copy link
Contributor

@hontas hontas commented Dec 22, 2019

Possible solution to #3712, #3119 and #2611.

Enables event dispatching for custom elements. As it is now, no events are dispatched from a custom element because component.$$.callbacks[type] is always falsy.

When compile target is custom element, always dispatch event.
Atm checking this by component.shadowRoot - perhaps there is a safer/better way?

@hontas
Copy link
Contributor Author

hontas commented Jan 12, 2020

Any comments on this?

@TehShrike
Copy link
Member

@hontas Svelte generally tries to be very conservative when it comes to adding any bytes to that would need to be shipped to the client-side – especially if those extra bytes only benefit the red-headed stepchild of the browser that is custom elements.

I think the target for fixing this issue would be for the component.dispatchEvent(event) line to only end up in the source code when the compiler was run with customElement: true.

I'm not sure what that change looks like when it comes to these internal functions, though. :-x

@Conduitry
Copy link
Member

I don't think we really have a good mechanism for varying what you get from the runtime imports. So far we've tried to keep them just regular imports. There's a proposal somewhere around here for adding a magic global $$dispatch that automatically imports createEventDispatcher and calls it - and while I'm not thrilled with that idea particularly, I do think it would be preferable to have that convert to one of two different imports rather than doing some magical rewriting of imports the user wrote themselves.

The simplest solution of course would be something like this, where we ship extra bytes to everyone. This might not be so awful, as it would be per-app bytes, not per-component bytes. I'm not sure which I prefer. If we merge this PR though, that is a decision it would be hard to walk back.

@hontas
Copy link
Contributor Author

hontas commented Mar 7, 2020

Sorry for the late response.
4 now we simply use element.dispatchEvent instead which is working fine, though it would be cooler if all svelte code worked in both scenarios, with/without customElement compile target.

Yet another option is to have warnings when compiling to custom elements so that it's clear what works and not.

@stale
Copy link

stale bot commented Jun 26, 2021

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale-bot label Jun 26, 2021
@stale stale bot removed the stale-bot label Jun 26, 2021
@stale stale bot removed the stale-bot label Jun 27, 2021
@hmil
Copy link

hmil commented Oct 8, 2021

+1 for this.

Svelte offers to export components as custom elements, the input properties are correctly mapped to the properties of the custom element, and yet its output events are not exported.

This input/output asymmetry appears like a bug. Users expect the custom element interface to "just work".

Svelte is an attractive framework for building component libraries and micro-frontend entry points due to its low overhead. But this issue is almost a blocker for adoption of the library in these scenario.

@hmil
Copy link

hmil commented Oct 8, 2021

IMO this tiny per-application overhead is worth it given how the current behavior clashes with users expectations.

It would make sense for Svelte 3.x to accept regressions in code size at the benefit of better "out of the box" functionality.

You could be looking at the crazy optimizations above for v4 because it seems like this would break a lot of internal APIs anyways.

@baseballyama baseballyama added this to the 4.x milestone Feb 26, 2023
@dummdidumm dummdidumm mentioned this pull request Apr 6, 2023
5 tasks
@benmccann benmccann changed the base branch from master to version-4 April 11, 2023 21:08
dummdidumm added a commit that referenced this pull request May 2, 2023
This is an overhaul of custom elements in Svelte. Instead of compiling to a custom element class, the Svelte component class is mostly preserved as-is. Instead a wrapper is introduced which wraps a Svelte component constructor and returns a HTML element constructor. This has a couple of advantages:

- component can be used both as a custom element as well as a regular component. This allows creating one wrapper custom element and using regular Svelte components inside. Fixes #3594, fixes #3128, fixes #4274, fixes #5486, fixes #3422, fixes #2969, helps with sveltejs/kit#4502
- all components are compiled with injected styles (inlined through Javascript), fixes #4274
- the wrapper instantiates the component in `connectedCallback` and disconnects it in `disconnectedCallback` (but only after one tick, because this could be a element move). Mount/destroy works as expected inside, fixes #5989, fixes #8191
- the wrapper forwards `addEventListener` calls to `component.$on`, which allows to listen to custom events, fixes #3119, closes #4142 
- some things are hard to auto-configure, like attribute hyphen preferences or whether or not setting a property should reflect back to the attribute. This is why `<svelte:options customElement={..}>` can also take an object to modify such aspects. This option allows to specify whether setting a prop should be reflected back to the attribute (default `false`), what to use when converting the property to the attribute value and vice versa (through `type`, default `String`, or when `export let prop = false` then `Boolean`), and what the corresponding attribute for the property is (`attribute`, default lowercased prop name). These options are heavily inspired by lit: https://lit.dev/docs/components/properties. Closes #7638, fixes #5705
- adds a `shadowdom` option to control whether or not encapsulate the custom element. Closes #4330, closes #1748 

Breaking changes:
- Wrapped Svelte component now stays as a regular Svelte component (invokeing it like before with `new Component({ target: ..})` won't create a custom element). Its custom element constructor is now a static property named `element` on the class (`Component.element`) and should be regularly invoked through setting it in the html.
- The timing of mount/destroy/update is different. Mount/destroy/updating a prop all happen after a tick, so `shadowRoot.innerHTML` won't immediately reflect the change (Lit does this too). If you rely on it, you need to await a promise
@dummdidumm
Copy link
Member

Closed via #8457

@dummdidumm dummdidumm closed this May 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants