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-hook

v1.0.4

Published

Make React Native views responsive for all devices with the use of 2 simple methods.

Downloads

1,323

Readme

React Native Responsive Hook: Streamline Your UI Across All Devices 🚀

Keen on shaping the future of responsive UI? Your contributions are invaluable! Reach out at [email protected].

Contents

react-native-responsive-hook

npm version npm downloads

react-native-responsive-hook is an intuitive library offering a suite of hooks that make crafting responsive UIs in React Native effortless. It builds upon react-native-responsive-screen, adding custom hooks and enhanced functionalities for precise breakpoint detection and scalable component design.

Experience streamlined development and consistent UI across devices. Learn more in this detailed Medium article! 🚀

React Native Responsive Hook Example

Installation

Install with npm for a seamless setup:

npm install react-native-responsive-hook --save

Usage

Adapt your UI elements effortlessly with the following steps:

  1. Import: Fetch useResponsive from react-native-responsive-hook.
  2. Retrieve: Access wp, hp, vw, vh, isLandscape, isPortrait, rem, rf, isIOS, isAndroid, breakpointGroup using the hook.
  3. Implement: Apply these dynamic values to your component styles for a fluid, adaptive UI.

Understanding Breakpoints

breakpointGroup classifies the screen width into categories for targeted styling:

  • group1: 0 - 399 pixels
  • group2: 400 - 599 pixels
  • group3: 600 - 767 pixels
  • group4: 768 - 1007 pixels
  • group5: 1008 - 1279 pixels
  • group6: 1280 pixels and beyond

Dynamic Dimensions with wp, hp, vw, and vh

  • wp(percent): Calculates width based on screen percentage.
  • hp(percent): Calculates height based on screen percentage.
  • vw(percent): Calculates width based on viewport width percentage.
  • vh(percent): Calculates height based on viewport height percentage.

These functions use density-independent pixels (dp) to ensure consistency across various devices.

Responsive Font Scaling with rem and rf

  • rem(size): Converts font size to rem units.
  • rf(size): Converts font size to responsive font units (rf).

rem utilizes a multiplier to scale fonts based on the device's screen size, while rf ensures font sizes remain within a predefined range.

Platform Detection with isIOS and isAndroid

  • isIOS: Indicates if the platform is iOS.
  • isAndroid: Indicates if the platform is Android.

Use these booleans to tailor your UI specifically for each platform.

Examples

Responsive Box Example

// Import necessary packages
import React from 'react';
import { StyleSheet, View, Text } from 'react-native';
import { useResponsive } from 'react-native-responsive-hook';

// Define the App component
const App = () => {
  const { styles } = useStyles(); // Use the hook to get styles

  return (
    <View style={styles.container}>
      <View style={styles.responsiveBox}>
        <Text style={styles.text}>Responsive Box - Adjusts based on orientation, screen size, and platform.</Text>
      </View>
    </View>
  );
};

// Define the useStyles hook
const useStyles = () => {
  const { isLandscape, isPortrait, wp, hp, vw, vh, rem, rf, isIOS, isAndroid, breakpointGroup } = useResponsive(); // Destructure all properties from useResponsive
  
  // Utilize the hook values to create dynamic styles
  const styles = StyleSheet.create({
    container: {
      flex: 1,
      backgroundColor: isLandscape ? 'lightblue' : 'gray', // Change background color based on orientation
      alignItems: 'center',
      justifyContent: 'center',
    },
    responsiveBox: {
      borderWidth: 2,
      borderColor: 'orange',
      flexDirection: 'column',
      justifyContent: 'space-around',
      width: isPortrait ? wp(85) : wp(50),  // Adjust width based on orientation
      height: hp(17),                       // Adjust height using hp function
      backgroundColor: getBackgroundColorByGroup(breakpointGroup) // Change box color based on breakpoint group
    },
    text: {
      color: 'white',
      fontSize: rem(16), // Use rem for font size
      fontFamily: isIOS ? 'Helvetica' : 'Roboto', // Use different font families for iOS and Android
    }
  });

  return {styles};
};

function getBackgroundColorByGroup(breakpointGroup) {
  switch(breakpointGroup) {
    case 'group1': return 'lightgreen';
    case 'group2': return 'lightpink';
    case 'group3': return 'lightyellow';
    case 'group4': return 'lightcoral';
    case 'group5': return 'lightskyblue';
    case 'group6': return 'lightsteelblue';
    default: return 'white';
  }
}

export default App;

Want to Contribute?

Your contributions are welcome! Feel free to submit pull requests or contact me directly to discuss how you can get involved