Maybe when you show Popup-B you can also hide Popup-A? That or you can use set states to only show one at a time. Then no need to worry about which one is at the front.
www.nocodeminute.com
Hope that helps!