Skip to content

Commit

Permalink
Strict TS typing for useResizeObserver (#3859)
Browse files Browse the repository at this point in the history
* Strict type useResizeObserver

* Release notes

* Fix typecheck error
  • Loading branch information
joel-jeremy authored Nov 20, 2024
1 parent 0696c81 commit 278ac0c
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function BudgetSummaries({ SummaryComponent }: BudgetSummariesProps) {
config: { mass: 3, tension: 600, friction: 80 },
}));

const containerRef = useResizeObserver(
const containerRef = useResizeObserver<HTMLDivElement>(
useCallback(rect => {
setWidthState(rect.width);
}, []),
Expand Down
2 changes: 1 addition & 1 deletion packages/desktop-client/src/components/sidebar/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export function Sidebar() {
dispatch(replaceModal('add-account'));
};

const containerRef = useResizeObserver(rect => {
const containerRef = useResizeObserver<HTMLDivElement>(rect => {
setSidebarWidth(rect.width);
});

Expand Down
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
6 changes: 6 additions & 0 deletions upcoming-release-notes/3859.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Maintenance
authors: [joel-jeremy]
---

Strict TS typing for useResizeObserver.ts

0 comments on commit 278ac0c

Please sign in to comment.