I’m trying to create a responsive square that will automatically resize to fit the size of its container.
I’ve tried using aspectRatio: 1/1, but whenever I set any width/height/maxWidth/maxHeight styles it seems to either overwrite the aspect ratio or fail to constrain in one direction. Either it constrains vertically and overflows on the horizontal when the parent’s width becomes less than its height, or vice-versa for the horizontal.
Is there a set of properties that will make this work as intended? And if not, is there a workaround?
Ah, context is important, in the Coordinate Container, aspectRatio is expressed as 1:1. You did not mention that you were on a flex container using custom styles for that.
You have the alignItems set to center, you need to set it to strech. You actually don't need the aspectRatio style. Setting it to strech will make it fill the entire height, the grow setting of 1 will make it so that it always fills the width. I've used the aspectRatio here just to duplicate your example.
That’s not quite what I was aiming for. I wanted the red square to maintain its square shape (all sides the same length), and just resize so it fully fits within its parent container without overflowing.
Better version. The 1st wasn’t really responsive in the browser. It just maintained size. This one actually adjusts. It’s setup to be 50% of the width of the viewport.