forked from deephaven/web-client-ui
-
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.
Only render ListView when non-zero height (deephaven#1909)
- Loading branch information
Showing
5 changed files
with
85 additions
and
12 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { identityExtractHTMLElement } from '@deephaven/utils'; | ||
import { useCallback, useRef, useState } from 'react'; | ||
import useMappedRef from './useMappedRef'; | ||
import useResizeObserver from './useResizeObserver'; | ||
|
||
export interface UseContentRectResult<T> { | ||
contentRect: DOMRectReadOnly; | ||
ref: (refValue: T) => void; | ||
} | ||
|
||
/** | ||
* Returns a callback ref that will track the `contentRect` of a given refValue. | ||
* If the `contentRect` is undefined, it will be set to a new `DOMRect` with | ||
* zeros for all dimensions. | ||
* @param map Optional mapping function to extract an HTMLElement from the given | ||
* refValue | ||
* @returns Content rect and a ref callback | ||
*/ | ||
export function useContentRect<T>( | ||
map: (ref: T) => HTMLElement | null = identityExtractHTMLElement | ||
): UseContentRectResult<T> { | ||
const [contentRect, setContentRect] = useState<DOMRectReadOnly>( | ||
new DOMRect() | ||
); | ||
|
||
const handleResize = useCallback( | ||
([firstEntry]: ResizeObserverEntry[], _observer: ResizeObserver): void => { | ||
setContentRect(firstEntry?.contentRect ?? new DOMRect()); | ||
}, | ||
[] | ||
); | ||
|
||
const observerRef = useRef<HTMLElement>(null); | ||
useResizeObserver(observerRef.current, handleResize); | ||
|
||
const ref = useMappedRef(observerRef, map); | ||
|
||
return { | ||
ref, | ||
contentRect, | ||
}; | ||
} | ||
|
||
export default useContentRect; |