GithubHelp home page GithubHelp logo

react-device-breakpoints's Introduction

React Device Breakpoints

Build Status Maintainability Test Coverage

Installation

npm i react-device-breakpoints --save

or with yarn:

yarn add react-device-breakpoints

Usage

App.js

import React from 'react'
import { Breakpoints } from 'react-device-breakpoints'

const breakpoints = {
    isDesktop: '(min-width: 1024px)',
    isTablet: '(max-width: 1023px) and (min-width: 768px)',
    isMobile: '(max-width: 767px)'
}

const App = () => (
    <Breakpoints {...breakpoints}>
        <MyComponent/>
    </Breakpoints>
)

export default App

With Hooks

MyComponent.js

import React, { useContext } from 'react'
import { useBreakpoints } from 'react-device-breakpoints'

const MyComponent = () => {
    const device = useBreakpoints()
    return (
        <div>
            {device.isDesktop &&
                <h1>Desktop</h1>
            }
            {device.isTablet &&
                <h1>Tablet</h1>
            }
            {device.isMobile &&
                <h1>Mobile</h1>
            }
            {device.isTouchDevice &&
                <h2>Touch Device</h2>
            }
        </div>
    )
}

export default MyComponent

With Consumer

MyComponent.js

import React, { useContext } from 'react'
import { Media } from 'react-device-breakpoints'

const MyComponent = () => {
    return (
        <Media>
            {(device, Media) => (
                <div>
                    {device.isDesktop &&
                        <h1>Desktop</h1>
                    }
                    {device.isTablet &&
                        <h1>Tablet</h1>
                    }
                    {device.isMobile &&
                        <h1>Mobile</h1>
                    }
                    {device.isTouchDevice &&
                        <h2>Touch Device</h2>
                    }
                    <Media.isDesktop>
                        <h1>Desktop</h1>
                    </Media.isDesktop>
                    <Media.isTablet>
                        <h1>Tablet</h1>
                    </Media.isTablet>
                    <Media.isMobile>
                        <h1>Mobile</h1>
                    </Media.isMobile>
                    <Media.isTouchDevice>
                        <h1>Touch Device</h1>
                    </Media.isTouchDevice>
                </div>
            )}
        </Media>
    )
}

export default MyComponent

The Device Object

React Device Breakpoints returns a device object as follows:

{
    [breakpointName]: Boolean
}

For example:

{
    isDesktop: true,
    isTablet: false,
    isMobile: false
}

It also includes a pre-built-in option for touch devices base of the following user agent test /Mobi|Tablet|iPad|iPhone|Android/.test(window.navigator.userAgent)

{
    isTouchDevice: false
}

react-device-breakpoints's People

Contributors

dahliacreative avatar

Stargazers

 avatar

Watchers

 avatar  avatar

react-device-breakpoints's Issues

SSR support?

This library works well on CSR but breaks in SSR mode.
Do you have plans to support SSR?

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.