Skip to content

Commit

Permalink
Merge pull request #470 from rtibbles/no_match_media
Browse files Browse the repository at this point in the history
Match media testing guard and bug fix
  • Loading branch information
rtibbles authored Oct 20, 2023
2 parents 6aab0a6 + 75e1515 commit 0444139
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ Changelog is rather internal in nature. See release notes for the public overvie

<!-- All new changelog items should come here -->

- [#470]
- **Description:** Fix bug and add test guard in MediaQuery implementation
- **Products impact:** none
- **Addresses:** -
- **Components:** none
- **Breaking:** no
- **Impacts a11y:** no
- **Guidance:** -

[#470]: https://github.com/learningequality/kolibri-design-system/pull/470

- [#469]
- **Description:** Throttle the resize listener handler
- **Products impact:** updated API
Expand Down
14 changes: 11 additions & 3 deletions lib/useKResponsiveWindow/MediaQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ export default class MediaQuery {
constructor(query, eventHandler) {
this.query = query;
this.eventHandler = eventHandler;
this._mediaQueryList = null;
}

/**
* @returns {Object} Media query list
*/
get mediaQueryList() {
return window.matchMedia(this.query);
if (!this._mediaQueryList) {
this._mediaQueryList = window.matchMedia(this.query);
}
return this._mediaQueryList;
}

/**
Expand All @@ -32,8 +36,8 @@ export default class MediaQuery {
* @returns {Object} Containing mediaQueryList, eventHandler, and stopListening
*/
startListening() {
//Prevent function execution if Nuxt is server side rendering
if (this.isNuxtServerSideRendering()) {
// Prevent function execution if Nuxt is server side rendering
if (this.isNuxtServerSideRendering() || !window.matchMedia) {
return;
}

Expand All @@ -48,6 +52,10 @@ export default class MediaQuery {
* Stop listening for media query events
*/
stopListening() {
// Prevent function execution if Nuxt is server side rendering
if (this.isNuxtServerSideRendering() || !window.matchMedia) {
return;
}
if (this.mediaQueryList.removeEventListener) {
this.mediaQueryList.removeEventListener('change', this.eventHandler);
} else {
Expand Down

0 comments on commit 0444139

Please sign in to comment.