react-responsiveness
TypeScript icon, indicating that this package has built-in type declarations

0.1.5 • Public • Published

React Responsiveness

What - Tiny plugin for working with responsiveness intervals, developed with a focus on ease of use (DX) and runtime performance.
Why - I am a bit obsessed with both performance and ease of use. See how it works.

Total Downloads Latest Release License Dependencies unpkg umd min:gzip size SSR compatibility status PRs Welcome

Installation

yarn

yarn add react-responsiveness

npm

npm i react-responsiveness

Demo

codesandbox.

Usage

A) Add provider

Show example
import { ResponsivenessProvider, Presets } from "react-responsiveness";

function App() {
    // ...
}

const WithResponsiveness = () => (
  <ResponsivenessProvider breakpoints={Presets.Bootstrap_5}>
    <App />
  </ResponsivenessProvider>
);
export default WithResponsiveness;

B) Use in any component

Show example
import { useResponsiveness } from "react-responsiveness";

const { isMin, isMax, isOnly, currentInterval } = useResponsiveness()

return (<>
   <div>Current interval {currentInterval}</div>
   {isMin('md') && (
       // @media (min-width: 768px)
       <div>content...</div>
   )}
   {isMax('md') && (
       // @media (max-width: 991.9px)
       <div>content...</div>
   )}
   {isOnly('md') && (
       // @media (min-width: 768px) and (max-width: 991.9px)
       <div>content...</div>
   )}
</>)

Available presets:

Bootstrap_3, Bootstrap_4, Bootstrap_5, Bulma, Chakra, Foundation, Ionic, Material_Design, Materialize, Material_UI, Quasar, Semantic_UI, Skeleton, Tailwind_CSS, Windi_CSS

Notes:

Show preset details
Presets.Bootstrap_5 = {
  xs: 0,
  sm: 576,
  md: 768,
  lg: 992,
  xl: 1200,
  xxl: 1400,
};
  • If you maintain a CSS framework (or use one often) and want its preset added, open an issue or a PR.
  • If you spot any inaccuracy in presets (either typo or due to library update), please, let me know, I'll correct it.

Can I add my own intervals? Of course:

<ResponsivenessProvider
  breakpoints={{
    small: 0,
    medium: 777,
    large: 1234,
  }}
>
  // ...
</ResponsivenessProvider>
Show usage example
import { useResponsiveness } from "react-responsiveness";

const { isOnly } = useResponsiveness()

return (<>
    {isOnly('medium') && (
        // @media (min-width: 777px) and (max-width: 1233.9px)
        <div>content...</div>
    )}
</>)

F.A.Q.

My IDE's TypeScript service does not pick up the types for the package. Is there a way to fix it?

Show answer

I've noticed this weird problem in some codesandbox.io instances.

This seems to fix it:

// src/react-responsiveness.d.ts
declare module "react-responsiveness" {
  export * from "react-responsiveness/dist/index" 
}

Re-exporting types from where they are declared in package.json doesn't make much sense, but it keeps TS happy.
Another way to fix it is to restart the TS service, but it doesn't always work.

How it works:

  • uses the native window.matchMedia(queryString) and only reacts to changes in the query's matches value. It's the same API powering CSS media queries
  • listeners are placed on the MediaQueryList instances, meaning they are garbage collected as soon as the app is unmounted, without leaving bound events behind on <body> or window object
  • no global pollution
  • in terms of memory and/or CPU consumption, listening to window.matchMadia 'change' events is a few hundred times lighter than using the "traditional" resize event listener method

Got issues?

Let me know!

Happy coding! :: }<(((*> ::

Package Sidebar

Install

npm i react-responsiveness

Weekly Downloads

33

Version

0.1.5

License

MIT

Unpacked Size

21.1 kB

Total Files

13

Last publish

Collaborators

  • andrei-gheorghiu