-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
12d753b
commit 9bffcab
Showing
4 changed files
with
106 additions
and
9 deletions.
There are no files selected for viewing
8 changes: 4 additions & 4 deletions
8
demo/src/app/components/collapse/demos/basic/collapse-basic.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 |
---|---|---|
@@ -1,13 +1,13 @@ | ||
<p> | ||
<button type="button" class="btn btn-outline-primary" (click)="isCollapsed = !isCollapsed" | ||
[attr.aria-expanded]="!isCollapsed" aria-controls="collapseExample"> | ||
<button type="button" class="btn btn-outline-primary" (click)="collapse.toggle()" [attr.aria-expanded]="!isCollapsed" | ||
aria-controls="collapseExample"> | ||
Toggle | ||
</button> | ||
</p> | ||
<div id="collapseExample" [ngbCollapse]="isCollapsed"> | ||
<div #collapse="ngbCollapse" [(ngbCollapse)]="isCollapsed"> | ||
<div class="card"> | ||
<div class="card-body"> | ||
You can collapse this card by clicking Toggle | ||
</div> | ||
</div> | ||
</div> | ||
</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
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,63 @@ | ||
import {NgbTransitionStartFn} from './ngbTransition'; | ||
import {reflow} from '../util'; | ||
|
||
export interface NgbCollapseCtx { | ||
direction: 'show' | 'hide'; | ||
maxHeight?: string; | ||
} | ||
|
||
function measureCollapsingElementHeightPx(element: HTMLElement): string { | ||
const {classList} = element; | ||
const hasShownClass = classList.contains('show'); | ||
if (!hasShownClass) { | ||
classList.add('show'); | ||
} | ||
|
||
element.style.height = ''; | ||
const height = element.getBoundingClientRect().height + 'px'; | ||
|
||
if (!hasShownClass) { | ||
classList.remove('show'); | ||
} | ||
|
||
return height; | ||
} | ||
|
||
export const ngbCollapsingTransition: NgbTransitionStartFn<NgbCollapseCtx> = | ||
(element: HTMLElement, context: NgbCollapseCtx) => { | ||
let {direction, maxHeight} = context; | ||
const {classList} = element; | ||
|
||
// No maxHeight -> running the transition for the first time | ||
if (!maxHeight) { | ||
maxHeight = measureCollapsingElementHeightPx(element); | ||
context.maxHeight = maxHeight; | ||
|
||
// Fix the height before starting the animation | ||
element.style.height = direction !== 'show' ? maxHeight : '0px'; | ||
|
||
classList.remove('collapse'); | ||
classList.remove('collapsing'); | ||
classList.remove('show'); | ||
|
||
reflow(element); | ||
|
||
// Start the animation | ||
classList.add('collapsing'); | ||
} | ||
|
||
// Start or revert the animation | ||
element.style.height = direction === 'show' ? maxHeight : '0px'; | ||
|
||
return () => { | ||
classList.remove('collapsing'); | ||
classList.add('collapse'); | ||
if (direction === 'show') { | ||
classList.add('show'); | ||
} else { | ||
classList.remove('show'); | ||
} | ||
|
||
element.style.height = ''; | ||
}; | ||
}; |
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