Last active
March 23, 2024 09:12
-
-
Save kerren/71c67335d328ae4796f8d1a6e7b3277d to your computer and use it in GitHub Desktop.
A directive that allows you to see if an input is in focus or not. See my blog article here https://blog.entrostat.com/angular-inputs-improved-error-ux/ and see StackOverflow article that inspired this here https://stackoverflow.com/questions/57536298/how-to-check-if-an-input-field-is-in-focus-in-angular-7/57536348#57536348
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Directive, HostListener, signal } from '@angular/core'; | |
@Directive({ | |
selector: '[appTrackFocus]', | |
standalone: true, | |
exportAs: 'isFocused', | |
}) | |
export class TrackFocusDirective { | |
isFocused = signal(false); | |
@HostListener('focus', ['$event']) onFocus() { | |
this.isFocused.set(true); | |
} | |
@HostListener('blur', ['$event']) onblur() { | |
this.isFocused.set(false); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Check out https://blog.entrostat.com/angular-inputs-improved-error-ux/ to see how to bind to this in your inputs and change their styles based on the
isFocused
signal.