-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(elements): Support SignIn.Captcha and sso-callback step (#4523)
- Loading branch information
Showing
6 changed files
with
96 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@clerk/elements': minor | ||
--- | ||
|
||
Introduce support for `<SignIn.Captcha />` and `<SignIn.Step name='sso-callback'>`. This allows rendering of a CAPTCHA widget when a sign in attempt is transferred to a sign up attempt. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { Slot } from '@radix-ui/react-slot'; | ||
import * as React from 'react'; | ||
|
||
import { CAPTCHA_ELEMENT_ID } from '~/internals/constants'; | ||
import { ClerkElementsRuntimeError } from '~/internals/errors'; | ||
|
||
import { useActiveTags } from '../hooks'; | ||
import { SignInRouterCtx } from './context'; | ||
|
||
export type SignInCaptchaElement = React.ElementRef<'div'>; | ||
|
||
type CaptchaElementProps = Omit< | ||
React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>, | ||
'id' | 'children' | ||
>; | ||
|
||
export type SignInCaptchaProps = | ||
| ({ | ||
asChild: true; | ||
/* Must only be a self-closing element/component */ | ||
children: React.ReactElement; | ||
} & CaptchaElementProps) | ||
| ({ asChild?: false; children?: undefined } & CaptchaElementProps); | ||
|
||
/** | ||
* The `<SignIn.Captcha>` component is used to render the Cloudflare Turnstile widget. It must be used within the `<SignIn.Step name="sso-callback">` component. | ||
* | ||
* If utilizing the `asChild` prop, the component must be a self-closing element or component. Any children passed to the immediate child component of <SignIn.Captcha> will be ignored. | ||
* | ||
* @param {boolean} [asChild] - If true, `<Captcha />` will render as its child element, passing along any necessary props. | ||
* | ||
* @example | ||
* <SignIn.Root> | ||
* <SignIn.Step name="sso-callback"> | ||
* <SignIn.Captcha /> | ||
* </SignIn.Step> | ||
* </SignIn.Root> | ||
* | ||
* @example | ||
* <SignIn.Root> | ||
* <SignIn.Step name="sso-callback"> | ||
* <SignIn.Captcha asChild> | ||
* <aside/> | ||
* </SignIn.Captcha> | ||
* </SignIn.Step> | ||
* </SignIn.Root> | ||
*/ | ||
|
||
export const SignInCaptcha = React.forwardRef<SignInCaptchaElement, SignInCaptchaProps>( | ||
({ asChild, children, ...rest }, forwardedRef) => { | ||
const routerRef = SignInRouterCtx.useActorRef(); | ||
const activeState = useActiveTags(routerRef, 'step:callback'); | ||
|
||
if (!activeState) { | ||
throw new ClerkElementsRuntimeError( | ||
'<Captcha> must be used within the <SignIn.Step name="sso-callback"> component.', | ||
); | ||
} | ||
|
||
const Comp = asChild ? Slot : 'div'; | ||
|
||
return ( | ||
<Comp | ||
id={CAPTCHA_ELEMENT_ID} | ||
{...rest} | ||
ref={forwardedRef} | ||
/> | ||
); | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import type { PropsWithChildren } from 'react'; | ||
|
||
import { useActiveTags } from '~/react/hooks'; | ||
import { SignInRouterCtx } from '~/react/sign-in/context'; | ||
|
||
export type SignInSSOCallbackProps = PropsWithChildren; | ||
|
||
export function SignInSSOCallback({ children }: SignInSSOCallbackProps) { | ||
const routerRef = SignInRouterCtx.useActorRef(); | ||
const activeState = useActiveTags(routerRef, 'step:callback'); | ||
|
||
return activeState ? children : null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters