Skip to content

chore(shared): Improve layout shift in payment element fallback #6387

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 9 commits into from
Jul 28, 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
7 changes: 7 additions & 0 deletions .changeset/fair-bars-agree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@clerk/shared': patch
---


Improve layout behaviour with `<PaymentElement fallback={} />`.
- Disables Stripe's loader, and promotes the usage of the `fallback` prop.
5 changes: 5 additions & 0 deletions .changeset/vast-hoops-teach.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/clerk-js': patch
---

Improve CLS when PaymentElement mounts in Checkout.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { handleError } from '@/ui/utils/errorHandler';
import { useSubscriberTypeContext, useSubscriberTypeLocalizationRoot } from '../../contexts';
import { descriptors, Flex, localizationKeys, Spinner, useAppearance, useLocalizations } from '../../customizables';
import type { LocalizationKey } from '../../localization';
import { PaymentElementSkeleton } from './PaymentElementSkeleton';

const useStripeAppearance = (node: HTMLElement | null) => {
const theme = useAppearance().parsedInternalTheme;
Expand Down Expand Up @@ -231,7 +232,7 @@ const AddPaymentSourceForm = ({ children }: PropsWithChildren) => {
})}
>
{children}
<PaymentElement />
<PaymentElement fallback={<PaymentElementSkeleton />} />
<Card.Alert>{card.error}</Card.Alert>
<FormButtons
isDisabled={!isFormReady}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import type { PropsWithChildren } from 'react';

import { Box, Flex, Grid } from '@/ui/customizables';

const SkeletonLine = (props: Parameters<typeof Box>[0]) => {
return (
<Box
sx={[
t => ({
height: t.space.$2,
width: '100%',
borderRadius: t.radii.$md,
background: t.colors.$neutralAlpha100,
}),
props.sx,
]}
/>
);
};

const SkeletonInput = () => {
return (
<SkeletonLine
sx={t => ({
height: t.space.$10,
width: '100%',
})}
/>
);
};

const LineGroup = (props: PropsWithChildren) => {
return (
<Flex
direction='col'
gap={2}
>
{props.children}
</Flex>
);
};

const PaymentElementSkeleton = () => {
return (
<Box
Copy link
Member Author

Choose a reason for hiding this comment

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

@alexcarpenter Do you think we need descriptors here ? Theming is already using, and I feel like it's ok to be opinionated about the layout of a skeleton.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I agree, I don't see why you'd modify the layout as they wouldn't change based on your configuration.

aria-label='Loading...'
sx={{
position: 'relative',
minHeight: 0,
flex: 1,
overflowY: 'auto',
}}
>
<Flex
direction='col'
gap={5}
>
<LineGroup>
<SkeletonLine
sx={t => ({
height: t.space.$3,
width: t.sizes.$24,
})}
/>
<SkeletonInput />
</LineGroup>

<Grid
columns={2}
gap={4}
>
<LineGroup>
<SkeletonLine
sx={t => ({
height: t.space.$3,
width: t.sizes.$20,
})}
/>

<SkeletonInput />
</LineGroup>
<LineGroup>
<SkeletonLine
sx={t => ({
height: t.space.$3,
width: t.sizes.$24,
})}
/>

<SkeletonInput />
</LineGroup>
</Grid>

<LineGroup>
<SkeletonLine
sx={t => ({
height: t.space.$3,
width: t.sizes.$16,
})}
/>

<SkeletonInput />
</LineGroup>

<LineGroup>
<SkeletonLine />
<SkeletonLine />
<SkeletonLine sx={{ width: '66.666667%' }} />
</LineGroup>
</Flex>
</Box>
);
};

export { PaymentElementSkeleton };
1 change: 1 addition & 0 deletions packages/shared/src/react/commerce.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ const PaymentElementInternalRoot = (props: PropsWithChildren) => {
key={externalClientSecret}
stripe={stripe}
options={{
loader: 'never',
clientSecret: externalClientSecret,
appearance: {
variables: stripeAppearance,
Expand Down
4 changes: 4 additions & 0 deletions packages/shared/src/react/stripe-react/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,10 @@ const createElementComponent = (type: StripeElementType, isServer: boolean): Fun
{!isReady && fallback}
<div
id={id}
style={{
height: isReady ? 'unset' : '0px',
visibility: isReady ? 'visible' : 'hidden',
}}
className={className}
ref={domNode}
/>
Expand Down
Loading