Skip to content

Instantly share code, notes, and snippets.

@jamesbirtles
Created May 9, 2023 12:39
Show Gist options
  • Save jamesbirtles/60c8cd2a9e8a69e6c69ccb766661bdd9 to your computer and use it in GitHub Desktop.
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
<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