forked from DSpace/dspace-angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
116404: Collapse the expandable menu when hovering outside of it
- Loading branch information
1 parent
d494a84
commit c6caf7e
Showing
4 changed files
with
112 additions
and
12 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { | ||
Directive, | ||
ElementRef, | ||
EventEmitter, | ||
HostListener, | ||
Output, | ||
} from '@angular/core'; | ||
|
||
/** | ||
* Directive to detect when the user hovers outside of the element the directive was put on | ||
* | ||
* BEWARE: it's probably not good for performance to use this excessively (on {@link ExpandableNavbarSectionComponent} | ||
* for example, a workaround for this problem was to add an `*ngIf` to prevent this Directive from always being active) | ||
*/ | ||
@Directive({ | ||
selector: '[dsHoverOutside]', | ||
standalone: true, | ||
}) | ||
export class HoverOutsideDirective { | ||
/** | ||
* Emits null when the user hovers outside of the element | ||
*/ | ||
@Output() | ||
public dsHoverOutside = new EventEmitter(); | ||
|
||
constructor(private _elementRef: ElementRef) {} | ||
|
||
@HostListener('document:mouseover', ['$event']) | ||
public onMouseOver(event: MouseEvent) { | ||
const hostElement = this._elementRef.nativeElement; | ||
const targetElement = event.target as HTMLElement; | ||
const hoveredInside = hostElement.contains(targetElement); | ||
|
||
if (!hoveredInside) { | ||
this.dsHoverOutside.emit(null); | ||
} | ||
} | ||
} |
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