Skip to content

Commit

Permalink
Make screenSizeMediaQuery static and add update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
jakelauritsen committed Jul 22, 2024
1 parent c63e700 commit cbb40d5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
24 changes: 14 additions & 10 deletions src/layout/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,9 @@
return window.matchMedia(query);
};

var mediaQuery = MaterialLayout.prototype.matchMedia_(
MaterialLayout.screenSizeMediaQuery_ = MaterialLayout.prototype.matchMedia_(
/** @type {string} */ (MaterialLayout.prototype.Constant_.MAX_WIDTH));
mediaQuery.onchange = screenSizeHandler;

MaterialLayout.prototype.screenSizeMediaQuery_ = mediaQuery;
MaterialLayout.screenSizeMediaQuery_.onchange = screenSizeHandler;

/**
* Handles scrolling on the content.
Expand Down Expand Up @@ -191,12 +189,18 @@
};

/**
* Handles changes in screen size.
*
* @private
* @typedef {Object} Matchable
* @property {boolean} matches - Indicates if the media query condition is met.
*/

function screenSizeHandler(e) {
/**
* Handles screen size changes by updating the layout and drawer elements
* based on the media query change event status.
*
* @param {Matchable} m - is any object that provides matches
*
*/
function screenSizeHandler(m) {
// modified to query dependent elements rather than binding materialLayout to windows media query result
var materialLayouts = document.querySelectorAll('.mdl-layout');

Expand All @@ -206,7 +210,7 @@
if (layout) {
var drawerElement = layout.querySelector('.mdl-layout__drawer');

if (e.matches) {
if (m.matches) {
layout.classList.add('is-small-screen');
if (drawerElement) {
drawerElement.setAttribute('aria-hidden', 'true');
Expand Down Expand Up @@ -450,7 +454,7 @@
// Keep an eye on screen size, and add/remove auxiliary class for styling
// of small screens.

screenSizeHandler(this.screenSizeMediaQuery_);
screenSizeHandler(MaterialLayout.screenSizeMediaQuery_);

// Initialize tabs, if any.
if (this.header_ && this.tabBar_) {
Expand Down
2 changes: 1 addition & 1 deletion test/unit/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('MaterialLayout', function () {
}

MockMediaQueryList.registry = {};
if(window.MaterialLayout.prototype.screenSizeMediaQuery_){
if(MaterialLayout.screenSizeMediaQuery_){
var query = window.MaterialLayout.prototype.Constant_.MAX_WIDTH
MockMediaQueryList.registry[query] = new MockMediaQueryList(query)
}
Expand Down

0 comments on commit cbb40d5

Please sign in to comment.