-
Notifications
You must be signed in to change notification settings - Fork 405
refactor(clerk-js): Social buttons mobile layout #7169
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
alexcarpenter
merged 9 commits into
main
from
alexcarpenter/social-buttons-layout-mobile
Nov 12, 2025
+267
−17
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ce629ce
refactor(clerk-js): Social buttons mobile layout
alexcarpenter d5c20d4
add changeset
alexcarpenter 384723d
fix missing text overflow
alexcarpenter 34506c3
stack 2 strategies on mobile
alexcarpenter 20412d5
update label rendering logic
alexcarpenter 0103127
rename
alexcarpenter c4ef279
Merge branch 'main' into alexcarpenter/social-buttons-layout-mobile
alexcarpenter 55ee6dc
rename
alexcarpenter ef90c7e
add unit tests
alexcarpenter 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': minor | ||
| --- | ||
|
|
||
| Update SocialButtons to show "Continue with" prefix for last auth strategy, and improve mobile layout consistency. |
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 |
|---|---|---|
|
|
@@ -28,7 +28,7 @@ import { distributeStrategiesIntoRows } from './utils'; | |
|
|
||
| const SOCIAL_BUTTON_BLOCK_THRESHOLD = 2; | ||
| const SOCIAL_BUTTON_PRE_TEXT_THRESHOLD = 1; | ||
| const MAX_STRATEGIES_PER_ROW = 6; | ||
| const MAX_STRATEGIES_PER_ROW = 5; | ||
|
|
||
| export type SocialButtonsProps = React.PropsWithChildren<{ | ||
| enableOAuthProviders: boolean; | ||
|
|
@@ -101,6 +101,8 @@ export const SocialButtons = React.memo((props: SocialButtonsRootProps) => { | |
| lastAuthenticationStrategy, | ||
| ); | ||
| const strategyRowOneLength = strategyRows.at(lastAuthenticationStrategyPresent ? 1 : 0)?.length ?? 0; | ||
| const remainingStrategiesLength = lastAuthenticationStrategyPresent ? strategies.length - 1 : strategies.length; | ||
| const shouldForceSingleColumnOnMobile = !lastAuthenticationStrategyPresent && strategies.length === 2; | ||
|
|
||
| const preferBlockButtons = | ||
| socialButtonsVariant === 'blockButton' | ||
|
|
@@ -151,34 +153,38 @@ export const SocialButtons = React.memo((props: SocialButtonsRootProps) => { | |
| sx={t => ({ | ||
| justifyContent: 'center', | ||
| [mqu.sm]: { | ||
| gridTemplateColumns: 'repeat(1, 1fr)', | ||
| // Force single-column on mobile when 2 strategies are present (without last auth) to prevent | ||
| // label overflow. When last auth is present, only 1 strategy remains here, so overflow isn't a concern. | ||
| gridTemplateColumns: shouldForceSingleColumnOnMobile ? 'repeat(1, minmax(0, 1fr))' : undefined, | ||
| }, | ||
| gridTemplateColumns: | ||
| strategies.length < 1 | ||
| ? `repeat(1, 1fr)` | ||
| ? `repeat(1, minmax(0, 1fr))` | ||
|
Member
Author
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. |
||
| : `repeat(${row.length}, ${ | ||
| rowIndex === 0 | ||
| ? `1fr` | ||
| ? `minmax(0, 1fr)` | ||
| : // Calculate the width of each button based on the width of the buttons within the first row. | ||
| // t.sizes.$2 is used here to represent the gap defined on the Grid component. | ||
| `calc((100% - (${strategyRowOneLength} - 1) * ${t.sizes.$2}) / ${strategyRowOneLength})` | ||
| `minmax(0, calc((100% - (${strategyRowOneLength} - 1) * ${t.sizes.$2}) / ${strategyRowOneLength}))` | ||
| })`, | ||
| })} | ||
| > | ||
| {row.map(strategy => { | ||
| const label = | ||
| strategies.length === SOCIAL_BUTTON_PRE_TEXT_THRESHOLD | ||
| ? `Continue with ${strategyToDisplayData[strategy].name}` | ||
| : strategyToDisplayData[strategy].name; | ||
| const shouldShowPreText = | ||
| remainingStrategiesLength === SOCIAL_BUTTON_PRE_TEXT_THRESHOLD || | ||
| (strategy === lastAuthenticationStrategy && row.length === 1); | ||
|
|
||
| const localizedText = | ||
| strategies.length === SOCIAL_BUTTON_PRE_TEXT_THRESHOLD | ||
| ? localizationKeys('socialButtonsBlockButton', { | ||
| provider: strategyToDisplayData[strategy].name, | ||
| }) | ||
| : localizationKeys('socialButtonsBlockButtonManyInView', { | ||
| provider: strategyToDisplayData[strategy].name, | ||
| }); | ||
| const label = shouldShowPreText | ||
| ? `Continue with ${strategyToDisplayData[strategy].name}` | ||
| : strategyToDisplayData[strategy].name; | ||
|
|
||
| const localizedText = shouldShowPreText | ||
| ? localizationKeys('socialButtonsBlockButton', { | ||
| provider: strategyToDisplayData[strategy].name, | ||
| }) | ||
| : localizationKeys('socialButtonsBlockButtonManyInView', { | ||
| provider: strategyToDisplayData[strategy].name, | ||
| }); | ||
|
|
||
| const imageOrInitial = strategyToDisplayData[strategy].iconUrl ? ( | ||
| <Image | ||
|
|
||
239 changes: 239 additions & 0 deletions
239
packages/clerk-js/src/ui/elements/__tests__/SocialButtons.test.tsx
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,239 @@ | ||
| // packages/clerk-js/src/ui/elements/__tests__/SocialButtons.test.tsx | ||
| import { render, screen } from '@testing-library/react'; | ||
| import { describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import { bindCreateFixtures } from '@/test/utils'; | ||
| import { CardStateProvider } from '@/ui/elements/contexts'; | ||
|
|
||
| import { SocialButtons } from '../SocialButtons'; | ||
|
|
||
| const { createFixtures } = bindCreateFixtures('SignIn'); | ||
|
|
||
| describe('SocialButtons', () => { | ||
| const mockOAuthCallback = vi.fn(); | ||
| const mockWeb3Callback = vi.fn(); | ||
| const mockAlternativePhoneCodeCallback = vi.fn(); | ||
|
|
||
| const defaultProps = { | ||
| oauthCallback: mockOAuthCallback, | ||
| web3Callback: mockWeb3Callback, | ||
| alternativePhoneCodeCallback: mockAlternativePhoneCodeCallback, | ||
| enableOAuthProviders: true, | ||
| enableWeb3Providers: true, | ||
| enableAlternativePhoneCodeProviders: true, | ||
| }; | ||
|
|
||
| describe('Without last authentication strategy', () => { | ||
| it('should show "Continue with" prefix for single strategy', async () => { | ||
| const { wrapper, fixtures } = await createFixtures(f => { | ||
| f.withSocialProvider({ provider: 'google' }); | ||
| }); | ||
|
|
||
| fixtures.clerk.client.lastAuthenticationStrategy = null; | ||
|
|
||
| render( | ||
| <CardStateProvider> | ||
| <SocialButtons | ||
| {...defaultProps} | ||
| showLastAuthenticationStrategy={false} | ||
| /> | ||
| </CardStateProvider>, | ||
| { wrapper }, | ||
| ); | ||
|
|
||
| const button = screen.getByRole('button', { name: /google/i }); | ||
| expect(button).toHaveTextContent('Continue with Google'); | ||
| }); | ||
|
|
||
| it('should NOT show "Continue with" prefix for either button when two strategies exist', async () => { | ||
| const { wrapper, fixtures } = await createFixtures(f => { | ||
| f.withSocialProvider({ provider: 'google' }); | ||
| f.withSocialProvider({ provider: 'github' }); | ||
| }); | ||
|
|
||
| fixtures.clerk.client.lastAuthenticationStrategy = null; | ||
|
|
||
| render( | ||
| <CardStateProvider> | ||
| <SocialButtons | ||
| {...defaultProps} | ||
| showLastAuthenticationStrategy={false} | ||
| /> | ||
| </CardStateProvider>, | ||
| { wrapper }, | ||
| ); | ||
|
|
||
| const googleButton = screen.getByRole('button', { name: /google/i }); | ||
| const githubButton = screen.getByRole('button', { name: /github/i }); | ||
|
|
||
| expect(googleButton).toHaveTextContent('Google'); | ||
| expect(googleButton).not.toHaveTextContent('Continue with Google'); | ||
| expect(githubButton).toHaveTextContent('GitHub'); | ||
| expect(githubButton).not.toHaveTextContent('Continue with GitHub'); | ||
| }); | ||
|
|
||
| it('should NOT show "Continue with" prefix for any button when 3+ strategies exist', async () => { | ||
| const { wrapper, fixtures } = await createFixtures(f => { | ||
| f.withSocialProvider({ provider: 'google' }); | ||
| f.withSocialProvider({ provider: 'github' }); | ||
| f.withSocialProvider({ provider: 'apple' }); | ||
| }); | ||
|
|
||
| fixtures.clerk.client.lastAuthenticationStrategy = null; | ||
|
|
||
| render( | ||
| <CardStateProvider> | ||
| <SocialButtons | ||
| {...defaultProps} | ||
| showLastAuthenticationStrategy={false} | ||
| /> | ||
| </CardStateProvider>, | ||
| { wrapper }, | ||
| ); | ||
|
|
||
| const buttons = screen.getAllByRole('button'); | ||
| buttons.forEach(button => { | ||
| expect(button).not.toHaveTextContent(/Continue with/i); | ||
| }); | ||
| }); | ||
|
|
||
| it('should return null when no strategies are enabled', async () => { | ||
| const { wrapper } = await createFixtures(); | ||
|
|
||
| const { container } = render( | ||
| <CardStateProvider> | ||
| <SocialButtons | ||
| {...defaultProps} | ||
| enableOAuthProviders={false} | ||
| enableWeb3Providers={false} | ||
| enableAlternativePhoneCodeProviders={false} | ||
| /> | ||
| </CardStateProvider>, | ||
| { wrapper }, | ||
| ); | ||
|
|
||
| expect(container.firstChild).toBeNull(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('With last authentication strategy', () => { | ||
| it('should show "Continue with" prefix for single strategy', async () => { | ||
| const { wrapper, fixtures } = await createFixtures(f => { | ||
| f.withSocialProvider({ provider: 'google' }); | ||
| }); | ||
|
|
||
| fixtures.clerk.client.lastAuthenticationStrategy = 'oauth_google'; | ||
|
|
||
| render( | ||
| <CardStateProvider> | ||
| <SocialButtons | ||
| {...defaultProps} | ||
| showLastAuthenticationStrategy | ||
| /> | ||
| </CardStateProvider>, | ||
| { wrapper }, | ||
| ); | ||
|
|
||
| const button = screen.getByRole('button', { name: /google/i }); | ||
| expect(button).toHaveTextContent('Continue with Google'); | ||
| expect(button).toHaveTextContent('Last used'); | ||
| }); | ||
|
|
||
| it('should show "Continue with" prefix for both buttons when two strategies exist', async () => { | ||
| const { wrapper, fixtures } = await createFixtures(f => { | ||
| f.withSocialProvider({ provider: 'google' }); | ||
| f.withSocialProvider({ provider: 'github' }); | ||
| }); | ||
|
|
||
| fixtures.clerk.client.lastAuthenticationStrategy = 'oauth_google'; | ||
|
|
||
| render( | ||
| <CardStateProvider> | ||
| <SocialButtons | ||
| {...defaultProps} | ||
| showLastAuthenticationStrategy | ||
| /> | ||
| </CardStateProvider>, | ||
| { wrapper }, | ||
| ); | ||
|
|
||
| const googleButton = screen.getByRole('button', { name: /google/i }); | ||
| const githubButton = screen.getByRole('button', { name: /github/i }); | ||
|
|
||
| // Both should show "Continue with" when last auth is present | ||
| expect(googleButton).toHaveTextContent('Continue with Google'); | ||
| expect(githubButton).toHaveTextContent('Continue with GitHub'); | ||
| }); | ||
|
|
||
| it('should show "Last used" badge on last auth strategy button', async () => { | ||
| const { wrapper, fixtures } = await createFixtures(f => { | ||
| f.withSocialProvider({ provider: 'google' }); | ||
| f.withSocialProvider({ provider: 'github' }); | ||
| }); | ||
|
|
||
| fixtures.clerk.client.lastAuthenticationStrategy = 'oauth_google'; | ||
|
|
||
| render( | ||
| <CardStateProvider> | ||
| <SocialButtons | ||
| {...defaultProps} | ||
| showLastAuthenticationStrategy | ||
| /> | ||
| </CardStateProvider>, | ||
| { wrapper }, | ||
| ); | ||
|
|
||
| const googleButton = screen.getByRole('button', { name: /google/i }); | ||
| expect(googleButton).toHaveTextContent('Last used'); | ||
|
|
||
| const badge = screen.getByText('Last used'); | ||
| expect(badge).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('should show "Continue with" prefix when last auth strategy is alone in its row', async () => { | ||
| const { wrapper, fixtures } = await createFixtures(f => { | ||
| f.withSocialProvider({ provider: 'google' }); | ||
| f.withSocialProvider({ provider: 'github' }); | ||
| f.withSocialProvider({ provider: 'apple' }); | ||
| }); | ||
|
|
||
| fixtures.clerk.client.lastAuthenticationStrategy = 'oauth_google'; | ||
|
|
||
| render( | ||
| <CardStateProvider> | ||
| <SocialButtons | ||
| {...defaultProps} | ||
| showLastAuthenticationStrategy | ||
| /> | ||
| </CardStateProvider>, | ||
| { wrapper }, | ||
| ); | ||
|
|
||
| // Google (last auth) should be in its own row and show "Continue with" | ||
| const googleButton = screen.getByRole('button', { name: /google/i }); | ||
| expect(googleButton).toHaveTextContent('Continue with Google'); | ||
| }); | ||
|
|
||
| it('should handle SAML strategies converted to OAuth', async () => { | ||
| const { wrapper, fixtures } = await createFixtures(f => { | ||
| f.withSocialProvider({ provider: 'google' }); | ||
| }); | ||
|
|
||
| // SAML strategy should be converted to OAuth | ||
| fixtures.clerk.client.lastAuthenticationStrategy = 'saml_google' as any; | ||
|
|
||
| render( | ||
| <CardStateProvider> | ||
| <SocialButtons | ||
| {...defaultProps} | ||
| showLastAuthenticationStrategy | ||
| /> | ||
| </CardStateProvider>, | ||
| { wrapper }, | ||
| ); | ||
|
|
||
| const googleButton = screen.getByRole('button', { name: /google/i }); | ||
| expect(googleButton).toHaveTextContent('Continue with Google'); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
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.