Skip to content

Commit 72874f4

Browse files
committed
refactor getBreakpoint function to generic name
1 parent ec3c787 commit 72874f4

3 files changed

Lines changed: 20 additions & 12 deletions

File tree

src/js/utils/getBreakpoint.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/js/utils/getCssVar.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Get the value from a CSS custom property.
3+
*
4+
* @param {string} name CSS custom property name (ex: '--breakpoint-mobile-to-desktop-nav').
5+
* @param {HTMLElement} element The element to get the CSS custom property from.
6+
* @return {string} The value.
7+
* @example getCssVar('--breakpoint-mobile-to-desktop-nav') => '1200'
8+
*/
9+
export default function getCssVar(name, element = document.documentElement) {
10+
if (!name) {
11+
console.warn('getCssVar: No name provided.')
12+
return ''
13+
}
14+
15+
const propName = name.startsWith('--') ? name : `--${name}`
16+
17+
return getComputedStyle(element).getPropertyValue(propName).trim()
18+
}

src/js/utils/isMobileNav.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import getBreakpoint from './getBreakpoint'
1+
import getCssVar from './getCssVar'
22

33
/**
44
* Check if the current viewport is mobile based on a CSS breakpoint.
@@ -8,6 +8,6 @@ import getBreakpoint from './getBreakpoint'
88
* @example isMobileNav() => true; !isMobileNav() => false
99
*/
1010
export default function isMobileNav(breakpointVar = '--breakpoint-mobile-to-desktop-nav') {
11-
const breakpoint = parseInt(getBreakpoint(breakpointVar), 10)
11+
const breakpoint = parseInt(getCssVar(breakpointVar), 10)
1212
return window.matchMedia(`(max-width: ${breakpoint - 1}px)`).matches
1313
}

0 commit comments

Comments
 (0)