Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/smooth-buttons-accept-one.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/react': patch
---

Allow unstyled button components to accept a single React element passed as an array.
24 changes: 24 additions & 0 deletions packages/react/src/components/__tests__/SignInButton.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,30 @@ describe('<SignInButton/>', () => {
expect(mockRedirectToSignIn).toHaveBeenCalled();
});

it('accepts a single child passed as an array', async () => {
const handler = vi.fn();

render(
<SignInButton>
{[
<button
key='custom'
onClick={handler}
type='button'
>
custom button
</button>,
]}
</SignInButton>,
);

const btn = screen.getByText('custom button');
await userEvent.click(btn);

expect(handler).toHaveBeenCalled();
expect(mockRedirectToSignIn).toHaveBeenCalled();
});

it('uses text passed as children', async () => {
render(<SignInButton>text</SignInButton>);

Expand Down
6 changes: 6 additions & 0 deletions packages/react/src/utils/childrenUtils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ export const assertSingleChild =
try {
return React.Children.only(children);
} catch {
const childArray = React.Children.toArray(children);

if (childArray.length === 1 && React.isValidElement(childArray[0])) {
return childArray[0];
}

return errorThrower.throw(multipleChildrenInButtonComponent(name));
}
};
Expand Down
Loading