All Projects → flexdinesh → React Socks

flexdinesh / React Socks

Licence: mit
🎉 React library to render components only on specific viewports 🔥

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to React Socks

Cordova Plugman
Apache Cordova Plugman
Stars: ✭ 379 (-5.25%)
Mutual labels:  library
Kooper
Kooper is a simple Go library to create Kubernetes operators and controllers.
Stars: ✭ 388 (-3%)
Mutual labels:  library
Fire Hpp
Fire for C++: Create fully functional CLIs using function signatures
Stars: ✭ 395 (-1.25%)
Mutual labels:  library
Androidplot
Charts and plots for Android
Stars: ✭ 381 (-4.75%)
Mutual labels:  library
Swaggen
OpenAPI/Swagger 3.0 Parser and Swift code generator
Stars: ✭ 385 (-3.75%)
Mutual labels:  library
Orc
Apache ORC - the smallest, fastest columnar storage for Hadoop workloads
Stars: ✭ 389 (-2.75%)
Mutual labels:  library
Lager
C++ library for value-oriented design using the unidirectional data-flow architecture — Redux for C++
Stars: ✭ 379 (-5.25%)
Mutual labels:  library
Virtualenv
Virtual Python Environment builder
Stars: ✭ 4,017 (+904.25%)
Mutual labels:  library
Linux Steam Integration
Helper for enabling better Steam integration on Linux
Stars: ✭ 386 (-3.5%)
Mutual labels:  library
Cornercutlinearlayout
Linear Layout that allow corner (parent and children) cuts, complex shadow and divider.
Stars: ✭ 391 (-2.25%)
Mutual labels:  library
Esp32 tft library
Full featured TFT library for ESP32 with demo application
Stars: ✭ 383 (-4.25%)
Mutual labels:  library
Pyexcelerate
Accelerated Excel XLSX Writing Library for Python 2/3
Stars: ✭ 384 (-4%)
Mutual labels:  library
Shadowview
An iOS Library that makes shadows management easy on UIView.
Stars: ✭ 391 (-2.25%)
Mutual labels:  library
Libzip
A C library for reading, creating, and modifying zip archives.
Stars: ✭ 379 (-5.25%)
Mutual labels:  library
Ptshowcaseviewcontroller
An initial implementation of a "showcase" view( controller) for iOS apps... Visualizes images, videos and PDF files beautifully! (by @pittleorg) [meta: image, photo, video, document, pdf, album, gallery, showcase, gallery, iOS, iPhone, iPad, component, library, viewer]
Stars: ✭ 395 (-1.25%)
Mutual labels:  library
Android Customtabs
Chrome CustomTabs for Android demystified. Simplifies development and provides higher level classes including fallback in case Chrome isn't available on device.
Stars: ✭ 378 (-5.5%)
Mutual labels:  library
React Native Simple Markdown
📜 React Native Markdown component (iOS & Android).
Stars: ✭ 389 (-2.75%)
Mutual labels:  library
Laravel Paystack
💳 📦 💰 Laravel 6, 7 and 8 Package for Paystack
Stars: ✭ 398 (-0.5%)
Mutual labels:  library
V8n
☑️ JavaScript fluent validation library
Stars: ✭ 3,858 (+864.5%)
Mutual labels:  library
Vugu
Vugu: A modern UI library for Go+WebAssembly (experimental)
Stars: ✭ 4,251 (+962.75%)
Mutual labels:  library

React Socks

Build Status npm version dependencies Status License: MIT

Wrap your components with React Socks to prevent unnecessary render in different viewports.

<Breakpoint small down>
  <MyAwesomeMobileMenu>
    This component will render only in mobile devices
  </MyAwesomeMobileMenu>
</Breakpoint>

Why? start with why

Conventionally we have been writing css media queries for different viewports to hide and show elements that are always present in the DOM. With React taking over the world, everything is about rendering components into the DOM. React Socks helps you conditionally render elements based on viewports.

  1. Render viewport specific components without hassle

  2. You can define your own breakpoints (Eg. xs, ipad, bigmonitors) and use them

  3. You can improve your app performance if you lazy load your viewport specific components

  4. Simpler and sweeter syntax for ease of use

Install

$ npm install --save react-socks

Usage

Just wrap your top level component with BreakpointProvider and use the Breakpoint component anywhere you need.

Note: BreakpointProvider was introduced only in v1.0.0. It's not available in previous alpha versions.

import  { Breakpoint, BreakpointProvider } from 'react-socks';

// entry file (usually App.js or index.js)
const App = () => (
  <BreakpointProvider>
    <Example />
  </BreakpointProvider>
);
// Example.js
const Example = () => {
  return (
    <div>
      <Breakpoint small down>
        <div>I will render only in mobile devices</div>
      </Breakpoint>

      <Breakpoint medium only>
        <div>I will render only in tablets (iPad, etc...)</div>
      </Breakpoint>

      <Breakpoint medium down>
        <div>I will render in tablets (iPad, etc...) and everything below (mobile devices)</div>
      </Breakpoint>

      <Breakpoint medium up>
        <div>I will render in tablets (iPad, etc...) and everything above (laptops, desktops)</div>
      </Breakpoint>

      <Breakpoint large up>
        <div>I will render in laptops, desktops and everything above</div>
      </Breakpoint>

      {/* Add breakpoints on the fly using custom queries */}

      <Breakpoint customQuery="(min-width: 500px)">
        <div style={{backgroundColor: 'red' }}>
          Custom breakpoint: (min-width : 500px)
        </div>
      </Breakpoint>
      
      <Breakpoint customQuery="(max-width: 1000px)">
        <div style={{backgroundColor: 'yellow' }}>
          Custom breakpoint: (max-width : 1000px)
        </div>
      </Breakpoint>
      
      <Breakpoint customQuery="(min-width: 500px) and (max-width: 700px)">
        <div style={{backgroundColor: 'lightblue' }}>
          Custom breakpoint: (min-width : 500px) && (max-width : 700px)
        </div>
      </Breakpoint>
    </div>
  );
};

API

Set Default Breakpoints

You can define your own breakpoints.

  • Pass an array of objects with the breakpoint name and width in px to setDefaultBreakpoints once in your App.js or your React entry file.

Note: You only need to set default breakpoints once in your app

import { setDefaultBreakpoints } from 'react-socks';

setDefaultBreakpoints([
  { xs: 0 },
  { s: 376 },
  { m: 426 },
  { l: 769 },
  { xl: 1025 }
]);

<Breakpoint m only>
    I will render only in m devices
</Breakpoint>

  • You can use any breakpoint name (Eg. cats, puppies, dinosaurs, etc) and width.
setDefaultBreakpoints([
  { cats: 0 },
  { dinosaurs: 900 }
]);

<Breakpoint cats only>
    Only cats can render me
</Breakpoint>
  • If you don't set a default breakpoint, the library will fallback to Bootstrap 4 default breakpoints as described below.
setDefaultBreakpoints([
  { xsmall: 0 }, // all mobile devices
  { small: 576 }, // mobile devices (not sure which one's this big)
  { medium: 768 }, // ipad, ipad pro, ipad mini, etc
  { large: 992 }, // smaller laptops
  { xlarge: 1200 } // laptops and desktops
]);

Breakpoint

Import the Breakpoint component anywhere in the your code and start using it with your breakpoint and modifier props.

// small is breakpoint
// down is modifier
<Breakpoint small down>
  <MyAwesomeMobileMenu>
    This component will render only in mobile devices
  </MyAwesomeMobileMenu>
</Breakpoint>

You have three modifiers

  • only - will render the component only in the specified breakpoint.

  • up - will render the component in the specified breakpoint and all the breakpoints above it (greater than the width).

  • down - will render the component in the specified breakpoint and all the breakpoints below it (less than the width).

Custom Breakpoints 🆕

Now, you can add a breakpoint of any width by using this prop: customQuery. Simply write your media query as a string and pass it to customQuery

  <Breakpoint customQuery="(min-width: 500px)">
    <div style={{backgroundColor: 'red' }}>
      Custom breakpoint: (min-width : 500px)
    </div>
  </Breakpoint>
  
  <Breakpoint customQuery="(max-width: 1000px)">
    <div style={{backgroundColor: 'yellow' }}>
      Custom breakpoint: (max-width : 1000px)
    </div>
  </Breakpoint>
  
  <Breakpoint customQuery="(min-width: 500px) and (max-width: 700px)">
    <div style={{backgroundColor: 'lightblue' }}>
      Custom breakpoint: (min-width : 500px) && (max-width : 700px)
    </div>
  </Breakpoint>

Note: customQuery will be ignored if you have mentioned any modifiers like up, down & only

Use customQuery only if you want to make use of arbitary breakpoints.

Use Current Width / Breakpoint Name

If you call useCurrentWidth in the render function, you can access the current width directly:

import { useCurrentWidth } from 'react-socks'

const CustomComponent = () => {
  const width = useCurrentWidth()
  if (width < 500) {
    return <h1>Hello!</h1>
  } else {
    return <h2>Hello!</h2>
  }
}

You can also use the current breakpoint name with useCurrentBreakpointName:

import { useCurrentBreakpointName } from 'react-socks'

const CustomComponent = () => {
  const breakpoint = useCurrentBreakpointName()
  if (breakpoint == 'small') {
    return <h1>Hello!</h1>
  } else {
    return <h2>Hello!</h2>
  }
}

Contributors

Thanks goes to these amazing people 🎉


Dinesh Pandiyan


Capelo


Adarsh


Patryk


WRNGFRNK


Farhad Yasir


Entkenntnis


Douglas Moore


Abdul rehman

License

MIT © Dinesh Pandiyan

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].