Skip to content
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

fix(clerk-js): Fix captcha layout shift on invisible and custom flows #5049

Merged
merged 5 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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/fast-kiwis-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/clerk-js': patch
---

Fix layout shift on transfer flow when captcha is enabled
2 changes: 1 addition & 1 deletion packages/clerk-js/src/ui/components/SignUp/SignUpForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export const SignUpForm = (props: SignUpFormProps) => {
</Col>
)}
<Col center>
<CaptchaElement maxHeight='0' />
<CaptchaElement />
<Col
gap={6}
sx={{
Expand Down
2 changes: 1 addition & 1 deletion packages/clerk-js/src/ui/components/SignUp/SignUpStart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ function _SignUpStart(): JSX.Element {
/>
</Form.ControlRow>
)}
{!shouldShowForm && <CaptchaElement maxHeight='0' />}
{!shouldShowForm && <CaptchaElement />}
</Flex>
</Card.Content>

Expand Down
52 changes: 43 additions & 9 deletions packages/clerk-js/src/ui/elements/CaptchaElement.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,47 @@
import { useEffect, useRef } from 'react';

import { CAPTCHA_ELEMENT_ID } from '../../utils/captcha';
import { Box } from '../customizables';

type CaptchaElementProps = {
maxHeight?: string;
};
export const CaptchaElement = () => {
const elementRef = useRef(null);
const maxHeightValueRef = useRef('0');
const minHeightValueRef = useRef('unset');
const marginBottomValueRef = useRef('unset');

useEffect(() => {
if (!elementRef.current) return;

const observer = new MutationObserver(mutations => {
mutations.forEach(mutation => {
const target = mutation.target as HTMLDivElement;
if (mutation.type === 'attributes' && mutation.attributeName === 'style' && elementRef.current) {
maxHeightValueRef.current = target.style.maxHeight || '0';
minHeightValueRef.current = target.style.minHeight || 'unset';
marginBottomValueRef.current = target.style.marginBottom || 'unset';
}
});
});

export const CaptchaElement = ({ maxHeight }: CaptchaElementProps) => (
<Box
id={CAPTCHA_ELEMENT_ID}
sx={{ display: 'block', alignSelf: 'center', maxHeight }}
/>
);
observer.observe(elementRef.current, {
attributes: true,
attributeFilter: ['style'],
});

return () => observer.disconnect();
}, []);

return (
<Box
ref={elementRef}
Copy link
Member

Choose a reason for hiding this comment

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

you can use a useCallback instead of useEffect + elementRef, but i think this comes down to personal preference, so feel free to ignore.

id={CAPTCHA_ELEMENT_ID}
style={{
display: 'block',
alignSelf: 'center',
maxHeight: maxHeightValueRef.current,
minHeight: minHeightValueRef.current,
marginBottom: marginBottomValueRef.current,
}}
/>
);
};
2 changes: 2 additions & 0 deletions packages/clerk-js/src/utils/captcha/turnstile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ export const getTurnstileToken = async (opts: CaptchaOptions) => {
if (visibleDiv) {
captchaWidgetType = 'smart';
widgetContainerQuerySelector = `#${CAPTCHA_ELEMENT_ID}`;
visibleDiv.style.maxHeight = '0';
} else {
console.error(
'Cannot initialize Smart CAPTCHA widget because the `clerk-captcha` DOM element was not found; falling back to Invisible CAPTCHA widget. If you are using a custom flow, visit https://clerk.com/docs/custom-flows/bot-sign-up-protection for instructions',
Expand All @@ -155,6 +156,7 @@ export const getTurnstileToken = async (opts: CaptchaOptions) => {
widgetContainerQuerySelector = `.${CAPTCHA_INVISIBLE_CLASSNAME}`;
const div = document.createElement('div');
div.classList.add(CAPTCHA_INVISIBLE_CLASSNAME);
div.style.maxHeight = '0';
document.body.appendChild(div);
}

Expand Down
Loading