Skip to content

Commit 20454f9

Browse files
committed
Tile aspect ratio is now adjustable.
1 parent 3fb608a commit 20454f9

File tree

3 files changed

+505
-64
lines changed

3 files changed

+505
-64
lines changed

react-native/components/createTiledComponent/index.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,16 @@ const globalStyles = StyleSheet.create({
2121
* height, and as many will be fit on a row as possible
2222
* without tiles being smaller than this; they may be
2323
* larger to fill the container horizontally).
24+
* @param aspectRatio When null, the height of each tile is fluid (and rows
25+
* are aligned to the top). Otherwise, the desired
26+
* aspect ratio of each tile (where 16/9 = 16:9).
2427
* @returns The created React component.
2528
*/
2629
export const createTiledComponent = (
2730
columnSpacing: number,
2831
rowSpacing: number,
29-
minimumTileSize: number
32+
minimumTileSize: number,
33+
aspectRatio: null | number
3034
): React.FunctionComponent<React.PropsWithChildren<Record<never, never>>> => {
3135
const Tiled: React.FunctionComponent<React.PropsWithChildren<Record<never, never>>> = ({ children }) => {
3236
const [sizing, setSizing] = React.useState<null | {
@@ -75,10 +79,14 @@ export const createTiledComponent = (
7579
style.paddingLeft = columnSpacing
7680
}
7781

78-
if (transformedChildren.length < sizing.perRow || rowSpacing === 0) {
79-
style.height = sizing.size
82+
if (aspectRatio === null) {
83+
if (transformedChildren.length >= sizing.perRow && rowSpacing !== 0) {
84+
style.paddingTop = rowSpacing
85+
}
86+
} else if (transformedChildren.length < sizing.perRow || rowSpacing === 0) {
87+
style.height = sizing.size / aspectRatio
8088
} else {
81-
style.height = sizing.size + rowSpacing
89+
style.height = sizing.size / aspectRatio + rowSpacing
8290
style.paddingTop = rowSpacing
8391
}
8492

react-native/components/createTiledComponent/readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const ExampleTiledComponent = createTiledComponent(
1212
20, // Column spacing.
1313
50, // Row spacing.
1414
150, // Minimum tile size.
15+
16 / 9, // Aspect ratio of each tile. May be null for fluid height.
1516
);
1617

1718
const ExampleScreen = () => (

0 commit comments

Comments
 (0)