Skip to content

Binding activeElement + removing focused element from DOM gives errorΒ #15742

@RWDavid

Description

@RWDavid

Describe the bug

I can bind to the document's active element via <svelte:document bind:activeElement />.

However, this generates a state_unsafe_mutation error when I click an element and remove it from the DOM tree.

Reproduction

REPL

<script>
	let activeElement = $state();
	let showButton = $state(true);
</script>

<svelte:document bind:activeElement />
{#if showButton}
	<button onclick={() => showButton = false}>hello</button>
{/if}

Logs

Svelte error: state_unsafe_mutation
Updating state inside a derived or a template expression is forbidden. If the value should not be reactive, declare it without `$state`
https://svelte.dev/e/state_unsafe_mutation

System Info

REPL version=5.25.9

Severity

annoyance

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions