src/app/shared/components/drawer/toggle-button/toggle-button.component.ts
Implements open/close button for the side drawers.
AfterViewInit
OnDestroy
changeDetection | ChangeDetectionStrategy.OnPush |
selector | ccf-drawer-toggle-button |
styleUrls | ./toggle-button.component.scss |
templateUrl | ./toggle-button.component.html |
Properties |
|
Methods |
HostBindings |
Accessors |
constructor(drawer: DrawerComponent, messageService: MessageService, cdr: ChangeDetectorRef)
|
||||||||||||||||
Creates an instance of toggle button component.
Parameters :
|
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'. |
handleMessage | ||||||||
handleMessage(msg: Message)
|
||||||||
Process an event message.
Parameters :
Returns :
boolean
true if change detection needs to be run. |
toggle |
toggle()
|
Updates the drawer state.
Returns :
void
|
Readonly className |
Type : string
|
Default value : 'ccf-drawer-toggle-button'
|
Decorators :
@HostBinding('class')
|
HTML class |
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') // eslint-disable-line
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="expand-collapse-icon" aria-hidden="false" aria-label="Close side drawer" (click)="toggle()">
{{ icon }}
</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;
display: flex;
align-items: center;
transition: 0.6s;
&.ccf-drawer-toggle-button-end {
right: unset;
left: calc(-1rem - 1px);
border-radius: 0.25rem 0rem 0rem 0.25rem;
}
.expand-collapse-icon {
position: relative;
right: 0.25rem;
transition: all 0.5s;
}
&.ccf-drawer-toggle-button-end .expand-collapse-icon {
right: unset;
left: -0.25rem;
}
}