Skip to content

Commit

Permalink
Strict type useResizeObserver
Browse files Browse the repository at this point in the history
  • Loading branch information
joel-jeremy committed Nov 14, 2024
1 parent 0696c81 commit 583c30d
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions packages/desktop-client/src/hooks/useResizeObserver.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
// @ts-strict-ignore
import { useRef, useCallback } from 'react';

export function useResizeObserver(
export function useResizeObserver<T extends Element>(
func: (contentRect: DOMRectReadOnly) => void,
): (el: unknown) => void {
const observer = useRef(null);
): (el: T) => void {
const observer = useRef<ResizeObserver | undefined>(undefined);
if (!observer.current) {
observer.current = new ResizeObserver(entries => {
func(entries[0].contentRect);
});
}

const elementRef = useCallback(el => {
observer.current.disconnect();
const elementRef = useCallback((el: T) => {
observer.current?.disconnect();
if (el) {
observer.current.observe(el, { box: 'border-box' });
observer.current?.observe(el, { box: 'border-box' });
}
}, []);

Expand Down

0 comments on commit 583c30d

Please sign in to comment.