Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
415f283
feat(clerk-js,react,types): Introduce state signals
dstaley Jul 31, 2025
fbd1f03
Merge branch 'main' into ds.feat/signals-sign-in
dstaley Jul 31, 2025
2d00936
fix(clerk-js): Mark state property as readonly
dstaley Jul 31, 2025
e71897c
Merge branch 'main' into ds.feat/signals-sign-in
dstaley Aug 1, 2025
66f9a55
feat(clerk-js): Add SignInBeta class
dstaley Aug 4, 2025
45e3607
Merge branch 'main' into ds.feat/signals-sign-in
dstaley Aug 4, 2025
57ef049
feat(clerk-js,types): Rename to SignInFuture, use resource-agnostic e…
dstaley Aug 5, 2025
a3614d7
fix(clerk-js): Rename internal_beta to internal_future
dstaley Aug 6, 2025
0ed14b6
chore(clerk-js): Narrow range for alien-signals
dstaley Aug 6, 2025
d82111c
fix(clerk-js): Lint
dstaley Aug 6, 2025
253bd9c
chore(clerk-js): Add JSDoc comments
dstaley Aug 6, 2025
bd5b8e6
Merge branch 'main' into ds.feat/signals-sign-in
dstaley Aug 6, 2025
18d4b57
chore(repo): Add empty changeset
dstaley Aug 6, 2025
63c4a45
chore(clerk-js): Add JSDoc comments
dstaley Aug 6, 2025
1bd1005
chore(clerk-js): Bump bundle limits
dstaley Aug 6, 2025
1e3bf21
Merge branch 'main' into ds.feat/signals-sign-in
dstaley Aug 7, 2025
e08b6aa
chore(repo): Add changeset
dstaley Aug 7, 2025
37f2eca
chore(clerk-js): Bump bundle limit
dstaley Aug 7, 2025
3ee4911
Merge branch 'main' into ds.feat/signals-sign-in
dstaley Aug 7, 2025
7c828e4
Merge branch 'main' into ds.feat/signals-sign-in
dstaley Aug 7, 2025
bc8457b
Merge branch 'main' into ds.feat/signals-sign-in
dstaley Aug 7, 2025
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
1 change: 1 addition & 0 deletions packages/clerk-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"@swc/helpers": "^0.5.17",
"@zxcvbn-ts/core": "3.0.4",
"@zxcvbn-ts/language-common": "3.0.4",
"alien-signals": "^2.0.5",
"browser-tabs-lock": "1.3.0",
"copy-to-clipboard": "3.3.3",
"core-js": "3.41.0",
Expand Down
2 changes: 2 additions & 0 deletions packages/clerk-js/src/core/clerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ import {
Waitlist,
} from './resources/internal';
import { navigateToTask } from './sessionTasks';
import { State } from './state';
import { warnings } from './warnings';

type SetActiveHook = (intent?: 'sign-out') => void | Promise<void>;
Expand Down Expand Up @@ -211,6 +212,7 @@ export class Clerk implements ClerkInterface {
public user: UserResource | null | undefined;
public __internal_country?: string | null;
public telemetry: TelemetryCollector | undefined;
public __internal_state: State = new State();

protected internal_last_error: ClerkAPIError | null = null;
// converted to protected environment to support `updateEnvironment` type assertion
Expand Down
5 changes: 5 additions & 0 deletions packages/clerk-js/src/core/events.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
import { createEventBus } from '@clerk/shared/eventBus';
import type { TokenResource } from '@clerk/types';

import type { SignIn } from './resources';

export const events = {
TokenUpdate: 'token:update',
UserSignOut: 'user:signOut',
EnvironmentUpdate: 'environment:update',
SessionTokenResolved: 'session:tokenResolved',
SignInUpdate: 'signin:update',
} as const;

type TokenUpdatePayload = { token: TokenResource | null };
export type SignInUpdatePayload = { resource: SignIn };

type InternalEvents = {
[events.TokenUpdate]: TokenUpdatePayload;
[events.UserSignOut]: null;
[events.EnvironmentUpdate]: null;
[events.SessionTokenResolved]: null;
[events.SignInUpdate]: SignInUpdatePayload;
};

export const eventBus = createEventBus<InternalEvents>();
3 changes: 3 additions & 0 deletions packages/clerk-js/src/core/resources/SignIn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import {
clerkVerifyPasskeyCalledBeforeCreate,
clerkVerifyWeb3WalletCalledBeforeCreate,
} from '../errors';
import { eventBus } from '../events';
import { BaseResource, UserData, Verification } from './internal';

export class SignIn extends BaseResource implements SignInResource {
Expand Down Expand Up @@ -451,6 +452,8 @@ export class SignIn extends BaseResource implements SignInResource {
this.createdSessionId = data.created_session_id;
this.userData = new UserData(data.user_data);
}

eventBus.emit('signin:update', { resource: this });
return this;
}

Expand Down
5 changes: 5 additions & 0 deletions packages/clerk-js/src/core/signals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { signal } from 'alien-signals';

import type { SignIn } from './resources/SignIn';

export const signInSignal = signal<{ resource: SignIn | null }>({ resource: null });
20 changes: 20 additions & 0 deletions packages/clerk-js/src/core/state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { State as StateInterface } from '@clerk/types';
import { computed, effect } from 'alien-signals';

import { eventBus, type SignInUpdatePayload } from './events';
import { signInSignal } from './signals';

export class State implements StateInterface {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JSDoc would be good here 👀 what's it for? Maybe it belongs on the State type instead.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put it on the __internal_state type for Clerk, which I'm okay with for the time being since that's where other JSDoc comments for properties are. I'll add more general doc comments to this file in a later PR.

signInSignal = signInSignal;

__internal_effect = effect;
__internal_computed = computed;

constructor() {
eventBus.on('signin:update', this.onSignInUpdated);
}

private onSignInUpdated = (payload: SignInUpdatePayload) => {
this.signInSignal({ resource: payload.resource });
};
}
1 change: 1 addition & 0 deletions packages/react/src/experimental.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export { CheckoutButton } from './components/CheckoutButton';
export { PlanDetailsButton } from './components/PlanDetailsButton';
export { SubscriptionDetailsButton } from './components/SubscriptionDetailsButton';
export { useSignInSignal } from './hooks/useClerkSignal';

export type {
__experimental_CheckoutButtonProps as CheckoutButtonProps,
Expand Down
55 changes: 55 additions & 0 deletions packages/react/src/hooks/useClerkSignal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import type { SignInResource } from '@clerk/types';
import { useCallback, useSyncExternalStore } from 'react';

import { useIsomorphicClerkContext } from '../contexts/IsomorphicClerkContext';
import { useAssertWrappedByClerkProvider } from './useAssertWrappedByClerkProvider';

function useClerkSignal(signal: 'signIn'): { isLoaded: boolean; signIn: SignInResource | null } {
useAssertWrappedByClerkProvider('useSignInSignal');

const clerk = useIsomorphicClerkContext();

const subscribe = useCallback(
(callback: () => void) => {
if (!clerk.loaded || !clerk.__internal_state) {
return () => {};
}

return clerk.__internal_state.__internal_effect(() => {
switch (signal) {
case 'signIn':
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- we know that the state is defined
clerk.__internal_state!.signInSignal();
break;
default:
throw new Error(`Unknown signal: ${signal}`);
}
callback();
});
},
[clerk, clerk.loaded, clerk.__internal_state],
);
const getSnapshot = useCallback(() => {
if (!clerk.__internal_state) {
return null;
}

switch (signal) {
case 'signIn':
return clerk.__internal_state.signInSignal().resource;
default:
throw new Error(`Unknown signal: ${signal}`);
}
}, [clerk.__internal_state]);

const value = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);

return {
isLoaded: clerk.loaded,
[signal]: value,
};
}

export function useSignInSignal() {
return useClerkSignal('signIn');
}
5 changes: 5 additions & 0 deletions packages/react/src/isomorphicClerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import type {
SignUpProps,
SignUpRedirectOptions,
SignUpResource,
State,
TaskSelectOrganizationProps,
UnsubscribeCallback,
UserButtonProps,
Expand Down Expand Up @@ -714,6 +715,10 @@ export class IsomorphicClerk implements IsomorphicLoadedClerk {
return this.clerkjs?.billing;
}

get __internal_state(): State | undefined {
return this.clerkjs?.__internal_state;
}

get apiKeys(): APIKeysNamespace | undefined {
return this.clerkjs?.apiKeys;
}
Expand Down
3 changes: 3 additions & 0 deletions packages/types/src/clerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import type { SessionVerificationLevel } from './sessionVerification';
import type { SignInResource } from './signIn';
import type { SignUpResource } from './signUp';
import type { ClientJSONSnapshot, EnvironmentJSONSnapshot } from './snapshots';
import type { State } from './state';
import type { Web3Strategy } from './strategies';
import type { TelemetryCollector } from './telemetry';
import type { UserResource } from './user';
Expand Down Expand Up @@ -227,6 +228,8 @@ export interface Clerk {
/** Current User. */
user: UserResource | null | undefined;

__internal_state: State | undefined;

/**
* @experimental This is an experimental API for the Billing feature that is available under a public beta, and the API is subject to change.
* @see https://clerk.com/docs/billing/overview
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export * from './sessionVerification';
export * from './signIn';
export * from './signUp';
export * from './ssr';
export * from './state';
export * from './strategies';
export * from './theme';
export * from './token';
Expand Down
12 changes: 12 additions & 0 deletions packages/types/src/state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { SignInResource } from './signIn';

export interface State {
signInSignal: {
(): {
resource: SignInResource | null;
};
(value: { resource: SignInResource | null }): void;
};
__internal_effect: (callback: () => void) => () => void;
__internal_computed: <T>(getter: (previousValue?: T) => T) => () => T;
}
39 changes: 9 additions & 30 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading