Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/soft-worms-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@clerk/ui": patch
---

Fixed custom page icons not rendering in React 19 due to a forwarded ref overwriting the internal node reference.
12 changes: 11 additions & 1 deletion packages/ui/src/utils/ExternalElementMounter.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import type { Ref } from 'react';
import { useEffect, useRef } from 'react';

type ExternalElementMounterProps = {
mount: (el: HTMLDivElement) => void;
unmount: (el?: HTMLDivElement) => void;
// In React 19, ref is a regular prop for function components (not wrapped in
// forwardRef). Without this field, ref ends up in ...rest and overwrites the
// internal nodeRef when spread onto the div, preventing mount from being called.
ref?: Ref<HTMLDivElement>;
};

export const ExternalElementMounter = ({ mount, unmount, ...rest }: ExternalElementMounterProps) => {
export const ExternalElementMounter = ({
mount,
unmount,
ref: _forwardedRef,
...rest
}: ExternalElementMounterProps) => {
const nodeRef = useRef(null);

useEffect(() => {
Expand Down
62 changes: 62 additions & 0 deletions packages/ui/src/utils/__tests__/ExternalElementMounter.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { render, waitFor } from '@testing-library/react';
import React from 'react';
import { describe, expect, it, vi } from 'vitest';

import { ExternalElementMounter } from '../ExternalElementMounter';

describe('ExternalElementMounter', () => {
it('calls mount with the host div element', async () => {
const mount = vi.fn();
const unmount = vi.fn();

render(
<ExternalElementMounter
mount={mount}
unmount={unmount}
/>,
);

await waitFor(() => expect(mount).toHaveBeenCalledOnce());
expect(mount).toHaveBeenCalledWith(expect.any(HTMLDivElement));
});

it('calls unmount with the same element when removed', async () => {
const mount = vi.fn();
const unmount = vi.fn();

const { unmount: removeComponent } = render(
<ExternalElementMounter
mount={mount}
unmount={unmount}
/>,
);

await waitFor(() => expect(mount).toHaveBeenCalledOnce());
const mountedEl = mount.mock.calls[0][0];
removeComponent();

expect(unmount).toHaveBeenCalledWith(mountedEl);
});

it('calls mount even when a forwarded ref is present in props (React 19 regression)', async () => {
// In React 19, ref is a regular prop. Before the fix, a ref forwarded through
// the component chain would land in ...rest and overwrite nodeRef in
// `<div ref={nodeRef} {...rest} />`, preventing mount from being called.
// React 18 strips ref before passing to non-forwardRef components, so the
// regression is only observable in a React 19 runtime — but this test guards
// against it being reintroduced.
const mount = vi.fn();
const unmount = vi.fn();

render(
React.createElement(ExternalElementMounter, {
mount,
unmount,
ref: { current: null },
}),
);

await waitFor(() => expect(mount).toHaveBeenCalledOnce());
expect(mount).toHaveBeenCalledWith(expect.any(HTMLDivElement));
});
});
Loading