This repository has been archived by the owner on Apr 14, 2023. It is now read-only.
-
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.
Merge branch 'master' of github.com:pjlamb12/ngx-plug-n-play
- Loading branch information
Showing
17 changed files
with
425 additions
and
69 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,7 +3,8 @@ | |
"version": "1.1.1", | ||
"peerDependencies": { | ||
"@angular/common": ">7.2.0", | ||
"@angular/core": ">7.2.0" | ||
"@angular/core": ">7.2.0", | ||
"@angular/animations": ">7.2.0" | ||
}, | ||
"author": { | ||
"email": "[email protected]", | ||
|
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,28 @@ | ||
# Accordion Module | ||
|
||
The `AccordionModule` contains one component, the `AccordionComponent`. It uses content projection to provide a trigger to open and close the accordion, and the body of the accordion. The parent component, the one implementing the accordion, thus determines all the styles for the component. | ||
|
||
Here's an example of use: | ||
|
||
```html | ||
<pnp-accordion> | ||
<ng-container accordion-header> | ||
<p class="accordion-trigger">Trigger</p> | ||
</ng-container> | ||
<div accordion-body> | ||
<p> | ||
Lorem, ipsum dolor | ||
</p> | ||
</div> | ||
</pnp-accordion> | ||
``` | ||
|
||
When the `.accordion-trigger` is clicked, an Angular animation is triggered to either scroll up or scroll down to show the `.accordion-body`. | ||
|
||
The `AccordionComponent` has one `Input`: | ||
|
||
- `triggerSelector: string` — This is the CSS class selector for the accordion trigger. It defaults to `.accordion-trigger`. If no projected content element has the same class, then a class inside the `AccordionComponent` is used as the trigger, `.accordion-header-container`. | ||
|
||
The component has one `Output`: | ||
|
||
- `isCollapsedUpdated: EventEmitter<boolean>`: — The `Output` emits true or false, letting the parent component know if the accordion is open or not. That way the parent component can change its display in some way depending on the status. It doesn't need to change the visibility of the `[accordion-body]`, but maybe an icon or something like that. |
10 changes: 10 additions & 0 deletions
10
projects/ngx-plug-n-play-lib/src/lib/accordion/accordion.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,10 @@ | ||
import { NgModule } from '@angular/core'; | ||
import { CommonModule } from '@angular/common'; | ||
import { AccordionComponent } from './accordion/accordion.component'; | ||
|
||
@NgModule({ | ||
declarations: [AccordionComponent], | ||
exports: [AccordionComponent], | ||
imports: [CommonModule], | ||
}) | ||
export class AccordionModule {} |
3 changes: 3 additions & 0 deletions
3
projects/ngx-plug-n-play-lib/src/lib/accordion/accordion/accordion.component.css
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,3 @@ | ||
.accordion-body-container { | ||
overflow: hidden; | ||
} |
6 changes: 6 additions & 0 deletions
6
projects/ngx-plug-n-play-lib/src/lib/accordion/accordion/accordion.component.html
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 @@ | ||
<div class="accordion-header-container" [class.accordion-open]="!isCollapsed"> | ||
<ng-content select="[accordion-header]"></ng-content> | ||
</div> | ||
<div class="accordion-body-container" [@collapse]="isCollapsed ? 'collapsed' : 'open'"> | ||
<ng-content select="[accordion-body]"></ng-content> | ||
</div> |
68 changes: 68 additions & 0 deletions
68
projects/ngx-plug-n-play-lib/src/lib/accordion/accordion/accordion.component.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,68 @@ | ||
import { AccordionComponent } from './accordion.component'; | ||
import { Component, ViewChild } from '@angular/core'; | ||
import { ComponentFixture, TestBed, async, fakeAsync, tick } from '@angular/core/testing'; | ||
import { By } from '@angular/platform-browser'; | ||
import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; | ||
|
||
@Component({ | ||
selector: 'app-test-host', | ||
template: ` | ||
<pnp-accordion> | ||
<ng-container accordion-header> | ||
<p class="accordion-trigger">Trigger Accordion</p> | ||
</ng-container> | ||
<div accordion-body> | ||
<p> | ||
Lorem, ipsum dolor sit. | ||
</p> | ||
</div> | ||
</pnp-accordion> | ||
`, | ||
}) | ||
class TestHostComponent { | ||
@ViewChild(AccordionComponent) accordion: AccordionComponent; | ||
} | ||
|
||
describe('AccordionComponent', () => { | ||
let testHostComponent: TestHostComponent; | ||
let component: AccordionComponent; | ||
let fixture: ComponentFixture<TestHostComponent>; | ||
|
||
beforeEach(async(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [BrowserAnimationsModule], | ||
declarations: [TestHostComponent, AccordionComponent], | ||
}).compileComponents(); | ||
})); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(TestHostComponent); | ||
testHostComponent = fixture.componentInstance; | ||
component = testHostComponent.accordion; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
|
||
it('should have the .accordion-trigger element as the trigger', () => { | ||
const classList: DOMTokenList = component.trigger.classList; | ||
|
||
expect(classList.contains('accordion-trigger')).toBeTruthy(); | ||
}); | ||
|
||
it('should call the function that will emit the new accordion value', () => { | ||
spyOn(component, 'updateCollapsedStatus'); | ||
component.toggleAccordionVisibility(); | ||
|
||
expect(component.updateCollapsedStatus).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should emit the new accordion value', () => { | ||
spyOn(component.isCollapsedUpdated, 'emit'); | ||
component.updateCollapsedStatus(); | ||
|
||
expect(component.isCollapsedUpdated.emit).toHaveBeenCalled(); | ||
}); | ||
}); |
48 changes: 48 additions & 0 deletions
48
projects/ngx-plug-n-play-lib/src/lib/accordion/accordion/accordion.component.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,48 @@ | ||
import { Component, OnInit, Output, EventEmitter, Input, ElementRef } from '@angular/core'; | ||
import { trigger } from '@angular/animations'; | ||
import { collapseAnimation } from '../../shared/animations/collapse-animation'; | ||
import { fromEvent, Subject } from 'rxjs'; | ||
import { takeUntil } from 'rxjs/operators'; | ||
|
||
@Component({ | ||
selector: 'pnp-accordion', | ||
templateUrl: './accordion.component.html', | ||
styleUrls: ['./accordion.component.css'], | ||
animations: [trigger('collapse', collapseAnimation())], | ||
}) | ||
export class AccordionComponent implements OnInit { | ||
@Output() isCollapsedUpdated: EventEmitter<boolean> = new EventEmitter<boolean>(); | ||
@Input() triggerSelector: string = '.accordion-trigger'; | ||
public isCollapsed: boolean = true; | ||
public trigger; | ||
private destroy$: Subject<boolean> = new Subject<boolean>(); | ||
|
||
constructor(private _elRef: ElementRef) {} | ||
|
||
ngOnInit() { | ||
this.updateCollapsedStatus(); | ||
this.trigger = this._elRef.nativeElement.querySelector(this.triggerSelector) | ||
? this._elRef.nativeElement.querySelector(this.triggerSelector) | ||
: this._elRef.nativeElement.querySelector('.accordion-header-container'); | ||
|
||
fromEvent(this.trigger, 'click') | ||
.pipe(takeUntil(this.destroy$)) | ||
.subscribe(() => { | ||
this.toggleAccordionVisibility(); | ||
}); | ||
} | ||
|
||
ngOnDestroy() { | ||
this.destroy$.next(true); | ||
this.destroy$.complete(); | ||
} | ||
|
||
updateCollapsedStatus() { | ||
this.isCollapsedUpdated.emit(this.isCollapsed); | ||
} | ||
|
||
toggleAccordionVisibility() { | ||
this.isCollapsed = !this.isCollapsed; | ||
this.updateCollapsedStatus(); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
projects/ngx-plug-n-play-lib/src/lib/shared/animations/collapse-animation.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,28 @@ | ||
import { state, style, animate, transition } from '@angular/animations'; | ||
|
||
export function collapseAnimation( | ||
slideDownDuration: string = '500ms', | ||
slideUpDuration: string = '500ms', | ||
startingOpacity: string = '1', | ||
endingOpacity: string = '1', | ||
) { | ||
return [ | ||
state( | ||
'collapsed', | ||
style({ | ||
height: '0', | ||
overflow: 'hidden', | ||
opacity: startingOpacity, | ||
}), | ||
), | ||
state( | ||
'open', | ||
style({ | ||
overflow: 'hidden', | ||
opacity: endingOpacity, | ||
}), | ||
), | ||
transition('collapsed=>open', animate(slideDownDuration)), | ||
transition('open=>collapsed', animate(slideUpDuration)), | ||
]; | ||
} |
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,41 @@ | ||
<h1>Accordion Component Demo</h1> | ||
<pnp-accordion> | ||
<ng-container accordion-header> | ||
<button class="btn btn-primary accordion-trigger mb-2">Open</button> | ||
</ng-container> | ||
<div class="card" accordion-body> | ||
<p class="card-body">Here is the content of the accordion dropdown.</p> | ||
</div> | ||
</pnp-accordion> | ||
|
||
<p> | ||
The <code>AccordionModule</code> contains one component, the <code>AccordionComponent</code>. It uses content | ||
projection to provide a trigger to open and close the accordion, and the body of the accordion. The parent | ||
component, the one implementing the accordion, thus determines all the styles for the component. | ||
</p> | ||
|
||
<p> | ||
When the <code>.accordion-trigger</code> is clicked, an Angular animation is triggered to either scroll up or scroll | ||
down to show the <code>.accordion-body</code>. | ||
</p> | ||
|
||
<p>The <code>AccordionComponent</code> has one <code>Input</code>:</p> | ||
|
||
<ul> | ||
<li> | ||
<code>triggerSelector: string</code> — This is the CSS class selector for the accordion trigger. It | ||
defaults to <code>.accordion-trigger</code>. If no projected content element has the same class, then a class | ||
inside the <code>AccordionComponent</code> is used as the trigger, <code>.accordion-header-container</code>. | ||
</li> | ||
</ul> | ||
|
||
<p>The component has one <code>Output</code>:</p> | ||
|
||
<ul> | ||
<li> | ||
<code>isCollapsedUpdated: EventEmitter<boolean></code>: — The <code>Output</code> emits true or | ||
false, letting the parent component know if the accordion is open or not. That way the parent component can | ||
change its display in some way depending on the status. It doesn't need to change the visibility of the | ||
<code>[accordion-body]</code>, but maybe an icon or something like that. | ||
</li> | ||
</ul> |
Empty file.
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,24 @@ | ||
import { async, ComponentFixture, TestBed } from '@angular/core/testing'; | ||
|
||
import { AccordionDemoComponent } from './accordion-demo.component'; | ||
|
||
describe('AccordionDemoComponent', () => { | ||
let component: AccordionDemoComponent; | ||
let fixture: ComponentFixture<AccordionDemoComponent>; | ||
|
||
beforeEach(async(() => { | ||
TestBed.configureTestingModule({ | ||
declarations: [AccordionDemoComponent], | ||
}).compileComponents(); | ||
})); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(AccordionDemoComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
}); |
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,12 @@ | ||
import { Component, OnInit } from '@angular/core'; | ||
|
||
@Component({ | ||
selector: 'pnp-accordion-demo', | ||
templateUrl: './accordion-demo.component.html', | ||
styleUrls: ['./accordion-demo.component.scss'], | ||
}) | ||
export class AccordionDemoComponent implements OnInit { | ||
constructor() {} | ||
|
||
ngOnInit() {} | ||
} |
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