Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions UPCOMING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Fix

- Fixed `isMobileView()` so it no longer throws when the default `.mobileNavigation` element is missing. Falls back to a `715px` viewport-width check
11 changes: 9 additions & 2 deletions src/js/modules/mobile-view.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { debounce } from '../index';

const DEFAULT_MOBILE_VIEW_MAX_WIDTH = 715;

/**
* Check if is a mobile device based on Agent
* @param {string} agent
Expand All @@ -13,11 +15,16 @@ export function isMobileAgent(agent = navigator.userAgent) {
/**
* Check if is a mobile based on selector being visible frontend
* @param {string} selector
* @param {number} fallbackWidth
* @returns {boolean}
* @example isMobileView()
*/
export function isMobileView(selector = '.mobileNavigation') {
return document.querySelector(selector).offsetParent !== null;
export function isMobileView(selector = '.mobileNavigation', fallbackWidth = DEFAULT_MOBILE_VIEW_MAX_WIDTH) {
const element = document.querySelector(selector);

return element ?
(element.offsetParent !== null) :
window.matchMedia(`(max-width: ${fallbackWidth}px)`).matches;
}

/**
Expand Down
Loading