-
Notifications
You must be signed in to change notification settings - Fork 378
feat(clerk-react, nextjs): Introduce commerce buttons #6365
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
Merged
panteliselef
merged 12 commits into
main
from
elef/com-961-dx-guide-introduce-prebuilt-components
Jul 23, 2025
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c7ebb9f
feat(clerk-react, nextjs): Introduce commerce buttons
panteliselef 48e99b5
add missing package json files
panteliselef f328d0f
cleanup buttons
panteliselef 0146677
Merge branch 'refs/heads/main' into elef/com-961-dx-guide-introduce-p…
panteliselef 216b929
add changesets
panteliselef 4919ef8
add docs and tests
panteliselef ae71b75
move portal root one level deep
panteliselef 8b1de67
update type docs
panteliselef 30e5d33
Merge branch 'main' into elef/com-961-dx-guide-introduce-prebuilt-com…
panteliselef 8b76c97
replace drawer object with the name of the component
panteliselef 748f635
Merge branch 'main' into elef/com-961-dx-guide-introduce-prebuilt-com…
panteliselef 8f29cb4
replace drawer object with the name of the component
panteliselef File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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/clerk-react': minor | ||
--- | ||
|
||
Expose `<CheckoutButton/>`, `<SubscriptionDetailsButton/>`, `<PlanDetailsButton/>` from `@clerk/clerk-react/experimental`. |
This file contains hidden or 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/nextjs': minor | ||
--- | ||
|
||
Expose `<CheckoutButton/>`, `<SubscriptionDetailsButton/>`, `<PlanDetailsButton/>` from `@clerk/nextjs/experimental`. |
This file contains hidden or 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 hidden or 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 hidden or 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,8 @@ | ||
'use client'; | ||
|
||
export { CheckoutButton, PlanDetailsButton, SubscriptionDetailsButton } from '@clerk/clerk-react/experimental'; | ||
export type { | ||
__experimental_CheckoutButtonProps as CheckoutButtonProps, | ||
__experimental_SubscriptionDetailsButtonProps as SubscriptionDetailsButtonProps, | ||
__experimental_PlanDetailsButtonProps as PlanDetailsButtonProps, | ||
} from '@clerk/types'; |
This file contains hidden or 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 hidden or 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,98 @@ | ||
import type { __experimental_CheckoutButtonProps } 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'; | ||
|
||
/** | ||
* @experimental A button component that opens the Clerk Checkout drawer when clicked. This component must be rendered | ||
* inside a `<SignedIn />` component to ensure the user is authenticated. | ||
* | ||
* @example | ||
* ```tsx | ||
* import { SignedIn } from '@clerk/clerk-react'; | ||
* import { CheckoutButton } from '@clerk/clerk-react/experimental'; | ||
* | ||
* // Basic usage with default "Checkout" text | ||
* function BasicCheckout() { | ||
* return ( | ||
* <SignedIn> | ||
* <CheckoutButton planId="plan_123" /> | ||
* </SignedIn> | ||
* ); | ||
* } | ||
* | ||
* // Custom button with organization subscription | ||
* function OrganizationCheckout() { | ||
* return ( | ||
* <SignedIn> | ||
* <CheckoutButton | ||
* planId="plan_123" | ||
* planPeriod="month" | ||
* subscriberType="org" | ||
* onSubscriptionComplete={() => console.log('Subscription completed!')} | ||
* > | ||
* <button className="custom-button">Subscribe Now</button> | ||
* </CheckoutButton> | ||
* </SignedIn> | ||
* ); | ||
* } | ||
* ``` | ||
* | ||
* @throws {Error} When rendered outside of a `<SignedIn />` component | ||
* @throws {Error} When `subscriberType="org"` is used without an active organization context | ||
*/ | ||
export const CheckoutButton = withClerk( | ||
({ clerk, children, ...props }: WithClerkProp<React.PropsWithChildren<__experimental_CheckoutButtonProps>>) => { | ||
const { | ||
planId, | ||
planPeriod, | ||
subscriberType, | ||
onSubscriptionComplete, | ||
newSubscriptionRedirectUrl, | ||
checkoutProps, | ||
...rest | ||
} = props; | ||
|
||
const { userId, orgId } = useAuth(); | ||
|
||
if (userId === null) { | ||
throw new Error('Ensure that `<CheckoutButton />` is rendered inside a `<SignedIn />` component.'); | ||
} | ||
brkalow marked this conversation as resolved.
Show resolved
Hide 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({ | ||
planId, | ||
planPeriod, | ||
subscriberType, | ||
onSubscriptionComplete, | ||
newSubscriptionRedirectUrl, | ||
...checkoutProps, | ||
}); | ||
}; | ||
|
||
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 }, | ||
); |
This file contains hidden or 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,66 @@ | ||
import type { __experimental_PlanDetailsButtonProps } from '@clerk/types'; | ||
import React from 'react'; | ||
|
||
import type { WithClerkProp } from '../types'; | ||
import { assertSingleChild, normalizeWithDefaultValue, safeExecute } from '../utils'; | ||
import { withClerk } from './withClerk'; | ||
|
||
/** | ||
* @experimental A button component that opens the Clerk Plan Details drawer when clicked. This component is part of | ||
* Clerk's Billing feature which is available under a public beta. | ||
* | ||
* @example | ||
* ```tsx | ||
* import { SignedIn } from '@clerk/clerk-react'; | ||
* import { PlanDetailsButton } from '@clerk/clerk-react/experimental'; | ||
* | ||
* // Basic usage with default "Plan details" text | ||
* function BasicPlanDetails() { | ||
* return ( | ||
* <PlanDetailsButton planId="plan_123" /> | ||
* ); | ||
* } | ||
* | ||
* // Custom button with custom text | ||
* function CustomPlanDetails() { | ||
* return ( | ||
* <PlanDetailsButton planId="plan_123"> | ||
* <button>View Plan Details</button> | ||
* </PlanDetailsButton> | ||
* ); | ||
* } | ||
* ``` | ||
* | ||
* @see https://clerk.com/docs/billing/overview | ||
*/ | ||
export const PlanDetailsButton = withClerk( | ||
({ clerk, children, ...props }: WithClerkProp<React.PropsWithChildren<__experimental_PlanDetailsButtonProps>>) => { | ||
const { plan, planId, initialPlanPeriod, planDetailsProps, ...rest } = props; | ||
children = normalizeWithDefaultValue(children, 'Plan details'); | ||
const child = assertSingleChild(children)('PlanDetailsButton'); | ||
|
||
const clickHandler = () => { | ||
if (!clerk) { | ||
return; | ||
} | ||
|
||
return clerk.__internal_openPlanDetails({ | ||
plan, | ||
planId, | ||
initialPlanPeriod, | ||
...planDetailsProps, | ||
}); | ||
}; | ||
|
||
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 }, | ||
); |
88 changes: 88 additions & 0 deletions
88
packages/react/src/components/SubscriptionDetailsButton.tsx
This file contains hidden or 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,88 @@ | ||
import type { __experimental_SubscriptionDetailsButtonProps } 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'; | ||
|
||
/** | ||
* @experimental A button component that opens the Clerk Subscription Details drawer when clicked. This component must be rendered | ||
* inside a `<SignedIn />` component to ensure the user is authenticated. | ||
* | ||
* @example | ||
* ```tsx | ||
* import { SignedIn } from '@clerk/clerk-react'; | ||
* import { SubscriptionDetailsButton } from '@clerk/clerk-react/experimental'; | ||
* | ||
* // Basic usage with default "Subscription details" text | ||
* function BasicSubscriptionDetails() { | ||
* return ( | ||
* <SubscriptionDetailsButton /> | ||
* ); | ||
* } | ||
* | ||
* // Custom button with organization subscription | ||
* function OrganizationSubscriptionDetails() { | ||
* return ( | ||
* <SubscriptionDetailsButton | ||
* for="org" | ||
* onSubscriptionCancel={() => console.log('Subscription canceled')} | ||
* > | ||
* <button>View Organization Subscription</button> | ||
* </SubscriptionDetailsButton> | ||
* ); | ||
* } | ||
* ``` | ||
* | ||
* @throws {Error} When rendered outside of a `<SignedIn />` component | ||
* @throws {Error} When `for="org"` is used without an active organization context | ||
* | ||
* @see https://clerk.com/docs/billing/overview | ||
*/ | ||
export const SubscriptionDetailsButton = withClerk( | ||
({ | ||
clerk, | ||
children, | ||
...props | ||
}: WithClerkProp<React.PropsWithChildren<__experimental_SubscriptionDetailsButtonProps>>) => { | ||
const { for: forProp, subscriptionDetailsProps, onSubscriptionCancel, ...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, | ||
onSubscriptionCancel, | ||
...subscriptionDetailsProps, | ||
}); | ||
}; | ||
|
||
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 }, | ||
); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.