Last active
March 8, 2024 13:51
-
-
Save dejurin/2e0e0a6f209040f43b88a555ca9601dd to your computer and use it in GitHub Desktop.
Qwik Polymorphic Component Example
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
import type { QwikIntrinsicElements } from "@builder.io/qwik"; | |
import { component$, Slot } from "@builder.io/qwik"; | |
export default component$( | |
<C extends keyof QwikIntrinsicElements>(props: PolyProps<C>) => { | |
const Component = (props.as ?? "div") as string; | |
return ( | |
<Component {...props}> | |
<Slot /> | |
</Component> | |
); | |
} | |
); | |
export type PolyProps<C extends keyof QwikIntrinsicElements> = | |
QwikIntrinsicElements[C] & { | |
as?: C; | |
}; | |
// These all work with correct types | |
/* | |
<> | |
<Poly>Hello from a div</Poly> | |
<Poly as="a" href="/blog">Blog</Poly> | |
<Poly as="input" onInput$={(ev, el) => console.log(el.value)} /> | |
// <Poly as={OtherComponent} someProp /> @todo | |
</> | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment