Skip to content

Add props at compile time #2559

Closed
Closed

Description

@CamJN

Stencil version:

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:

Activity

  1. cjorasch commented on Jul 7, 2020

    @cjorasch

    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.

    export function MyButton(props: JSX.IntrinsicElements['ion-button']) {
      return <ion-button fill="solid" {...props} />
    }

    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.

  2. CamJN commented on Jul 7, 2020

    @CamJN
    Author

    Yes, I’m aware of functional components and in my case state is needed so that won’t work.

  3. max-scopp commented on Jul 26, 2020

    @max-scopp

    Afaik, you get that already from the typescript typings?

    Otherwise, in non-typescript projects, you can use @Watch to validate each new prop. And if that is at mount-time null or undefined, you know it's not supplied yet and you can throw an error.

  4. CamJN commented on Jul 26, 2020

    @CamJN
    Author

    @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.

  5. max-scopp commented on Jul 27, 2020

    @max-scopp

    So first I'd like to clarify two things:

    • If you're running your components in an parent Project, like React, those props are reflected from the wrapper, which may generate the types a bit differently
    • If you're using your web-components in some sort of HTML, the props are per-spec all optional and are reflected by the Custom Data Format <- this would require custom @Watch code for vanilla usage without any types

    Otherwise, 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.

    image

  6. CamJN commented on Jul 28, 2020

    @CamJN
    Author

    @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.

  7. bitflower commented on Aug 6, 2020

    @bitflower
    Contributor

    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.

  8. CamJN commented on Aug 6, 2020

    @CamJN
    Author

    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.

  9. max-scopp commented on Aug 7, 2020

    @max-scopp

    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 implements and 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 extends keyword 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? ;)

  10. CamJN commented on Aug 7, 2020

    @CamJN
    Author

    @max-scopp I was pretty sure it wasn't currently possible, that's why I marked this as a feature request.

  11. johnjenkins commented on Jun 3, 2021

    @johnjenkins
    Contributor

    I think my @Mixin PR 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 Omit and Pick if they don't want everything.

    Let me know if you think it's a good fit and I'll link this issue

  12. added
    Feature: Want this? Upvote it!
    on Feb 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Feature: Want this? Upvote it!

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions