File

src/app/shared/components/drawer/toggle-button/toggle-button.component.ts

Description

Implements open/close button for the side drawers.

Implements

AfterViewInit OnDestroy

Metadata

Index

Properties
Methods
HostBindings
Accessors

Constructor

constructor(drawer: DrawerComponent, messageService: MessageService, cdr: ChangeDetectorRef)

Creates an instance of toggle button component.

Parameters :
Name Type Optional Description
drawer DrawerComponent No

The owning side drawer.

messageService MessageService No

Service used to send and receive event messages.

cdr ChangeDetectorRef No

The change detector reference.

HostBindings

class
Type : "ccf-drawer-toggle-button"
Default value : 'ccf-drawer-toggle-button'

HTML class

class.ccf-drawer-toggle-button-end
Type : boolean

Whether this button is attach to a drawer in position 'end'.

Methods

handleMessage
handleMessage(msg: Message)

Process an event message.

Parameters :
Name Type Optional Description
msg Message No

The event.

Returns : boolean

true if change detection needs to be run.

toggle
toggle()

Updates the drawer state.

Returns : void

Properties

Readonly className
Type : string
Default value : 'ccf-drawer-toggle-button'
Decorators :
@HostBinding('class')

HTML class

Accessors

classEnd
getclassEnd()

Whether this button is attach to a drawer in position 'end'.

Returns : boolean
icon
geticon()

Gets the name of the icon to display.

Returns : string
import {
  AfterViewInit,
  ChangeDetectionStrategy,
  ChangeDetectorRef,
  Component,
  HostBinding,
  OnDestroy,
} from '@angular/core';
import { Subscription } from 'rxjs';

import { DrawerComponent } from '../drawer/drawer.component';
import { Message, MessageService } from '../messages';

/**
 * Implements open/close button for the side drawers.
 */
@Component({
  selector: 'ccf-drawer-toggle-button',
  templateUrl: './toggle-button.component.html',
  styleUrls: ['./toggle-button.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ToggleButtonComponent implements AfterViewInit, OnDestroy {
  /** HTML class */
  @HostBinding('class') readonly className = 'ccf-drawer-toggle-button';
  /** Whether this button is attach to a drawer in position 'end'. */
  @HostBinding('class.ccf-drawer-toggle-button-end')
  get classEnd(): boolean {
    return this.position === 'end';
  }

  /** Gets the name of the icon to display. */
  get icon(): string {
    let expand = 'arrow_right';
    let collapse = 'arrow_left';
    if (this.position === 'end') {
      [expand, collapse] = [collapse, expand];
    }

    return this.opened ? collapse : expand;
  }

  /** Position of the owning side drawer. */
  private position: 'start' | 'end' = 'start';
  /** Whether the owning drawer is opened. */
  private opened = false;
  /** Subscriptions managed by this component. */
  private readonly subscriptions = new Subscription();

  /**
   * Creates an instance of toggle button component.
   *
   * @param drawer The owning side drawer.
   * @param messageService Service used to send and receive event messages.
   * @param cdr The change detector reference.
   */
  constructor(
    private readonly drawer: DrawerComponent,
    messageService: MessageService,
    private readonly cdr: ChangeDetectorRef,
  ) {
    const channel = messageService.connect(this);
    this.subscriptions.add(
      channel.getMessagesFromSource(drawer).subscribe((msg) => {
        if (this.handleMessage(msg)) {
          cdr.markForCheck();
        }
      }),
    );
  }

  /**
   * Initializes this component.
   */
  ngAfterViewInit(): void {
    setTimeout(() => {
      this.position = this.drawer.position;
      this.cdr.markForCheck();
    });
  }

  /**
   * Cleans up all subscriptions.
   */
  ngOnDestroy(): void {
    this.subscriptions.unsubscribe();
  }

  /**
   * Process an event message.
   *
   * @param msg The event.
   * @returns true if change detection needs to be run.
   */
  handleMessage(msg: Message): boolean {
    if (msg.payload.type === 'drawer-toggled') {
      this.opened = msg.payload.opened;
      return true;
    }

    return false;
  }

  /**
   * Updates the drawer state.
   */
  toggle(): void {
    const drawer = this.drawer;
    const { opened, expanded } = drawer;
    if (opened) {
      if (expanded) {
        drawer.closeExpanded();
      } else {
        drawer.close();
      }
    } else {
      drawer.open();
    }
  }
}
<mat-icon
  [class.hidden]="icon === 'arrow_left'"
  class="expand-collapse-icon"
  aria-hidden="false"
  aria-label="Close side drawer"
  (click)="toggle()"
>
  arrow_right
</mat-icon>
<mat-icon
  [class.hidden]="icon === 'arrow_right'"
  class="expand-collapse-icon"
  aria-hidden="false"
  aria-label="Close side drawer"
  (click)="toggle()"
>
  arrow_left
</mat-icon>

./toggle-button.component.scss

:host {
  position: absolute;
  top: calc(50% - 2.0625rem);
  right: -1.0625rem;
  width: 1.0625rem;
  height: 3rem;
  border-radius: 0rem 0.25rem 0.25rem 0rem;
  cursor: pointer;
  transition: 0.25s;

  &.ccf-drawer-toggle-button-end {
    right: unset;
    left: calc(-1rem - 1px);
    border-radius: 0.25rem 0rem 0rem 0.25rem;
  }

  .expand-collapse-icon {
    position: absolute;
    right: 0rem;
    transition: 0.6s;
    padding-left: 0.15rem;

    &.hidden {
      opacity: 0;
    }
  }

  &.ccf-drawer-toggle-button-end .expand-collapse-icon {
    right: unset;
    left: -0.25rem;
  }
}

.mat-icon {
  right: 0.3rem;
  top: 0.675rem;
  padding-top: 0.05rem;
}
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""