Skip to content

Commit

Permalink
feat: emit action before directive destroy
Browse files Browse the repository at this point in the history
chore: extend peer deps to include angular v17

Refs: #1418
Release-As: 16.1.0
  • Loading branch information
k3nsei committed Nov 14, 2023
1 parent 1565794 commit ec25af9
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 12 deletions.
14 changes: 12 additions & 2 deletions .all-contributorsrc
Expand Up @@ -144,13 +144,23 @@
"login": "karptonite",
"name": "Daniel Karp",
"avatar_url": "https://avatars.githubusercontent.com/u/132278?v=4",
"profile": "https://twitter.com/karptonite",
"profile": "https://github.com/karptonite",
"contributions": [
"bug"
]
},
{
"login": "tinesoft",
"name": "Tine Kondo",
"avatar_url": "https://avatars.githubusercontent.com/u/4053092?v=4",
"profile": "https://github.com/tinesoft",
"contributions": [
"ideas", "review"
]
}
],
"contributorsPerLine": 7,
"linkToUsage": false,
"skipCi": true
"skipCi": true,
"commitType": "docs"
}
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -43,7 +43,8 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
<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>
</tr>
<tr>
<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>
<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>
<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>
</tr>
</tbody>
</table>
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "ng-in-viewport",
"version": "16.0.0",
"version": "16.1.0",
"private": true,
"license": "MIT",
"scripts": {
Expand Down
6 changes: 3 additions & 3 deletions projects/ng-in-viewport/package.json
@@ -1,6 +1,6 @@
{
"name": "ng-in-viewport",
"version": "16.0.0",
"version": "16.1.0",
"description": "Allows us to check if an element is within the browsers visual viewport",
"keywords": [
"angular",
Expand Down Expand Up @@ -37,8 +37,8 @@
"tslib": "^2.3.0"
},
"peerDependencies": {
"@angular/common": "^14.0.0 || ^15.0.0 || >=16.0.0",
"@angular/core": "^14.0.0 || ^15.0.0 || >=16.0.0"
"@angular/common": "^14.0.0 || ^15.0.0 || ^16.0.0 || >=17.0.0",
"@angular/core": "^14.0.0 || ^15.0.0 || ^16.0.0 || >=17.0.0"
},
"publishConfig": {
"provenance": true
Expand Down
Expand Up @@ -112,6 +112,14 @@ describe('GIVEN InViewportDirective', () => {
it('THEN `unregister` methods from service should be called', () => {
expect(service.unregister).toHaveBeenCalledWith(node, config);
});

it('THEN `action` method from host component should be called by action output', () => {
expect(host.action).toHaveBeenCalledWith({
[InViewportMetadata]: { entry: undefined },
target: node,
visible: false,
});
});
});
});

Expand Down Expand Up @@ -169,6 +177,14 @@ describe('GIVEN InViewportDirective', () => {
it('THEN `unregister` methods from service should be called', () => {
expect(service.unregister).not.toHaveBeenCalled();
});

it('THEN `action` method from host component should be called by action output', () => {
expect(host.action).not.toHaveBeenCalledWith({
[InViewportMetadata]: { entry: undefined },
target: node,
visible: false,
});
});
});
});
});
Expand Up @@ -61,7 +61,7 @@ export class InViewportDirective implements AfterViewInit, OnDestroy {

public ngAfterViewInit(): void {
if (!isPlatformBrowser(this.platformId)) {
this.emit(undefined, true);
this.emit(undefined, true, true);
return;
}

Expand All @@ -81,6 +81,8 @@ export class InViewportDirective implements AfterViewInit, OnDestroy {
public ngOnDestroy(): void {
if (isPlatformBrowser(this.platformId)) {
this.inViewportService.unregister(this.nativeElement, this.config);

this.emit(undefined, true, false);
}
}

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

private emit(entry: IntersectionObserverEntry, force: false): void;
private emit(entry: undefined, force: true): void;
private emit(entry: IntersectionObserverEntry | undefined, force: boolean): void {
private emit(entry: undefined, force: true, forcedValue: boolean): void;
private emit(entry: IntersectionObserverEntry | undefined, force: boolean, forcedValue?: boolean): void {
this.inViewportAction.emit({
[InViewportMetadata]: { entry },
target: this.nativeElement,
visible: force || !entry || this.isVisible(entry),
visible: force ? !!forcedValue : !entry || this.isVisible(entry),
});

if (this.config.checkFn) {
this.inViewportCustomCheck.emit(this.config.checkFn(entry, { force, config: this.config }));
this.inViewportCustomCheck.emit(
this.config.checkFn(entry, {
force,
forcedValue: force ? !!forcedValue : undefined,
config: this.config,
})
);
}
}
}
10 changes: 10 additions & 0 deletions projects/ng-in-viewport/src/lib/values/check-fn.ts
Expand Up @@ -3,7 +3,17 @@ import { isFunction, isNil, uniqueId } from 'lodash-es';
import { Config } from './config';

export interface InViewportCheckFnOptions {
/**
* Whether action was triggered programmatically.
*/
force: boolean;
/**
* When an action is triggered programmatically, this property will hold a simulated visibility state.
*/
forcedValue?: boolean;
/**
* Instance of a configuration object.
*/
config: Config;
}

Expand Down

1 comment on commit ec25af9

@vercel
Copy link

@vercel vercel bot commented on ec25af9 Nov 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

ng-in-viewport – ./

ng-in-viewport.vercel.app
ng-in-viewport-k3nsei.vercel.app
ng-in-viewport-git-develop-k3nsei.vercel.app

Please sign in to comment.