-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathisMobileNav.js
More file actions
26 lines (21 loc) · 826 Bytes
/
isMobileNav.js
File metadata and controls
26 lines (21 loc) · 826 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import getCssVar from './getCssVar'
/**
* Check if the current viewport is mobile based on a CSS breakpoint.
*
* @param {string} breakpointVar CSS custom property name for the breakpoint.
* @return {boolean} True if viewport is mobile, false otherwise.
* @example isMobileNav() => true; !isMobileNav() => false
*/
export default function isMobileNav(breakpointVar = '--breakpoint-mobile-to-desktop-nav') {
const rawValue = getCssVar(breakpointVar)
if (!rawValue) {
console.warn(`isMobileNav: Variable ${breakpointVar} is empty or undefined. Returning false.`)
return false
}
const breakpoint = parseInt(rawValue, 10)
if (isNaN(breakpoint)) {
console.warn(`isMobileNav: Could not parse "${rawValue}" as a number.`)
return false
}
return window.matchMedia(`(max-width: ${breakpoint - 1}px)`).matches
}