forked from arkon/ng-click-outside
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: seperate exclude function into own directive to make ng-click-o…
…utisde better treeshakable when exclude is not needed (#41) Co-authored-by: Manuel Schmidt <[email protected]>
- Loading branch information
Showing
8 changed files
with
134 additions
and
55 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
58 changes: 58 additions & 0 deletions
58
projects/ng-click-outside2/src/lib/ng-click-outside-exclude.directive.spec.ts
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,58 @@ | ||
import { NgClickOutsideExcludeDirective } from './ng-click-outside-exclude.directive'; | ||
import {Component, ViewChild} from "@angular/core"; | ||
import {NgClickOutsideDirective} from "./ng-click-outside.directive"; | ||
import {ComponentFixture, TestBed} from "@angular/core/testing"; | ||
import {DOCUMENT} from "@angular/common"; | ||
import {By} from "@angular/platform-browser"; | ||
|
||
|
||
@Component({ | ||
// eslint-disable-next-line @angular-eslint/component-selector | ||
selector: 'test-click', | ||
standalone: true, | ||
imports: [NgClickOutsideDirective, NgClickOutsideExcludeDirective], | ||
template: ` | ||
<button id="b-1" (click)="clickButton1 = clickButton1 + 1"></button> | ||
<button id="b-2" (clickOutside)="clickOutsideButton2 = clickOutsideButton2 + 1" [clickOutsideExclude]="'.no-outside-click'" | ||
(click)="clickButton2 = clickButton2 + 1" [clickOutsideEnabled]="enabled" | ||
></button> | ||
<button id="b-3" (click)="clickButton3 = clickButton3 + 1" class="no-outside-click"></button> | ||
` | ||
}) | ||
class TestClickOutsideComponent { | ||
@ViewChild(NgClickOutsideDirective) ngClickOutsideDirective?: NgClickOutsideDirective | ||
clickOutsideButton2 = 0; | ||
clickButton1 = 0; | ||
clickButton2 = 0; | ||
clickButton3 = 0; | ||
enabled = true | ||
} | ||
|
||
describe('NgClickOutsideExcludeDirective', () => { | ||
let component: TestClickOutsideComponent; | ||
let fixture: ComponentFixture<TestClickOutsideComponent>; | ||
|
||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
imports: [TestClickOutsideComponent], | ||
providers: [{provider: DOCUMENT, useValue: document}] | ||
}).compileComponents(); | ||
|
||
fixture = TestBed.createComponent(TestClickOutsideComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}) | ||
|
||
|
||
it('should excludeDirective should be defined when attribute exclude exists ', () => { | ||
expect(component.ngClickOutsideDirective!.excludeDirective).toBeDefined(); | ||
}); | ||
|
||
it('should not count click of Button 3 as it is excluded', () => { | ||
const button3 = fixture.debugElement.query(By.css('#b-3')); | ||
button3.nativeElement.click(); | ||
expect(component.clickButton3).toBe(1); | ||
expect(component.clickButton2).toBe(0); | ||
expect(component.clickOutsideButton2).toBe(0); }); | ||
|
||
}); |
52 changes: 52 additions & 0 deletions
52
projects/ng-click-outside2/src/lib/ng-click-outside-exclude.directive.ts
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,52 @@ | ||
import {Directive, inject, Inject, Input} from '@angular/core'; | ||
import {DOCUMENT} from "@angular/common"; | ||
|
||
/** | ||
* Optimization for Treeshaking: https://angular.io/guide/lightweight-injection-tokens | ||
*/ | ||
export abstract class NgClickOutsideExcludeToken { | ||
abstract isExclude(target: any): boolean; | ||
} | ||
|
||
@Directive({ | ||
selector: '[clickOutsideExclude]', | ||
standalone: true, | ||
providers: [ | ||
{provide: NgClickOutsideExcludeToken, useExisting: NgClickOutsideExcludeDirective} | ||
] | ||
}) | ||
export class NgClickOutsideExcludeDirective extends NgClickOutsideExcludeToken { | ||
|
||
/** | ||
* A comma-separated string of DOM element queries to exclude when clicking outside of the element. | ||
* For example: `[exclude]="'button,.btn-primary'"`. | ||
*/ | ||
@Input() clickOutsideExclude = ''; | ||
|
||
private document: Document = inject(DOCUMENT); | ||
|
||
public excludeCheck(): HTMLElement[] { | ||
if (this.clickOutsideExclude) { | ||
try { | ||
const nodes = Array.from(this.document.querySelectorAll(this.clickOutsideExclude)) as Array<HTMLElement>; | ||
if (nodes) { | ||
return nodes; | ||
} | ||
} catch (err) { | ||
console.error('[ng-click-outside] Check your exclude selector syntax.', err); | ||
} | ||
} | ||
return []; | ||
} | ||
|
||
public isExclude(target: any): boolean { | ||
const nodesExcluded = this.excludeCheck(); | ||
for (let excludedNode of nodesExcluded) { | ||
if (excludedNode.contains(target)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
*/ | ||
|
||
export * from './lib/ng-click-outside.directive'; | ||
export * from './lib/ng-click-outside-exclude.directive'; |