@deepkit/desktop-ui
Version:
Library for desktop UI widgets in Angular 10+
54 lines (46 loc) • 1.53 kB
text/typescript
/*
* Deepkit Framework
* Copyright (C) 2021 Deepkit UG, Marc J. Schmidt
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the MIT License.
*
* You should have received a copy of the MIT License along with this program.
*/
import { Directive, ElementRef, HostListener, Input, OnInit } from '@angular/core';
import { nextTick } from '@deepkit/core';
({
selector: '[duiClassMin]',
standalone: false,
})
export class DuiResponsiveDirective implements OnInit {
clazz: { [className: string]: boolean } = {};
protected lastRequest: any;
() duiClassMin: { [className: string]: number } = {};
constructor(
private element: ElementRef,
) { }
ngOnInit() {
this.onResize();
}
('window:resize')
onResize() {
if (this.lastRequest) {
cancelAnimationFrame(this.lastRequest);
}
this.lastRequest = nextTick(() => {
const element: HTMLElement = this.element.nativeElement;
for (const [name, number] of Object.entries(this.duiClassMin)) {
const valid = element.offsetWidth > number;
if (this.clazz[name] !== valid) {
this.clazz[name] = valid;
if (valid) {
element.classList.add(name);
} else {
element.classList.remove(name);
}
}
}
});
}
}