-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: New hooks for element size tracking (#413)
Co-authored-by: Guilherme Gazzo <guilhermegazzo@gmail.com>
- Loading branch information
Showing
38 changed files
with
1,818 additions
and
1,577 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
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
15 changes: 15 additions & 0 deletions
15
packages/fuselage-hooks/src/useBorderBoxSize.server.spec.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,15 @@ | ||
/** | ||
* @jest-environment node | ||
*/ | ||
|
||
import { renderHook } from '@testing-library/react-hooks/server'; | ||
import { useRef } from 'react'; | ||
|
||
import { useBorderBoxSize } from './useBorderBoxSize'; | ||
|
||
it('immediately returns zero size', () => { | ||
const { result } = renderHook(() => useBorderBoxSize(useRef())); | ||
|
||
expect(result.current.inlineSize).toStrictEqual(0); | ||
expect(result.current.blockSize).toStrictEqual(0); | ||
}); |
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,101 @@ | ||
import { renderHook, act } from '@testing-library/react-hooks'; | ||
import { useRef, RefObject } from 'react'; | ||
import { withResizeObserverMock } from 'testing-utils/mocks/withResizeObserverMock'; | ||
|
||
import { useBorderBoxSize } from './useBorderBoxSize'; | ||
|
||
withResizeObserverMock(); | ||
|
||
beforeAll(() => { | ||
jest.useFakeTimers(); | ||
}); | ||
|
||
let element: HTMLElement; | ||
|
||
beforeEach(() => { | ||
element = document.createElement('div'); | ||
element.style.width = '40px'; | ||
element.style.height = '30px'; | ||
element.style.padding = '5px'; | ||
document.body.append(element); | ||
}); | ||
|
||
afterEach(() => { | ||
element.remove(); | ||
}); | ||
|
||
const wrapRef = (ref: RefObject<HTMLElement>) => { | ||
Object.assign(ref, { current: element }); | ||
return ref; | ||
}; | ||
|
||
it('immediately returns size', async () => { | ||
const { result } = renderHook(() => useBorderBoxSize(wrapRef(useRef()))); | ||
|
||
expect(result.current.inlineSize).toStrictEqual(50); | ||
expect(result.current.blockSize).toStrictEqual(40); | ||
}); | ||
|
||
it('gets the observed element size after resize', async () => { | ||
const { result } = renderHook(() => useBorderBoxSize(wrapRef(useRef()))); | ||
|
||
// triggers MutationObserver | ||
await act(async () => { | ||
element.style.width = '30px'; | ||
element.style.height = '40px'; | ||
element.style.padding = '15px'; | ||
}); | ||
|
||
// waits for debounced state mutation | ||
await act(async () => { | ||
jest.advanceTimersByTime(0); | ||
}); | ||
|
||
expect(result.current.inlineSize).toStrictEqual(60); | ||
expect(result.current.blockSize).toStrictEqual(70); | ||
|
||
// triggers MutationObserver | ||
await act(async () => { | ||
element.style.boxSizing = 'border-box'; | ||
}); | ||
|
||
// waits for debounced state mutation | ||
await act(async () => { | ||
jest.advanceTimersByTime(0); | ||
}); | ||
|
||
expect(result.current.inlineSize).toStrictEqual(30); | ||
expect(result.current.blockSize).toStrictEqual(40); | ||
}); | ||
|
||
it('debounces the observed element size', async () => { | ||
const halfDelay = 50; | ||
const delay = 2 * halfDelay; | ||
|
||
const { result } = renderHook(() => | ||
useBorderBoxSize(wrapRef(useRef()), { debounceDelay: delay }) | ||
); | ||
|
||
// triggers MutationObserver | ||
await act(async () => { | ||
element.style.width = '30px'; | ||
element.style.height = '40px'; | ||
element.style.padding = '15px'; | ||
}); | ||
|
||
// waits for debounced state mutation | ||
await act(async () => { | ||
jest.advanceTimersByTime(halfDelay); | ||
}); | ||
|
||
expect(result.current.inlineSize).toStrictEqual(50); | ||
expect(result.current.blockSize).toStrictEqual(40); | ||
|
||
// wait the callback trigger from ResizeObserver | ||
await act(async () => { | ||
jest.advanceTimersByTime(halfDelay); | ||
}); | ||
|
||
expect(result.current.inlineSize).toStrictEqual(60); | ||
expect(result.current.blockSize).toStrictEqual(70); | ||
}); |
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,67 @@ | ||
import { RefObject, useState } from 'react'; | ||
|
||
import { useDebouncedCallback } from './useDebouncedCallback'; | ||
import { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect'; | ||
|
||
export const useBorderBoxSize = ( | ||
ref: RefObject<HTMLElement>, | ||
options: { | ||
debounceDelay?: number; | ||
} = {} | ||
): Readonly<{ | ||
inlineSize: number; | ||
blockSize: number; | ||
}> => { | ||
const [size, setSize] = useState(() => ({ | ||
inlineSize: ref.current?.offsetWidth ?? 0, | ||
blockSize: ref.current?.offsetHeight ?? 0, | ||
})); | ||
|
||
const setSizeWithDebounce = useDebouncedCallback( | ||
setSize, | ||
options.debounceDelay | ||
); | ||
|
||
useIsomorphicLayoutEffect(() => { | ||
const element = ref.current; | ||
|
||
if (!element) { | ||
return; | ||
} | ||
|
||
const observer = new ResizeObserver((entries: ResizeObserverEntry[]) => { | ||
if (entries.length === 0 || entries[0].borderBoxSize.length === 0) { | ||
return; | ||
} | ||
|
||
const borderBoxSize = entries[0].borderBoxSize[0]; | ||
|
||
setSizeWithDebounce((prevSize) => { | ||
if ( | ||
prevSize.inlineSize === borderBoxSize.inlineSize && | ||
prevSize.blockSize === borderBoxSize.blockSize | ||
) { | ||
return prevSize; | ||
} | ||
|
||
return { | ||
inlineSize: borderBoxSize.inlineSize, | ||
blockSize: borderBoxSize.blockSize, | ||
}; | ||
}); | ||
}); | ||
|
||
observer.observe(element); | ||
|
||
setSize({ | ||
inlineSize: element.offsetWidth, | ||
blockSize: element.offsetHeight, | ||
}); | ||
|
||
return () => { | ||
observer.unobserve(element); | ||
}; | ||
}, [setSizeWithDebounce]); | ||
|
||
return size; | ||
}; |
15 changes: 15 additions & 0 deletions
15
packages/fuselage-hooks/src/useContentBoxSize.server.spec.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,15 @@ | ||
/** | ||
* @jest-environment node | ||
*/ | ||
|
||
import { renderHook } from '@testing-library/react-hooks/server'; | ||
import { useRef } from 'react'; | ||
|
||
import { useContentBoxSize } from './useContentBoxSize'; | ||
|
||
it('immediately returns zero size', () => { | ||
const { result } = renderHook(() => useContentBoxSize(useRef())); | ||
|
||
expect(result.current.inlineSize).toStrictEqual(0); | ||
expect(result.current.blockSize).toStrictEqual(0); | ||
}); |
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,101 @@ | ||
import { renderHook, act } from '@testing-library/react-hooks'; | ||
import { useRef, RefObject } from 'react'; | ||
import { withResizeObserverMock } from 'testing-utils/mocks/withResizeObserverMock'; | ||
|
||
import { useContentBoxSize } from './useContentBoxSize'; | ||
|
||
withResizeObserverMock(); | ||
|
||
beforeAll(() => { | ||
jest.useFakeTimers(); | ||
}); | ||
|
||
let element: HTMLElement; | ||
|
||
beforeEach(() => { | ||
element = document.createElement('div'); | ||
element.style.width = '40px'; | ||
element.style.height = '30px'; | ||
element.style.padding = '5px'; | ||
document.body.append(element); | ||
}); | ||
|
||
afterEach(() => { | ||
element.remove(); | ||
}); | ||
|
||
const wrapRef = (ref: RefObject<HTMLElement>) => { | ||
Object.assign(ref, { current: element }); | ||
return ref; | ||
}; | ||
|
||
it('immediately returns size', async () => { | ||
const { result } = renderHook(() => useContentBoxSize(wrapRef(useRef()))); | ||
|
||
expect(result.current.inlineSize).toStrictEqual(40); | ||
expect(result.current.blockSize).toStrictEqual(30); | ||
}); | ||
|
||
it('gets the observed element size after resize', async () => { | ||
const { result } = renderHook(() => useContentBoxSize(wrapRef(useRef()))); | ||
|
||
// triggers MutationObserver | ||
await act(async () => { | ||
element.style.width = '30px'; | ||
element.style.height = '40px'; | ||
element.style.padding = '15px'; | ||
}); | ||
|
||
// waits for debounced state mutation | ||
await act(async () => { | ||
jest.advanceTimersByTime(0); | ||
}); | ||
|
||
expect(result.current.inlineSize).toStrictEqual(30); | ||
expect(result.current.blockSize).toStrictEqual(40); | ||
|
||
// triggers MutationObserver | ||
await act(async () => { | ||
element.style.boxSizing = 'border-box'; | ||
}); | ||
|
||
// waits for debounced state mutation | ||
await act(async () => { | ||
jest.advanceTimersByTime(0); | ||
}); | ||
|
||
expect(result.current.inlineSize).toStrictEqual(0); | ||
expect(result.current.blockSize).toStrictEqual(10); | ||
}); | ||
|
||
it('debounces the observed element size', async () => { | ||
const halfDelay = 50; | ||
const delay = 2 * halfDelay; | ||
|
||
const { result } = renderHook(() => | ||
useContentBoxSize(wrapRef(useRef()), { debounceDelay: delay }) | ||
); | ||
|
||
// triggers MutationObserver | ||
await act(async () => { | ||
element.style.width = '30px'; | ||
element.style.height = '40px'; | ||
element.style.padding = '15px'; | ||
}); | ||
|
||
// waits for debounced state mutation | ||
await act(async () => { | ||
jest.advanceTimersByTime(halfDelay); | ||
}); | ||
|
||
expect(result.current.inlineSize).toStrictEqual(40); | ||
expect(result.current.blockSize).toStrictEqual(30); | ||
|
||
// wait the callback trigger from ResizeObserver | ||
await act(async () => { | ||
jest.advanceTimersByTime(halfDelay); | ||
}); | ||
|
||
expect(result.current.inlineSize).toStrictEqual(30); | ||
expect(result.current.blockSize).toStrictEqual(40); | ||
}); |
Oops, something went wrong.