Skip to content

Commit ec25af9

Browse files
authored
feat: emit action before directive destroy
chore: extend peer deps to include angular v17 Refs: #1418 Release-As: 16.1.0
1 parent 1565794 commit ec25af9

File tree

7 files changed

+57
-12
lines changed

7 files changed

+57
-12
lines changed

.all-contributorsrc

+12-2
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,23 @@
144144
"login": "karptonite",
145145
"name": "Daniel Karp",
146146
"avatar_url": "https://avatars.githubusercontent.com/u/132278?v=4",
147-
"profile": "https://twitter.com/karptonite",
147+
"profile": "https://github.com/karptonite",
148148
"contributions": [
149149
"bug"
150150
]
151+
},
152+
{
153+
"login": "tinesoft",
154+
"name": "Tine Kondo",
155+
"avatar_url": "https://avatars.githubusercontent.com/u/4053092?v=4",
156+
"profile": "https://github.com/tinesoft",
157+
"contributions": [
158+
"ideas", "review"
159+
]
151160
}
152161
],
153162
"contributorsPerLine": 7,
154163
"linkToUsage": false,
155-
"skipCi": true
164+
"skipCi": true,
165+
"commitType": "docs"
156166
}

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
4343
<td align="center" valign="top" width="14.28%"><a href="https://github.com/mpschaeuble"><img src="https://avatars.githubusercontent.com/u/18322360?v=4?s=100" width="100px;" alt="mpschaeuble"/><br /><sub><b>mpschaeuble</b></sub></a><br /><a href="https://github.com/k3nsei/ng-in-viewport/issues?q=author%3Ampschaeuble" title="Bug reports">🐛</a></td>
4444
</tr>
4545
<tr>
46-
<td align="center" valign="top" width="14.28%"><a href="https://twitter.com/karptonite"><img src="https://avatars.githubusercontent.com/u/132278?v=4?s=100" width="100px;" alt="Daniel Karp"/><br /><sub><b>Daniel Karp</b></sub></a><br /><a href="https://github.com/k3nsei/ng-in-viewport/issues?q=author%3Akarptonite" title="Bug reports">🐛</a></td>
46+
<td align="center" valign="top" width="14.28%"><a href="https://github.com/karptonite"><img src="https://avatars.githubusercontent.com/u/132278?v=4?s=100" width="100px;" alt="Daniel Karp"/><br /><sub><b>Daniel Karp</b></sub></a><br /><a href="https://github.com/k3nsei/ng-in-viewport/issues?q=author%3Akarptonite" title="Bug reports">🐛</a></td>
47+
<td align="center" valign="top" width="14.28%"><a href="https://github.com/tinesoft"><img src="https://avatars.githubusercontent.com/u/4053092?v=4?s=100" width="100px;" alt="Tine Kondo"/><br /><sub><b>Tine Kondo</b></sub></a><br /><a href="#ideas-tinesoft" title="Ideas, Planning, & Feedback">🤔</a> <a href="https://github.com/k3nsei/ng-in-viewport/pulls?q=is%3Apr+reviewed-by%3Atinesoft" title="Reviewed Pull Requests">👀</a></td>
4748
</tr>
4849
</tbody>
4950
</table>

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ng-in-viewport",
3-
"version": "16.0.0",
3+
"version": "16.1.0",
44
"private": true,
55
"license": "MIT",
66
"scripts": {

projects/ng-in-viewport/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ng-in-viewport",
3-
"version": "16.0.0",
3+
"version": "16.1.0",
44
"description": "Allows us to check if an element is within the browsers visual viewport",
55
"keywords": [
66
"angular",
@@ -37,8 +37,8 @@
3737
"tslib": "^2.3.0"
3838
},
3939
"peerDependencies": {
40-
"@angular/common": "^14.0.0 || ^15.0.0 || >=16.0.0",
41-
"@angular/core": "^14.0.0 || ^15.0.0 || >=16.0.0"
40+
"@angular/common": "^14.0.0 || ^15.0.0 || ^16.0.0 || >=17.0.0",
41+
"@angular/core": "^14.0.0 || ^15.0.0 || ^16.0.0 || >=17.0.0"
4242
},
4343
"publishConfig": {
4444
"provenance": true

projects/ng-in-viewport/src/lib/directives/in-viewport.directive.spec.ts

+16
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,14 @@ describe('GIVEN InViewportDirective', () => {
112112
it('THEN `unregister` methods from service should be called', () => {
113113
expect(service.unregister).toHaveBeenCalledWith(node, config);
114114
});
115+
116+
it('THEN `action` method from host component should be called by action output', () => {
117+
expect(host.action).toHaveBeenCalledWith({
118+
[InViewportMetadata]: { entry: undefined },
119+
target: node,
120+
visible: false,
121+
});
122+
});
115123
});
116124
});
117125

@@ -169,6 +177,14 @@ describe('GIVEN InViewportDirective', () => {
169177
it('THEN `unregister` methods from service should be called', () => {
170178
expect(service.unregister).not.toHaveBeenCalled();
171179
});
180+
181+
it('THEN `action` method from host component should be called by action output', () => {
182+
expect(host.action).not.toHaveBeenCalledWith({
183+
[InViewportMetadata]: { entry: undefined },
184+
target: node,
185+
visible: false,
186+
});
187+
});
172188
});
173189
});
174190
});

projects/ng-in-viewport/src/lib/directives/in-viewport.directive.ts

+13-5
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export class InViewportDirective implements AfterViewInit, OnDestroy {
6161

6262
public ngAfterViewInit(): void {
6363
if (!isPlatformBrowser(this.platformId)) {
64-
this.emit(undefined, true);
64+
this.emit(undefined, true, true);
6565
return;
6666
}
6767

@@ -81,6 +81,8 @@ export class InViewportDirective implements AfterViewInit, OnDestroy {
8181
public ngOnDestroy(): void {
8282
if (isPlatformBrowser(this.platformId)) {
8383
this.inViewportService.unregister(this.nativeElement, this.config);
84+
85+
this.emit(undefined, true, false);
8486
}
8587
}
8688

@@ -89,16 +91,22 @@ export class InViewportDirective implements AfterViewInit, OnDestroy {
8991
}
9092

9193
private emit(entry: IntersectionObserverEntry, force: false): void;
92-
private emit(entry: undefined, force: true): void;
93-
private emit(entry: IntersectionObserverEntry | undefined, force: boolean): void {
94+
private emit(entry: undefined, force: true, forcedValue: boolean): void;
95+
private emit(entry: IntersectionObserverEntry | undefined, force: boolean, forcedValue?: boolean): void {
9496
this.inViewportAction.emit({
9597
[InViewportMetadata]: { entry },
9698
target: this.nativeElement,
97-
visible: force || !entry || this.isVisible(entry),
99+
visible: force ? !!forcedValue : !entry || this.isVisible(entry),
98100
});
99101

100102
if (this.config.checkFn) {
101-
this.inViewportCustomCheck.emit(this.config.checkFn(entry, { force, config: this.config }));
103+
this.inViewportCustomCheck.emit(
104+
this.config.checkFn(entry, {
105+
force,
106+
forcedValue: force ? !!forcedValue : undefined,
107+
config: this.config,
108+
})
109+
);
102110
}
103111
}
104112
}

projects/ng-in-viewport/src/lib/values/check-fn.ts

+10
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,17 @@ import { isFunction, isNil, uniqueId } from 'lodash-es';
33
import { Config } from './config';
44

55
export interface InViewportCheckFnOptions {
6+
/**
7+
* Whether action was triggered programmatically.
8+
*/
69
force: boolean;
10+
/**
11+
* When an action is triggered programmatically, this property will hold a simulated visibility state.
12+
*/
13+
forcedValue?: boolean;
14+
/**
15+
* Instance of a configuration object.
16+
*/
717
config: Config;
818
}
919

0 commit comments

Comments
 (0)