@@ -234,6 +234,7 @@ export class PfTooltip extends HTMLElement {
234234 if ( this . tooltip === null ) {
235235 this . _createTooltip ( ) ;
236236 this . _styleTooltip ( ) ;
237+ this . _checkPlacement ( ) ;
237238 this . _showTooltip ( ) ;
238239 //notify frameworks
239240 this . dispatchEvent ( new CustomEvent ( 'pf-tooltip.opened' , { } ) ) ;
@@ -253,6 +254,8 @@ export class PfTooltip extends HTMLElement {
253254 this . _removeTooltip ( ) ;
254255 //notify frameworks
255256 this . dispatchEvent ( new CustomEvent ( 'pf-tooltip.closed' , { } ) ) ;
257+ // reset position after tooltip is closed
258+ this . _placement = this . getAttribute ( 'placement' ) ? this . getAttribute ( 'placement' ) : 'right' ;
256259 } , this . _duration ) ;
257260 }
258261 } , this . _delay + this . _duration ) ;
@@ -287,6 +290,24 @@ export class PfTooltip extends HTMLElement {
287290 this . tooltip . setAttribute ( 'class' , `tooltip ${ this . _placement } ${ this . _animation } ` ) ;
288291 }
289292
293+ /**
294+ * update the placement of tooltip
295+ */
296+ _updatePlacement ( ) {
297+ switch ( this . _placement ) {
298+ case 'top' :
299+ return 'bottom' ;
300+ case 'bottom' :
301+ return 'top' ;
302+ case 'left' :
303+ return 'right' ;
304+ case 'right' :
305+ return 'left' ;
306+ default :
307+ return this . _placement ;
308+ }
309+ }
310+
290311 /**
291312 * Styles the tooltip based on placement attribute
292313 * @private
@@ -319,6 +340,18 @@ export class PfTooltip extends HTMLElement {
319340 this . tooltip . style . top = `${ rect . top + scroll . y - tooltipDimensions . h / 2 + linkDimensions . h / 2 } px` ;
320341 this . tooltip . style . left = `${ rect . left + scroll . x + linkDimensions . w } px` ;
321342 }
343+
344+ this . tooltip . className . indexOf ( this . _placement ) === - 1 && ( this . tooltip . className = this . tooltip . className . replace ( / \b ( t o p | b o t t o m | l e f t | r i g h t ) + / , this . _placement ) ) ;
345+ }
346+
347+ /**
348+ * check the placement of tooltip
349+ */
350+ _checkPlacement ( ) {
351+ if ( ! pfUtil . isElementInViewport ( this . tooltip ) ) {
352+ this . _placement = this . _updatePlacement ( ) ;
353+ this . _styleTooltip ( ) ;
354+ }
322355 }
323356
324357 /**
0 commit comments