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

Allow textContent / innerHTML / innerText binding without setting the contenteditable attribute #11654

Open
Serator opened this issue May 16, 2024 · 3 comments

Comments

@Serator
Copy link

Serator commented May 16, 2024

Describe the problem

Svelte already allows you to create read-only bindings (e.g. <svelte:window bind:innerWidth />).
But in the case of element content binding, the contenteditable attribute is required. Otherwise we get an error:

'contenteditable' attribute is required for textContent, innerHTML and innerText two-way bindings

Svelte 5 (runes) example

But this can be easily hacked by adding contenteditable="false".

Svelte 4 example (there is no example with Svelte 5 because of this.)

Describe the proposed solution

Remove the requirement of a mandatory contenteditable attribute for binding to element content.

Importance

would make my life easier

@Serator
Copy link
Author

Serator commented May 16, 2024

#3311 (comment) - additional information on the topic.

@7nik
Copy link

7nik commented May 16, 2024

Why do you need it when you, in general, control the content of non-contenteditable elements?

@Serator
Copy link
Author

Serator commented May 16, 2024

In my script, I get a block of code from a .md file and wrap it in a component. The "Copy" button takes the contents of the code block and places it on the user's clipboard.

Now for this, I'm doing something like:

let codeElement: HTMLElement | undefined = $state()
let codeListing = $derived(codeElement?.innerText ?? '')

...

<div bind:this={codeElement}>
  {@render children()}
</div>

but I'd like to do it this way:

let codeListing = $state('')

...

<div bind:innerText={codeListing}>
  {@render children()}
</div>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants