File

src/app/features/slide-toggle/slide-toggle/slide-toggle.component.ts

Metadata

Index

Properties
Methods
Inputs
Outputs
HostBindings
Accessors

Inputs

disabled
Type : boolean
Default value : false

Whether or not the slider is disabled

labels
Type : [string, string]
Default value : ['Left', 'Right']

The two selection options to be toggled

value
Type : string
Default value : 'Left'

Input value for toggle slider

Outputs

valueChange
Type : EventEmitter

Emits the datatype with the currently selected option

HostBindings

class
Type : "ccf-slide-toggle"
Default value : 'ccf-slide-toggle'

HTML class name

Methods

updateToggle
updateToggle(selection: boolean)

Updates and emits the currently selected option

Parameters :
Name Type Optional Description
selection boolean No

The current toggle state (true=left, false=right)

Returns : void

Properties

Readonly clsName
Type : string
Default value : 'ccf-slide-toggle'
Decorators :
@HostBinding('class')

HTML class name

Accessors

left
getleft()

Determines if left toggle option is selected

Returns : boolean
import { ChangeDetectionStrategy, Component, EventEmitter, HostBinding, Input, Output } from '@angular/core';

@Component({
  selector: 'ccf-slide-toggle',
  templateUrl: './slide-toggle.component.html',
  styleUrls: ['./slide-toggle.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class SlideToggleComponent {
  /**
   * HTML class name
   */
  @HostBinding('class') readonly clsName = 'ccf-slide-toggle';

  /**
   * The two selection options to be toggled
   */
  @Input() labels: [string, string] = ['Left', 'Right'];

  /**
   * Input value for toggle slider
   */
  @Input() value = 'Left';

  /**
   * Whether or not the slider is disabled
   */
  @Input() disabled = false;

  /**
   * Emits the datatype with the currently selected option
   */
  @Output() readonly valueChange = new EventEmitter<string>();

  /**
   * Determines if left toggle option is selected
   */
  get left(): boolean {
    const { value, labels } = this;
    return value !== labels[1];
  }

  /**
   * Updates and emits the currently selected option
   *
   * @param selection The current toggle state (true=left, false=right)
   */
  updateToggle(selection: boolean): void {
    this.value = selection ? this.labels[0] : this.labels[1];
    this.valueChange.emit(this.value);
  }
}
<span
  class="slide-label"
  [class.disabled]="disabled"
  [class.highlighted]="left"
  (click)="left ? '' : toggle.toggle(); updateToggle(true)"
>
  {{ labels[0].charAt(0).toUpperCase() + labels[0].slice(1) }}
</span>
<mat-slide-toggle
  [disabled]="disabled"
  [checked]="!left"
  hideIcon
  (change)="updateToggle(!$event.checked)"
  class="slider"
  #toggle
>
</mat-slide-toggle>
<span
  class="slide-label"
  [class.disabled]="disabled"
  [class.highlighted]="!left"
  (click)="!left ? '' : toggle.toggle(); updateToggle(false)"
>
  {{ labels[1].charAt(0).toUpperCase() + labels[1].slice(1) }}
</span>

./slide-toggle.component.scss

:host {
  .slide-label {
    margin: 0 1rem;
    transition: color 0.6s;
    cursor: pointer;
    opacity: 50%;

    &.highlighted {
      opacity: 100%;
    }
  }

  .disabled {
    opacity: 30% !important;
    cursor: not-allowed;
  }

  ::ng-deep .slider {
    .mdc-switch__track::before,
    .mdc-switch__track::after {
      background-color: rgba(67, 71, 101, 0.5) !important;
    }
    .mdc-switch__shadow {
      background-color: rgba(67, 71, 101);
    }
  }
}
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""