How to make shapes stack to the left when others are invisible?

I have a “clear search field” button, which is invisible as long as the search field isn’t focused. Another favorite search button (heart) is situated on the right of this cancel button.

However, I would like this favorite search button to be stacking to the left whenever the “clear search field” button is invisible (i.e. when search bar unfocused.

Whatnextapp (2)

Can this be done? Or is there a work around to avoid the gap?