Code Monkey home page Code Monkey logo

react-native-toolkit / react-native-responsive-dimensions Goto Github PK

View Code? Open in Web Editor NEW
474.0 9.0 39.0 3.63 MB

Resposive fontSize, height and width for react-native components, that automatically adjusts itself based on screen-size of the device.

Home Page: https://www.npmjs.com/package/react-native-responsive-dimensions

License: MIT License

JavaScript 11.86% TypeScript 80.40% Shell 7.75%
react-native responsive react-native-component font-size dimensions responsive-design hacktoberfest

react-native-responsive-dimensions's Introduction

๐Ÿ“๐Ÿ“ฑ React Native Responsive Dimensions ๐ŸŒ๐Ÿ“

Responsive height, width and responsive fontSize for your react native components!

The dimensions auto adjust based on the window size (view port) or the screen size of the device ๐Ÿ™Œ๐Ÿฝ

Support for responsive dimension hooks to help auto-adjust dimensions for devices whose display or screen sizes may change such as foldable phones or browser windows! ๐Ÿ˜Ž

Build Status Maintainability Test Coverage

Version Downloads Bundlephobia

Star on GitHub Watch on GitHub Twitter Follow


Compatible with Expo & React Native Web ๐Ÿš€

PRs Welcome ๐Ÿ‘โœจ

Installation

#npm
npm install --save react-native-responsive-dimensions

#yarn
yarn add react-native-responsive-dimensions

Usage

While working with mobile devices, there are two kinds of dimensions you will have to focus on

  • Window Dimensions ๏นฃ which is the size of the window / view port on which your app is being displayed
  • Screen Dimensions ๏นฃ this is the total screen dimensions of your device (your app may occupy the entire screen or only a portion of the screen)

Working with Window Dimensions

import { StyleSheet } from "react-native";
import {
  responsiveHeight,
  responsiveWidth,
  responsiveFontSize
} from "react-native-responsive-dimensions";

const styles = StyleSheet.create({
  container: {
    justifyContent: "center",
    height: responsiveHeight(50), // 50% of window height
    width: responsiveWidth(50), // 50% of window width
    alignItems: "center"
  },
  sampleText: {
    fontSize: responsiveFontSize(2) // 2% of total window size
  }
});

Working with Screen Dimensions

import { StyleSheet } from "react-native";
import {
  responsiveScreenHeight,
  responsiveScreenWidth,
  responsiveScreenFontSize
} from "react-native-responsive-dimensions";

const styles = StyleSheet.create({
  container: {
    justifyContent: "center",
    height: responsiveScreenHeight(50), // 50% of Screen height
    width: responsiveScreenWidth(50), // 50% of Screen width
    alignItems: "center"
  },
  sampleText: {
    fontSize: responsiveScreenFontSize(2) // 2% of total screen size
  }
});

Responsive hooks

The above responsive dimension methods do not auto update once the value is set. They are suitable for using within StyleSheet.create method as the values don't change once set. However, you might want your views to respond to dimension changes such as screen rotation, device folding (in foldable devices) & browser window resizing (react-native-web).

The values set by these hooks auto respond to changes. The following hooks are available for use ๏นฃ

  • useResponsiveHeight
  • useResponsiveWidth
  • useResponsiveFontSize
  • useResponsiveScreenHeight
  • useResponsiveScreenWidth
  • useResponsiveScreenFontSize
  • useDimensionsChange

Sample Usage

import React from "react";
import { View } from "react-native";
import {
  useResponsiveHeight,
  useResponsiveWidth
} from "react-native-responsive-dimensions";

const App = () => {
  const height = useResponsiveHeight(25);
  const width = useResponsiveWidth(25);

  return <View style={{ height, width }} />;
};

Reacting to dimension changes with useDimensionsChange

useDimensionsChange basically calls a function whenever the dimensions update with new window & screen dimensions as arguments. This is a good place to include your layout animations if your UI layout reacts to dimension updates and you want to make the transitions smooth.

import React, { useCallback } from "react";
import { View, LayoutAnimation } from "react-native";
import {
  useResponsiveHeight,
  useResponsiveWidth,
  useDimensionsChange
} from "react-native-responsive-dimensions";

const App = () => {
  const height = useResponsiveHeight(25);
  const width = useResponsiveWidth(25);

  useDimensionsChange(
    useCallback(({ window, screen }) => {
      LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut);
    }, [])
  );

  return <View style={{ height, width }} />;
};

Examples

Why Responsive Dimensions

I built responsive dimensions as a personal tool to tackle some of my problems I face during my everyday app development. You might want to use it if your usecase comes under one of the following scenarios.

  • While working with React Native UI (especially animations) there are lots of scenarios that require calculating a certain percentage of the display area.

  • If your app supports tablets then you might want to scale some of your fonts & UI Components based on the display size.

  • If you are using react-native-web or targetting foldable devices your UI needs to react to the changes in the window dimensions.

react-native-responsive-dimensions's People

Contributors

ashishpandey001 avatar beginci avatar daniakash avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-native-responsive-dimensions's Issues

Does not respond to screen size changes on external display

I was trying to use this to respond to screen size changes on an external display like a LCD projector.

react-native-responsive-dimensions does not seem to change font size on the external screen based on its dimensions or resolution.

Using GitHub - mybigday/react-native-external-display: React Native view renderer in External Display

(open the screenshots in separate tabs, as Github resizes the images)

640 x 480
image

1280 x 720
image

Expected result would have been that both screens would fit about the same amount of text with the font being bigger on the 1280 x 720 screen, but this did not happen.

Is there any way to make this work with the current version of react-native-responsive-dimensions?

SYSTEM INFO:
yarn run v1.22.5
System:
OS: macOS Mojave 10.14.6
CPU: (8) x64 Intel(R) Core(TM) i7-4770K CPU @ 3.50GHz
Memory: 4.42 GB / 32.00 GB
Shell: 3.2.57 - /bin/bash
Binaries:
Node: 14.17.0 - /var/folders/9l/wwt6ljv10b9fhwjsybmw3d140000gn/T/yarn--1621697472231-0.6683796296679365/node
Yarn: 1.22.5 - /var/folders/9l/wwt6ljv10b9fhwjsybmw3d140000gn/T/yarn--1621697472231-0.6683796296679365/yarn
npm: 6.14.13 - ~/.nodenv/versions/14.17.0/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
Managers:
CocoaPods: 1.10.1 - /usr/local/bin/pod
SDKs:
iOS SDK:
Platforms: iOS 13.2, DriverKit 19.0, macOS 10.15, tvOS 13.2, watchOS 6.1
Android SDK:
API Levels: 23, 28, 29, 30
Build Tools: 28.0.2, 28.0.3, 29.0.2
System Images: android-23 | Google APIs Intel x86 Atom, android-24 | Google Play Intel x86 Atom, android-29 | Google APIs Intel x86 Atom, android-29 | Google Play Intel x86 Atom
Android NDK: Not Found
IDEs:
Android Studio: 4.2 AI-202.7660.26.42.7322048
Xcode: 11.3.1/11C505 - /usr/bin/xcodebuild
Languages:
Java: 11.0.8 - /usr/bin/javac
Python: 3.7.7 - /Users/christian/.pyenv/shims/python
npmPackages:
@react-native-community/cli: Not Found
react: 16.13.1 => 16.13.1
react-native: 0.63.4 => 0.63.4
react-native-macos: Not Found
npmGlobalPackages:
react-native: Not Found
Done in 3.15s.

Missing declaration file

I am receiving an error when I try to import this that says "Could not find a declaration file for module 'react-native-responsive-dimensions". ' implicitly has an 'any' type. Try 'npm install @types/react-native-responsive-dimensions' if it exists or add a new declaration (.d.ts) file containing 'declare module 'react-native-responsive-dimensions';'

Debounce Responsive hooks?

Hi, would it be possible to add a debounce to the responsive hooks, so the number of re-renders per time is limited?

At the moment the hook is firing very often during resizing the browser window or rotating the device, causing a lot of re-rendering.

Android support

Nice job on iOS !

Is Android is support by this package ?

update release on npm

Hello,

It has been a long time since you merged in my PR, you should release the current master on npm so that people can use the package via npm registry instead of using the master branch.

responsiveFontSize() not working on samsung glaxy note 8

for ios its working fine below is screenshot
simulator screen shot - iphone 6 - 2018-04-25 at 14 54 46

but for androd its not working only on this device
screenshot_20180423-134218

Device specification :-

Resolution | 1440 x 2960 pixels, 18.5:9 ratio (~521 ppi density)
Size | 6.3 inches, 101.1 cm2ย (~83.2% screen-to-body ratio)

RN method roundToNearestPixel is not used

Hi @DaniAkash,

your solution is good, I use a very similar one for my RN projects. The difference from my solution is that I use roundToNearestPixel RN method that rounds a layout size (dp) to the nearest layout size that corresponds to an integer number of pixels. And I was about to create a npm package of my own until I saw yours.

I would be happy to provide a PR, if you are actively maintaining this package :)

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.