Svelte: (Re)introduce <svelte:document>?

Created on 29 Jul 2019  路  7Comments  路  Source: sveltejs/svelte

Is your feature request related to a problem? Please describe.

I noticed Svelte used to have a <svelte:document> but then it got removed. However, listeners on document work a bit differently than on document.body. For example, a mousemove / mouseup event on document.body stops the minute you move the cursor out of the browser viewport, but on document they keep getting fired.

Describe the solution you'd like

A <svelte:document> element to work with events on the Document object (especially events that happen exclusively on a document).

Describe alternatives you've considered

You can add events to document imperatively, but a declarative alternative would be nicer.

How important is this feature to you?

I've just started exploring Svelte, so I've not tried many things in it. For my particular usecase I would not be able to use <svelte:document> but, for the sake of completeness, I think it would be a good addition.

proposal

Most helpful comment

<svelte:document> would be useful for implementing selectionchange events, which operate on the document. Please reconsider adding it, as browser inconsistencies for mouse events are outside of Svelte's control anyways

All 7 comments

As described in the linked ticket, <svelte:document> was removed because there were inconsistencies in mouse events between browsers. We don't want to be in the business of normalizing events.

There's nothing really in the way of adding this again, but the question is whether it would cause more confusion than it's worth. Even if we mention in the docs the differences between the events emitted in different browsers, issues will almost certainly be opened about Svelte 'bugs' in certain browsers or about feature requests to make it behave the same across browsers.

We don't want to be in the business of normalizing events.

I agree, I think it's great that Svelte aims to work with the DOM, warts and all. But the warts include the caveats around onmouseenter / onmouseleave on document, and they're hardly the only areas where browser implementations disagree.

Having <svelte:window> and <svelte:body> as proxies to attach events to their DOM counterparts makes not having <svelte:document> a curious omission. I feel like it's at the same level of convenience as the other <svelte:x> elements.

That being said, <svelte:document> can definitely wait until there are more use cases it can help with.

Thank you for considering it!

<svelte:document> would be useful for implementing selectionchange events, which operate on the document. Please reconsider adding it, as browser inconsistencies for mouse events are outside of Svelte's control anyways

So at the current status, if I'd like to add event listener to document (for example, listen to visibility change), the only option have is document.addEventListener / removeEventListener?

@naorye right now, yes, unless you can use an event from <svelte:window>

That being said, can definitely wait until there are more use cases it can help with.

I think visibilitychange is a good use case to reintroduce svelte:document?

Another document-only API that I need is Pointer Lock: https://developer.mozilla.org/en-US/docs/Web/API/Pointer_Lock_API

Specifically, I need document's onpointerlockchange, onpointerlockerror, and pointerLockElement

Please introduce <svelte:document> even if you don't support binding to the problematic mouse events.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

noypiscripter picture noypiscripter  路  3Comments

bestguy picture bestguy  路  3Comments

ricardobeat picture ricardobeat  路  3Comments

1u0n picture 1u0n  路  3Comments

robnagler picture robnagler  路  3Comments