React Native UI Scaling Utility

0.4.2 · active · verified Sun Apr 19

react-native-size-matters is a lightweight utility library for React Native designed to simplify the scaling of UI elements across various device screen sizes. It helps developers avoid manually adjusting layouts for different screen dimensions by providing a set of robust scaling functions (e.g., `scale`, `verticalScale`, `moderateScale`, `moderateVerticalScale`) and a `ScaledSheet` API that automatically processes style annotations. The library is currently stable at version `0.4.2`, with recent minor updates suggesting an active development and maintenance cadence. Its key differentiators include being lightweight with zero external dependencies beyond `react-native` itself, and offering flexible scaling options, including linear and moderate scaling with adjustable factors. It promotes a "develop once" approach, aiming for consistent UI appearance starting from a standard ~5-inch mobile device.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates both functional scaling (`moderateScale`, `verticalScale`) and the `ScaledSheet` API with various annotations (`@s`, `@vs`, `@ms`, `@ms<factor>`) to scale UI elements like text and boxes, adapting them to different screen sizes in React Native. It also shows a regular `StyleSheet` for comparison.

import React from 'react';
import { View, Text, StyleSheet } from 'react-native';
import { scale, verticalScale, moderateScale, ScaledSheet } from 'react-native-size-matters';

const App = () => {
  return (
    <View style={styles.container}>
      <Text style={styles.scaledText}>Scaled Text (Width based: 20@s)</Text>
      <View style={styles.box}>
        <Text style={styles.boxText}>Box</Text>
      </View>
      <Text style={{ fontSize: moderateScale(16, 0.3), marginTop: verticalScale(10) }}>
        Moderately Scaled Text
      </Text>
      <Text style={rawStyles.normalText}>Normal Text (No Scaling)</Text>
    </View>
  );
};

const rawStyles = StyleSheet.create({
  normalText: {
    fontSize: 16,
    color: '#666',
    marginTop: 20
  }
});

const styles = ScaledSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
    padding: '20@ms'
  },
  scaledText: {
    fontSize: '20@s',
    color: '#333',
    marginBottom: '10@vs'
  },
  box: {
    width: '150@s',
    height: '100@vs',
    backgroundColor: 'lightblue',
    justifyContent: 'center',
    alignItems: 'center',
    borderRadius: '8@ms0.3',
    marginVertical: '20@vs'
  },
  boxText: {
    fontSize: '18@ms',
    color: 'darkblue'
  }
});

export default App;

view raw JSON →