-
Notifications
You must be signed in to change notification settings - Fork 4.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Animate directive #12067
Merged
Merged
Animate directive #12067
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
624c511
Fixed #12057 - Add animation directive
cetincakiroglu bab43d5
Add demo page for animate directive
cetincakiroglu 7db065d
Refactor
cetincakiroglu ff19584
refactor
cetincakiroglu 22555ca
Merge master
cetincakiroglu 8264715
Add animate directive route
cetincakiroglu 4d9498e
Update animate demo
cetincakiroglu b7f1b37
Refactor
cetincakiroglu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
import { NgModule, Directive, ElementRef, Input, Renderer2, Inject, NgZone } from '@angular/core'; | ||
import { CommonModule, DOCUMENT } from '@angular/common'; | ||
import { DomHandler } from '../dom/domhandler'; | ||
|
||
@Directive({ | ||
selector: '[pAnimate]', | ||
host: { | ||
'[class.p-animate]': 'true' | ||
} | ||
}) | ||
export class Animate { | ||
|
||
@Input() enterClass: string; | ||
|
||
@Input() leaveClass: string; | ||
|
||
documentScrollListener: Function | null = null; | ||
|
||
loadListener: Function = () => { }; | ||
|
||
entered: boolean; | ||
|
||
observer: IntersectionObserver; | ||
|
||
loaded: boolean; | ||
|
||
constructor(@Inject(DOCUMENT) private document: Document, private host: ElementRef, public el: ElementRef, public renderer: Renderer2, private zone: NgZone) { } | ||
|
||
ngOnInit() { | ||
if (this.isInViewport()) { | ||
this.enter(); | ||
} | ||
this.bindLoadListener(); | ||
} | ||
|
||
bindIntersectionObserver() { | ||
const options = { | ||
root: null, | ||
rootMargin: '0px', | ||
threshold: 1.0 | ||
} | ||
|
||
this.zone.runOutsideAngular(() => { | ||
this.observer = new IntersectionObserver(el => this.isVisible(el), options); | ||
this.observer.observe(this.host.nativeElement); | ||
}) | ||
} | ||
|
||
isInViewport() { | ||
let rect = this.host.nativeElement.getBoundingClientRect(); | ||
|
||
return ( | ||
rect.top >= 0 && | ||
rect.left >= 0 && | ||
rect.bottom <= ((window.innerHeight + rect.height) || this.document.documentElement.clientHeight) && | ||
rect.right <= (window.innerWidth || this.document.documentElement.clientWidth) | ||
); | ||
} | ||
|
||
isVisible(element: IntersectionObserverEntry[]) { | ||
const [intersectionObserverEntry] = element; | ||
this.entered = intersectionObserverEntry.isIntersecting; | ||
} | ||
|
||
animate() { | ||
if (this.loaded) { | ||
if (this.isInViewport() && this.entered) { | ||
this.enter(); | ||
} | ||
|
||
if (!this.isInViewport() && !this.entered) { | ||
this.leave(); | ||
} | ||
} | ||
} | ||
|
||
enter() { | ||
this.host.nativeElement.style.visibility = 'visible'; | ||
DomHandler.addClass(this.host.nativeElement, this.enterClass); | ||
} | ||
|
||
leave() { | ||
DomHandler.removeClass(this.host.nativeElement, this.enterClass); | ||
this.host.nativeElement.style.visibility = 'hidden'; | ||
} | ||
|
||
bindDocumentScrollListener() { | ||
if (!this.documentScrollListener) { | ||
this.zone.runOutsideAngular(() => { | ||
this.documentScrollListener = this.renderer.listen(window, 'scroll', () => { | ||
if (!this.observer) { | ||
this.bindIntersectionObserver(); | ||
} | ||
this.animate(); | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
unbindDocumentScrollListener() { | ||
if (this.documentScrollListener) { | ||
this.documentScrollListener(); | ||
this.documentScrollListener = null; | ||
} | ||
} | ||
|
||
bindLoadListener() { | ||
this.zone.runOutsideAngular(() => { | ||
this.loadListener = this.renderer.listen(window, 'load', () => { | ||
if (!this.loaded) { | ||
this.animate(); | ||
} | ||
if (!this.documentScrollListener) { | ||
this.bindDocumentScrollListener(); | ||
} | ||
this.loaded = true; | ||
}); | ||
}) | ||
} | ||
|
||
unbindLoadListener() { | ||
if (this.loadListener) { | ||
this.loadListener(); | ||
this.loadListener = null; | ||
} | ||
} | ||
|
||
unbindIntersectionObserver() { | ||
this.observer.unobserve(this.host.nativeElement); | ||
} | ||
|
||
ngOnDestroy() { | ||
this.unbindDocumentScrollListener(); | ||
this.unbindLoadListener(); | ||
this.unbindIntersectionObserver(); | ||
} | ||
} | ||
|
||
@NgModule({ | ||
imports: [CommonModule], | ||
exports: [Animate], | ||
declarations: [Animate] | ||
}) | ||
export class AnimateModule { } |
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,6 @@ | ||
{ | ||
"$schema": "ng-packagr/ng-package.schema.json", | ||
"lib": { | ||
"entryFile": "public_api.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 @@ | ||
export * from './animate'; |
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
15 changes: 15 additions & 0 deletions
15
src/app/showcase/components/animate/animate-routing.module.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,15 @@ | ||
import {NgModule} from '@angular/core'; | ||
import {RouterModule} from '@angular/router' | ||
import {AnimateDemo} from './animatedemo'; | ||
|
||
@NgModule({ | ||
imports: [ | ||
RouterModule.forChild([ | ||
{path:'',component: AnimateDemo} | ||
]) | ||
], | ||
exports: [ | ||
RouterModule | ||
] | ||
}) | ||
export class AnimateDemoRoutingModule {} |
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,76 @@ | ||
<div class="content-section introduction"> | ||
<div class="feature-intro"> | ||
<h1>Animate</h1> | ||
<p>Animate manages PrimeFlex CSS classes declaratively to during enter animations on scroll or on page load.</p> | ||
</div> | ||
<app-demoActions github="animate" stackblitz="animate-demo"></app-demoActions> | ||
</div> | ||
|
||
<div class="content-section implementation"> | ||
<div class="card flex flex-column align-items-center"> | ||
<div pAnimate enterClass="flip" class="flex justify-content-center align-items-center h-20rem w-20rem border-round shadow-2 animation-duration-1000 animation-ease-out"> | ||
<span class="text-900 text-3xl font-bold">flip</span> | ||
</div> | ||
<div class="h-30rem"></div> | ||
<div pAnimate enterClass="flipup" class="flex justify-content-center align-items-center h-20rem w-20rem border-round shadow-2 animation-duration-1000 animation-ease-out"> | ||
<span class="text-900 text-3xl font-bold">flipup</span> | ||
</div> | ||
<div class="h-30rem"></div> | ||
<div pAnimate enterClass="flipleft" class="flex justify-content-center align-items-center h-20rem w-20rem border-round shadow-2 animation-duration-1000 animation-ease-out"> | ||
<span class="text-900 text-3xl font-bold">flipleft</span> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<div class="content-section documentation"> | ||
<p-tabView> | ||
<p-tabPanel header="Documentation"> | ||
<h5>Import</h5> | ||
<app-code lang="typescript" ngNonBindable ngPreserveWhitespaces> | ||
import {AnimateModule} from 'primeng/animate'; | ||
</app-code> | ||
|
||
<h5>Getting Started</h5> | ||
<p>Animate uses <a href="http://primefaces.org/primeflex/animations">PrimeFlex animations</a>, however it can perform animations with custom CSS classes too. Takes <i>enterClass</i> property to simply add animation class during scroll or page load to manage elements animation if the element is in viewport.</p> | ||
|
||
<p><b>Enter Animation</b></p> | ||
<app-code lang="markup" ngNonBindable ngPreserveWhitespaces> | ||
<div pAnimate enterClass="flip" class="flex justify-content-center align-items-center h-20rem w-20rem border-round shadow-2 animation-duration-1000 animation-ease-out"></div> | ||
</app-code> | ||
|
||
<h5>Properties</h5> | ||
<div class="doc-tablewrapper"> | ||
<table class="doc-table"> | ||
<thead> | ||
<tr> | ||
<th>Name</th> | ||
<th>Type</th> | ||
<th>Default</th> | ||
<th>Description</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<tr> | ||
<td>enterClass</td> | ||
<td>string</td> | ||
<td>null</td> | ||
<td>Selector to define the CSS class for animation.</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
</div> | ||
|
||
<h5>Events</h5> | ||
<p>Directive has no events.</p> | ||
|
||
<h5>Dependencies</h5> | ||
<p>None.</p> | ||
</p-tabPanel> | ||
|
||
<p-tabPanel header="Source"> | ||
<a href="https://github.com/primefaces/primeng/tree/master/src/app/showcase/components/animate" class="btn-viewsource" target="_blank"> | ||
<span>View on GitHub</span> | ||
</a> | ||
</p-tabPanel> | ||
</p-tabView> | ||
</div> |
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,25 @@ | ||
import {NgModule} from '@angular/core'; | ||
import {CommonModule} from '@angular/common'; | ||
import {FormsModule} from '@angular/forms' | ||
import {AnimateDemo} from './animatedemo'; | ||
import {AnimateDemoRoutingModule} from './animate-routing.module'; | ||
import {AnimateModule} from 'primeng/animate'; | ||
import {TabViewModule} from 'primeng/tabview'; | ||
import {AppCodeModule} from '../../app.code.component'; | ||
import {AppDemoActionsModule} from '../../app.demoactions.component'; | ||
|
||
@NgModule({ | ||
imports: [ | ||
CommonModule, | ||
FormsModule, | ||
AnimateDemoRoutingModule, | ||
AnimateModule, | ||
TabViewModule, | ||
AppDemoActionsModule, | ||
AppCodeModule | ||
], | ||
declarations: [ | ||
AnimateDemo | ||
] | ||
}) | ||
export class AnimateDemoModule {} |
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,6 @@ | ||
import { Component } from '@angular/core'; | ||
|
||
@Component({ | ||
templateUrl: './animatedemo.html' | ||
}) | ||
export class AnimateDemo { } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, you also need to unsubscribe from IntersecrtionObserver(use method unobserve)