@akshay-khapare/react-native-responsive-size v1.1.2
React Native Responsive Size ๐ฑ
A production-ready, performance-optimized React Native utility for managing responsive dimensions across different screen sizes and orientations. Built with TypeScript and designed for reliability.
Personal Note
This package was created to solve specific responsive design challenges in my personal React Native projects. I've made it public in case others find it useful for their projects. While I maintain it primarily for my own use, I'm happy to share it with the community.
โจ Features
- ๐ฑ Responsive calculations based on screen dimensions
- ๐ Automatic updates on screen rotation
- โก Lightweight with zero dependencies
- ๐ก๏ธ Full TypeScript support with proper typings
- ๐ Platform-specific handling (iOS/Android)
- ๐ฏ Notch/Dynamic Island detection
- ๐ Extended functionality for width, height, spacing, and radius
- ๐ Automatic cleanup to prevent memory leaks
- ๐ NEW: Performance optimization with value caching
- ๐งฐ NEW: Configurable with custom options
- ๐ NEW: Comprehensive error handling
- ๐งช NEW: Fully tested with Jest
๐ฆ Installation
# Using npm
npm install react-native-device-info @akshay-khapare/react-native-responsive-size
# Using yarn
yarn add react-native-device-info @akshay-khapare/react-native-responsive-size
# Using pnpm
pnpm add react-native-device-info @akshay-khapare/react-native-responsive-sizeMake sure to install react-native-device-info as it is a required peer dependency for this package.
Requirements
- React Native >= 0.60.0
- React Native Device Info >= 8.0.0
๐ Usage
Basic Example
import { ResValue, wp, hp, fs, spacing, radius } from '@akshay-khapare/react-native-responsive-size';
// Responsive value that scales with screen height
const buttonHeight = ResValue(50); // Consistent 50px base height across devices
// Width percentage (0-100)
const containerWidth = wp(90); // 90% of screen width
// Height percentage (0-100)
const containerHeight = hp(50); // 50% of screen height
// Font size
const titleSize = fs(24); // Responsive font size
// Spacing
const padding = spacing(16); // Responsive padding
// Border radius
const borderRadius = radius(8); // Responsive border radiusCleanup in App.tsx
To prevent memory leaks, make sure to clean up event listeners when your app unmounts:
import { cleanup } from '@akshay-khapare/react-native-responsive-size';
import { useEffect } from 'react';
function App() {
useEffect(() => {
// Cleanup function to remove dimension listeners
return () => cleanup();
}, []);
return (
// Your app content
);
}NEW: Configuration Options
import { configure } from '@akshay-khapare/react-native-responsive-size';
// Configure the library with custom options
configure({
// Use a different standard screen height (default: 812)
standardScreenHeight: 844, // iPhone 12/13 height
// Enable/disable performance caching (default: true)
enableCaching: true
});๐ API Reference
Functions
ResValue(baseSize: number, standardScreenHeight?: number): Calculate responsive values that scale proportionally with screen height- Perfect for creating consistent UI elements without direct width/height dependencies
- Automatically adjusts sizes based on device screen proportions
- Performance-optimized with caching for frequent calculations
Example uses:
// Button height that scales consistently across devices const buttonHeight = ResValue(50); // Base size of 50px // Container padding that maintains proportions const padding = ResValue(20); // Base padding of 20px // Custom scaling with different reference height const customSize = ResValue(30, 720); // Base: 30px, Reference: 720px
wp(percentage: number): Convert width percentage to responsive pixels- Handles invalid inputs with graceful degradation
- Performance-optimized with caching
hp(percentage: number): Convert height percentage to responsive pixels- Handles invalid inputs with graceful degradation
- Performance-optimized with caching
fs(size: number): Convert font size to responsive size- Safe handling of negative inputs
spacing(size: number): Convert spacing value to responsive size- Safe handling of negative inputs
radius(size: number): Convert border radius to responsive size- Safe handling of negative inputs
cleanup(): Remove dimension change listeners and clear cacheshasNotch(): Check if device has a notch/dynamic islandconfigure(options): Configure the library with custom optionsstandardScreenHeight: Custom reference height (default: 812px - iPhone X)enableCaching: Enable/disable performance caching (default: true)
getScreenDimensions(): Get current screen dimensions- Returns
{ width: number, height: number }
- Returns
Example Component
import { wp, hp, fs, spacing, radius } from '@akshay-khapare/react-native-responsive-size';
const styles = StyleSheet.create({
container: {
width: wp(90),
height: hp(20),
padding: spacing(16),
borderRadius: radius(8),
},
text: {
fontSize: fs(16),
marginBottom: spacing(8),
},
});๐ Best Practices
- Always call
cleanupin your App.tsx useEffect return function to prevent memory leaks - Use consistent base sizes across your app for uniform scaling
- Test your UI on different screen sizes and orientations
- Consider platform-specific adjustments when necessary
- For better performance, avoid calling responsive functions in tight loops
- Consider setting
enableCaching: falseif you're experiencing unexpected behavior - For highly dynamic UIs, benchmark with and without caching to find optimal settings
๐ Performance Considerations
This library is optimized for performance with built-in caching. When you use functions like ResValue, wp, or hp multiple times with the same parameters, the results are cached to avoid recalculation.
The cache is automatically invalidated when:
- Screen dimensions change (rotation)
- Configuration is updated via
configure() - The library is cleaned up with
cleanup()
If you're experiencing any issues with cached values not updating as expected, you can disable caching:
import { configure } from '@akshay-khapare/react-native-responsive-size';
configure({ enableCaching: false });๐ค Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Development
# Install dependencies
npm install
# Build the package
npm run build
# Run tests
npm test
# Lint code
npm run lint
# Format code
npm run format๐ Changelog
See CHANGELOG.md for details on version updates.
๐ License
MIT