-
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.
- Loading branch information
Showing
9 changed files
with
146 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import mediaQuery from 'css-mediaquery'; | ||
|
||
Object.defineProperty(window, 'matchMedia', { | ||
writable: true, | ||
value: jest.fn().mockImplementation(query => ({ | ||
matches: mediaQuery.match(query, { | ||
width: window.innerWidth, | ||
height: window.innerHeight | ||
}), | ||
media: query, | ||
onchange: null, | ||
addListener: jest.fn(), // Deprecated | ||
removeListener: jest.fn(), // Deprecated | ||
addEventListener: jest.fn(), | ||
removeEventListener: jest.fn(), | ||
dispatchEvent: jest.fn() | ||
})) | ||
}); |
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,10 @@ | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import { useMatchMedia } from '../src/useMatchMedia'; | ||
import '../__mocks__/useMatchMedia.mock'; | ||
|
||
describe('useMatchMedia - checks for media queries via window.matchMedia', () => { | ||
test('should match media query', () => { | ||
const { result } = renderHook(() => useMatchMedia('(min-width: 600px)')); | ||
expect(result.current).toBe(true); | ||
}); | ||
}); |
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,14 @@ | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import { useWindowSize } from '../src/useWindowSize'; | ||
|
||
describe('useWindowSize - gets window dimensions', () => { | ||
test('should return window dimensions', () => { | ||
const { result } = renderHook(() => useWindowSize()); | ||
expect(result.current).toStrictEqual({ | ||
innerHeight: 768, | ||
innerWidth: 1024, | ||
outerHeight: 768, | ||
outerWidth: 1024 | ||
}); | ||
}); | ||
}); |
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,26 @@ | ||
import { useRef, useState, useCallback, useEffect } from 'react'; | ||
|
||
export function useMatchMedia(query: string) { | ||
const matchListRef = useRef<MediaQueryList | null>(null); | ||
const [isMatch, setIsMatch] = useState<boolean>(false); | ||
const onMediaQueryListEvent = useCallback((e: MediaQueryListEvent) => { | ||
setIsMatch(e.matches); | ||
}, []); | ||
|
||
useEffect(() => { | ||
matchListRef.current = window.matchMedia(query); | ||
matchListRef.current.addEventListener('change', onMediaQueryListEvent); | ||
setIsMatch(matchListRef.current.matches); | ||
|
||
return function unmountUseMatchMedia() { | ||
if (matchListRef.current) { | ||
matchListRef.current.removeEventListener( | ||
'change', | ||
onMediaQueryListEvent | ||
); | ||
} | ||
}; | ||
}, [query]); | ||
|
||
return isMatch; | ||
} |
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,46 @@ | ||
import { useState, useEffect } from 'react'; | ||
|
||
type WindowSize = { | ||
innerHeight: number; | ||
innerWidth: number; | ||
outerHeight: number; | ||
outerWidth: number; | ||
}; | ||
|
||
export function useWindowSize() { | ||
const [windowSize, setWindowSize] = useState<WindowSize>(getSize()); | ||
|
||
function getSize() { | ||
if (typeof window !== 'undefined') { | ||
return { | ||
innerHeight: window.innerHeight, | ||
innerWidth: window.innerWidth, | ||
outerHeight: window.outerHeight, | ||
outerWidth: window.outerWidth | ||
}; | ||
} else { | ||
console.error('ReferenceError: typeof "window" is undefined'); | ||
|
||
return { | ||
innerHeight: 0, | ||
innerWidth: 0, | ||
outerHeight: 0, | ||
outerWidth: 0 | ||
}; | ||
} | ||
} | ||
|
||
function handleResize() { | ||
setWindowSize(getSize()); | ||
} | ||
|
||
useEffect(() => { | ||
window.addEventListener('resize', handleResize); | ||
|
||
return function unmountUseWindowSize() { | ||
window.removeEventListener('resize', handleResize); | ||
}; | ||
}, []); | ||
|
||
return windowSize; | ||
} |
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