@automattic/viewport
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

viewport

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

For React helpers, please check the @automattic/viewport-react package.

Usage

Simple usage:

import { isDesktop, isMobile } from '@automattic/viewport';

if ( isDesktop() ) {
	// Render a component optimized for desktop view
} else if ( isMobile() ) {
	// Render a component optimized for mobile view
}

Using one of the other breakpoints:

import { isWithinBreakpoint } from '@automattic/viewport';

if ( isWithinBreakpoint( '>1400px' ) ) {
	// Render a component optimized for a very large screen
} else {
	// Render alternative component
}

Registering to listen to changes, using vanilla methods:

import { subscribeIsDesktop } from '@automattic/viewport';

class MyComponent extends React.Component {
	sizeChanged = ( matches ) => {
		console.log( `Screen changed to ${ matches ? 'desktop' : 'non-desktop' } size` );
		this.forceUpdate();
	};

	componentDidMount() {
		this.unsubscribe = subscribeIsDesktop( this.sizeChanged );
	}

	componentWillUnmount() {
		this.unsubscribe();
	}
}

Note: the above usage is more easily accomplished using the hooks and higher-order components in @automattic/viewport-react.

Supported methods

  • isWithinBreakpoint( breakpoint ): Whether the current screen size matches the breakpoint.
  • isMobile(): Whether the current screen size matches a mobile breakpoint (equivalent to "<480px"). See note at end of document.
  • isDesktop(): Whether the current screen size matches a desktop breakpoint (equivalent to ">960px"). See note at end of document.
  • subscribeIsWithinBreakpoint( breakpoint, listener ): Register a listener for size changes that affect the breakpoint. Returns the unsubscribe function.
  • subscribeIsMobile( listener ): Register a listener for size changes that affect the mobile breakpoint (equivalent to "<480px"). Returns the unsubscribe function. See note at end of document.
  • subscribeIsDesktop( listener ): Register a listener for size changes that affect the desktop breakpoint (equivalent to ">960px"). Returns the unsubscribe function. See note at end of document.
  • getWindowInnerWidth(): Get the inner width for the browser window. Warning: This method triggers a layout recalc, potentially resulting in performance issues. Please use a breakpoint instead wherever possible.

Supported breakpoints

  • '<480px'
  • '<660px'
  • '<782px'
  • '<800px'
  • '<960px'
  • '<1040px'
  • '<1280px'
  • '<1400px'
  • '>480px'
  • '>660px'
  • '>782px'
  • '>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)

Readme

Keywords

none

Package Sidebar

Install

npm i @automattic/viewport

Weekly Downloads

5,384

Version

1.1.0

License

GPL-2.0-or-later

Unpacked Size

57.2 kB

Total Files

12

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