Skip to content

Instantly share code, notes, and snippets.

@AlejandroPerezMartin
Last active April 11, 2023 15:18
Show Gist options
  • Select an option

  • Save AlejandroPerezMartin/ecd014cb8104c235b582f3a3e1649cf7 to your computer and use it in GitHub Desktop.

Select an option

Save AlejandroPerezMartin/ecd014cb8104c235b582f3a3e1649cf7 to your computer and use it in GitHub Desktop.
Angular 9+ Directive to remove focus after clicking the specified selector/s
import { Directive, HostListener, ElementRef } from '@angular/core';
/**
* This directive removes focus from the selectors after clicking on them
*/
@Directive({
selector: 'button, a' // your selectors here!
})
export class FocusRemover {
constructor(private elRef: ElementRef) {}
@HostListener('click') onClick() {
this.elRef.nativeElement.blur();
}
}
@gforcedev
Copy link
Copy Markdown

Thanks so much this worked fantastic in my app!

@AlejandroPerezMartin
Copy link
Copy Markdown
Author

AlejandroPerezMartin commented Jun 12, 2018

@gforcedev 😃 👍

@AntonVoronezh
Copy link
Copy Markdown

i think this is a good solution

@vitaliidasaev
Copy link
Copy Markdown

For Angular 9 and upper used blur() because Renderer is outdated.

import {Directive, HostListener, ElementRef} from '@angular/core';

/**
 * This directive removes focus from the selectors after clicking on them
 */
@Directive({
  selector: 'button, a', // your selectors here!
})
export class FocusRemover {
  constructor(private elRef: ElementRef) {}

  @HostListener('click') onClick() {
    this.elRef.nativeElement.blur();
  }
}

@AlejandroPerezMartin
Copy link
Copy Markdown
Author

updated gist, thanks @vitaliidasaev

@AbhinavAtul
Copy link
Copy Markdown

AbhinavAtul commented Oct 31, 2020

@AlejandroPerezMartin
Copy link
Copy Markdown
Author

no license @AbhinavAtul, use it the way you want! it's a really simple script so I don't think it's needed

@carlabruti
Copy link
Copy Markdown

Works great, thanks !

@Tamal2017
Copy link
Copy Markdown

Tamal2017 commented Mar 10, 2021

Please @AlejandroPerezMartin how am I going to use this Directive?
Can I implement it for Angular 8?

@AlejandroPerezMartin
Copy link
Copy Markdown
Author

Please @AlejandroPerezMartin how am I going to use this Directive?
Can I implement it for Angular 8?

I'm not using Angular anymore so I'm not sure, but I believe you can use a previous version of this gist: https://gist.github.com/AlejandroPerezMartin/ecd014cb8104c235b582f3a3e1649cf7/0bff5561689e5cbbfa878fab3bc2ec716e58eb58

@Tamal2017
Copy link
Copy Markdown

ok thanks

@ElenaG518
Copy link
Copy Markdown

very nice!

@mrtabaa
Copy link
Copy Markdown

mrtabaa commented Mar 13, 2022

In my case, it has to be wrapped with setTimout() like this otherwise the timing messes up with it to function properly:

@HostListener('click')
onClick() {
setTimeout(() => this.elRef.nativeElement.blur());
}

@m4n50n
Copy link
Copy Markdown

m4n50n commented Apr 11, 2023

Thanks👌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment