1.0.15 • Published 2 years ago

@hashiprobr/react-native-aspect-view v1.0.15

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

react-native-aspect-view

A React Native View that keeps a consistent aspect ratio in all platforms

React Native offers the aspectRatio layout prop to control the size of the undefined dimension of a component. However, React Native for Web implements this prop using the aspect-ratio CSS property, which is similar but not equivalent.

The AspectView React Component is a View that uses aspectRatio in Android and iOS but does not use aspect-ratio in the web. Instead, it uses onLayout to emulate the behavior.

Peer dependencies

{
    "react": "^17.0.1",
    "react-native": ">=0.64.3"
}

Install

With npm:

npm install @hashiprobr/react-native-aspect-view

With yarn:

yarn add @hashiprobr/react-native-aspect-view

If using Expo, add the module to webpack.config.js:

const createExpoWebpackConfigAsync = require('@expo/webpack-config');

module.exports = async function (env, argv) {
    const config = await createExpoWebpackConfigAsync({
        ...env,
        babel: {
            dangerouslyAddModulePathsToTranspile: [
                '@hashiprobr/react-native-aspect-view',
            ],
        },
    }, argv);
    return config;
};

If webpack.config.js does not exist, create it with:

expo customize:web

Props

namedescription
ratioa number representing the width/height ratio (default 1)

...View props

Example

Square view:

import { Text } from 'react-native';

import AspectView from '@hashiprobr/react-native-aspect-view';

export default function MyComponent() {
    return (
        <AspectView ratio={1}>
            <Text>hello world</Text>
        </AspectView>
    );
}

Width larger than height:

import { Text } from 'react-native';

import AspectView from '@hashiprobr/react-native-aspect-view';

export default function MyComponent() {
    return (
        <AspectView ratio={4/3}>
            <Text>hello world</Text>
        </AspectView>
    );
}

Height larger than width:

import { Text } from 'react-native';

import AspectView from '@hashiprobr/react-native-aspect-view';

export default function MyComponent() {
    return (
        <AspectView ratio={3/4}>
            <Text>hello world</Text>
        </AspectView>
    );
}
1.0.15

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago