Skip to content

Instantly share code, notes, and snippets.

@razvanstatescu
Created December 26, 2020 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save razvanstatescu/6fc6fb889b8a54461de04a86067673a1 to your computer and use it in GitHub Desktop.
Save razvanstatescu/6fc6fb889b8a54461de04a86067673a1 to your computer and use it in GitHub Desktop.
An Angular directive that adjust input width based on the content size.
import { Directive, ElementRef, HostListener } from '@angular/core';
@Directive({
selector: '[dynamic-input]',
})
export class DynamicInputDirective {
constructor(private el: ElementRef) {
this.resize();
}
@HostListener('keyup') onKeyUp() {
this.resize();
}
private resize() {
this.el.nativeElement.setAttribute(
'size',
this.el.nativeElement.value.length || 2
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment