Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@automattic/viewport

Package Overview
Dependencies
Maintainers
37
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@automattic/viewport

Vanilla helpers for tracking viewport changes.

  • 1.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2K
decreased by-59.55%
Maintainers
37
Weekly downloads
 
Created
Source

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)

FAQs

Package last updated on 06 Jun 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc