Skip to main content

viewportSize

viewportSize returns the current viewport size as a breakpoint key. It uses window.matchMedia under the hood and returns one of:

KeyRange
xsup to 767px
sm768px – 900px
md901px – 1200px
lg1201px – 1439px
xl1440px and up

This lets you branch your JavaScript on breakpoint without hard-coding matchMedia queries everywhere. It reflects the viewport at the moment it's called, so re-check it inside a (debounced) resize handler if you need to respond to changes.

Usage examples​

Basic​

window.BAO.utils.viewportSize() // 'md'

Branching behaviour by breakpoint​

const size = window.BAO.utils.viewportSize()

if (size === 'xs' || size === 'sm') {
// Mobile-only behaviour
}