-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(overlay): add connected overlay directive (#496)
- Loading branch information
Showing
12 changed files
with
263 additions
and
39 deletions.
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,83 @@ | ||
import { | ||
it, | ||
describe, | ||
expect, | ||
beforeEach, | ||
inject, | ||
async, | ||
fakeAsync, | ||
flushMicrotasks, | ||
beforeEachProviders | ||
} from '@angular/core/testing'; | ||
import {TestComponentBuilder, ComponentFixture} from '@angular/compiler/testing'; | ||
import {Component, provide, ViewChild} from '@angular/core'; | ||
import {ConnectedOverlayDirective, OverlayOrigin} from './overlay-directives'; | ||
import {OVERLAY_CONTAINER_TOKEN, Overlay} from './overlay'; | ||
import {ViewportRuler} from './position/viewport-ruler'; | ||
import {OverlayPositionBuilder} from './position/overlay-position-builder'; | ||
import {ConnectedPositionStrategy} from './position/connected-position-strategy'; | ||
|
||
|
||
describe('Overlay directives', () => { | ||
let builder: TestComponentBuilder; | ||
let overlayContainerElement: HTMLElement; | ||
let fixture: ComponentFixture<ConnectedOverlayDirectiveTest>; | ||
|
||
beforeEachProviders(() => [ | ||
Overlay, | ||
OverlayPositionBuilder, | ||
ViewportRuler, | ||
provide(OVERLAY_CONTAINER_TOKEN, {useFactory: () => { | ||
overlayContainerElement = document.createElement('div'); | ||
return overlayContainerElement; | ||
}}) | ||
]); | ||
|
||
beforeEach(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => { | ||
builder = tcb; | ||
})); | ||
|
||
beforeEach(async(() => { | ||
builder.createAsync(ConnectedOverlayDirectiveTest).then(f => { | ||
fixture = f; | ||
fixture.detectChanges(); | ||
}); | ||
})); | ||
|
||
it(`should create an overlay and attach the directive's template`, () => { | ||
expect(overlayContainerElement.textContent).toContain('Menu content'); | ||
}); | ||
|
||
it('should destroy the overlay when the directive is destroyed', fakeAsync(() => { | ||
fixture.destroy(); | ||
flushMicrotasks(); | ||
|
||
expect(overlayContainerElement.textContent.trim()).toBe(''); | ||
})); | ||
|
||
it('should use a connected position strategy with a default set of positions', () => { | ||
let testComponent: ConnectedOverlayDirectiveTest = | ||
fixture.debugElement.componentInstance; | ||
let overlayDirective = testComponent.connectedOverlayDirective; | ||
|
||
let strategy = | ||
<ConnectedPositionStrategy> overlayDirective.overlayRef.getState().positionStrategy; | ||
expect(strategy) .toEqual(jasmine.any(ConnectedPositionStrategy)); | ||
|
||
let positions = strategy.positions; | ||
expect(positions.length).toBeGreaterThan(0); | ||
}); | ||
}); | ||
|
||
|
||
@Component({ | ||
template: ` | ||
<button overlay-origin #trigger="overlayOrigin">Toggle menu</button> | ||
<template connected-overlay [origin]="trigger"> | ||
<p>Menu content</p> | ||
</template>`, | ||
directives: [ConnectedOverlayDirective, OverlayOrigin], | ||
}) | ||
class ConnectedOverlayDirectiveTest { | ||
@ViewChild(ConnectedOverlayDirective) connectedOverlayDirective: ConnectedOverlayDirective; | ||
} |
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,106 @@ | ||
import { | ||
Directive, | ||
TemplateRef, | ||
ViewContainerRef, | ||
OnInit, | ||
Input, | ||
OnDestroy, | ||
ElementRef | ||
} from '@angular/core'; | ||
import {Overlay} from './overlay'; | ||
import {OverlayRef} from './overlay-ref'; | ||
import {TemplatePortal} from '../portal/portal'; | ||
import {OverlayState} from './overlay-state'; | ||
import {ConnectionPositionPair} from './position/connected-position'; | ||
|
||
/** Default set of positions for the overlay. Follows the behavior of a dropdown. */ | ||
let defaultPositionList = [ | ||
new ConnectionPositionPair( | ||
{originX: 'start', originY: 'bottom'}, | ||
{overlayX: 'start', overlayY: 'top'}), | ||
new ConnectionPositionPair( | ||
{originX: 'start', originY: 'top'}, | ||
{overlayX: 'start', overlayY: 'bottom'}), | ||
]; | ||
|
||
|
||
/** | ||
* Directive to facilitate declarative creation of an Overlay using a ConnectedPositionStrategy. | ||
*/ | ||
@Directive({ | ||
selector: '[connected-overlay]' | ||
}) | ||
export class ConnectedOverlayDirective implements OnInit, OnDestroy { | ||
private _overlayRef: OverlayRef; | ||
private _templatePortal: TemplatePortal; | ||
|
||
@Input() origin: OverlayOrigin; | ||
@Input() positions: ConnectionPositionPair[]; | ||
|
||
// TODO(jelbourn): inputs for size, scroll behavior, animation, etc. | ||
|
||
constructor( | ||
private _overlay: Overlay, | ||
templateRef: TemplateRef<any>, | ||
viewContainerRef: ViewContainerRef) { | ||
this._templatePortal = new TemplatePortal(templateRef, viewContainerRef); | ||
} | ||
|
||
get overlayRef() { | ||
return this._overlayRef; | ||
} | ||
|
||
/** @internal */ | ||
ngOnInit() { | ||
this._createOverlay(); | ||
} | ||
|
||
/** @internal */ | ||
ngOnDestroy() { | ||
this._destroyOverlay(); | ||
} | ||
|
||
/** Creates an overlay and attaches this directive's template to it. */ | ||
private _createOverlay() { | ||
if (!this.positions || !this.positions.length) { | ||
this.positions = defaultPositionList; | ||
} | ||
|
||
let overlayConfig = new OverlayState(); | ||
overlayConfig.positionStrategy = | ||
this._overlay.position().connectedTo( | ||
this.origin.elementRef, | ||
{originX: this.positions[0].overlayX, originY: this.positions[0].originY}, | ||
{overlayX: this.positions[0].overlayX, overlayY: this.positions[0].overlayY}); | ||
|
||
this._overlay.create(overlayConfig).then(ref => { | ||
this._overlayRef = ref; | ||
this._overlayRef.attach(this._templatePortal); | ||
}); | ||
} | ||
|
||
/** Destroys the overlay created by this directive. */ | ||
private _destroyOverlay() { | ||
this._overlayRef.dispose(); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Directive applied to an element to make it usable as an origin for an Overlay using a | ||
* ConnectedPositionStrategy. | ||
*/ | ||
@Directive({ | ||
selector: '[overlay-origin]', | ||
exportAs: 'overlayOrigin', | ||
}) | ||
export class OverlayOrigin { | ||
constructor(private _elementRef: ElementRef) { } | ||
|
||
get elementRef() { | ||
return this._elementRef; | ||
} | ||
} | ||
|
||
|
||
export const OVERLAY_DIRECTIVES = [ConnectedOverlayDirective, OverlayOrigin]; |
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
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
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
Oops, something went wrong.