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

[@mantine/hooks] Support ref cleanup function in React 19 with useMergedRef #7304

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
65 changes: 65 additions & 0 deletions packages/@mantine/hooks/src/use-merged-ref/use-merged-ref.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,69 @@ describe('@mantine/hook/use-merged-ref', () => {
expect(fnRefValue! instanceof HTMLButtonElement).toBe(true);
expect(objectRef.current instanceof HTMLButtonElement).toBe(true);
});

it('when ref callback does not return a function, ref callback is called with null on unmount', () => {
const refCalled: unknown[] = [];

const fnRef = (node: HTMLButtonElement | null) => {
refCalled.push(node);
};

const { unmount } = render(<TestComponent refs={[fnRef]} />);
expect(refCalled).toEqual([expect.any(HTMLButtonElement)]);

unmount();
expect(refCalled).toEqual([expect.any(HTMLButtonElement), null]);
});

describe('react 18', () => {
it('when ref callback returns a function, ref callback is called with null on unmount', () => {
const refCalled: unknown[] = [];
const cleanupCalled: unknown[] = [];

const fnRef = (node: HTMLButtonElement | null) => {
refCalled.push(node);
return () => {
cleanupCalled.push(node);
};
};

const { unmount } = render(<TestComponent refs={[fnRef]} />);
expect(refCalled).toEqual([expect.any(HTMLButtonElement)]);
expect(cleanupCalled).toEqual([]);

unmount();
expect(refCalled).toEqual([expect.any(HTMLButtonElement), null]);
expect(cleanupCalled).toEqual([]);
});

it('when ref callbacks that return a function and those that do not are mixed, each behaves accordingly', () => {
const refCalled: unknown[] = [];
const cleanupCalled: unknown[] = [];

const fnRef = (node: HTMLButtonElement | null) => {
refCalled.push(node);
return () => {
cleanupCalled.push(node);
};
};

const ref2Called: unknown[] = [];
const fnRef2 = (node: HTMLButtonElement | null) => {
ref2Called.push(node);
};

const { unmount } = render(<TestComponent refs={[fnRef, fnRef2]} />);

expect(refCalled).toEqual([expect.any(HTMLButtonElement)]);
expect(ref2Called).toEqual([expect.any(HTMLButtonElement)]);
expect(cleanupCalled).toEqual([]);

unmount();

expect(refCalled).toEqual([expect.any(HTMLButtonElement), null]);
expect(ref2Called).toEqual([expect.any(HTMLButtonElement), null]);
expect(cleanupCalled).toEqual([]);
});
});
});
33 changes: 28 additions & 5 deletions packages/@mantine/hooks/src/use-merged-ref/use-merged-ref.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,41 @@
import { Ref, useCallback } from 'react';
import { Ref, useCallback, type RefCallback } from 'react';

type PossibleRef<T> = Ref<T> | undefined;

export function assignRef<T>(ref: PossibleRef<T>, value: T) {
type RefCleanup<T> = ReturnType<RefCallback<T>>;

export function assignRef<T>(ref: PossibleRef<T>, value: T): RefCleanup<T> {
if (typeof ref === 'function') {
ref(value);
return ref(value);
} else if (typeof ref === 'object' && ref !== null && 'current' in ref) {
(ref as React.MutableRefObject<T>).current = value;
ref.current = value;
}
}

export function mergeRefs<T>(...refs: PossibleRef<T>[]) {
const cleanupMap = new Map<PossibleRef<T>, Exclude<RefCleanup<T>, void>>();

return (node: T | null) => {
refs.forEach((ref) => assignRef(ref, node));
refs.forEach((ref) => {
const cleanup = assignRef(ref, node);
if (cleanup) {
cleanupMap.set(ref, cleanup);
}
});

if (cleanupMap.size > 0) {
return () => {
refs.forEach((ref) => {
const cleanup = cleanupMap.get(ref);
if (cleanup) {
cleanup();
} else {
assignRef(ref, null);
}
});
cleanupMap.clear();
};
}
};
}

Expand Down
Loading