-
Notifications
You must be signed in to change notification settings - Fork 4
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 #54 from dolthub/liuliu/add-useSetContainerHeight
liuliu/add useSetContainerHeight to hooks
- Loading branch information
Showing
4 changed files
with
67 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
44 changes: 44 additions & 0 deletions
44
packages/hooks/src/__tests__/useSetContainerHeight.test.ts
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,44 @@ | ||
import { renderHook } from "@testing-library/react-hooks"; | ||
import useSetContainerHeight from "../useSetContainerHeight"; | ||
import { useReactiveHeight } from "../useReactiveSize"; | ||
|
||
describe("useSetContainerHeight", () => { | ||
const componentID = "testComponent"; | ||
|
||
beforeAll(() => { | ||
const mockElement = document.createElement("div"); | ||
mockElement.id = componentID; | ||
|
||
mockElement.getBoundingClientRect = jest.fn().mockReturnValue({ top: 100 }); | ||
|
||
document.body.appendChild(mockElement); | ||
|
||
global.document.getElementById = jest.fn(id => { | ||
if (id === componentID) { | ||
return mockElement; | ||
} | ||
return null; | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
const mockElement = document.getElementById(componentID); | ||
if (mockElement) { | ||
document.body.removeChild(mockElement); | ||
} | ||
}); | ||
|
||
it("calculates container height correctly based on window height", () => { | ||
const { result: windowHeightResult } = renderHook(() => | ||
useReactiveHeight(), | ||
); | ||
const windowHeight = windowHeightResult.current; | ||
|
||
const { result } = renderHook(() => useSetContainerHeight(componentID)); | ||
|
||
const expectedHeight = windowHeight - 100; | ||
|
||
expect(result.current.containerHeight).toBe(expectedHeight); | ||
}); | ||
}); |
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,21 @@ | ||
import { useEffect, useState } from "react"; | ||
import { useReactiveHeight } from "./useReactiveSize"; | ||
|
||
type ReturnType = { | ||
containerHeight: number; | ||
}; | ||
|
||
export default function useSetContainerHeight(componentID: string): ReturnType { | ||
const [containerHeight, setContainerHeight] = useState(0); | ||
const scrollContainer = document.getElementById(componentID); | ||
const windowHeight = useReactiveHeight(); | ||
const scrollContainerTop = scrollContainer?.getBoundingClientRect().top; | ||
|
||
useEffect(() => { | ||
const top = scrollContainerTop || 0; | ||
const height = windowHeight - top; | ||
setContainerHeight(height); | ||
}, [windowHeight, scrollContainer, scrollContainerTop]); | ||
|
||
return { containerHeight }; | ||
} |