Binding to Element Size
Basic implementation
import { bindable, BindingMode, customAttribute } from 'aurelia';
@customAttribute({
name: 'rectsize',
hasDynamicOptions: true
})
export class RectSize {
public static inject = [INode];
@bindable({ mode: BindingMode.fromView })
public value!: ResizeObserverSize;
@bindable()
public boxSize!: 'border-box' | 'content-box';
private observer!: ResizeObserver;
constructor(
private readonly element: HTMLElement
) {
}
public binding(): void {
let observer = this.observer;
if (observer === void 0) {
observer = this.observer = this.createObserver();
}
observer.observe(this.element, { box: 'border-box' });
}
public unbinding(): void {
this.observer.disconnect();
this.observer = (void 0)!;
}
private createObserver(): ResizeObserver {
return new ResizeObserver((entries) => {
this.handleResize(entries[0]);
});
}
/**
* @internal
*/
private handleResize(entry: ResizeObserverEntry): void {
this.value = this.boxSize === 'content-box' ? entry.contentBoxSize : entry.borderBoxSize;
}
}Working with polyfill
Example usage
Special note:
Last updated
Was this helpful?