Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strict TS typing for useResizeObserver #3859

Merged
merged 3 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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