metonym / svelte-highlight

Syntax Highlighting for Svelte using highlight.js
https://svhe.onrender.com
MIT License
262 stars 14 forks source link

Is it possible to pass code as a slot? #238

Closed marcaddeo closed 1 year ago

marcaddeo commented 2 years ago

Is it possible to pass code using a slot instead of a prop?

For example, I'd like to be able to do <Highlight language={typescript}> const a = "test"; </Highlight> instead of having to put the code in a prop.

The reason I'd like to see this is I'm using <Highlight /> wrapped in a <code-block> web component and it would feel more natural to pass it as a child/slot.

metonym commented 1 year ago

It's an intentional design choice for code to be a prop. The reason is reactivity; if code changes, it must be re-highlighted.

There is no built-in Svelte way to react to slot changes. You would need a mutation observer to re-highlight the child node text if it changes.