npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-native-responsive-utils

v1.0.0

Published

A library for handling responsive dimensions in React Native

Downloads

4

Readme

React Native Responsive Utils

React Native Responsive Utils is a library that provides utility functions for handling responsive dimensions in React Native applications.

Installation

To install the library, use the package manager npm or yarn. You need to have React Native installed in your project.

npm install react-native-responsive-utils

or

yarn add react-native-responsive-utils

Usage

Import the utility functions from the library:

import {
  convertWidthToPixel,
  convertHeightToPixel,
  addOrientationChangeListener,
  calculateResponsiveFontSize,
  calculateResponsiveSpacing
} from 'react-native-responsive-utils';

Converting Percentage to Pixels

You can convert width and height percentages to responsive pixel values using the convertWidthToPixel and convertHeightToPixel functions:

const widthInPixels = convertWidthToPixel('50%'); // Converts 50% of the screen width to pixels
const heightInPixels = convertHeightToPixel(25); // Converts 25% of the screen height to pixels

Getting Screen Orientation

You can get the current screen orientation using the addOrientationChangeListener function, which registers a callback to be invoked when the orientation changes:

const removeListener = addOrientationChangeListener((orientation) => {
  console.log('Screen orientation:', orientation);
});

// To remove the event listener later
removeListener();

Calculating Responsive Font Size and Spacing

You can calculate responsive font sizes and spacing values based on the device size using the calculateResponsiveFontSize and calculateResponsiveSpacing functions:

const responsiveFontSize = calculateResponsiveFontSize(16); // Calculates responsive font size based on the device size
const responsiveSpacing = calculateResponsiveSpacing(10); // Calculates responsive spacing value based on the device size

Examples

Example 1: Responsive Layout

import React from 'react';
import { View, StyleSheet } from 'react-native';
import {
  convertWidthToPixel,
  convertHeightToPixel,
  addOrientationChangeListener
} from 'react-native-responsive-utils';

const App = () => {
  const screenWidth = convertWidthToPixel('100%');
  const screenHeight = convertHeightToPixel('100%');

  const styles = StyleSheet.create({
    container: {
      width: screenWidth,
      height: screenHeight,
      backgroundColor: 'lightblue',
      justifyContent: 'center',
      alignItems: 'center',
    },
  });

  addOrientationChangeListener((orientation) => {
    console.log('New orientation:', orientation);
  });

  return <View style={styles.container}>{/* Your content here */}</View>;
};

export default App;

In this example, the convertWidthToPixel and convertHeightToPixel functions are used to set the dimensions of a container view to occupy the entire screen, regardless of the device size or orientation. The addOrientationChangeListener function is used to listen for orientation changes and log the new orientation.

Example 2: Responsive Font Size

import React from 'react';
import { Text, StyleSheet } from 'react-native';
import { calculateResponsiveFontSize } from 'react-native-responsive-utils';

const App = () => {
  const baseFontSize = 20;
  const responsiveFontSize = calculateResponsiveFontSize(baseFontSize);

  const styles = StyleSheet.create({
    text: {
      fontSize: responsiveFontSize,
      fontWeight: 'bold',
      color: 'black',
    },
  });

  return <Text style={styles.text}>Responsive Text</Text>;
};

export default App;

In this example, the calculateResponsiveFontSize function

is used to calculate a responsive font size based on the device size. The responsiveFontSize value is then used in the Text component's style to display a responsive text.

These examples demonstrate how the library can be used to create responsive layouts and text sizes in your React Native applications. Feel free to explore the various functions provided by the library and customize them according to your specific needs.

Contributing

Contributions are welcome! Please feel free to open issues or submit pull requests for any improvements or fixes.

License

This library is open-source and available under the MIT License.