-
Notifications
You must be signed in to change notification settings - Fork 402
fix(clerk-js): Fix captcha layout shift on invisible and custom flows #5049
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
299b92e
fix(clerk-js): Refactor how we handle the style on the captcha element
anagstef edb4e74
before rendering the challenge initialize max-height to 0 for all
anagstef e79e8ac
add comments
anagstef 703b576
Update .changeset/fast-kiwis-visit.md
anagstef e285189
Merge branch 'main' into stefanos/fraud-337
anagstef File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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} | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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', | ||
anagstef marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| maxHeight: maxHeightValueRef.current, | ||
| minHeight: minHeightValueRef.current, | ||
| marginBottom: marginBottomValueRef.current, | ||
| }} | ||
| /> | ||
| ); | ||
| }; | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.