Writing Reusable Angular code

Consider we have 2 (or more) components that have the same look and feel but technically have a totally different purpose. 

We end up creating 2 different components for this with a lot of repeated code.

Example:

Our application's UI elements use the following:

  • 2 types of color: primary, secondary
  • 2 types of appearances: solid, stroked
Solid ChipStroked Chip

We have 2 types of components here:

  • CustomButtonComponent
  • ChipComponent

Both of these components could have the same code that takes the color and appearance as the Input() to assign the appropriate class to it

instead of doing this for each of the component, we could create a BaseComponent that does this job for us. This could then be inherited by the CustomButtonComponent and ChipComponent

base.component.ts

import { DirectiveHostBindingInput } from '@angular/core';

@Directive()
export class BaseComponent {
  @Input() appearance'solid' | 'stroked' = 'solid';
  @Input() color'primary' | 'secondary' = 'primary';
  @HostBinding('class'protected get classNames() {
    return {
      [this.appearance]: true,
      [this.color]: true,
    };
  }
}


custom-chip.component.ts

import { Component } from '@angular/core';
import { BaseComponent } from '../base.component';

@Component({
  selector: 'custom-chip',
  template: '<span><ng-content></ng-content></span>',
  standalone: true,
  styles: [
    `
    :host{
      display: inline-flex;
      border-radius: 10px;
      padding: 7px;
      justify-content: center;
    }
  `,
  ],
})
export class CustomChip extends BaseComponent {}


custom-button.component.ts

import { Component } from '@angular/core';
import { BaseComponent } from '../base.component';

@Component({
  selector: 'custom-button, button[customBtn]',
  template: '<span><ng-content></ng-content></span>',
  standalone: true,
  styles: [
    `
    :host {
      display: inline-flex;
      border-radius: 2px;
      padding: 7px;
      justify-content: center;
    }
  `,
  ],
})
export class CustomButton extends BaseComponent {}

app.component.ts

import { Component } from '@angular/core';
import { CustomButton } from './ui-components/custom-button.component';
import { CustomChip } from './ui-components/custom-chip.component';

@Component({
  selector: 'app-root',
  standalone: true,
  template: `
  <custom-chip [appearance]="'solid'" [color]="'secondary'">Solid Chip</custom-chip>
  <custom-chip [appearance]="'stroked'" [color]="'primary'">Stroked Chip</custom-chip>

  <button customBtn [appearance]="'stroked'" [color]="'secondary'">Stroked Button</button>
  <button customBtn [appearance]="'solid'" [color]="'secondary'">Solid Button</button>
  `,
  imports: [CustomButtonCustomChip],
})
export class AppComponent {}

Stackblitz















































Comments

Popular posts from this blog

Inside the JavaScript Memory Box: Visualizing Variables, References, and Copies

React & State Management: All Concepts

React: Communication between components