Home > Software engineering >  pure JS foundation breakpoint changes event listener
pure JS foundation breakpoint changes event listener

Time:10-19

The foundation documentation use JQuery to listen to breakpoint changes

https://get.foundation/sites/docs/media-queries.html

$(window).on('changed.zf.mediaquery', function(event, newSize, oldSize) {
     if (
    (oldSize === "small" && newSize === "large") ||
    (oldSize === "small" && newSize === "xxlarge") ||
    (oldSize === "medium" && newSize === "large") ||
    (oldSize === "medium" && newSize === "xxlarge")
  ) {
    //my custom function
    displayFacets("desktop");
  } else if (
    (oldSize === "xxlarge" && newSize === "small") ||
    (oldSize === "large" && newSize === "small") ||
    (oldSize === "large" && newSize === "medium") ||
    (oldSize === "xxlarge" && newSize === "medium")
  ) {
    displayFacets("mobile");
  }
});

How to convert this in pure JS ?

CodePudding user response:

Foundation is open-source so you can always look into their source code to figure out how they solved specific tasks.

The relevant part to your question can be found here: foundation.util.mediaQuery.js

What they do is to listen for the resize event of the window:

  _watcher() {
    $(window).off('resize.zf.mediaquery').on('resize.zf.mediaquery', () => {
      var newSize = this._getCurrentSize(), currentSize = this.current;

      if (newSize !== currentSize) {
        // Change the current media query
        this.current = newSize;

        // Broadcast the media query change on the window
        $(window).trigger('changed.zf.mediaquery', [newSize, currentSize]);
      }
    });
  }

and check which media query is currently matching by using window.matchMedia(…).matches:

  _getCurrentSize() {
    var matched;

    for (var i = 0; i < this.queries.length; i  ) {
      var query = this.queries[i];

      if (window.matchMedia(query.value).matches) {
        matched = query;
      }
    }

    return matched && this._getQueryName(matched);
  },

Replacing the on, off and trigger in these parts of the code should be straightforward. Extracting the breakpoints might be a bit more problematic but that is also in that file in the _init function.

Alternatively you can also use MediaQueryList.onchange:

var mql = window.matchMedia('(max-width: 600px)');

mql.onchange = (e) => {
    if (e.matches) {
    /* the viewport is 600 pixels wide or less */
    console.log('This is a narrow screen — less than 600px wide.')
  } else {
    /* the viewport is more than than 600 pixels wide */
    console.log('This is a wide screen — more than 600px wide.')
  }
}

To get an event if a media query matches or not.

CodePudding user response:

It's not quite clear what exactly you have in mind. But you can achieve something like that with matchMedia. For example you can create an EventListener that reacts on whether a certain MediaQuery matches or not. See this example: https://developer.mozilla.org/en-US/docs/Web/API/MediaQueryList/onchange

EDIT: ok after your changes to the example it can be seen that you actually only want to distinguish between mobile and desktop. You can implement this quite easily with the functions mentioned:

const mql = window.matchMedia('(max-width: 600px)');
mql.onchange = (e) => {
  if (e.matches) {
    /* the viewport is 600 pixels wide or less */
    displayFacets("mobile");
  } else {
    /* the viewport is more than than 600 pixels wide */
    displayFacets("desktop");
  }
}
  • Related