-
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
1 parent
d23853e
commit 9a01490
Showing
4 changed files
with
64 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./useWindowSize"; |
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 { renderHook, act } from "@testing-library/react-hooks"; | ||
import useWindowSize from "./useWindowSize"; | ||
import { describe, expect, it } from "vitest"; | ||
|
||
describe("useWindowSize", () => { | ||
it("should return the initial window size", () => { | ||
// mock | ||
window.innerWidth = 1024; | ||
window.innerHeight = 768; | ||
|
||
// act | ||
const { result } = renderHook(() => useWindowSize()); | ||
|
||
// assert | ||
expect(result.current.width).toBe(1024); | ||
expect(result.current.height).toBe(768); | ||
}); | ||
|
||
it("should update the window size on resize", () => { | ||
// mock | ||
const { result } = renderHook(() => useWindowSize()); | ||
|
||
// act | ||
act(() => { | ||
window.innerWidth = 1280; | ||
window.innerHeight = 720; | ||
window.dispatchEvent(new Event("resize")); | ||
}); | ||
|
||
// assert | ||
expect(result.current.width).toBe(1280); | ||
expect(result.current.height).toBe(720); | ||
}); | ||
}); |
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,28 @@ | ||
import { useLayoutEffect, useState } from "react"; | ||
|
||
export default function useWindowSize() { | ||
const [size, setSize] = useState({ | ||
width: 0, | ||
height: 0, | ||
}); | ||
|
||
useLayoutEffect(() => { | ||
const onResize = () => { | ||
setSize({ | ||
width: window.innerWidth, | ||
height: window.innerHeight, | ||
}); | ||
}; | ||
|
||
onResize(); | ||
|
||
window.addEventListener("resize", onResize); | ||
|
||
return () => window.removeEventListener("resize", onResize); | ||
}, []); | ||
|
||
return { | ||
height: size.height, | ||
width: size.width, | ||
}; | ||
} |