@@ -219,13 +219,31 @@ export class Dialog implements OnInit, AfterViewInit, OnDestroy {
219219 // split always retuns an array, so we can just use the auto position logic
220220 // for single positions too
221221 const placements = this . dialogConfig . placement . split ( "," ) ;
222- for ( const placement of placements ) {
222+ const weightedPlacements = placements . map ( placement => {
223223 const pos = findPosition ( parentEl , el , placement ) ;
224- if ( position . checkPlacement ( el , pos ) ) {
225- dialogPlacement = placement ;
226- break ;
227- }
228- }
224+ let box = position . getPlacementBox ( el , pos ) ;
225+ let hiddenHeight = box . bottom - window . innerHeight - window . scrollY ;
226+ let hiddenWidth = box . right - window . innerWidth - window . scrollX ;
227+ // if the hiddenHeight or hiddenWidth is negative, reset to offsetHeight or offsetWidth
228+ hiddenHeight = hiddenHeight < 0 ? el . offsetHeight : hiddenHeight ;
229+ hiddenWidth = hiddenWidth < 0 ? el . offsetWidth : hiddenWidth ;
230+ const area = el . offsetHeight * el . offsetWidth ;
231+ const hiddenArea = hiddenHeight * hiddenWidth ;
232+ let visibleArea = area - hiddenArea ;
233+ // if the visibleArea is 0 set it back to area (to calculate the percentage in a useful way)
234+ visibleArea = visibleArea === 0 ? area : visibleArea ;
235+ const visiblePercent = visibleArea / area ;
236+ return {
237+ placement,
238+ weight : visiblePercent
239+ } ;
240+ } ) ;
241+
242+ // sort the placments from best to worst
243+ weightedPlacements . sort ( ( a , b ) => b . weight - a . weight ) ;
244+ // pick the best!
245+ dialogPlacement = weightedPlacements [ 0 ] . placement ;
246+
229247 // calculate the final position
230248 const pos = findPosition ( parentEl , el , dialogPlacement ) ;
231249
0 commit comments