Skip to content

feat(clerk-js,types): Support switching to free plan #5810

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
merged 4 commits into from
May 3, 2025
Merged
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/eager-goats-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@clerk/clerk-js': patch
'@clerk/types': patch
---

Add support for switching to the free plan
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export const CheckoutComplete = ({ checkout }: { checkout: __experimental_Commer
as='h2'
textVariant='h2'
localizationKey={
checkout.subscription?.status === 'active'
checkout.totals.totalDueNow.amount > 0
? localizationKeys('__experimental_commerce.checkout.title__paymentSuccessful')
: localizationKeys('__experimental_commerce.checkout.title__subscriptionSuccessful')
}
Expand All @@ -89,7 +89,7 @@ export const CheckoutComplete = ({ checkout }: { checkout: __experimental_Commer
colorScheme='secondary'
sx={t => ({ textAlign: 'center', paddingInline: t.space.$8, marginBlockStart: t.space.$2 })}
localizationKey={
checkout.subscription?.status === 'active'
checkout.totals.totalDueNow.amount > 0
? localizationKeys('__experimental_commerce.checkout.description__paymentSuccessful')
: localizationKeys('__experimental_commerce.checkout.description__subscriptionSuccessful')
}
Expand All @@ -113,14 +113,14 @@ export const CheckoutComplete = ({ checkout }: { checkout: __experimental_Commer
<LineItems.Group variant='secondary'>
<LineItems.Title
title={
checkout.subscription?.status === 'active'
checkout.totals.totalDueNow.amount > 0
? localizationKeys('__experimental_commerce.checkout.lineItems.title__paymentMethod')
: localizationKeys('__experimental_commerce.checkout.lineItems.title__subscriptionBegins')
}
/>
<LineItems.Description
text={
checkout.subscription?.status === 'active'
checkout.totals.totalDueNow.amount > 0
? checkout.paymentSource
? `${capitalize(checkout.paymentSource.cardType)} ⋯ ${checkout.paymentSource.last4}`
: '–'
Expand Down
171 changes: 87 additions & 84 deletions packages/clerk-js/src/ui/components/Plans/PlanDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ export const PlanDetails = (props: __experimental_PlanDetailsProps) => {
return (
<SubscriberTypeContext.Provider value={props.subscriberType || 'user'}>
<PlansContextProvider>
<_PlanDetails {...props} />
<PlanDetailsInternal {...props} />
</PlansContextProvider>
</SubscriberTypeContext.Provider>
);
};

const _PlanDetails = ({
const PlanDetailsInternal = ({
plan,
onSubscriptionCancel,
portalRoot,
Expand All @@ -51,7 +51,8 @@ const _PlanDetails = ({
const [planPeriod, setPlanPeriod] = useState<__experimental_CommerceSubscriptionPlanPeriod>(_planPeriod);

const { setIsOpen } = useDrawerContext();
const { activeOrUpcomingSubscription, revalidate, buttonPropsForPlan } = usePlansContext();
const { activeOrUpcomingSubscription, revalidate, buttonPropsForPlan, isDefaultPlanImplicitlyActiveOrUpcoming } =
usePlansContext();
const subscriberType = useSubscriberTypeContext();

if (!plan) {
Expand Down Expand Up @@ -202,7 +203,7 @@ const _PlanDetails = ({
</Drawer.Body>
) : null}

{plan.amount > 0 ? (
{!plan.isDefault || !isDefaultPlanImplicitlyActiveOrUpcoming ? (
<Drawer.Footer>
{subscription ? (
subscription.canceledAt ? (
Expand Down Expand Up @@ -315,7 +316,11 @@ interface HeaderProps {
const Header = React.forwardRef<HTMLDivElement, HeaderProps>((props, ref) => {
const { plan, subscription, closeSlot, planPeriod, setPlanPeriod } = props;

const { captionForSubscription } = usePlansContext();
const { captionForSubscription, isDefaultPlanImplicitlyActiveOrUpcoming, subscriptions } = usePlansContext();

const isImplicitlyActiveOrUpcoming = isDefaultPlanImplicitlyActiveOrUpcoming && plan.isDefault;

const showBadge = !!subscription || isImplicitlyActiveOrUpcoming;

return (
<Box
Expand Down Expand Up @@ -352,14 +357,14 @@ const Header = React.forwardRef<HTMLDivElement, HeaderProps>((props, ref) => {
})}
/>
) : null}
{subscription ? (
{showBadge ? (
<Box
elementDescriptor={descriptors.planDetailBadgeContainer}
sx={t => ({
marginBlockEnd: t.space.$3,
})}
>
{subscription.status === 'active' ? (
{subscription?.status === 'active' || (isImplicitlyActiveOrUpcoming && subscriptions.length === 0) ? (
<Badge
elementDescriptor={descriptors.planDetailBadge}
localizationKey={localizationKeys('badge__currentPlan')}
Expand Down Expand Up @@ -398,88 +403,86 @@ const Header = React.forwardRef<HTMLDivElement, HeaderProps>((props, ref) => {
) : null}
</Box>

{plan.amount > 0 ? (
<Flex
elementDescriptor={descriptors.planDetailFeeContainer}
align='center'
wrap='wrap'
sx={t => ({
marginTop: t.space.$3,
columnGap: t.space.$1x5,
})}
>
<>
<Text
elementDescriptor={descriptors.planDetailFee}
variant='h1'
colorScheme='body'
<Flex
elementDescriptor={descriptors.planDetailFeeContainer}
align='center'
wrap='wrap'
sx={t => ({
marginTop: t.space.$3,
columnGap: t.space.$1x5,
})}
>
<>
<Text
elementDescriptor={descriptors.planDetailFee}
variant='h1'
colorScheme='body'
>
{plan.currencySymbol}
{(subscription && subscription.planPeriod === 'annual') || planPeriod === 'annual'
? plan.annualMonthlyAmountFormatted
: plan.amountFormatted}
</Text>
<Text
elementDescriptor={descriptors.planDetailFeePeriod}
variant='caption'
colorScheme='secondary'
sx={t => ({
textTransform: 'lowercase',
':before': {
content: '"/"',
marginInlineEnd: t.space.$1,
},
})}
localizationKey={localizationKeys('__experimental_commerce.month')}
/>
{plan.annualMonthlyAmount > 0 ? (
<Box
elementDescriptor={descriptors.planDetailFeePeriodNotice}
sx={[
_ => ({
width: '100%',
display: 'grid',
gridTemplateRows:
(subscription && subscription.planPeriod === 'annual') || planPeriod === 'annual' ? '1fr' : '0fr',
}),
]}
// @ts-ignore - Needed until React 19 support
inert={
(subscription && subscription.planPeriod === 'annual') || planPeriod === 'annual' ? 'true' : undefined
}
>
{plan.currencySymbol}
{(subscription && subscription.planPeriod === 'annual') || planPeriod === 'annual'
? plan.annualMonthlyAmountFormatted
: plan.amountFormatted}
</Text>
<Text
elementDescriptor={descriptors.planDetailFeePeriod}
variant='caption'
colorScheme='secondary'
sx={t => ({
textTransform: 'lowercase',
':before': {
content: '"/"',
marginInlineEnd: t.space.$1,
},
})}
localizationKey={localizationKeys('__experimental_commerce.month')}
/>
{plan.annualMonthlyAmount > 0 ? (
<Box
elementDescriptor={descriptors.planDetailFeePeriodNotice}
sx={[
_ => ({
width: '100%',
display: 'grid',
gridTemplateRows:
(subscription && subscription.planPeriod === 'annual') || planPeriod === 'annual' ? '1fr' : '0fr',
}),
]}
// @ts-ignore - Needed until React 19 support
inert={
(subscription && subscription.planPeriod === 'annual') || planPeriod === 'annual' ? 'true' : undefined
}
elementDescriptor={descriptors.planDetailFeePeriodNoticeInner}
sx={{
overflow: 'hidden',
minHeight: 0,
}}
>
<Box
elementDescriptor={descriptors.planDetailFeePeriodNoticeInner}
sx={{
overflow: 'hidden',
minHeight: 0,
}}
<Text
elementDescriptor={descriptors.planDetailFeePeriodNoticeLabel}
variant='caption'
colorScheme='secondary'
sx={t => ({
width: '100%',
display: 'flex',
alignItems: 'center',
columnGap: t.space.$1,
})}
>
<Text
elementDescriptor={descriptors.planDetailFeePeriodNoticeLabel}
variant='caption'
colorScheme='secondary'
sx={t => ({
width: '100%',
display: 'flex',
alignItems: 'center',
columnGap: t.space.$1,
})}
>
<Icon
icon={InformationCircle}
colorScheme='neutral'
size='sm'
aria-hidden
/>{' '}
<Span localizationKey={localizationKeys('__experimental_commerce.billedAnnually')} />
</Text>
</Box>
<Icon
icon={InformationCircle}
colorScheme='neutral'
size='sm'
aria-hidden
/>{' '}
<Span localizationKey={localizationKeys('__experimental_commerce.billedAnnually')} />
</Text>
</Box>
) : null}
</>
</Flex>
) : null}
</Box>
) : null}
</>
</Flex>

{!!subscription && (
<Text
Expand Down
Loading