Skip to content

Commit 730262f

Browse files
fix(clerk-js): Conditionally render avatar (#5348)
1 parent cb6c4bd commit 730262f

File tree

4 files changed

+64
-41
lines changed

4 files changed

+64
-41
lines changed

.changeset/purple-lamps-beg.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@clerk/clerk-js': patch
3+
'@clerk/types': patch
4+
---
5+
6+
Conditionally render the avatar and badge components within PlanCard.

packages/clerk-js/src/ui/components/PricingTable/PlanCard.tsx

Lines changed: 56 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,24 @@ interface PlanCardProps {
3333

3434
export function PlanCard(props: PlanCardProps) {
3535
const { plan, period, setPeriod, onSelect, props: pricingTableProps, isCompact = false } = props;
36+
const {
37+
id,
38+
slug,
39+
name,
40+
description,
41+
avatarUrl,
42+
features,
43+
isActiveForPayer,
44+
hasBaseFee,
45+
currencySymbol,
46+
amountFormatted,
47+
annualMonthlyAmountFormatted,
48+
} = plan;
3649
const { ctaPosition = 'top', collapseFeatures = false } = pricingTableProps;
3750
const [showAllFeatures, setShowAllFeatures] = React.useState(false);
38-
const totalFeatures = plan.features.length;
51+
const totalFeatures = features.length;
3952
const hasFeatures = totalFeatures > 0;
4053
const canToggleFeatures = isCompact && totalFeatures > 3;
41-
const isActivePlan = plan.isActiveForPayer;
4254
const prefersReducedMotion = usePrefersReducedMotion();
4355
const { animations: appearanceAnimations } = useAppearance().parsedLayout;
4456
const planCardFeePeriodNoticeAnimation: ThemableCssProp = t => ({
@@ -54,9 +66,9 @@ export function PlanCard(props: PlanCardProps) {
5466

5567
return (
5668
<Box
57-
key={plan.id}
69+
key={id}
5870
elementDescriptor={[descriptors.planCard, isCompact ? descriptors.planCardCompact : descriptors.planCardDefault]}
59-
elementId={descriptors.planCard.setId(plan.slug)}
71+
elementId={descriptors.planCard.setId(slug)}
6072
sx={t => ({
6173
display: 'flex',
6274
flexDirection: 'column',
@@ -79,44 +91,49 @@ export function PlanCard(props: PlanCardProps) {
7991
padding: isCompact ? t.space.$3 : t.space.$4,
8092
})}
8193
>
82-
<Flex
83-
elementDescriptor={descriptors.planCardAvatarContainer}
84-
align='start'
85-
justify='between'
86-
sx={{ width: '100%' }}
87-
>
88-
<Avatar
89-
boxElementDescriptor={descriptors.planCardAvatar}
90-
size={_ => 40}
91-
title={plan.name}
92-
initials={plan.name[0]}
93-
rounded={false}
94-
imageUrl={plan.avatarUrl}
95-
/>
96-
{isActivePlan ? (
97-
<Badge
98-
localizationKey={localizationKeys('badge__currentPlan')}
99-
colorScheme='secondary'
100-
/>
101-
) : null}
102-
</Flex>
94+
{avatarUrl || isActiveForPayer ? (
95+
<Box
96+
elementDescriptor={descriptors.planCardAvatarBadgeContainer}
97+
sx={t => ({
98+
display: 'flex',
99+
alignItems: 'start',
100+
justifyContent: 'space-between',
101+
gap: t.space.$3,
102+
marginBlockEnd: t.space.$3,
103+
})}
104+
>
105+
{avatarUrl ? (
106+
<Avatar
107+
boxElementDescriptor={descriptors.planCardAvatar}
108+
size={_ => 40}
109+
title={name}
110+
initials={name[0]}
111+
rounded={false}
112+
imageUrl={avatarUrl}
113+
/>
114+
) : null}
115+
{isActiveForPayer ? (
116+
<Badge
117+
localizationKey={localizationKeys('badge__currentPlan')}
118+
colorScheme='secondary'
119+
/>
120+
) : null}
121+
</Box>
122+
) : null}
103123
<Heading
104124
elementDescriptor={descriptors.planCardTitle}
105125
as='h2'
106126
textVariant={isCompact ? 'h3' : 'h2'}
107-
sx={t => ({
108-
marginTop: t.space.$3,
109-
})}
110127
>
111-
{plan.name}
128+
{name}
112129
</Heading>
113-
{!isCompact && plan.description ? (
130+
{!isCompact && description ? (
114131
<Text
115132
elementDescriptor={descriptors.planCardDescription}
116133
variant='subtitle'
117134
colorScheme='secondary'
118135
>
119-
{plan.description}
136+
{description}
120137
</Text>
121138
) : null}
122139
<Flex
@@ -128,15 +145,15 @@ export function PlanCard(props: PlanCardProps) {
128145
columnGap: t.space.$1x5,
129146
})}
130147
>
131-
{plan.hasBaseFee ? (
148+
{hasBaseFee ? (
132149
<>
133150
<Text
134151
elementDescriptor={descriptors.planCardFee}
135152
variant={isCompact ? 'h2' : 'h1'}
136153
colorScheme='body'
137154
>
138-
{plan.currencySymbol}
139-
{period === 'month' ? plan.amountFormatted : plan.annualMonthlyAmountFormatted}
155+
{currencySymbol}
156+
{period === 'month' ? amountFormatted : annualMonthlyAmountFormatted}
140157
</Text>
141158
<Text
142159
elementDescriptor={descriptors.planCardFeePeriod}
@@ -202,7 +219,7 @@ export function PlanCard(props: PlanCardProps) {
202219
/>
203220
)}
204221
</Flex>
205-
{plan.hasBaseFee ? (
222+
{hasBaseFee ? (
206223
<Box
207224
elementDescriptor={descriptors.planCardPeriodToggle}
208225
sx={t => ({
@@ -246,7 +263,7 @@ export function PlanCard(props: PlanCardProps) {
246263
rowGap: isCompact ? t.space.$2 : t.space.$3,
247264
})}
248265
>
249-
{plan.features.slice(0, showAllFeatures ? totalFeatures : 3).map(feature => (
266+
{features.slice(0, showAllFeatures ? totalFeatures : 3).map(feature => (
250267
<Box
251268
elementDescriptor={descriptors.planCardFeaturesListItem}
252269
elementId={descriptors.planCardFeaturesListItem.setId(feature.slug)}
@@ -302,10 +319,10 @@ export function PlanCard(props: PlanCardProps) {
302319
<Button
303320
block
304321
textVariant={isCompact ? 'buttonSmall' : 'buttonLarge'}
305-
variant={isActivePlan ? 'bordered' : 'solid'}
306-
colorScheme={isActivePlan ? 'secondary' : 'primary'}
322+
variant={isCompact || isActiveForPayer ? 'bordered' : 'solid'}
323+
colorScheme={isCompact || isActiveForPayer ? 'secondary' : 'primary'}
307324
localizationKey={
308-
isActivePlan
325+
isActiveForPayer
309326
? localizationKeys('__experimental_commerce.manageMembership')
310327
: localizationKeys('__experimental_commerce.getStarted')
311328
}

packages/clerk-js/src/ui/customizables/elementDescriptors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ export const APPEARANCE_KEYS = containsAllElementsConfigKeys([
219219
'planCardHeader',
220220
'planCardTitle',
221221
'planCardDescription',
222-
'planCardAvatarContainer',
222+
'planCardAvatarBadgeContainer',
223223
'planCardAvatar',
224224
'planCardFeatures',
225225
'planCardFeaturesList',

packages/types/src/appearance.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ export type ElementsConfig = {
345345
planCardDefault: WithOptions;
346346
planCardCompact: WithOptions;
347347
planCardHeader: WithOptions;
348-
planCardAvatarContainer: WithOptions;
348+
planCardAvatarBadgeContainer: WithOptions;
349349
planCardAvatar: WithOptions;
350350
planCardTitle: WithOptions;
351351
planCardDescription: WithOptions;

0 commit comments

Comments
 (0)