-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expose host:tagName on Components #758
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Love this idea! but just not sure this might be a bit problematic, as "as" might be too generic. How about |
|
Totally agree @manucorporat. There might also be confusion between Maybe leave What's your feeling? |
const Heading = component$((props: {as: 'h1' | 'h2' | 'h3'}) => {
const styles = {
h1: 'font-size: 3rem',
h2: 'font-size: 2.5rem',
h3: 'font-size: 2rem',
};
return <Host tagName={props.as} style={styles[props.as]}><Slot/></Host>
}); |
|
I noticed that creating a component that visually appears as a button but can be either a link or a button depending on if an href is present: const Button = component$((props: {href?: string}) => {
const tagName: keyof HTMLElementTagNameMap = props.href ? 'a' : 'button';
return <Host tagName={tagName} href={props.href} ><Slot/></Host>
})Won't typecheck because const Button = component$((props: {href?: string}) => {
const tagName: keyof HTMLElementTagNameMap = props.href ? 'a' : 'button';
return <Host tagName={tagName} {...props} ><Slot/></Host>
})I think the first example is a valid use case, but I'd be interested to hear thoughts before I look at making changes. 👍 |
|
No! i was looking into this feature the other day since the Builder SDK for Qwik will need something very similar, RenderBlocks acting as any html tag, reviewing PR! |
I am not sure how this is going t work... since currently the I can see we could support: <MyCmp host:tagName="thing">but not sure how we can support |
Fair point. Maybe something to revisit later down the road. If you're happy I'll fix the build error so you can merge! 🚀 |
|
Sounds good! but the current PR is not working right? i dont see the part of the logic that makes it work, I am confused :) |
|
ohh! it's exploiting the |
|
In the dom, it's not possible to change the tag name of a DOM node |
|
I wasn't sure if that was the best way to do it, but it got my tests passing. Is there are better place to catch that? |
|
Sorry, accidentally hit Approve, I am leaving this to @manucorporat |
|
So, i like the idea of this PR, and we will need this feature, however, the current way this implemented... is very implementation dependant in (qwik-dom) Domino, which might be replaced at some point. Having a feature that we cant port. the only way I can see this being implemented in by allowing: or then in the componentQrl function do this: |
|
Sounds good @manucorporat. I'll try to make the changes this afternoon/evening. I still think the design system use case is important, but if it's out of reach for now I'm happy to leave it as an enhancement somewhere in the future. 👍 |
|
I think you can still implement your idea for the design system, but you will have to create your own component$() that redirects the as prop |
|
Got it scaled back to just host:tagName and moved the implementation into componentQrl. Let me know if I need to make any other changes. 👍 |
What is it?
Description
Exposes a
tagNameattribute on the<Host/>tag and ahost:tagNameattribute on Qwik Components.This PR also adds a better type that is used for the
tagNameattribute,host:tagNameand the tagName render option which gives users autocomplete for allhtmltags, while retaining the ability to use custom tag names as well.ie, typing an m gives you autocomplete for
main,map,mark,marquee,menu,meta, andmeter. Butmisko,manu, andmy-componentare also valid!Use cases and why
When building a design system, it is often desirable to reuse a component but render into a variety of tags. This is not currently possible because the only way to set a host element tag name is as an argument to
component$and not from withincomponent$Imagine a Header component that would be used like so:
<Header as='h3' text='Hello!'/>Within the component definition we can check the as attribute for different styles and set the tagName on the host element:
and then use it like this:
Or a component that just renders as a 'pill'. Maybe we want a div, but maybe we want an
li,dd,span, or any other valid HTML tag. With an as attribute we can have that flexibility.<Pill as='span'/>🚀Checklist: