react-native-smart-dimension
is a lightweight and flexible library for handling responsive design in React Native applications. It provides hooks to manage device types, screen dimension, and scalable layouts for various screen sizes, including mobile and tablet devices.
You can install the library using npm or yarn:
npm install react-native-smart-dimension
or
yarn add react-native-smart-dimension
- Detects device type (mobile or tablet)
- Provides responsive height, width, and font scaling utilities
- Listens for screen dimension changes in real-time
- Supports percentage-based and font scaling calculations
- Optimized for React Native projects
import { useDeviceType } from "react-native-smart-dimension";
const { isTablet, platform } = useDeviceType();
console.log(`Device Type: ${isTablet ? "Tablet" : "Mobile"}, Platform: ${platform}`);
import { usedimensionListener } from "react-native-smart-dimension";
const { screen, window } = usedimensionListener();
console.log("Window dimension:", window);
console.log("Screen dimension:", screen);
import { useResponsiveHeight, useResponsiveWidth } from "react-native-smart-dimension";
const height = useResponsiveHeight(50); // 50% of window height
const width = useResponsiveWidth(80); // 80% of window width
import { useResponsiveFontSize } from "react-native-smart-dimension";
const fontSize = useResponsiveFontSize(2); // Scales based on screen size
import { useTableView } from "react-native-smart-dimension";
const { columnWidth, gap } = useTableView(3, 5); // 3 columns with 5% gap
Returns an object with:
-
isTablet
(boolean) – Whether the device is a tablet -
platform
(string) – Platform type (ios
orandroid
)
Returns an object with:
-
screen
– Full screen dimension -
window
– Usable window dimension
Calculates the responsive height based on percentage.
Calculates the responsive width based on percentage.
Scales the font size based on screen dimension.
Returns column width and gap based on total columns and percentage gap.
Utility function to calculate percentage-based dimension.
Utility function to calculate font size based on aspect ratio.
Pull requests and contributions are welcome! Feel free to improve this library and make it better for everyone.
MIT License