0.0.15 • Published 5 years ago

react-use-breakpoints v0.0.15

Weekly downloads
134
License
MIT
Repository
github
Last release
5 years ago

react-use-breakpoints

A small collection of react hooks that will help you with your responsive designs.

Code sample

https://codesandbox.io/s/xjjvnr3yjp?fontsize=14

Basic usage

Unless you pass in your own breakpoints, the hook will be using pre-defined (defaultBreakpoints) breakpoints.

import { useBreakpoints } from "react-use-breakpoints";

const { breakpoint, windowSize } = useBreakpoints();

Default breakpoints

export const defaultBreakpoints = {
  xs: 0,
  sm: 600,
  md: 960,
  lg: 1280,
  xl: 1920,
};

Custom breakpoints

It's also possible to define custom breakpoints.

import { useBreakpoints } from "react-use-breakpoints";

const customBreakpoints = {
  mobile: 0,
  tablet: 600,
  desktop: 1200
};

const { breakpoint, windowSize } = useBreakpoints(customBreakpoints);

You can use BreakpointContext to provide your custom breakpoints.

import React, { useContext } from "react";
import {
  Breakpoints,
  BreakpointContext,
  useBreakpoints
} from "react-use-breakpoints";

const customBreakpoints = {
  mobile: 0,
  tablet: 600,
  desktop: 1200
};

function App() {
  return (
    <Breakpoints breakpoints={customBreakpoints}>
      <div className="App">
        <CustomBreakpoints />
      </div>
    </Breakpoints>
  );
}

function CustomBreakpoints() {
  const breakpoints = useContext(BreakpointContext);
  const { breakpoint } = useBreakpoints(breakpoints);
  ...
}

Use WindowSize hook

This hook provides inner and outer window widths as the window is being resized.

import { useWindowSize } from "react-use-breakpoints";

const { innerWidth, outerWidth } = useWindowSize();
0.0.15

5 years ago

0.0.14

5 years ago

0.0.13

5 years ago

0.0.12

5 years ago

0.0.11

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago