Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce useMediaUp and useMediaDown (#23)
Introduce useMediaUp and useMediaDown. Both accept a breakpoint and calculate if the current window width is up/down the breakpoint requirements. Uses window.matchMedia to validate the size and ResizeObserver to update the value on page resize. closes #22
- Loading branch information
1 parent
03cebd7
commit aacd612
Showing
12 changed files
with
137 additions
and
22 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
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,60 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
|
||
import { renderHook } from "@testing-library/react"; | ||
import { setMedia } from "mock-match-media"; | ||
import { wrapper } from "@/utils/mock-theme"; | ||
import { useMediaDown, useMediaUp } from "../use-media"; | ||
|
||
describe("useMediaUp", () => { | ||
test("should return true if the window width is more than the breakpoint value", async () => { | ||
setMedia({ | ||
width: "600px", | ||
type: "screen", | ||
orientation: "landscape" | ||
}); | ||
|
||
const hook = renderHook(() => useMediaUp("md"), { wrapper }); | ||
|
||
expect(hook.result.current).toEqual(true); | ||
}); | ||
|
||
test("should return false if the window width is less than the breakpoint value", async () => { | ||
setMedia({ | ||
width: "400px", | ||
type: "screen", | ||
orientation: "landscape" | ||
}); | ||
|
||
const hook = renderHook(() => useMediaUp("md"), { wrapper }); | ||
|
||
expect(hook.result.current).toEqual(false); | ||
}); | ||
}); | ||
|
||
describe("useMediaDown", () => { | ||
test("should return true if the window width is less than the breakpoint value", async () => { | ||
setMedia({ | ||
width: "400px", | ||
type: "screen", | ||
orientation: "landscape" | ||
}); | ||
|
||
const hook = renderHook(() => useMediaDown("md"), { wrapper }); | ||
|
||
expect(hook.result.current).toEqual(true); | ||
}); | ||
|
||
test("should return false if the window width is less than the breakpoint value", async () => { | ||
setMedia({ | ||
width: "600px", | ||
type: "screen", | ||
orientation: "landscape" | ||
}); | ||
|
||
const hook = renderHook(() => useMediaDown("md"), { wrapper }); | ||
|
||
expect(hook.result.current).toEqual(false); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from "./use-color-by-key"; | ||
export * from "./use-media"; |
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,34 @@ | ||
import { useCallback, useEffect, useState } from "react"; | ||
import { useTheme } from "@emotion/react"; | ||
import { TBreakpoint } from "@/theme"; | ||
|
||
const useMatchMedia = (breakpoint: keyof TBreakpoint, queryPrefix: string) => { | ||
const [isMatch, setIsMatch] = useState(false); | ||
const { breakpoint: themeBreakpoint } = useTheme(); | ||
|
||
const query = `(${queryPrefix}: ${themeBreakpoint[breakpoint]}px)`; | ||
|
||
const calculate = useCallback(() => setIsMatch(window.matchMedia(query).matches), [query]); | ||
|
||
useEffect(() => { | ||
calculate(); | ||
}, [calculate]); | ||
|
||
useEffect(() => { | ||
const observer = new ResizeObserver(() => { | ||
calculate(); | ||
}); | ||
|
||
observer.observe(document.body); | ||
|
||
return () => { | ||
observer.unobserve(document.body); | ||
}; | ||
}, [calculate]); | ||
|
||
return isMatch; | ||
} | ||
|
||
export const useMediaUp = (breakpoint: keyof TBreakpoint) => useMatchMedia(breakpoint, "min-width"); | ||
|
||
export const useMediaDown = (breakpoint: keyof TBreakpoint) => useMatchMedia(breakpoint, "max-width"); |
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 "mock-match-media/jest-setup"; | ||
|
||
global.ResizeObserver = require('resize-observer-polyfill') | ||
|
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