forked from learningequality/kolibri-design-system
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request learningequality#377 from akolson/implement-kRespo…
…nsiveWindow-composable Implement k responsive window composable
- Loading branch information
Showing
11 changed files
with
683 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { defineComponent } from '@vue/composition-api'; | ||
import { mount } from '@vue/test-utils'; | ||
import useKWindowDimensions from '../useKWindowDimensions'; | ||
|
||
const resizeWindow = (width, height) => { | ||
window.innerWidth = width; | ||
window.innerHeight = height; | ||
window.dispatchEvent(new Event('resize')); | ||
}; | ||
|
||
describe('useKWindowDimensions composable', () => { | ||
let TestComponent; | ||
beforeAll(() => { | ||
TestComponent = defineComponent({ | ||
setup() { | ||
return { | ||
...useKWindowDimensions(), | ||
}; | ||
}, | ||
}); | ||
}); | ||
|
||
it('check if windowWidth and windowHeight properties are initialized on mount', () => { | ||
const wrapper = mount(TestComponent); | ||
expect(wrapper.vm.windowWidth).toEqual(expect.any(Number)); | ||
expect(wrapper.vm.windowHeight).toEqual(expect.any(Number)); | ||
}); | ||
|
||
it('check if windowWidth and windowHeight change when window is resized', () => { | ||
const wrapper = mount(TestComponent); | ||
expect(wrapper.vm.windowWidth).not.toEqual(600); | ||
expect(wrapper.vm.windowHeight).not.toEqual(400); | ||
resizeWindow(600, 400); | ||
expect(wrapper.vm.windowWidth).toEqual(600); | ||
expect(wrapper.vm.windowHeight).toEqual(400); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import Vue from 'vue'; | ||
import VueCompositionAPI from '@vue/composition-api'; | ||
|
||
Vue.use(VueCompositionAPI); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* Class representing a MediaQuery | ||
*/ | ||
export default class MediaQuery { | ||
/** | ||
*Create a media query | ||
* @param {String} query - The query string | ||
* @param {CallableFunction} eventHandler - The event callback function | ||
*/ | ||
constructor(query, eventHandler) { | ||
this.query = query; | ||
this.eventHandler = eventHandler; | ||
} | ||
|
||
/** | ||
* @returns {Object} Media query list | ||
*/ | ||
get mediaQueryList() { | ||
return window.matchMedia(this.query); | ||
} | ||
|
||
/** | ||
* Check if Nuxt is server side rendering | ||
* @returns {Boolean} | ||
*/ | ||
isNuxtServerSideRendering() { | ||
return process && process.server; | ||
} | ||
|
||
/** | ||
* Start listening for media query events | ||
* @returns {Object} Containing mediaQueryList, eventHandler, and stopListening | ||
*/ | ||
startListening() { | ||
//Prevent function execution if Nuxt is server side rendering | ||
if (this.isNuxtServerSideRendering()) { | ||
return; | ||
} | ||
|
||
if (this.mediaQueryList.addEventListener) { | ||
this.mediaQueryList.addEventListener('change', this.eventHandler); | ||
} else { | ||
this.mediaQueryList.addListener(this.eventHandler); | ||
} | ||
} | ||
|
||
/** | ||
* Stop listening for media query events | ||
*/ | ||
stopListening() { | ||
if (this.mediaQueryList.removeEventListener) { | ||
this.mediaQueryList.removeEventListener('change', this.eventHandler); | ||
} else { | ||
this.mediaQueryList.removeListener(this.eventHandler); | ||
} | ||
} | ||
} |
Oops, something went wrong.