Repository navigation
Closed
Labels
Feature: Want this? Upvote it!This PR or Issue may be a great consideration for a future idea.
Description
Stencil version:
@stencil/[email protected]
I'm submitting a:
- bug report
- feature request
- support request
Current behavior:
You have to enumerate every possible attribute on an html element that you want to enhance with your webcomponent.
Expected behavior:
It would be REALLY nice to be able to say, for example this is a <button>, so I need all of those additional properties too, and not have to type them out as @Prop()s.
Steps to reproduce:
Related code:
This is what you currently have to write:
import { Component, h, Prop } from '@stencil/core';
@Component({
tag: 'my-button',
styleUrl: 'my-button.scss',
shadow: true
})
export class Button {
@Prop() autoFocus:boolean;
@Prop() disabled:boolean;
@Prop() form:string;
@Prop() formAction:string;
@Prop() formaction:string;
@Prop() formEncType:string;
@Prop() formenctype:string;
@Prop() formMethod:string;
@Prop() formmethod:string;
@Prop() formNoValidate:boolean;
@Prop() formnovalidate:boolean;
@Prop() formTarget:string;
@Prop() formtarget:string;
@Prop() name:string;
@Prop() type:string;
@Prop() value:string | string[] | number;
render() {
return (
<button autoFocus={this.autoFocus} disabled={this.disabled} form={this.form} formAction={this.formAction} formaction={this.formaction} formEncType={this.formEncType} formenctype={this.formenctype} formMethod={this.formMethod} formmethod={this.formmethod} formNoValidate={this.formNoValidate} formnovalidate={this.formnovalidate} formTarget={this.formTarget} formtarget={this.formtarget} name={this.name} type={this.type} value={this.value} >
<slot />
</button>
);
}
}compared to something like this:
import { Component, h, Prop, Isa } from '@stencil/core';
@Component({
tag: 'my-button',
styleUrl: 'my-button.scss',
shadow: true
})
export class Button {
@Isa() button;
render() {
return (
<button {...this.injectedProps} >
<slot />
</button>
);
}
}Other information:
Reacted by Csaba Benkő, LifeIsStrange and jptcnde
Metadata
Metadata
Assignees
Labels
Feature: Want this? Upvote it!This PR or Issue may be a great consideration for a future idea.
Activity
cjorasch commentedon Jul 7, 2020
One approach that I have sometimes used when wrapping other components is to use a Functional Component as the wrapper. This allows you to extend or alter all of the properties and to customize the rendering while supporting all properties.
This works well for simple component extensions but is limited in terms or additional state, custom css, etc. This may or may not work for your use case but it is sometimes useful.
CamJN commentedon Jul 7, 2020
Yes, I’m aware of functional components and in my case state is needed so that won’t work.
max-scopp commentedon Jul 26, 2020
Afaik, you get that already from the typescript typings?
Otherwise, in non-typescript projects, you can use
@Watchto validate each new prop. And if that is at mount-timenullorundefined, you know it's not supplied yet and you can throw an error.CamJN commentedon Jul 26, 2020
@max-scopp can you clarify how you would do this with typescript? I tried a few things already but nothing worked. Perhaps I missed something.
max-scopp commentedon Jul 27, 2020
So first I'd like to clarify two things:
@Watchcode for vanilla usage without any typesOtherwise, all props are by default optional, but you can enforce required props using the Non-null assertion operator, which intern will make the prop required.
In the image below you'll see the source left of my button, on the right top the resulting types for your consumers and on the bottom right the correct error-case when the required props are not defined.
The error isn't really sounding helpful, but it will correctly reflect the types. The downside: You need to figure out yourself what kind of props are missing, the compiler won't specifically tell you due to the way the LocalJSX types are structured.
CamJN commentedon Jul 28, 2020
@max-scopp I think, based on your clarification, that you got what I wanted backwards. I want the props to be added to the component definition automatically not enforced/added at the point of use. I know how to ensure props are passed at the point of use. I just don't want to have to manually list all of the extra props in the class definition if I'm just impersonating a specific html element.
bitflower commentedon Aug 6, 2020
Would have a generator be an option for you? Like a script that would auto-generate the code for the props. Just thinkin' out loud as I had these use cases in the past too and wondered if that would be a good idea.
CamJN commentedon Aug 6, 2020
A generator would be better than nothing, but it would leave a functionality gap in the case where the element type gets another attribute added after you run the generator (due to incomplete typescript info for jsx or whatever) then you wouldn't get that attribute picked up on next compile, you'd have to keep checking for changes and make them manually.
max-scopp commentedon Aug 7, 2020
OH, I think I finally understand. You want an
<my-button/>that's exactly like an<button />but don't want to explictily tell stencil that<my-button />has the same interface and logic as<button />.I'm afraid apart from the half-baked solutions this is not possible. You can help yourself by using
implementsand let the IDE to it's magic so you don't have to type it. But you still have to explicitly have the code and@Prop's for it.There is no way around. Manu wrote a big thread on Slack why that's (currently) not doable. You basically want the
extendskeyword to work in that case, right?Anyways, even though I forgot why, this is known and a ton of people already asked for this. They'll try to make it work, but it's not too important when you account the time needed to make this work properly and play nicely.
Up for a challange? ;)
CamJN commentedon Aug 7, 2020
@max-scopp I was pretty sure it wasn't currently possible, that's why I marked this as a feature request.
johnjenkins commentedon Jun 3, 2021
I think my
@MixinPR would go some way to help with this (#2921) < The whole point of it is to reduce boilerplate.When scoping out and writing tests, one of the main use-cases I had in-mind is 3rd party / community libs too; ie maybe someone will write a bunch of standard prop mixins for common components. Users can then overwrite anything they like or use
OmitandPickif they don't want everything.Let me know if you think it's a good fit and I'll link this issue