Comments (15)
A different example, where an imported
use:action
may want to communicate back to the node via an event. If it happens to use one of the special event names, it stops working when migrated unlessbubbles
is added, easier if the code is within your app, but may trip people up if they don't know about this breaking change and the special behavior for the affected event types isn't documented.
Yeah as I've said this either needs to be documented as a breaking change or fixed in some way.
from svelte.
Looks like they now need "bubbles" set on the custom event:
input.dispatchEvent(new CustomEvent('input', { bubbles: true }))
from svelte.
Hmmn, I say it works ... for the example it does, for something in a separate package it doesn't seem to. I'll test it some more.
from svelte.
The problem is that manually created events don't bubble by default and actual dom events without the colon use the global listener (which requires bubbling to work).
So when you switch to colonless events the native events gets delegated to the body but your dispatched event never reaches the body.
I don't think there's a way to fix this without either removing the delegated events thing (which I don't think is feasible now) or monkey patching dispatch event.
I wonder if a decent workaround could be listening to the capture phase and check if the event will bubble and if not dispatch a new event that bubbles. But I fear this could get more convoluted than necessary
from svelte.
Can dispatch
from svelte/events
be implemented to "fix" this?
from svelte.
Can
dispatch
fromsvelte/events
be implemented to "fix" this?
I guess the problem is if libraries are using this, if it's user lang code you can just add bubble: true
from svelte.
Are you missing the bit about behavior changing depending on the event name? Not only is it different to how it used to behave, it's also inconsistent.
I need to confirm if bubbles worked from library code, when I tried it last night it didn't appear to, but it was late and I may have been doing something stupid.
Update: I was doing something stupid! (set bubbles on event detail instead of the event itself)
from svelte.
Are you missing the bit about behavior changing depending on the event name? Not only is it different to how it used to behave, it's also inconsistent.
Nope I've actually explained why that happen.
need to confirm if bubbles worked from library code, when I tried it last night it didn't appear to, but it was late and I may have been doing something stupid.
With bubble it should work.
from svelte.
Maybe I'm not explaining it well. I know the trusted events that the browser dispatches work, but all the events dispatched from clicking the button in the example are manually dispatched, and the one with the name "custom" works without bubbles
which is why it seems to be inconsistent.
Another question: what if you don't want an event to bubble? The listener and dispatch are both on the same DOM element so bubbling shouldn't be needed.
from svelte.
Maybe I'm not explaining it well. I know the trusted events that the browser dispatches work, but all the events dispatched from clicking the button in the example are manually dispatched, and the one with the name "custom" works without
bubbles
which is why it seems to be inconsistent.
Because the only events that are delegated are the "non custom" ones
from svelte.
Because the only events that are delegated are the "non custom" ones
Why? They are all custom events, and none of the events are set to bubble, but one does?
It changes how events worked with the on:
syntax, so would at least be a breaking change, but it's inconsistent and different to the normal expectations of how events work with the DOM. Having onwhatever
behave differently to dom.addEventListener('whatever', () => {})
or dom.onwhatever = () => {}
in an action, but only for events with certain names is going to be very confusing.
For comparison, without Svelte, it works how I'd expect - the events only reach the div
listeners if they are set to bubble, and they always reach the input
listeners without or without bubbling (because they are dispatched from that element):
<!DOCTYPE html>
<div id="div">
<input id="input" type="text">
<button id="bubbles">bubbles</button>
<button id="regular">regular</button>
</div>
<script>
function handle(name) {
return function(e) {
console.log(name, e.type)
}
}
div.addEventListener('input', handle('div'))
div.addEventListener('custom', handle('div'))
input.addEventListener('input', handle('input'))
input.addEventListener('custom', handle('input'))
bubbles.addEventListener('click', () => {
input.dispatchEvent(new CustomEvent('input', { bubbles: true }))
input.dispatchEvent(new CustomEvent('custom', { bubbles: true }))
})
regular.addEventListener('click', () => {
input.dispatchEvent(new CustomEvent('input'))
input.dispatchEvent(new CustomEvent('custom'))
})
</script>
from svelte.
Why? They are all custom events, and none of the events are set to bubble, but one does?
What I mean is that click is a known Dom event so it's delegated to the body because they can add a global click listener and run all the events that bubbles. "custom" is not a known Dom event so they can't do that and so they add the listener to the Dom element itself. Since none of your events bubbles the only one logged is "custom" that is attached to the Dom element and not to the body.
I'm not saying is desiderabile. I'm just explaining why it's happening.
from svelte.
It seems like a simplistic / fundamentally flawed approach to me from my (possibly limited) understanding. You can't really tell from the event name / type whether the event you're going to get will be a custom event or a native event (as in "dispatched by the browser" vs "dispatched programatically"). AFAIK the only way to check is by looking at the event itself, e.g. for the isTrusted
property on it.
This seems like a step backwards. It breaks expectations of how HTML + JS work. I'd much rather have correct & consistent than something that may only usually be of benefit to benchmark runs.
from svelte.
A different example, where an imported use:action
may want to communicate back to the node via an event. If it happens to use one of the special event names, it stops working when migrated unless bubbles
is added, easier if the code is within your app, but may trip people up if they don't know about this breaking change and the special behavior for the affected event types isn't documented.
from svelte.
We just need to document what events are delegated.
We only delegate a specific amount of events: https://github.com/sveltejs/svelte/blob/main/packages/svelte/src/constants.js#L37-L61
You only need to pass bubbles: true
when dispatching events that are delegated.
from svelte.
Related Issues (20)
- Svelte 5: Whitespace before the closing brace of `{#snippet}` will throw a parse error.
- Svelte 5: cannot use `@render` for $$slots HOT 7
- Low quality animation example HOT 6
- Svelte 5: Snippet Shorthand HOT 2
- Svelte 5: Mutating promise based arrays do not trigger updates HOT 7
- Svelte 5: Document $svelte.is(a,b) using Object.is(a,b). HOT 2
- Imported global styles apply differently in dev and build mode HOT 1
- Svelte 5 migration: erroneous `$state` declaration when `each` block present HOT 1
- Svelte 5 migration: event modifier transformer doesn't respect indentation HOT 2
- Svelte 5 migration: Transformed declarations with TS annotations cause broken output
- Svelte 4/5 incompatibility: exporting the same variable with two names breaks the export. HOT 2
- Addition assignment operator (+=) broken
- "node is null" when manipulating an array state in a Snippet HOT 4
- Styles are not removed after transition slide end HOT 1
- Svelte 5 migration: statements should be reordered
- Svelte 5: leaking private implementation details ($$events, $$slots) HOT 3
- Svelte 5: Snippets aren't deconflicted HOT 5
- Svelte5: snippets components invoked multiple times HOT 4
- The exported names of the reactive Map, Set, and others conflict with platform globals HOT 6
- Svelte 5: non-keyed each blocks do not retain text direction in elements with `dir="auto"` HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from svelte.