From: Nicolas Coden Date: Mon, 6 Aug 2018 21:56:56 +0000 (+0200) Subject: feat: add "MediaQuery.upTo()" and support of "down" mq to "MediaQuery.is()" X-Git-Tag: v6.6.0~1^2~2^2~16 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3eda0e04a66c754e200b63ff0fbd8dfbf5b8f4c5;p=thirdparty%2Ffoundation%2Ffoundation-sites.git feat: add "MediaQuery.upTo()" and support of "down" mq to "MediaQuery.is()" --- diff --git a/js/foundation.util.mediaQuery.js b/js/foundation.util.mediaQuery.js index ae408543a..cd3f5888b 100644 --- a/js/foundation.util.mediaQuery.js +++ b/js/foundation.util.mediaQuery.js @@ -142,6 +142,23 @@ var MediaQuery = { return false; }, + /** + * Checks if the screen is as wide as a breakpoint or smaller. + * @function + * @param {String} size - Name of the breakpoint to check. + * @returns {Boolean} `true` if the breakpoint matches, `false` if it's wider. + */ + upTo(size) { + const firstMatch = this.queries.find((query) => { + return window.matchMedia(query.value).matches + }); + const firstMatchName = (typeof matched === 'object') + ? firstMatch.name + : firstMatch; + + return size === firstMatchName; + }, + /** * Checks if the screen matches to a breakpoint. * @function @@ -160,6 +177,10 @@ var MediaQuery = { if (bpModifier === 'down') { return this.atLeast(bpSize); } + // At leat the breakpoint (included) + if (!bpModifier || bpModifier === 'up') { + return this.upTo(bpSize); + } throw new Error(` invalid breakpoint passed to MediaQuery.is().