Fix to resize the popup if the viewport does not permit

We made this change for a project and I thought it might be useful to have back in the original repo.