Created
May 9, 2023 12:39
-
-
Save jamesbirtles/60c8cd2a9e8a69e6c69ccb766661bdd9 to your computer and use it in GitHub Desktop.
Example svelte component with an `as` property allowing for any element to be chosen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script lang="ts"> | |
import type { HTMLAttributes, SvelteHTMLElements } from 'svelte/elements'; | |
type T = $$Generic<{ as?: keyof SvelteHTMLElements }>; | |
type ElementAttrs = T['as'] extends keyof SvelteHTMLElements | |
? SvelteHTMLElements[T['as']] | |
: HTMLAttributes<HTMLDivElement>; | |
type $$Props = { class?: string } & T & ElementAttrs; | |
let className = ''; | |
export { className as class }; | |
export let as: keyof SvelteHTMLElements | undefined = undefined; | |
</script> | |
<svelte:element this={as ?? 'div'} class="swiper-slide {className}" {...$$restProps}> | |
<slot /> | |
</svelte:element> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment