-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2444 slow middle box resizing (#2471)
* hook for callbackDebounce and resize observer * use hook everywhere, remove external resize observer lib * back to previous statement list debounce handling but with custom observer hook * debounced resize for statement list except annotator, remove disableUserSelect from redux and css by js instead
- Loading branch information
Showing
15 changed files
with
130 additions
and
75 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,5 +1,19 @@ | ||
import { useContainerDimensions } from "./useContainerDimensions"; | ||
import useDebounce from "./useDebounce"; | ||
import useDebouncedCallback from "./useDebouncedCallback"; | ||
import useKeyLift from "./useKeyLift"; | ||
import useKeyPress from "./useKeyPress"; | ||
import { useResizeObserver } from "./useResizeObserver"; | ||
import { useSearchParams } from "./useSearchParamsContext"; | ||
import { useWindowSize } from "./useWindowSize"; | ||
|
||
export { useDebounce, useKeyPress, useSearchParams }; | ||
export { | ||
useDebounce, | ||
useKeyPress, | ||
useKeyLift, | ||
useContainerDimensions, | ||
useSearchParams, | ||
useDebouncedCallback, | ||
useResizeObserver, | ||
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,33 @@ | ||
import { useCallback, useRef } from "react"; | ||
|
||
const useDebouncedCallback = <T extends (...args: any[]) => void>( | ||
callback: T, | ||
delay: number | ||
): T => { | ||
const timeoutRef = useRef<number | undefined>(); | ||
|
||
const debouncedCallback = useCallback( | ||
(...args: Parameters<T>) => { | ||
if (timeoutRef.current) { | ||
clearTimeout(timeoutRef.current); | ||
} | ||
|
||
timeoutRef.current = window.setTimeout(() => { | ||
callback(...args); | ||
}, delay); | ||
}, | ||
[callback, delay] | ||
); | ||
|
||
const cleanup = useCallback(() => { | ||
if (timeoutRef.current) { | ||
clearTimeout(timeoutRef.current); | ||
} | ||
}, []); | ||
|
||
useCallback(() => cleanup, [cleanup]); | ||
|
||
return debouncedCallback as T; | ||
}; | ||
|
||
export default useDebouncedCallback; |
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 @@ | ||
import { useDebouncedCallback } from "hooks"; | ||
import { useLayoutEffect, useState, useRef } from "react"; | ||
|
||
interface UseResizeObserverOptions { | ||
debounceDelay?: number; | ||
} | ||
|
||
interface Size { | ||
width: number | undefined; | ||
height: number | undefined; | ||
} | ||
|
||
export const useResizeObserver = <T extends HTMLElement>({ | ||
debounceDelay = 0, | ||
}: UseResizeObserverOptions = {}) => { | ||
const ref = useRef<T | null>(null); | ||
const animationFrameRef = useRef<number | null>(null); | ||
const [size, setSize] = useState<Size>({ | ||
width: undefined, | ||
height: undefined, | ||
}); | ||
|
||
const debouncedCallback = useDebouncedCallback((value: Size) => { | ||
setSize(value); | ||
}, debounceDelay); | ||
|
||
useLayoutEffect(() => { | ||
const element = ref.current; | ||
if (!element || typeof ResizeObserver === "undefined") { | ||
return; | ||
} | ||
|
||
const resizeObserver = new ResizeObserver((entries) => { | ||
animationFrameRef.current = window.requestAnimationFrame(() => { | ||
if (!Array.isArray(entries) || entries.length === 0) { | ||
return; | ||
} | ||
|
||
const entry = entries[0]; | ||
const { width, height } = entry.contentRect; | ||
|
||
debouncedCallback({ | ||
width: Math.round(width), | ||
height: Math.round(height), | ||
}); | ||
}); | ||
}); | ||
|
||
resizeObserver.observe(element); | ||
|
||
return () => { | ||
if (animationFrameRef.current !== null) { | ||
window.cancelAnimationFrame(animationFrameRef.current); | ||
} | ||
resizeObserver.disconnect(); | ||
}; | ||
}, [debouncedCallback]); | ||
|
||
return { ref, ...size }; | ||
}; |
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
Oops, something went wrong.