Skip to content
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

Add TypeScript decorators #7

Closed
justinfagnani opened this issue Jan 19, 2018 · 12 comments
Closed

Add TypeScript decorators #7

justinfagnani opened this issue Jan 19, 2018 · 12 comments

Comments

@justinfagnani
Copy link
Contributor

Decorators similar to those in https://github.com/Polymer/polymer-decorators should work well with this base class. They can transform fields into accessors as described in #6.

@kenchris
Copy link
Contributor

Do you mind if I take this one?

@justinfagnani
Copy link
Contributor Author

@kenchris sounds great!

@sorvell
Copy link
Member

sorvell commented Jun 8, 2018

@kenchris still interested in tackling this?

@kenchris
Copy link
Contributor

kenchris commented Jun 8, 2018

Yeah, sure

@justinfagnani
Copy link
Contributor Author

I have some decorators adapted from the Polymer 2 decorators here: https://stackblitz.com/edit/lit-element-typescript

@pshihn
Copy link

pshihn commented Jun 13, 2018

I know you guys are working on this. If it's any help, I have most of this in a base class (extended from LitElement) - I've been using in some projects. I was using it often enough to carve it out into a separate package: https://github.com/pshihn/alit-element

It doesn't match the exact spec of polymer's decorators, but is very close:

@element('alit-card')
export class AlitCard extends AlitElement {
  @property() name?: string;
  @property() age: number = 30;
  @property() description: string = 'This is the default description';
  
  @query('.card')
  card?: HTMLDivElement;

  @listen('click', '#toggleButton')
  toggleBorder() {
    this.borderShowing = !this.borderShowing;
    this.card.style.border = this.borderShowing ? '2px solid' : 'none';
  }
  
  @observe('age', 'description')
  ageChanged(records: ChangeRecord[]) {
    // do stuff when age or description changes
  }
}

Decorator source

@kenchris
Copy link
Contributor

@property has been added in the latest API refactor - I don't know if we want to add more for now @sorvell ?

@MaKleSoft
Copy link

I've been using a customized version of @pshihn's decorators and I have to say, I find the @listen and @query decorators tremendously helpful, @listen because it removes a lot of boilerplate and @query because it allows for type safety when used with Typescript. @element and @observe, while not as important for me personally, do have their uses as well. Overall, these decorators along with a couple of helper functions have made my code a lot cleaner and more readable. Maybe these don't need to be part of the core but I do recommend fully embracing decorators since they simply make for a much more pleasant dev experience.

@aadamsx
Copy link

aadamsx commented Sep 9, 2018

@Property has been added in the latest API refactor

That's great @kenchris, is Babel7 support on the horizon too? I ran into an issue with Bable7 and decorators here: lit/lit#478 (comment)

@aadamsx
Copy link

aadamsx commented Sep 10, 2018

I don't know if we want to add more [decorators]

Also, @kenchris which decorators are being considered? Will they be close to what @pshihn has posted above? This syntax looks great by the way.

@Christian24
Copy link
Contributor

@aadamsx As I see it now the following decorators are currently available:

  • @customElement
  • @property
  • @query
  • @queryAll
  • @eventOptions

However, I would love to see @listen too. @kenchris what do you think? Would you be open to a pull request?

keanulee pushed a commit that referenced this issue Nov 13, 2018
Tools content and some loose ends
@sorvell
Copy link
Member

sorvell commented Dec 10, 2018

Closing this as a set of decorators has been added. We'll accept separate, new issues for discussion of any new decorators.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants