Skip to content

fix: address event delegation duplication behaviour #12014

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

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cyan-news-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: address event delegation duplication behaviour
2 changes: 1 addition & 1 deletion packages/svelte/src/internal/client/dom/elements/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function create_event(event_name, dom, handler, options) {
* @this {EventTarget}
*/
function target_handler(/** @type {Event} */ event) {
if (!options.capture) {
if (!options.capture && /** @type {Event & {__root: any}} */ (event).__root === undefined) {
// Only call in the bubble phase, else delegated events would be called before the capturing events
handle_event_propagation(dom, event);
}
Expand Down
16 changes: 16 additions & 0 deletions packages/svelte/tests/runtime-runes/samples/event-on-2/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

export default test({
mode: ['client'],

test({ assert, target, logs }) {
const [b1] = target.querySelectorAll('button');

const keydown = new window.KeyboardEvent('keydown', { bubbles: true });

b1?.dispatchEvent(keydown);
flushSync();
assert.deepEqual(logs, ['parent keydown']);
}
});
20 changes: 20 additions & 0 deletions packages/svelte/tests/runtime-runes/samples/event-on-2/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<script>
import { on } from "svelte/events";

function handleParentKeyDown() {
console.log("parent keydown");
}
function keydownOne(node) {
on(node, "keydown", (e) => {});
}
function keydownTwo(node) {
on(node, "keydown", (e) => {});
}
function keydownThree(node) {
on(node, "keydown", (e) => {});
}
</script>

<div onkeydown={handleParentKeyDown}>
<button use:keydownOne use:keydownTwo use:keydownThree>button</button>
</div>
Loading