The string returned by your custom formater will be processed as "normal" HTML, so Svelte syntax is not available anymore... Namely on:click
is Svelte syntax and won't be processed by the browser.
I'm no custom element expert, but unfortunately, according to others, what you're trying to do is impossible. That is, you can't register custom events listeners on custom elements from the HTML alone. You would necessarily have to do it from JS. Something like this:
<script>
document.querySelector('custom-button')
.addEventListener('my-event', e => {
console.log(e.detail)
})
</script>
Furthermore, beware that events from custom elements in Svelte currently suffer some limitations (see this issue).
So, in order to have custom events for your custom elements, you'd have to use some kind of workaround. For example, here's a component that would trigger the listener in the above snippet:
<svelte:options tag="custom-button" />
<script>
import { onMount } from 'svelte';
let el
onMount(() => {
const interval = setInterval(() => {
let event = new CustomEvent('my-event', {
detail: {time: Date.now()},
bubbles: true,
composed: true, // needed for the event to traverse beyond shadow dom
})
el.dispatchEvent(event)
}, 1000)
return () => {
clearInterval(interval)
}
})
</script>
<button bind:this={el}><slot /></button>
Note that such code will have limited support with older browsers.
All that being said, for just precisely the case in your example, you could apparently make it work by registering your event with native browser onclick
instead of Svelte's on:click
. I'm still no CE expert, but my guess is the browser processes standard events that are available on all native elements like click or keydown on CE elements too...
So, it appears this should work for you:
return '<ul class="actions_row"><li><custom-button onclick="handleClick">Edit</custom-button></li></ul>';
Notice: onclick="handleClick"
instead of on:click={handleClick}
. You're now in standard browser land, so usual rules apply, the same way as if you were using a normal <button>
... handleClick
has to be available in scope, etc.