coreui-angular-ex-dev
Version:
CoreUI Components Library for Angular
77 lines (65 loc) • 2.01 kB
text/typescript
import { booleanAttribute, Directive, ElementRef, HostBinding, Input, OnDestroy, OnInit } from '@angular/core';
import { Subscription } from 'rxjs';
import { DropdownService } from '../dropdown.service';
export class DropdownMenuDirective implements OnInit, OnDestroy {
constructor(
public elementRef: ElementRef,
private dropdownService: DropdownService
) {}
/**
* Set alignment of dropdown menu.
* @type {'start' | 'end' }
*/
alignment?: 'start' | 'end' | string;
/**
* Toggle the visibility of dropdown menu component.
*/
visible = false;
/**
* Sets a darker color scheme to match a dark navbar.
* @type boolean
*/
dark: string | boolean = false;
private dropdownStateSubscription!: Subscription;
get hostClasses(): any {
return {
'dropdown-menu': true,
'dropdown-menu-dark': this.dark,
[`dropdown-menu-${this.alignment}`]: !!this.alignment,
show: this.visible
};
}
get hostStyles() {
// workaround for popper position calculate (see also: dropdown.component)
return {
visibility: this.visible ? null : '',
display: this.visible ? null : ''
};
}
ngOnInit(): void {
this.dropdownStateSubscribe();
}
ngOnDestroy(): void {
this.dropdownStateSubscribe(false);
}
private dropdownStateSubscribe(subscribe: boolean = true): void {
if (subscribe) {
this.dropdownStateSubscription =
this.dropdownService.dropdownState$.subscribe((state) => {
if ('visible' in state) {
this.visible =
state.visible === 'toggle' ? !this.visible : state.visible;
}
});
} else {
this.dropdownStateSubscription?.unsubscribe();
}
}
}