@automattic/viewport-react

1.0.0 • Public • Published

viewport

This package contains React helpers to identify and track changes to the viewport. This can be used for displaying different components depending on a desktop or mobile view.

For vanilla methods, please check the @automattic/viewport package.

Usage

Using a hook:

import { useMobileBreakpoint } from '@automattic/viewport-react';

export default function MyComponent( props ) {
	const isMobile = useMobileBreakpoint();
	return <div>Screen size: { isMobile ? 'mobile' : 'not mobile' }</div>;
}

Using a higher-order component:

import { withMobileBreakpoint } from '@automattic/viewport-react';

class MyComponent extends React.Component {
	render() {
		const { isBreakpointActive: isMobile } = this.props;
		return <div>Screen size: { isMobile ? 'mobile' : 'not mobile' }</div>;
	}
}

export default withMobileBreakpoint( MyComponent );

Supported hooks

  • useBreakpoint( breakpoint ): Returns the current status for a breakpoint, and keeps it updated.
  • useMobileBreakpoint(): Returns the current status for the mobile breakpoint, and keeps it updated.
  • useDesktopBreakpoint(): Returns the current status for the desktop breakpoint, and keeps it updated.

Supported higher-order components

  • withBreakpoint( breakpoint )( WrappedComponent ): Returns a wrapped component with the current status for a breakpoint as the isBreakpointActive prop.
  • withMobileBreakpoint( WrappedComponent ): Returns a wrapped component with the current status for the mobile breakpoint as the isBreakpointActive prop.
  • withDesktopBreakpoint( WrappedComponent ): Returns a wrapped component with the current status for the desktop breakpoint as the isBreakpointActive prop.

Supported breakpoints

  • '<480px'
  • '<660px'
  • '<800px'
  • '<960px'
  • '<1040px'
  • '<1280px'
  • '<1400px'
  • '>480px'
  • '>660px'
  • '>800px'
  • '>960px'
  • '>1040px'
  • '>1280px'
  • '>1400px'
  • '480px-660px'
  • '480px-960px'
  • '660px-960px'

Note: As implemented in Calypso media query mixins, minimums are exclusive, while maximums are inclusive. i.e.:

  • '>480px' is equivalent to @media (min-width: 481px)
  • '<960px' is equivalent to @media (max-width: 960px)
  • '480px-960px' is equivalent to @media (max-width: 960px) and (min-width: 481px)

/@automattic/viewport-react/

    Package Sidebar

    Install

    npm i @automattic/viewport-react

    Weekly Downloads

    7,153

    Version

    1.0.0

    License

    GPL-2.0-or-later

    Unpacked Size

    38.8 kB

    Total Files

    10

    Last publish

    Collaborators

    • porada
    • imranh920
    • gmjuhasz
    • rcarvalho
    • briowill
    • bgrgicak
    • dhenridev
    • daledupreez-a8c
    • jeherve
    • yuliyan
    • micbosia8c
    • jeremy.yip
    • wpvip-bot
    • etobiesen
    • kzoschke
    • brunobasto
    • kat3samsin
    • fmfernandes
    • newspack
    • robertsreberski_a8c
    • msurdi-a8c
    • chihsuan
    • manzoorwanijk
    • rjchow
    • andrea-sdl
    • scjr
    • spsiddarthan
    • t2dw4t
    • ehg_
    • wwa
    • sirreal
    • elazzabi
    • royho
    • luismulinari
    • macbre
    • mjangda
    • matticbot
    • a8c
    • blowery
    • noahtallen
    • hanifn
    • sgomes
    • tyxla
    • saroshaga
    • parkcityj
    • nejclovrencic
    • sirbrillig
    • chriszarate
    • robersongomes
    • johngodley