1
1
import Integer from "@ui5/webcomponents-base/dist/types/Integer.js" ;
2
+ import ResizeHandler from "@ui5/webcomponents-base/dist/delegate/ResizeHandler.js" ;
2
3
import Popup from "./Popup.js" ;
3
4
import PopoverPlacementType from "./types/PopoverPlacementType.js" ;
4
5
import PopoverVerticalAlign from "./types/PopoverVerticalAlign.js" ;
@@ -250,6 +251,12 @@ const metadata = {
250
251
* @public
251
252
*/
252
253
class Popover extends Popup {
254
+ constructor ( ) {
255
+ super ( ) ;
256
+
257
+ this . _handleResize = this . handleResize . bind ( this ) ;
258
+ }
259
+
253
260
static get metadata ( ) {
254
261
return metadata ;
255
262
}
@@ -266,6 +273,14 @@ class Popover extends Popup {
266
273
return 10 ; // px
267
274
}
268
275
276
+ onEnterDOM ( ) {
277
+ ResizeHandler . register ( this , this . _handleResize ) ;
278
+ }
279
+
280
+ onExitDOM ( ) {
281
+ ResizeHandler . deregister ( this , this . _handleResize ) ;
282
+ }
283
+
269
284
isOpenerClicked ( event ) {
270
285
const target = event . target ;
271
286
return target === this . _opener || ( target . getFocusDomRef && target . getFocusDomRef ( ) === this . _opener ) || event . composedPath ( ) . indexOf ( this . _opener ) > - 1 ;
@@ -277,14 +292,15 @@ class Popover extends Popup {
277
292
* @param {boolean } preventInitialFocus prevents applying the focus inside the popover
278
293
* @public
279
294
*/
280
- openBy ( opener , preventInitialFocus = false ) {
295
+ async openBy ( opener , preventInitialFocus = false ) {
281
296
if ( ! opener || this . opened ) {
282
297
return ;
283
298
}
284
299
285
300
this . _opener = opener ;
301
+ this . _openerRect = opener . getBoundingClientRect ( ) ;
286
302
287
- super . open ( preventInitialFocus ) ;
303
+ await super . open ( preventInitialFocus ) ;
288
304
}
289
305
290
306
/**
@@ -332,21 +348,36 @@ class Popover extends Popup {
332
348
&& openerRect . right === 0 ;
333
349
}
334
350
351
+ handleResize ( ) {
352
+ if ( this . opened ) {
353
+ this . reposition ( ) ;
354
+ }
355
+ }
356
+
335
357
reposition ( ) {
336
358
this . show ( ) ;
337
359
}
338
360
339
361
show ( ) {
340
362
let placement ;
341
- const popoverSize = this . popoverSize ;
342
- const openerRect = this . _opener . getBoundingClientRect ( ) ;
363
+ const popoverSize = this . getPopoverSize ( ) ;
364
+
365
+ if ( popoverSize . width === 0 || popoverSize . height === 0 ) {
366
+ // size can not be determined properly at this point, popover will be shown with the next reposition
367
+ return ;
368
+ }
343
369
344
- if ( this . shouldCloseDueToNoOpener ( openerRect ) && this . isFocusWithin ( ) ) {
370
+ if ( this . isOpen ( ) ) {
371
+ // update opener rect if it was changed during the popover being opened
372
+ this . _openerRect = this . _opener . getBoundingClientRect ( ) ;
373
+ }
374
+
375
+ if ( this . shouldCloseDueToNoOpener ( this . _openerRect ) && this . isFocusWithin ( ) ) {
345
376
// reuse the old placement as the opener is not available,
346
377
// but keep the popover open as the focus is within
347
378
placement = this . _oldPlacement ;
348
379
} else {
349
- placement = this . calcPlacement ( openerRect , popoverSize ) ;
380
+ placement = this . calcPlacement ( this . _openerRect , popoverSize ) ;
350
381
}
351
382
352
383
const stretching = this . horizontalAlign === PopoverHorizontalAlign . Stretch ;
@@ -379,7 +410,7 @@ class Popover extends Popup {
379
410
}
380
411
}
381
412
382
- get popoverSize ( ) {
413
+ getPopoverSize ( ) {
383
414
let width ,
384
415
height ;
385
416
let rect = this . getBoundingClientRect ( ) ;
@@ -391,17 +422,15 @@ class Popover extends Popup {
391
422
return { width, height } ;
392
423
}
393
424
394
- this . style . visibility = "hidden" ;
395
425
this . style . display = "block" ;
426
+ this . style . top = "-10000px" ;
427
+ this . style . left = "-10000px" ;
396
428
397
429
rect = this . getBoundingClientRect ( ) ;
398
430
399
431
width = rect . width ;
400
432
height = rect . height ;
401
433
402
- this . hide ( ) ;
403
- this . style . visibility = "visible" ;
404
-
405
434
return { width, height } ;
406
435
}
407
436
@@ -595,6 +624,7 @@ class Popover extends Popup {
595
624
switch ( this . horizontalAlign ) {
596
625
case PopoverHorizontalAlign . Center :
597
626
case PopoverHorizontalAlign . Stretch :
627
+
598
628
left = targetRect . left - ( popoverSize . width - targetRect . width ) / 2 ;
599
629
break ;
600
630
case PopoverHorizontalAlign . Left :
0 commit comments