Skip to content

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

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 2 commits into from
Jun 4, 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/lovely-bugs-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

feat: bind `activeElement` and `pointerLockElement` in `<svelte:document>`
2 changes: 2 additions & 0 deletions documentation/docs/02-template-syntax/07-special-elements.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,9 @@ As with `<svelte:window>`, this element may only appear the top level of your co

You can also bind to the following properties:

- `activeElement`
- `fullscreenElement`
- `pointerLockElement`
- `visibilityState`

All are readonly.
Expand Down
2 changes: 2 additions & 0 deletions packages/svelte/elements.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1364,7 +1364,9 @@ export interface SvelteMediaTimeRange {
}

export interface SvelteDocumentAttributes extends HTMLAttributes<Document> {
readonly 'bind:activeElement'?: Document['activeElement'] | undefined | null;
readonly 'bind:fullscreenElement'?: Document['fullscreenElement'] | undefined | null;
readonly 'bind:pointerLockElement'?: Document['pointerLockElement'] | undefined | null;
readonly 'bind:visibilityState'?: Document['visibilityState'] | undefined | null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2773,6 +2773,11 @@ export const template_visitors = {
call_expr = b.call('$.bind_window_size', b.literal(node.name), setter);
break;

// document
case 'activeElement':
call_expr = b.call('$.bind_active_element', setter);
break;

// media
case 'muted':
call_expr = b.call(`$.bind_muted`, state.node, getter, setter);
Expand Down
9 changes: 9 additions & 0 deletions packages/svelte/src/compiler/phases/bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,20 @@ export const binding_properties = {
omit_in_ssr: true
},
// document
activeElement: {
valid_elements: ['svelte:document'],
omit_in_ssr: true
},
fullscreenElement: {
valid_elements: ['svelte:document'],
event: 'fullscreenchange',
omit_in_ssr: true
},
pointerLockElement: {
valid_elements: ['svelte:document'],
event: 'pointerlockchange',
omit_in_ssr: true
},
visibilityState: {
valid_elements: ['svelte:document'],
event: 'visibilitychange',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { listen } from './shared.js';

/**
* @param {(activeElement: Element | null) => void} update
* @returns {void}
*/
export function bind_active_element(update) {
listen(document, ['focusin', 'focusout'], (event) => {
if (event && event.type === 'focusout' && /** @type {FocusEvent} */ (event).relatedTarget) {
// The tests still pass if we remove this, because of JSDOM limitations, but it is necessary
// to avoid temporarily resetting to `document.body`
return;
}

update(document.activeElement);
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { add_form_reset_listener } from '../misc.js';
/**
* Fires the handler once immediately (unless corresponding arg is set to `false`),
* then listens to the given events until the render effect context is destroyed
* @param {Element | Window} target
* @param {EventTarget} target
* @param {Array<string>} events
* @param {() => void} handler
* @param {(event?: Event) => void} handler
* @param {any} call_handler_immediately
*/
export function listen(target, events, handler, call_handler_immediately = true) {
Expand Down
1 change: 1 addition & 0 deletions packages/svelte/src/internal/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export { event, delegate, replay_events } from './dom/elements/events.js';
export { autofocus, remove_textarea_child } from './dom/elements/misc.js';
export { set_style } from './dom/elements/style.js';
export { animation, transition } from './dom/elements/transitions.js';
export { bind_active_element } from './dom/elements/bindings/document.js';
export { bind_checked, bind_files, bind_group, bind_value } from './dom/elements/bindings/input.js';
export {
bind_buffered,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

// This test is slightly inaccurate, because blurring elements (or focusing the `<body>` directly)
// doesn't trigger the relevant `focusin` event in JSDOM.
export default test({
test({ assert, target, logs }) {
const [btn1, btn2] = target.querySelectorAll('button');

flushSync(() => btn1.focus());
assert.deepEqual(logs, ['...', 'BODY', 'one']);

flushSync(() => btn2.focus());
assert.deepEqual(logs, ['...', 'BODY', 'one', 'two']);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script>
let active;

$: console.log(active?.id || active?.nodeName || '...');
</script>

<svelte:document bind:activeElement={active} />

<button id="one">one</button>
<button id="two">two</button>
Original file line number Diff line number Diff line change
@@ -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

"start": {
"line": 5,
"column": 17
Expand Down
Loading