Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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/curly-jeans-sleep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/clerk-react': minor
---

Expose `<CheckoutButton/>`, `<SubscriptionDetailsButton/>`, `<PlanDetailsButton/>` from `@clerk/clerk-react/experimental`.
5 changes: 5 additions & 0 deletions .changeset/dark-coins-shake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/nextjs': minor
---

Expose `<CheckoutButton/>`, `<SubscriptionDetailsButton/>`, `<PlanDetailsButton/>` from `@clerk/nextjs/experimental`.
5 changes: 5 additions & 0 deletions packages/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@
"types": "./dist/types/webhooks.d.ts",
"import": "./dist/esm/webhooks.js",
"require": "./dist/cjs/webhooks.js"
},
"./experimental": {
"types": "./dist/types/experimental.d.ts",
"import": "./dist/esm/experimental.js",
"require": "./dist/cjs/experimental.js"
}
Comment thread
panteliselef marked this conversation as resolved.
},
"types": "./dist/types/index.d.ts",
Expand Down
3 changes: 3 additions & 0 deletions packages/nextjs/src/experimental.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use client';

export { CheckoutButton, PlanDetailsButton, SubscriptionDetailsButton } from '@clerk/clerk-react/experimental';
13 changes: 12 additions & 1 deletion packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,24 @@
"default": "./dist/errors.js"
}
},
"./experimental": {
"import": {
"types": "./dist/experimental.d.mts",
"default": "./dist/experimental.mjs"
},
"require": {
"types": "./dist/experimental.d.ts",
"default": "./dist/experimental.js"
}
},
"./package.json": "./package.json"
},
"main": "./dist/index.js",
"files": [
"dist",
"internal",
"errors"
"errors",
"experimental"
],
"scripts": {
"build": "tsup",
Expand Down
68 changes: 68 additions & 0 deletions packages/react/src/components/CheckoutButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import type { __internal_CheckoutProps } from '@clerk/types';
import React from 'react';

import { useAuth } from '../hooks';
import type { WithClerkProp } from '../types';
import { assertSingleChild, normalizeWithDefaultValue, safeExecute } from '../utils';
import { withClerk } from './withClerk';

export type { __internal_CheckoutProps };

export const CheckoutButton = withClerk(
({ clerk, children, ...props }: WithClerkProp<React.PropsWithChildren<__internal_CheckoutProps>>) => {
const {
appearance,
planId,
planPeriod,
subscriberType,
onSubscriptionComplete,
portalId,
portalRoot,
Comment thread
panteliselef marked this conversation as resolved.
Outdated
newSubscriptionRedirectUrl,
onClose,
...rest
} = props;

const { userId, orgId } = useAuth();

if (userId === null) {
throw new Error('Ensure that `<CheckoutButton />` is rendered inside a `<SignedIn />` component.');
}
Comment thread
brkalow marked this conversation as resolved.

if (orgId === null && subscriberType === 'org') {
throw new Error('Wrap `<CheckoutButton for="organization" />` with a check for an active organization.');
}

children = normalizeWithDefaultValue(children, 'Checkout');
const child = assertSingleChild(children)('CheckoutButton');

const clickHandler = () => {
if (!clerk) {
return;
}

return clerk.__internal_openCheckout({
appearance,
planId,
planPeriod,
subscriberType,
onSubscriptionComplete,
portalId,
portalRoot,
newSubscriptionRedirectUrl,
onClose,
});
};

const wrappedChildClickHandler: React.MouseEventHandler = async e => {
if (child && typeof child === 'object' && 'props' in child) {
await safeExecute(child.props.onClick)(e);
}
return clickHandler();
};

const childProps = { ...rest, onClick: wrappedChildClickHandler };
return React.cloneElement(child as React.ReactElement<unknown>, childProps);
},
{ component: 'CheckoutButton', renderWhileLoading: true },
);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
42 changes: 42 additions & 0 deletions packages/react/src/components/PlanDetailsButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import type { __internal_PlanDetailsProps } from '@clerk/types';
import React from 'react';

import type { WithClerkProp } from '../types';
import { assertSingleChild, normalizeWithDefaultValue, safeExecute } from '../utils';
import { withClerk } from './withClerk';

export type { __internal_PlanDetailsProps };

export const PlanDetailsButton = withClerk(
({ clerk, children, ...props }: WithClerkProp<React.PropsWithChildren<__internal_PlanDetailsProps>>) => {
const { plan, planId, appearance, initialPlanPeriod, portalId, portalRoot, ...rest } = props;
children = normalizeWithDefaultValue(children, 'Plan details');
const child = assertSingleChild(children)('PlanDetailsButton');

const clickHandler = () => {
if (!clerk) {
return;
}

return clerk.__internal_openPlanDetails({
plan,
planId,
appearance,
initialPlanPeriod,
portalId,
portalRoot,
});
};

const wrappedChildClickHandler: React.MouseEventHandler = async e => {
if (child && typeof child === 'object' && 'props' in child) {
await safeExecute(child.props.onClick)(e);
}
return clickHandler();
};

const childProps = { ...rest, onClick: wrappedChildClickHandler };
return React.cloneElement(child as React.ReactElement<unknown>, childProps);
},
{ component: 'PlanDetailsButton', renderWhileLoading: true },
);
54 changes: 54 additions & 0 deletions packages/react/src/components/SubscriptionDetailsButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import type { __internal_SubscriptionDetailsProps } from '@clerk/types';
import React from 'react';

import { useAuth } from '../hooks';
import type { WithClerkProp } from '../types';
import { assertSingleChild, normalizeWithDefaultValue, safeExecute } from '../utils';
import { withClerk } from './withClerk';

export type { __internal_SubscriptionDetailsProps };

export const SubscriptionDetailsButton = withClerk(
({ clerk, children, ...props }: WithClerkProp<React.PropsWithChildren<__internal_SubscriptionDetailsProps>>) => {
const { for: forProp, appearance, onSubscriptionCancel, portalId, portalRoot, ...rest } = props;
children = normalizeWithDefaultValue(children, 'Subscription details');
const child = assertSingleChild(children)('SubscriptionDetailsButton');

const { userId, orgId } = useAuth();

if (userId === null) {
throw new Error('Ensure that `<SubscriptionDetailsButton />` is rendered inside a `<SignedIn />` component.');
}

if (orgId === null && forProp === 'org') {
throw new Error(
'Wrap `<SubscriptionDetailsButton for="organization" />` with a check for an active organization.',
);
}

const clickHandler = () => {
if (!clerk) {
return;
}

return clerk.__internal_openSubscriptionDetails({
for: forProp,
appearance,
onSubscriptionCancel,
portalId,
portalRoot,
});
};

const wrappedChildClickHandler: React.MouseEventHandler = async e => {
if (child && typeof child === 'object' && 'props' in child) {
await safeExecute(child.props.onClick)(e);
}
return clickHandler();
};

const childProps = { ...rest, onClick: wrappedChildClickHandler };
return React.cloneElement(child as React.ReactElement<unknown>, childProps);
},
{ component: 'SubscriptionDetailsButton', renderWhileLoading: true },
);
3 changes: 3 additions & 0 deletions packages/react/src/experimental.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { CheckoutButton } from './components/CheckoutButton';
export { PlanDetailsButton } from './components/PlanDetailsButton';
export { SubscriptionDetailsButton } from './components/SubscriptionDetailsButton';
11 changes: 10 additions & 1 deletion packages/react/src/utils/childrenUtils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@ import { multipleChildrenInButtonComponent } from '../errors/messages';

export const assertSingleChild =
(children: React.ReactNode) =>
(name: 'SignInButton' | 'SignUpButton' | 'SignOutButton' | 'SignInWithMetamaskButton') => {
(
name:
| 'SignInButton'
| 'SignUpButton'
| 'SignOutButton'
| 'SignInWithMetamaskButton'
| 'CheckoutButton'
| 'SubscriptionDetailsButton'
| 'PlanDetailsButton',
) => {
try {
return React.Children.only(children);
} catch {
Expand Down
1 change: 1 addition & 0 deletions packages/react/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default defineConfig(overrideOptions => {
index: 'src/index.ts',
internal: 'src/internal.ts',
errors: 'src/errors.ts',
experimental: 'src/experimental.ts',
},
dts: true,
onSuccess: shouldPublish ? 'pnpm publish:local' : undefined,
Expand Down