svelte-html-event
TypeScript icon, indicating that this package has built-in type declarations

0.0.6 • Public • Published

svelte-html-event

npm version

A simple type function to get the correct event type for a Builtin HTML element in Svelte.

To install dependencies:

bun install svelte-html-event

Usage

<script lang="ts">
import type { SvelteHTMLElementEvent } from 'svelte-html-event';

function onClick(e: SvelteHTMLElementEvent<'button', 'on:click'>) {
    console.log(e.detail);
}
</script>

<button on:click={onClick}>Click me</button>

screenshot

Also, you can use custom attributes as well.
This is really useful when you use use: directive by some libraries like svelte-dnd-action.

For Svelte Custom Elements, you can use the builtin SvelteCustomElementEvent instead.

<script lang="ts">
   import type { ComponentEvents } from 'svelte';
   import Component from './Component.svelte';

   function handleCloseEvent(event: ComponentEvents<Component>['close']) {
	  console.log(event.detail);
   }
</script>

<Component on:close={handleCloseEvent} />

Inspiration

This article inspired me to create this package.

License

MIT

Readme

Keywords

none

Package Sidebar

Install

npm i svelte-html-event

Weekly Downloads

40

Version

0.0.6

License

MIT

Unpacked Size

13.1 kB

Total Files

5

Last publish

Collaborators

  • ryoppippi