A comprehensive responsive utility library for React Native applications that helps handle different screen sizes, orientations, and device characteristics.
- 📱 Device-aware scaling and dimensions
- 🔄 Orientation support
- 🌓 Dark/Light mode adaptation
- ⌨️ Keyboard-aware layouts
- 📏 Safe area handling
- 🔤 Dynamic text scaling
- 🖥️ Multi-window mode support
- 🔄 RTL support
- ♿ Accessibility utilities
- Copy the
srcdirectory to your project - Import the utilities in your components:
import responsive from './path/to/src/core';- In your project's
package.json, add a local dependency:
{
"dependencies": {
"react-native-responsive": "file:../path/to/this/library"
}
}- Run:
npm install-
Version Control:
- Easy version management through npm
- Semantic versioning support
- Dependency tracking
-
Updates and Maintenance:
- Centralized updates
- Bug fixes available to all projects
- Feature additions propagate automatically
-
Build Optimization:
- Pre-compiled TypeScript definitions
- Tree-shaking support
- Smaller bundle sizes
-
Development Workflow:
- Consistent API across projects
- TypeScript support out of the box
- Better IDE integration
-
Prepare for Publishing:
# Build the library npm run build # Test the build npm test # Update version npm version patch # or minor, or major
-
Publish to npm:
# Login to npm npm login # Publish the package npm publish
-
Using in Other Projects:
# Install from npm npm install react-native-responsive # Import in your code import responsive from 'react-native-responsive';
-
Version Management:
- Use semantic versioning (MAJOR.MINOR.PATCH)
- Document breaking changes
- Maintain a changelog
-
TypeScript Support:
- Keep type definitions up to date
- Export all necessary types
- Document type usage
-
Documentation:
- Keep README.md updated
- Add JSDoc comments
- Provide usage examples
-
Testing:
- Maintain test coverage
- Test on different devices
- Test with different React Native versions
-
Performance:
- Optimize bundle size
- Use proper tree-shaking
- Minimize dependencies
-
Dependencies:
- Keep peer dependencies minimal
- Use exact versions for critical dependencies
- Document required peer dependencies
-
Compatibility:
- Test with different React Native versions
- Support both old and new React Native features
- Handle breaking changes gracefully
-
Maintenance:
- Regular updates
- Bug fix releases
- Security patches
-
Community:
- Accept contributions
- Respond to issues
- Provide support
// Scale a size based on screen width
const scaledSize = responsive.scale(16);
// Scale width and height separately
const scaledWidth = responsive.scaleWidth(100);
const scaledHeight = responsive.scaleHeight(200);
// Get percentage-based dimensions
const width = responsive.widthPercentage(50); // 50% of screen width
const height = responsive.heightPercentage(25); // 25% of screen heightconst styles = StyleSheet.create({
container: responsive.createResponsiveStyle({
padding: 16,
margin: 8,
borderRadius: 8
})
});// Scale text size
const fontSize = responsive.fontScale(16);
// Use predefined text styles
const styles = StyleSheet.create({
title: responsive.getTextStyle('title1', {
color: '#000000'
})
});// Add safe area insets
const styles = responsive.withSafeArea({
padding: 16
});
// Get safe area insets
const insets = responsive.getSafeAreaInsets();// Get adaptive colors
const backgroundColor = responsive.adaptiveColor('#FFFFFF', '#000000');
// Create adaptive styles
const styles = responsive.withAdaptiveColors(
{ backgroundColor: '#FFFFFF' },
{ backgroundColor: '#000000' }
);Configure the responsive behavior:
responsive.configure({
baseWidth: 375, // iPhone X width
baseHeight: 812, // iPhone X height
enableOrientation: true,
widthFactor: 1,
heightFactor: 1,
fontScaleFactor: 0.9,
allowTextScaling: true,
tabletWidthFactor: 0.8,
tabletHeightFactor: 0.8,
splitScreenEnabled: true,
flipStylesInRTL: true,
adjustForKeyboard: true,
multiWindowThreshold: 0.75
});Get device characteristics:
const deviceInfo = responsive.deviceInfo;
// Access device properties
const isTablet = deviceInfo.isTablet;
const isPortrait = deviceInfo.isPortrait;
const screenWidth = deviceInfo.width;
const screenHeight = deviceInfo.height;scale(size: number): Scale a size based on screen widthscaleWidth(size: number): Scale width dimensionscaleHeight(size: number): Scale height dimensionfontScale(size: number, respectDeviceSettings?: boolean): Scale font sizemoderateScale(size: number, factor?: number): Scale with moderation factorwidthPercentage(percentage: number): Get percentage of screen widthheightPercentage(percentage: number): Get percentage of screen height
createResponsiveStyle(styles: Record<string, any>): Create responsive stylesresponsiveDimensions(width: number, height: number): Get scaled dimensionsspacing(multiplier?: number): Get consistent spacingwithSafeArea(style?: Record<string, any>, sides?: Array<'top' | 'bottom' | 'left' | 'right'>): Add safe area insetswithRTL(styles: Record<string, any>): Handle RTL layoutswithKeyboardAware(normalStyles: Record<string, any>, keyboardVisibleStyles: Record<string, any>): Handle keyboard visibility
getDeviceInfo(): Get current device informationgetFoldableAdjustedDimensions(): Get dimensions adjusted for foldable devicesgetKeyboardAdjustedHeight(): Get height adjusted for keyboardisDeviceRotating(): Check if device is rotating
dynamicTypeSize(style: keyof TextStyles, respectDeviceSettings?: boolean): Get dynamic type sizegetTextStyle(size: number | keyof TextStyles, additionalStyles?: Record<string, any>): Get text style
adaptiveColor(lightModeColor: string, darkModeColor: string): Get adaptive colorwithAdaptiveColors(lightModeStyles: Record<string, any>, darkModeStyles: Record<string, any>): Get adaptive stylesgetColorScheme(): Get current color scheme
This library is open for contributions. Feel free to submit issues and pull requests.
MIT
import { View, StyleSheet } from 'react-native';
import responsive from './core';
const GridExample = () => {
const styles = StyleSheet.create({
container: responsive.createResponsiveStyle({
flex: 1,
padding: responsive.spacing(2)
}),
row: {
flexDirection: 'row',
marginBottom: responsive.spacing(2)
},
// Responsive grid items
gridItem: {
width: responsive.widthPercentage(48), // 48% of screen width
height: responsive.scaleHeight(100),
marginHorizontal: responsive.widthPercentage(1), // 1% margin
backgroundColor: responsive.adaptiveColor('#F5F5F5', '#333333'),
borderRadius: responsive.scale(8)
}
});
return (
<View style={styles.container}>
<View style={styles.row}>
<View style={styles.gridItem} />
<View style={styles.gridItem} />
</View>
<View style={styles.row}>
<View style={styles.gridItem} />
<View style={styles.gridItem} />
</View>
</View>
);
};Problem Solved: Creates a responsive grid that:
- Adapts to different screen sizes
- Maintains proper spacing
- Handles dark/light mode
- Works on both phones and tablets
import { View, TextInput, StyleSheet } from 'react-native';
import responsive from './core';
const FormExample = () => {
const styles = StyleSheet.create({
container: responsive.withSafeArea({
flex: 1,
padding: responsive.spacing(2)
}),
input: {
...responsive.getTextStyle('body'),
height: responsive.scale(44),
borderWidth: responsive.scale(1),
borderColor: responsive.adaptiveColor('#CCCCCC', '#666666'),
borderRadius: responsive.scale(8),
paddingHorizontal: responsive.spacing(2),
marginBottom: responsive.spacing(2)
},
label: {
...responsive.getTextStyle('callout'),
marginBottom: responsive.spacing(1),
color: responsive.adaptiveColor('#333333', '#CCCCCC')
}
});
return (
<View style={styles.container}>
<Text style={styles.label}>Email</Text>
<TextInput
style={styles.input}
placeholder="Enter your email"
placeholderTextColor={responsive.adaptiveColor('#999999', '#666666')}
/>
</View>
);
};Problem Solved: Creates form elements that:
- Scale appropriately on all devices
- Maintain proper touch target sizes
- Adapt to system font settings
- Handle safe areas
- Support dark/light mode
import { View, Text, Image, StyleSheet } from 'react-native';
import responsive from './core';
const CardExample = () => {
const styles = StyleSheet.create({
card: {
...responsive.responsiveDimensions(300, 400),
backgroundColor: responsive.adaptiveColor('#FFFFFF', '#1A1A1A'),
borderRadius: responsive.scale(12),
padding: responsive.spacing(2),
margin: responsive.spacing(2),
shadowColor: '#000',
shadowOffset: {
width: 0,
height: responsive.scale(2)
},
shadowOpacity: 0.25,
shadowRadius: responsive.scale(3.84),
elevation: responsive.scale(5)
},
image: {
width: '100%',
height: responsive.scale(200),
borderRadius: responsive.scale(8),
marginBottom: responsive.spacing(2)
},
title: {
...responsive.getTextStyle('title2'),
color: responsive.adaptiveColor('#000000', '#FFFFFF'),
marginBottom: responsive.spacing(1)
},
description: {
...responsive.getTextStyle('body'),
color: responsive.adaptiveColor('#666666', '#AAAAAA')
}
});
return (
<View style={styles.card}>
<Image
source={{ uri: 'https://example.com/image.jpg' }}
style={styles.image}
/>
<Text style={styles.title}>Card Title</Text>
<Text style={styles.description}>
This is a responsive card that adapts to different screen sizes and themes.
</Text>
</View>
);
};Problem Solved: Creates cards that:
- Scale proportionally
- Maintain proper aspect ratios
- Adapt shadows and elevation
- Handle text scaling
- Support dark/light mode
import { View, Text, StyleSheet, StatusBar } from 'react-native';
import responsive from './core';
const HeaderExample = () => {
const styles = StyleSheet.create({
header: {
...responsive.withSafeArea({
backgroundColor: responsive.adaptiveColor('#FFFFFF', '#000000'),
paddingTop: responsive.spacing(2),
paddingBottom: responsive.spacing(2),
borderBottomWidth: responsive.scale(1),
borderBottomColor: responsive.adaptiveColor('#EEEEEE', '#333333')
}, ['top'])
},
title: {
...responsive.getTextStyle('title1'),
color: responsive.adaptiveColor('#000000', '#FFFFFF'),
textAlign: 'center'
},
statusBar: {
height: StatusBar.currentHeight,
backgroundColor: responsive.adaptiveColor('#FFFFFF', '#000000')
}
});
return (
<>
<View style={styles.statusBar} />
<View style={styles.header}>
<Text style={styles.title}>App Title</Text>
</View>
</>
);
};Problem Solved: Creates headers that:
- Handle safe areas properly
- Adapt to status bar height
- Scale text appropriately
- Support dark/light mode
- Work on different devices
import { Modal, View, Text, StyleSheet, TouchableOpacity } from 'react-native';
import responsive from './core';
const ModalExample = ({ visible, onClose }) => {
const styles = StyleSheet.create({
modal: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'rgba(0, 0, 0, 0.5)'
},
content: {
...responsive.responsiveDimensions(300, 400),
backgroundColor: responsive.adaptiveColor('#FFFFFF', '#1A1A1A'),
borderRadius: responsive.scale(16),
padding: responsive.spacing(3)
},
title: {
...responsive.getTextStyle('title2'),
color: responsive.adaptiveColor('#000000', '#FFFFFF'),
marginBottom: responsive.spacing(2)
},
button: {
...responsive.getAccessibleTouchTarget({
backgroundColor: responsive.adaptiveColor('#007AFF', '#0A84FF'),
borderRadius: responsive.scale(8),
padding: responsive.spacing(2),
alignItems: 'center'
})
},
buttonText: {
...responsive.getTextStyle('callout'),
color: '#FFFFFF'
}
});
return (
<Modal
visible={visible}
transparent
animationType="fade"
>
<View style={styles.modal}>
<View style={styles.content}>
<Text style={styles.title}>Modal Title</Text>
<TouchableOpacity
style={styles.button}
onPress={onClose}
>
<Text style={styles.buttonText}>Close</Text>
</TouchableOpacity>
</View>
</View>
</Modal>
);
};Problem Solved: Creates modals that:
- Scale appropriately on all devices
- Maintain proper touch targets
- Handle safe areas
- Support dark/light mode
- Provide proper accessibility
-
Screen Size Adaptation
- Problem: Components look too large on small screens or too small on large screens
- Solution: Use
scale,scaleWidth, andscaleHeightfor proportional scaling
-
Text Scaling
- Problem: Text becomes unreadable on different devices
- Solution: Use
fontScaleanddynamicTypeSizefor proper text scaling
-
Safe Area Handling
- Problem: Content hidden behind notches or system UI
- Solution: Use
withSafeAreato respect safe areas
-
Orientation Changes
- Problem: Layout breaks when device rotates
- Solution: Use
responsiveDimensionsand percentage-based layouts
-
Accessibility
- Problem: Touch targets too small on some devices
- Solution: Use
getAccessibleTouchTargetfor proper sizing
-
Dark/Light Mode
- Problem: Colors don't adapt to system theme
- Solution: Use
adaptiveColorandwithAdaptiveColors
-
RTL Support
- Problem: Layout breaks in RTL languages
- Solution: Use
withRTLfor proper RTL handling
-
Keyboard Handling
- Problem: Content hidden behind keyboard
- Solution: Use
withKeyboardAwarefor keyboard-aware layouts