Limit max width

Looking for a more flexible or better way to calulate the popup position.