-
-
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.
feat: add useHideOnScrollDown and truncateToNearestWord
- Loading branch information
Showing
9 changed files
with
297 additions
and
2 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,131 @@ | ||
import { useHideOnScrollDown } from '@/hooks/dom' | ||
import { act, renderHook } from '@testing-library/react' | ||
|
||
describe('useHideOnScrollDown', () => { | ||
beforeEach(() => { | ||
Object.defineProperty(globalThis, 'scrollY', { value: 0, writable: true }) | ||
}) | ||
|
||
it('should initialize as visible', () => { | ||
const { result } = renderHook(() => useHideOnScrollDown()) | ||
expect(result.current).toBe(true) | ||
}) | ||
|
||
it('should stay visible when scrolling up', () => { | ||
const { result } = renderHook(() => useHideOnScrollDown()) | ||
|
||
act(() => { | ||
Object.defineProperty(globalThis, 'scrollY', { value: 10, writable: true }) | ||
globalThis.dispatchEvent(new Event('scroll')) | ||
Object.defineProperty(globalThis, 'scrollY', { value: 5, writable: true }) | ||
globalThis.dispatchEvent(new Event('scroll')) | ||
}) | ||
|
||
expect(result.current).toBe(true) | ||
}) | ||
|
||
it('should hide when scrolling down past threshold', () => { | ||
const { result } = renderHook(() => useHideOnScrollDown(undefined, 30)) | ||
|
||
act(() => { | ||
Object.defineProperty(globalThis, 'scrollY', { value: 40, writable: true }) | ||
globalThis.dispatchEvent(new Event('scroll')) | ||
}) | ||
|
||
expect(result.current).toBe(false) | ||
}) | ||
|
||
it('should show again when scrolling up after being hidden', () => { | ||
const { result } = renderHook(() => useHideOnScrollDown(undefined, 30)) | ||
|
||
act(() => { | ||
Object.defineProperty(globalThis, 'scrollY', { value: 40, writable: true }) | ||
globalThis.dispatchEvent(new Event('scroll')) | ||
}) | ||
expect(result.current).toBe(false) | ||
|
||
act(() => { | ||
Object.defineProperty(globalThis, 'scrollY', { value: 20, writable: true }) | ||
globalThis.dispatchEvent(new Event('scroll')) | ||
}) | ||
expect(result.current).toBe(true) | ||
}) | ||
|
||
it('should use targetRef height if provided', () => { | ||
const container = document.createElement('div') | ||
Object.defineProperty(container, 'offsetHeight', { value: 60, writable: true }) | ||
const targetRef = { current: container } as React.RefObject<HTMLElement> | ||
|
||
const { result } = renderHook(() => useHideOnScrollDown(targetRef)) | ||
|
||
act(() => { | ||
Object.defineProperty(globalThis, 'scrollY', { value: 70, writable: true }) | ||
globalThis.dispatchEvent(new Event('scroll')) | ||
}) | ||
|
||
expect(result.current).toBe(false) | ||
}) | ||
|
||
it('should handle null targetRef gracefully', () => { | ||
const targetRef = { current: null } as React.RefObject<HTMLElement> | ||
const { result } = renderHook(() => useHideOnScrollDown(targetRef, 50)) | ||
|
||
act(() => { | ||
Object.defineProperty(globalThis, 'scrollY', { value: 60, writable: true }) | ||
globalThis.dispatchEvent(new Event('scroll')) | ||
}) | ||
|
||
expect(result.current).toBe(false) | ||
}) | ||
|
||
it('should update visibility state when ref changes', () => { | ||
const container1 = document.createElement('div') | ||
Object.defineProperty(container1, 'offsetHeight', { value: 40, writable: true }) | ||
|
||
const container2 = document.createElement('div') | ||
Object.defineProperty(container2, 'offsetHeight', { value: 80, writable: true }) | ||
|
||
const targetRef = { current: container1 } as React.RefObject<HTMLElement> | ||
|
||
const { rerender, result } = renderHook( | ||
({ ref }) => useHideOnScrollDown(ref), | ||
{ initialProps: { ref: targetRef } }, | ||
) | ||
|
||
act(() => { | ||
Object.defineProperty(globalThis, 'scrollY', { value: 50, writable: true }) | ||
globalThis.dispatchEvent(new Event('scroll')) | ||
}) | ||
expect(result.current).toBe(false) | ||
|
||
act(() => { | ||
// @ts-expect-error - testing ref change | ||
targetRef.current = container2 | ||
rerender({ ref: targetRef }) | ||
}) | ||
|
||
act(() => { | ||
Object.defineProperty(globalThis, 'scrollY', { value: 70, writable: true }) | ||
globalThis.dispatchEvent(new Event('scroll')) | ||
}) | ||
expect(result.current).toBe(false) | ||
}) | ||
|
||
it('should support default window target', () => { | ||
const { result } = renderHook(() => useHideOnScrollDown()) | ||
|
||
act(() => { | ||
Object.defineProperty(globalThis, 'scrollY', { value: 100, writable: true }) | ||
globalThis.dispatchEvent(new Event('scroll')) | ||
}) | ||
|
||
expect(result.current).toBe(false) | ||
|
||
act(() => { | ||
Object.defineProperty(globalThis, 'scrollY', { value: 50, writable: true }) | ||
globalThis.dispatchEvent(new Event('scroll')) | ||
}) | ||
|
||
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
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,72 @@ | ||
'use client' | ||
|
||
import type { RefObject } from 'react' | ||
import { useEffect, useRef, useState } from 'react' | ||
import { useEventListener } from './useEventListener' | ||
|
||
/** | ||
* useHideOnScrollDown | ||
* | ||
* Custom hook to toggle visibility on scroll. | ||
* It hides the element when scrolling down and shows it when scrolling up. | ||
* | ||
* @param {React.RefObject<HTMLElement>} [targetRef] - The reference to the target element (e.g., header). If not provided, defaults to `null` (uses `threshold`). | ||
* @param {number} [threshold] - The scroll position threshold before hiding (used if `targetRef` is not provided). | ||
* @returns {boolean} - Whether the element should be visible. | ||
* | ||
* @example | ||
* ```tsx | ||
* const headerRef = useRef<HTMLElement>(null) | ||
* const isVisible = useHideOnScrollDown(headerRef) | ||
* | ||
* return ( | ||
* <header ref={headerRef} style={{ opacity: isVisible ? 1 : 0 }}> | ||
* Header content | ||
* </header> | ||
* ) | ||
* ``` | ||
* | ||
* @example | ||
* ```tsx | ||
* const isVisible = useHideOnScrollDown(null, 100) | ||
* | ||
* return ( | ||
* <header style={{ opacity: isVisible ? 1 : 0 }}> | ||
* Header content | ||
* </header> | ||
* ) | ||
*/ | ||
export const useHideOnScrollDown = ( | ||
targetRef?: RefObject<HTMLElement>, | ||
threshold: number = 50, | ||
): boolean => { | ||
const [isVisible, setIsVisible] = useState(true) | ||
const lastScrollY = useRef(0) | ||
const [hideThreshold, setHideThreshold] = useState( | ||
targetRef?.current?.offsetHeight ?? threshold, | ||
) | ||
|
||
// When targetRef changes, update the hideThreshold | ||
useEffect(() => { | ||
setHideThreshold(targetRef?.current?.offsetHeight ?? threshold) | ||
globalThis.dispatchEvent(new Event('scroll')) // Force update on scroll | ||
}, [targetRef, threshold]) | ||
|
||
useEventListener('scroll', () => { | ||
const currentScrollY = globalThis.scrollY | ||
|
||
if (currentScrollY < hideThreshold) { | ||
setIsVisible(true) | ||
} | ||
else if (currentScrollY > lastScrollY.current) { | ||
setIsVisible(false) | ||
} | ||
else { | ||
setIsVisible(true) | ||
} | ||
|
||
lastScrollY.current = currentScrollY | ||
}) | ||
|
||
return isVisible | ||
} |
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