The transform style prop rotates, scales, skews and moves a view after layout has already placed it. Layout never sees the result, so a rotated stamp or an enlarged card can cover its neighbors without pushing them aside. Any room a transformed view needs beyond its layout box is room you have to leave yourself.
Key terms
- transform: a style prop that takes an array of transform objects or a space-separated string and changes how and where a view is drawn.
- transformOrigin: the point around which a view's transforms are applied,
centerby default. - translateX and translateY: transforms that move a view by a number of dp or by a percentage of its own width or height.
Layout runs first
Flexbox computes every frame before any transform applies. The native side resolves the transform from the view's computed frame size, so the transform reads layout and never feeds back into it. When you scale a view up, its siblings keep their positions and the scaled view grows over them. To keep the area around it clear, give the transformed view or its neighbors a margin, or give the container some padding.
One key per object
Each object in the array holds exactly one key and its value. Two keys in one object fail in development:
tsxtransform: [{rotate: '-8deg'}, {scale: 1.05}] // ok transform: [{rotate: '-8deg', scale: 1.05}] // error in development
The second line fails with You must specify exactly one property per transform object. The available keys are matrix, perspective, rotate, rotateX, rotateY, rotateZ, scale, scaleX, scaleY, translate (an array of two values, x and y), translateX, translateY, skewX and skewY, and development builds check their value types too. A rotation takes a string in deg or rad, a skew a string in degrees, scale, scaleX and scaleY a number, and perspective a number other than zero. So {rotate: 15} fails, where {rotate: '15deg'} works. matrix takes a 4x4 matrix as 16 numbers in column-major order, and combining it with other transform objects logs a console error.
The string form uses CSS-like functions: 'rotate(-8deg) scale(1.05)' is the string version of the first array above. In both forms the operations are multiplied together in order to build one matrix. The old standalone props such as rotation, scaleX and transformMatrix are deprecated in favor of transform.
Percentages and the pivot point
A translate value is either a number of dp or a percentage string. translateX: '50%' moves the view by half of its own width, and a translateY percentage is relative to the view's own height, so the size of the parent plays no part. Percentage translation is supported on the New Architecture.
transformOrigin sets the point around which the transforms are applied. It defaults to center and accepts px values, percentages and the keywords top, left, right, bottom and center, written as one, two or three values. In the two-value form the first value is usually the x-offset and the second the y-offset. A top or bottom keyword may come before the horizontal one, so 'top right' works the same as 'right top'. left and right are accepted only in the x position, though, so 'center left' and 'center right' throw an error. A third value is the z-offset and must be in px. Parts you leave out start from the center with no z-offset, the same as '50% 50% 0'.
There is also an array form with exactly three items, such as ['0%', '100%', 0] for the bottom-left corner, whose last item can't be a percentage. It avoids string parsing and suits Animated values.
Stamping a ticket
The TicketStub example places a USED stamp right after a ticket in a row, then pulls it back across the ticket's edge:
tsxtransformOrigin: 'left top', transform: [{translateX: '-50%'}, {rotate: '-12deg'}],
The stamp is 80 dp wide, so '-50%' moves it 40 dp left, over the last 40 dp of the ticket. With the origin at 'left top', the rotation turns the stamp around its top-left corner instead of its center: that corner stays where the translate put it while the rest of the stamp turns around it.
Layout still reserves the stamp's original slot. The row keeps its width, and a view placed after the stamp starts where it would without any transform, leaving a gap between it and the drawn stamp. Closing that gap is a job for margins, since the transform can't move anything in layout.
Code Examples
import {StyleSheet, Text, View} from 'react-native';
export function TicketStub({seat}: {seat: string}) {
return (
<View style={styles.row}>
<View style={styles.ticket}>
<Text style={styles.seat}>{seat}</Text>
</View>
<View style={styles.stamp}>
<Text style={styles.stampText}>USED</Text>
</View>
</View>
);
}
const styles = StyleSheet.create({
row: {flexDirection: 'row', alignItems: 'center', padding: 16},
ticket: {
width: 200,
height: 80,
justifyContent: 'center',
paddingHorizontal: 16,
borderRadius: 8,
backgroundColor: '#fef3c7',
},
seat: {fontSize: 16, fontWeight: '600'},
stamp: {
width: 80,
height: 32,
alignItems: 'center',
justifyContent: 'center',
borderWidth: 2,
borderColor: '#dc2626',
borderRadius: 4,
// Pivot on the top-left corner instead of the center
transformOrigin: 'left top',
// -50% of the stamp's own width: 40 dp to the left
transform: [{translateX: '-50%'}, {rotate: '-12deg'}],
},
stampText: {color: '#dc2626', fontWeight: '700', letterSpacing: 2},
});