-
Notifications
You must be signed in to change notification settings - Fork 318
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(clerk-js): Fix captcha layout shift on invisible and custom flows (…
- Loading branch information
Showing
5 changed files
with
63 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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-js': patch | ||
--- | ||
|
||
Fix captcha layout shift on transfer flow, custom flows and invisible |
This file contains 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 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 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 |
---|---|---|
@@ -1,13 +1,53 @@ | ||
import { useEffect, useRef } from 'react'; | ||
|
||
import { CAPTCHA_ELEMENT_ID } from '../../utils/captcha'; | ||
import { Box } from '../customizables'; | ||
|
||
type CaptchaElementProps = { | ||
maxHeight?: string; | ||
}; | ||
/** | ||
* This component uses a MutationObserver to listen for DOM changes made by our Turnstile logic, | ||
* which operates outside the React lifecycle. It stores the observed state in ref to ensure that | ||
* any external style changes, such as updates to max-height, min-height, or margin-bottom persist across re-renders, | ||
* preventing unwanted layout resets. | ||
*/ | ||
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} | ||
id={CAPTCHA_ELEMENT_ID} | ||
style={{ | ||
display: 'block', | ||
alignSelf: 'center', | ||
maxHeight: maxHeightValueRef.current, | ||
minHeight: minHeightValueRef.current, | ||
marginBottom: marginBottomValueRef.current, | ||
}} | ||
/> | ||
); | ||
}; |
This file contains 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