From 8599c493995ab3dff36ea173dbfe983ca6296e17 Mon Sep 17 00:00:00 2001 From: Brandy Carney Date: Tue, 15 Aug 2017 17:09:45 -0400 Subject: [PATCH] refactor(popover): add wrapper style directly, get el/ev off popover --- .../core/src/components/popover/popover.tsx | 48 +++++++++---------- .../src/components/popover/test/basic.html | 3 +- 2 files changed, 23 insertions(+), 28 deletions(-) diff --git a/packages/core/src/components/popover/popover.tsx b/packages/core/src/components/popover/popover.tsx index c4854c07b1..f4713749d5 100644 --- a/packages/core/src/components/popover/popover.tsx +++ b/packages/core/src/components/popover/popover.tsx @@ -1,4 +1,4 @@ -import { Component, Element, Event, EventEmitter, Listen, Prop } from '@stencil/core'; +import { Component, Element, Event, EventEmitter, Listen, Prop, State } from '@stencil/core'; import { AnimationBuilder, Animation, AnimationController, Config } from '../../index'; import { createThemedClasses } from '../../utils/theme'; @@ -20,6 +20,8 @@ import iOSLeaveAnimation from './animations/ios.leave'; export class Popover { private animation: Animation; + @State() positioned: boolean = false; + @Element() private el: HTMLElement; @Event() private ionPopoverDidLoad: EventEmitter; @@ -52,12 +54,18 @@ export class Popover { } - private positionPopover(nativeEl: HTMLElement, ev: any, props: any) { - console.debug('Position popover', nativeEl, ev, props); + private positionPopover() { + const props = POPOVER_POSITION_PROPERTIES[this.mode]; + console.debug('Position popover', this.el, this.ev, props); // Declare the popover elements - let contentEl = nativeEl.querySelector('.popover-content') as HTMLElement; - let arrowEl = nativeEl.querySelector('.popover-arrow') as HTMLElement; + let contentEl = this.el.querySelector('.popover-content') as HTMLElement; + let arrowEl = this.el.querySelector('.popover-arrow') as HTMLElement; + + // If no event was passed, hide the arrow + if (!this.ev) { + arrowEl.style.display = 'none'; + } // Set the default transform origin direction let origin = { @@ -79,7 +87,7 @@ export class Popover { } // If ev was passed, use that for target element - let targetDim = ev && ev.target && ev.target.getBoundingClientRect(); + let targetDim = this.ev && this.ev.target && (this.ev.target as HTMLElement).getBoundingClientRect(); // The target is the object that dispatched the event that was passed let target = { @@ -102,11 +110,6 @@ export class Popover { height: arrowDim.height } - // If no ev was passed, hide the arrow - if (!targetDim) { - arrowEl.style.display = 'none'; - } - let arrowCSS = { top: target.top + target.height, left: target.left + (target.width / 2) - (arrow.width / 2) @@ -135,7 +138,7 @@ export class Popover { // If the popover when popped down stretches past bottom of screen, // make it pop up if there's room above if (this.showFromBottom(target, popover, body)) { - nativeEl.className = nativeEl.className + ' popover-bottom'; + this.el.className = this.el.className + ' popover-bottom'; origin.y = 'bottom'; popoverCSS.top = target.top - popover.height; @@ -160,7 +163,7 @@ export class Popover { // Since the transition starts before styling is done we // want to wait for the styles to apply before showing the wrapper - this.displayWrapper(); + this.positioned = true; } private showFromBottom(target: any, popover: any, body: any): boolean { @@ -171,11 +174,6 @@ export class Popover { return target.top + target.height + popover.height > body.height; } - private displayWrapper() { - let popoverWrapperEle = this.el.querySelector('.popover-wrapper') as HTMLElement; - popoverWrapperEle.style.opacity = '1'; - } - private _present(resolve: Function) { if (this.animation) { this.animation.destroy(); @@ -199,7 +197,7 @@ export class Popover { animation.onFinish((a: any) => { a.destroy(); this.ionViewDidEnter(); - this.positionPopover(this.el, this.ev, POPOVER_POSITION_PROPERTIES[this.mode]); + this.positionPopover(); resolve(); }).play(); }); @@ -271,19 +269,17 @@ export class Popover { render() { const ThisComponent = this.component; - const dialogClasses = createThemedClasses( - this.mode, - this.color, - 'popover-wrapper' - ); + const wrapperClasses = createThemedClasses(this.mode, this.color, 'popover-wrapper'); + + const wrapperStyle = this.positioned ? { 'opacity' : '1' } : {}; return [ , -
-
+
+
Show Popover Show Long List Popover + No Event Popover @@ -83,8 +84,6 @@ items += 'Item ' + i + ''; } - console.log(items); - this.innerHTML = `