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

feat: bind activeElement and pointerLockElement in <svelte:document> #11879

Merged
merged 2 commits into from
Jun 4, 2024

Conversation

wackbyte
Copy link
Contributor

@wackbyte wackbyte commented Jun 3, 2024

Adds support for bind:activeElement and bind:pointerLockElement to <svelte:document>. Closes #2671.

I want to add tests (if necessary, because bind:visibilityState seems to be untested?), but I need some guidance on where exactly they would go.

Svelte 5 rewrite

Please note that the Svelte codebase is currently being rewritten for Svelte 5. Changes should target Svelte 5, which lives on the default branch (main).

If your PR concerns Svelte 4 (including updates to svelte.dev.docs), please ensure the base branch is svelte-4 and not main.

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests and linting

  • Run the tests with pnpm test and lint the project with pnpm lint

Copy link

changeset-bot bot commented Jun 3, 2024

🦋 Changeset detected

Latest commit: 92f8b46

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@wackbyte wackbyte force-pushed the document-props branch 4 times, most recently from 50199a9 to 020104e Compare June 3, 2024 16:21
@wackbyte
Copy link
Contributor Author

wackbyte commented Jun 3, 2024

Question: The listener fires once when losing or gaining focus, but fires twice when changing focus between two elements (element 1 loses focus, element 2 gains focus). Should it be debounced? Is it feasible to?

@Rich-Harris
Copy link
Member

Thank you. We can't use focus and blur events here because they don't bubble; it needs to be focusin and focusout instead. It's finicky to test because JSDOM doesn't do a great job of matching browser behaviour here, so I took the liberty of adding one.

The listener fires once when losing or gaining focus, but fires twice when changing focus between two elements

We can solve this by checking event.relatedTarget in the focusout handler

@wackbyte
Copy link
Contributor Author

wackbyte commented Jun 4, 2024

Thank you! focus and blur do seem to work when the listener is capturing, but either way works.

@Rich-Harris
Copy link
Member

Ah gotcha, missed the , true and saw it stop working when I refactored to use the listen helper for the sake of code size

@@ -1,7 +1,7 @@
[
{
"code": "bind_invalid_name",
"message": "`bind:clientWidth` is not a valid binding. Possible bindings for <svelte:document> are focused, fullscreenElement, visibilityState, this",
"message": "`bind:clientWidth` is not a valid binding. Possible bindings for <svelte:document> are focused, activeElement, fullscreenElement, pointerLockElement, visibilityState, this",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not strictly related to this PR, but a note to self: what is focused? document.focused isn't a thing so this seems odd, maybe a bug? also it'd be nice if these things were sorted alphabetically rather than randomly. will investigate after lunch

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay, looks like we added focused to all elements a while back. it should probably exclude <svelte:document> and some other things. can look into that as a separate PR

@wackbyte
Copy link
Contributor Author

wackbyte commented Jun 4, 2024

JSDOM doesn't implement the pointer lock API as far as I can tell, so I don't think a test can be created for it. I have made sure it works in the REPL, though.

@Rich-Harris
Copy link
Member

Yeah, we work around that limitation for fullscreenElement like this...

Object.defineProperties(window.document, {
fullscreenElement: {
value: div,
configurable: true
}
});

...but it feels like a bit of a pointless test at that point to be honest

@Rich-Harris Rich-Harris merged commit c0c1a56 into sveltejs:main Jun 4, 2024
6 of 8 checks passed
@Rich-Harris
Copy link
Member

thanks!

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 this pull request may close these issues.

<svelte:window bind:activeElement>
2 participants