|
1 |
| -import type { SignInProps } from '@clerk/types'; |
| 1 | +import type { SignInButtonProps, SignInProps } from '@clerk/types'; |
2 | 2 | import React from 'react';
|
3 | 3 |
|
4 |
| -import type { SignInButtonProps } from './types'; |
5 | 4 | import { assertSingleChild, normalizeWithDefaultValue, safeExecute, withClerk, type WithClerkProp } from './utils';
|
6 | 5 |
|
7 | 6 | export type { SignInButtonProps };
|
8 | 7 |
|
9 |
| -export const SignInButton = withClerk(({ clerk, children, ...props }: WithClerkProp<SignInButtonProps>) => { |
10 |
| - const { signUpFallbackRedirectUrl, forceRedirectUrl, fallbackRedirectUrl, signUpForceRedirectUrl, mode, ...rest } = |
11 |
| - props; |
12 |
| - children = normalizeWithDefaultValue(children, 'Sign in'); |
13 |
| - const child = assertSingleChild(children)('SignInButton'); |
14 |
| - |
15 |
| - const clickHandler = () => { |
16 |
| - const opts: SignInProps = { |
17 |
| - forceRedirectUrl, |
18 |
| - fallbackRedirectUrl, |
19 |
| - signUpFallbackRedirectUrl, |
20 |
| - signUpForceRedirectUrl, |
| 8 | +export const SignInButton = withClerk( |
| 9 | + ({ clerk, children, ...props }: WithClerkProp<React.PropsWithChildren<SignInButtonProps>>) => { |
| 10 | + const { signUpFallbackRedirectUrl, forceRedirectUrl, fallbackRedirectUrl, signUpForceRedirectUrl, mode, ...rest } = |
| 11 | + props; |
| 12 | + children = normalizeWithDefaultValue(children, 'Sign in'); |
| 13 | + const child = assertSingleChild(children)('SignInButton'); |
| 14 | + |
| 15 | + const clickHandler = () => { |
| 16 | + const opts: SignInProps = { |
| 17 | + forceRedirectUrl, |
| 18 | + fallbackRedirectUrl, |
| 19 | + signUpFallbackRedirectUrl, |
| 20 | + signUpForceRedirectUrl, |
| 21 | + }; |
| 22 | + |
| 23 | + if (!clerk) { |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + if (mode === 'modal') { |
| 28 | + return clerk.openSignIn({ ...opts, appearance: props.appearance }); |
| 29 | + } |
| 30 | + return clerk.redirectToSignIn({ |
| 31 | + ...opts, |
| 32 | + signInFallbackRedirectUrl: fallbackRedirectUrl, |
| 33 | + signInForceRedirectUrl: forceRedirectUrl, |
| 34 | + }); |
21 | 35 | };
|
22 | 36 |
|
23 |
| - if (!clerk) { |
24 |
| - return; |
25 |
| - } |
26 |
| - |
27 |
| - if (mode === 'modal') { |
28 |
| - return clerk.openSignIn(opts); |
29 |
| - } |
30 |
| - return clerk.redirectToSignIn({ |
31 |
| - ...opts, |
32 |
| - signInFallbackRedirectUrl: fallbackRedirectUrl, |
33 |
| - signInForceRedirectUrl: forceRedirectUrl, |
34 |
| - }); |
35 |
| - }; |
36 |
| - |
37 |
| - const wrappedChildClickHandler: React.MouseEventHandler = async e => { |
38 |
| - if (child && typeof child === 'object' && 'props' in child) { |
39 |
| - await safeExecute(child.props.onClick)(e); |
40 |
| - } |
41 |
| - return clickHandler(); |
42 |
| - }; |
43 |
| - |
44 |
| - const childProps = { ...rest, onClick: wrappedChildClickHandler }; |
45 |
| - return React.cloneElement(child as React.ReactElement<unknown>, childProps); |
46 |
| -}, 'SignInButton'); |
| 37 | + const wrappedChildClickHandler: React.MouseEventHandler = async e => { |
| 38 | + if (child && typeof child === 'object' && 'props' in child) { |
| 39 | + await safeExecute(child.props.onClick)(e); |
| 40 | + } |
| 41 | + return clickHandler(); |
| 42 | + }; |
| 43 | + |
| 44 | + const childProps = { ...rest, onClick: wrappedChildClickHandler }; |
| 45 | + return React.cloneElement(child as React.ReactElement<unknown>, childProps); |
| 46 | + }, |
| 47 | + 'SignInButton', |
| 48 | +); |
0 commit comments