Skip to content

[wip] chore(clerk-js): Export Tasks as experimental component for custom flows #5843

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
6 changes: 6 additions & 0 deletions .changeset/silent-terms-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@clerk/clerk-js': patch
'@clerk/react': patch
---

Expose `__experimental_Tasks` component for after-auth custom flows out of `SignIn/SignUp` components
23 changes: 23 additions & 0 deletions packages/clerk-js/src/core/clerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1037,6 +1037,29 @@ export class Clerk implements ClerkInterface {
);
};

public mountTasks = (node: HTMLDivElement): void => {
this.assertComponentsReady(this.#componentControls);

void this.#componentControls.ensureMounted({ preloadHint: '__experimental_Tasks' }).then(controls =>
controls.mountComponent({
name: '__experimental_Tasks',
appearanceKey: 'tasks',
node,
}),
);

this.telemetry?.record(eventPrebuiltComponentMounted('__experimental_Tasks'));
};

public unmountTasks = (node: HTMLDivElement): void => {
this.assertComponentsReady(this.#componentControls);
void this.#componentControls.ensureMounted().then(controls =>
controls.unmountComponent({
node,
}),
);
};

/**
* `setActive` can be used to set the active session and/or organization.
*/
Expand Down
10 changes: 3 additions & 7 deletions packages/clerk-js/src/ui/components/SessionTasks/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useClerk } from '@clerk/shared/react';
import { eventComponentMounted } from '@clerk/shared/telemetry';
import type { SessionTask } from '@clerk/types';
import { useCallback, useContext, useEffect } from 'react';

import { SESSION_TASK_ROUTE_BY_KEY } from '../../../core/sessionTasks';
Expand Down Expand Up @@ -36,7 +35,7 @@ const SessionTasksStart = withCardStateProvider(() => {
);
});

function SessionTaskRoutes(): JSX.Element {
function SessionTasksRoutes(): JSX.Element {
return (
<Switch>
<Route path={SESSION_TASK_ROUTE_BY_KEY['org']}>
Expand All @@ -56,10 +55,7 @@ function SessionTaskRoutes(): JSX.Element {
);
}

/**
* @internal
*/
export function SessionTask(): JSX.Element {
export function __experimental_Tasks(): JSX.Element {
const clerk = useClerk();
const { navigate } = useRouter();
const signInContext = useContext(SignInContext);
Expand All @@ -86,7 +82,7 @@ export function SessionTask(): JSX.Element {

return (
<SessionTasksContext.Provider value={{ nextTask, redirectUrlComplete }}>
<SessionTaskRoutes />
<SessionTasksRoutes />
</SessionTasksContext.Provider>
);
}
6 changes: 3 additions & 3 deletions packages/clerk-js/src/ui/components/SignIn/SignIn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useClerk } from '@clerk/shared/react';
import type { SignInModalProps, SignInProps } from '@clerk/types';
import React from 'react';

import { SessionTasks as LazySessionTasks } from '../../../ui/lazyModules/components';
import { __experimental_Tasks as LazyTasks } from '../../../ui/lazyModules/components';
import { normalizeRoutingOptions } from '../../../utils/normalizeRoutingOptions';
import { SignInEmailLinkFlowComplete, SignUpEmailLinkFlowComplete } from '../../common/EmailLinkCompleteFlowCard';
import {
Expand Down Expand Up @@ -131,7 +131,7 @@ function SignInRoutes(): JSX.Element {
<LazySignUpVerifyPhone />
</Route>
<Route path='tasks'>
<LazySessionTasks />
<LazyTasks />
</Route>
<Route index>
<LazySignUpContinue />
Expand All @@ -143,7 +143,7 @@ function SignInRoutes(): JSX.Element {
</Route>
)}
<Route path='tasks'>
<LazySessionTasks />
<LazyTasks />
</Route>
<Route index>
<SignInStart />
Expand Down
4 changes: 2 additions & 2 deletions packages/clerk-js/src/ui/components/SignUp/SignUp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useClerk } from '@clerk/shared/react';
import type { SignUpModalProps, SignUpProps } from '@clerk/types';
import React from 'react';

import { SessionTasks as LazySessionTasks } from '../../../ui/lazyModules/components';
import { __experimental_Tasks as LazyTasks } from '../../../ui/lazyModules/components';
import { SignUpEmailLinkFlowComplete } from '../../common/EmailLinkCompleteFlowCard';
import { SignUpContext, useSignUpContext, withCoreSessionSwitchGuard } from '../../contexts';
import { Flow } from '../../customizables';
Expand Down Expand Up @@ -85,7 +85,7 @@ function SignUpRoutes(): JSX.Element {
</Route>
</Route>
<Route path='tasks'>
<LazySessionTasks />
<LazyTasks />
</Route>
<Route index>
<SignUpStart />
Expand Down
5 changes: 3 additions & 2 deletions packages/clerk-js/src/ui/lazyModules/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ export const PlanDetails = lazy(() =>
componentImportPaths.PlanDetails().then(module => ({ default: module.PlanDetails })),
);

export const SessionTasks = lazy(() =>
componentImportPaths.SessionTasks().then(module => ({ default: module.SessionTask })),
export const __experimental_Tasks = lazy(() =>
componentImportPaths.SessionTasks().then(module => ({ default: module.__experimental_Tasks })),
);

export const preloadComponent = async (component: unknown) => {
Expand Down Expand Up @@ -133,6 +133,7 @@ export const ClerkComponents = {
PricingTable,
Checkout,
PlanDetails,
__experimental_Tasks,
};

export type ClerkComponentName = keyof typeof ClerkComponents;
1 change: 1 addition & 0 deletions packages/react/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export {
GoogleOneTap,
Waitlist,
PricingTable,
__experimental_Tasks,
} from './uiComponents';

export {
Expand Down
28 changes: 28 additions & 0 deletions packages/react/src/components/uiComponents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -600,3 +600,31 @@ export const PricingTable = withClerk(
},
{ component: 'PricingTable', renderWhileLoading: true },
);

export const __experimental_Tasks = withClerk(
({ clerk, component, fallback, ...props }: WithClerkProp<FallbackProp>) => {
const mountingStatus = useWaitForComponentMount(component);
const shouldShowFallback = mountingStatus === 'rendering' || !clerk.loaded;

const rendererRootProps = {
...(shouldShowFallback && fallback && { style: { display: 'none' } }),
};

return (
<>
{shouldShowFallback && fallback}
{clerk.loaded && (
<ClerkHostRenderer
component={component}
mount={clerk.mountTasks}
unmount={clerk.unmountTasks}
updateProps={(clerk as any).__unstable__updateProps}
props={props}
rootProps={rendererRootProps}
/>
)}
</>
);
},
{ component: '__experimental_Tasks', renderWhileLoading: true },
);
17 changes: 17 additions & 0 deletions packages/react/src/isomorphicClerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ export class IsomorphicClerk implements IsomorphicLoadedClerk {
private premountMethodCalls = new Map<MethodName<BrowserClerk>, MethodCallback>();
private premountWaitlistNodes = new Map<HTMLDivElement, WaitlistProps | undefined>();
private premountPricingTableNodes = new Map<HTMLDivElement, PricingTableProps | undefined>();
private premountTasksNodes = new Map<HTMLDivElement, undefined>();
// A separate Map of `addListener` method calls to handle multiple listeners.
private premountAddListenerCalls = new Map<
ListenerCallback,
Expand Down Expand Up @@ -1050,6 +1051,22 @@ export class IsomorphicClerk implements IsomorphicLoadedClerk {
}
};

mountTasks = (node: HTMLDivElement) => {
if (this.clerkjs && this.loaded) {
this.clerkjs.mountTasks(node);
} else {
this.premountTasksNodes.set(node, undefined);
}
};

unmountTasks = (node: HTMLDivElement) => {
if (this.clerkjs && this.loaded) {
this.clerkjs.unmountTasks(node);
} else {
this.premountTasksNodes.delete(node);
}
};

addListener = (listener: ListenerCallback): UnsubscribeCallback => {
if (this.clerkjs) {
return this.clerkjs.addListener(listener);
Expand Down
4 changes: 4 additions & 0 deletions packages/types/src/appearance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -867,4 +867,8 @@ export type Appearance<T = Theme> = T & {
* Theme overrides that only apply to the `<Checkout />` component
*/
checkout?: T;
/**
* Theme overrides that only apply to the `<Tasks />` component
*/
tasks?: T;
};
14 changes: 14 additions & 0 deletions packages/types/src/clerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,20 @@ export interface Clerk {
*/
unmountPricingTable: (targetNode: HTMLDivElement) => void;

/**
* Mounts tasks component at the target element.
* @param targetNode Target node to mount the Tasks component.
* @param props configuration parameters.
*/
mountTasks: (targetNode: HTMLDivElement) => void;
/**
* Unmount tasks component from the target element.
* If there is no component mounted at the target node, results in a noop.
*
* @param targetNode Target node to unmount the Tasks component from.
*/
unmountTasks: (targetNode: HTMLDivElement) => void;

/**
* Register a listener that triggers a callback each time important Clerk resources are changed.
* Allows to hook up at different steps in the sign up, sign in processes.
Expand Down
Loading