Coder Social home page Coder Social logo

d4rekanguok / use-zone-breakpoints Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 781 KB

Simple React hook for breakpoints detection with window.matchMedia (SSR friendly)

TypeScript 93.78% JavaScript 6.22%
react react-hook responsive responsive-layout media-queries media-query-list match-media

use-zone-breakpoints's Introduction

use-breakpoints

React hook* for setting breakpoints in the browsers with `window.matchMedia`, SSR friendly
* It's actually not a hook, it's a container component with a context hook


Acknowledgement

Huge thanks to @chpio for fixing bugs & improve the code.


Why

I needed to detect browser's width on resize when developing a fairly animation-heavy website with Gatsby.js. However I realized that I only need to know which 'zone' (area between breakpoints) the browser is in & it made more sense to use window.matchMedia instead (Plus it's more fun to try out a 'new' API).

This is actually not really a hook, no, it was a hook until I realized each hook usage in a component added new event listeners to the dom. Granted, it probably doesn't matter, but I prefer to attach only the events that I needed, as often as I needed to.

How

Install

yarn add use-zone-breakpoints

Wrap the root of your app with <ZoneManager /> from use-zone-breakpoints. It accepts a single props: An array of breakpoints in pixels.

  <ZoneManager breakpoints={[ 576, 767, 991, 1199 ]}>
    <App />
  </ZoneManager>

It'll create zones in between these breakpoints. For example, these are the zone created with the breakpoints above:

breakpoints 0 -> 575 576 -> 766 767 -> 990 991 -> 1198 1199 -> โˆž
zone 0 1 2 3 4

Example

Codesandbox

Minimal one pager:

import React from 'react'
import { render } from 'react-dom'
import { useZone, ZoneManager } from 'use-zone-breakpoints'

const ZoneDisplay = () => {
  const zone = useZone()
  return (<h1>{zone}</h1>)
}

const App = () => (
  <ZoneManager breakpoints={[ 360, 480, 640, 960, 1280 ]}>
    <h1>Zone</h1>
    <ZoneDisplay />
  </ZoneManager>
)

const $root = document.getElementById('root')
render(<App />, $root)

Use with Gatsby

Import <ZoneManager> & wrap root element in gatsby-browser.js & gatsby-ssr.js:

export const wrapRootElement = ({ element }) => (
  <ZoneManager breakpoints={[ 360, 960 ]}>{ element }</ZoneManager>
)

Then import useZone to get zone in any functional components.

Use with js-in-css

I keep an array of breakpoints as a constant:

const BREAKPOINTS = [360, 640, 960]

This breakpoints can be shared with a styled library, like this example with emotion

use-zone-breakpoints's People

Contributors

chpio avatar d4rekanguok avatar dependabot[bot] avatar

Watchers

 avatar  avatar

use-zone-breakpoints's Issues

Breaks Hydration

The first value for the zone state should be set in useEffect to match the SSR-rendered dom when hydrating.

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.