Skip to content

Svelte 5: Event target is not retargeted after shadow dom #11891

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

Closed
GatienBouyer opened this issue Jun 3, 2024 · 0 comments · Fixed by #11900
Closed

Svelte 5: Event target is not retargeted after shadow dom #11891

GatienBouyer opened this issue Jun 3, 2024 · 0 comments · Fixed by #11900
Assignees

Comments

@GatienBouyer
Copy link

GatienBouyer commented Jun 3, 2024

Describe the bug

When using shadowDOM, event are "retargeted" to hide the inner representation of the element.

Composed events dispatched from within a shadow root are retargeted, meaning that to any listener on an element hosting a shadow root or any of its ancestors, they appear to come from the hosting element.

from https://lit.dev/docs/components/events/#shadowdom-retargeting

I usually use retargeting to get the instance of the custom element (e.g. a card element) rather than the image or text element inside it. It allows having a single listener on the container when a huge number of elements is expected.

With Svelte 5 and the new on[event] syntax, the Event.target is not retargeted.

It was working with Svelte 4.

The issue can be circumvented by manually adding the event listener (see example code below).

Other issues related to the subject:

Reproduction

Here is the REPL :
https://svelte-5-preview.vercel.app/#H4sIAAAAAAAAA31TUU_bMBD-K57FQ5CaVLyWthNiSDwUmACJh3kKxr62BteO7Es7FOW_75wEWjY2KZLjy9133933peFLYyHyyY-GO7kBPuFnVcVHHF-rdIlbsAh0j74OKkWmUQVT4Vw4gWZT-YCs8e7K1w5btgx-w8RQJfjpQZLgxfgSrPUPPlhdPMfhswVklH3CZuwookTIjrv4gJllx2w2Z00KCUyJhdT6YgsOFyYiOAiZ4Moa9SL4iGVwmC5QeRe9hcL6VfaYChcTdtRAgTKsAL_SubqmsdvH41M2HrPLi8XiJn-4uV186xHankx_0MuydgqNd8y7rmfqNzT70Gr4_N9md9_Prjtw4abj_Vbd9Eues36FuVk5H4DJk5PXskMsIY0ey7XcQvkCr8Od5fn835XOl2m1RpVgYUP5pXEIQXajvNVqs2VPxukJrk2cNWnVbZK2a9t2ek_XScB8lxScT8eHtzQDIdBJZtl4bZYGNJ9gqKEdvXtrb4C9xZ7job2UlTGyfR6DXySyptD91eKiZ99vPO2b4BX6kL2LEOuKDNGLJjBNUpRxLbXf3XqP5LEuJBGlWt918axhRBcmZFBfgROcvWk-NHGgEPS5tPZJkuAf9EaGxI9gtVd1olaoAOThgSg5M1aSMHvElFsYAgz39DYT_TpYt8Dhb_ibcyEroqXP18bqLCG8s6PHLFm2I838rlB1RL8ZGseCDEfdDxQiEmw2Y7XTsDQO9DDH59V9zp8AowNdEov2M7F_tr8B8XsZ6FQEAAA=

Main Svelte file:

<script>
	import {onMount} from "svelte";
	import "./HelloWorld.js";
	let elt1 = $state();
	onMount(() => {
		elt1.addEventListener("click", (e) => {
			console.log(`addEL: ${e.target?.tagName}`); // HELLO-WORLD, as expected
		});
	});

	function onclick(e) {
		console.log(`onclick: ${e.target?.tagName}`); // SPAN, error!
	}
</script>

<!-- svelte-ignore a11y_click_events_have_key_events -->
<!-- svelte-ignore a11y_no_static_element_interactions -->
<div bind:this={elt1} {onclick}>
	<hello-world></hello-world>
</div>

Custom element definition in HelloWorld.js:

class HelloWorld extends HTMLElement {
	constructor() {
		super();
		this._shadowRoot = this.attachShadow({ mode: "open" });
	}
	connectedCallback() {
		const text = document.createElement("span");
		text.innerText="Hello world";
		this._shadowRoot.appendChild(text);
	}
}
if (window.customElements.get("hello-world") == undefined) {
	window.customElements.define("hello-world", HelloWorld);
}

Logs

No response

System Info

REPL: Svelte compiler version 5.0.0-next.148

Severity

blocking an upgrade

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

Successfully merging a pull request may close this issue.

2 participants