Giter Site home page Giter Site logo

responsive-react's Introduction

responsive-react

NPM

Conditional rendering of react component for responsive display. Device-type based rendering of react components, like render when desktop and for mobile or tablet

Installing

npm install responsive-react --save

For typescript projects please use typed-responsive-react

Live Example

Getting started

Approach 1

import { Responsive } from "react-responsive";

<>
  <Responsive displayIn={["Mobile", "Tablet"]}>
    This is a MOBILE/TABLET
  </Responsive>
  <Responsive displayIn={["Laptop"]}>
    This is a LAPTOP or a Larger screen
  </Responsive>
</>

Note: iPad pro in landscape is considered as a laptop screen here.

Approach 2

import {
  isMobileDevice, 
  isTabletDevice,
  isLaptopDevice,
  isBiggerThanLaptop
} from "react-responsive/utilResponsive";

<>
  {isMobileDevice() && <h2>This is a MOBILE</h2>}

  {isTabletDevice() && <h2>This is a TABLET</h2>}

  {isLaptopDevice() && <h2>This is a LAPTOP</h2>}

  {isBiggerThanLaptop() && <h2>This is a LARGER SCREEN</h2>}
</>

Additional helper function:

import {getDeviceTypeInfo} from "react-responsive/utilResponsive";
 // helper function
const {
  width,
  height,
  deviceTypeVariant, 
  deviceType,
  orientation,
  isFallback
} = getDeviceTypeInfo();

/*
width = Width of viewport
height = Height of viewport
deviceTypeVariant = [MobileSmall, MobileMedium, MobileLarge, Tablet, iPadPro, LaptopSmall, LaptopLarge, LargerThanLaptop, LargeScreenMax]
deviceType = [Laptop, Tablet, Mobile, LargerThanLaptop]
orientation = [Landscape, Portrait]
isFallback = [true, false] default false. true if it detects some awkward dimension (rare)
*/

Edge cases:


  1. Resolution 768 x 1024 is considered tablet (mostly iPads), while 1024 x 768 is macbook air

  2. iPad pro has large screen (1024 x 1366 portrait) compared to other tablets (when landscaped is as big as a midium sized laptop)

responsive-react's People

Contributors

siddharthachowdhury avatar

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.