-
Notifications
You must be signed in to change notification settings - Fork 270
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(elements,ui,clerk-js): Legal consent elements support and improv…
…ements (#4427) Co-authored-by: Tom Milewski <[email protected]>
- Loading branch information
Showing
17 changed files
with
250 additions
and
35 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/elements": minor | ||
--- | ||
|
||
Added support for `__experimental_legalAccepted` field |
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,8 @@ | ||
--- | ||
"@clerk/clerk-js": patch | ||
"@clerk/types": patch | ||
--- | ||
|
||
- Changed `__experimental_legalAccepted` checkbox Indicator element descriptor and element id | ||
- Changed `__experimental_legalAccepted` checkbox Label element descriptor and element id | ||
- Added two new element descriptors `formFieldCheckboxInput`, `formFieldCheckboxLabel`. |
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
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
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
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,75 @@ | ||
import * as Common from '@clerk/elements/common'; | ||
import React from 'react'; | ||
|
||
import { useAppearance } from '~/contexts'; | ||
import { useEnvironment } from '~/hooks/use-environment'; | ||
import { useLocalizations } from '~/hooks/use-localizations'; | ||
import * as Field from '~/primitives/field'; | ||
|
||
import { LinkRenderer } from './link-renderer'; | ||
|
||
export function LegalAcceptedField({ | ||
className, | ||
checked = false, | ||
...restProps | ||
}: Omit<React.ComponentProps<typeof Common.Input>, 'type'>) { | ||
const { t } = useLocalizations(); | ||
const { displayConfig } = useEnvironment(); | ||
const { parsedAppearance } = useAppearance(); | ||
const termsUrl = parsedAppearance.options.termsPageUrl || displayConfig.termsUrl; | ||
const privacyPolicyUrl = parsedAppearance.options.privacyPageUrl || displayConfig.privacyPolicyUrl; | ||
|
||
let localizedText: string | undefined; | ||
|
||
if (termsUrl && privacyPolicyUrl) { | ||
localizedText = t('signUp.__experimental_legalConsent.checkbox.label__termsOfServiceAndPrivacyPolicy', { | ||
termsOfServiceLink: termsUrl, | ||
privacyPolicyLink: privacyPolicyUrl, | ||
}); | ||
} else if (termsUrl) { | ||
localizedText = t('signUp.__experimental_legalConsent.checkbox.label__onlyTermsOfService', { | ||
termsOfServiceLink: termsUrl, | ||
}); | ||
} else if (privacyPolicyUrl) { | ||
localizedText = t('signUp.__experimental_legalConsent.checkbox.label__onlyPrivacyPolicy', { | ||
privacyPolicyLink: privacyPolicyUrl, | ||
}); | ||
} | ||
|
||
return ( | ||
<Common.Field | ||
name='__experimental_legalAccepted' | ||
asChild | ||
> | ||
<Field.Root> | ||
<div className='flex justify-center gap-2'> | ||
<Common.Input | ||
type='checkbox' | ||
asChild | ||
checked={checked} | ||
{...restProps} | ||
> | ||
<Field.Checkbox /> | ||
</Common.Input> | ||
|
||
<Common.Label asChild> | ||
<Field.Label> | ||
<span> | ||
<LinkRenderer | ||
text={localizedText || ''} | ||
className='underline underline-offset-2' | ||
/> | ||
</span> | ||
</Field.Label> | ||
</Common.Label> | ||
</div> | ||
|
||
<Common.FieldError asChild> | ||
{({ message }) => { | ||
return <Field.Message intent='error'>{message}</Field.Message>; | ||
}} | ||
</Common.FieldError> | ||
</Field.Root> | ||
</Common.Field> | ||
); | ||
} |
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,44 @@ | ||
import React, { memo, useMemo } from 'react'; | ||
|
||
interface LinkRendererProps extends Omit<React.AnchorHTMLAttributes<HTMLAnchorElement>, 'href' | 'children' | 'class'> { | ||
text: string; | ||
className?: string; | ||
} | ||
|
||
const LINK_REGEX = /\[([^\]]+)\]\(([^)]+)\)/g; // parses [text](url) | ||
|
||
export const LinkRenderer: React.FC<LinkRendererProps> = memo(({ text, ...linkProps }) => { | ||
const memoizedLinkProps = useMemo(() => linkProps, [linkProps]); | ||
|
||
const renderedContent = useMemo(() => { | ||
const parts: (string | JSX.Element)[] = []; | ||
let lastIndex = 0; | ||
|
||
text.replace(LINK_REGEX, (match, linkText, url, offset) => { | ||
if (offset > lastIndex) { | ||
parts.push(text.slice(lastIndex, offset)); | ||
} | ||
parts.push( | ||
<a | ||
{...memoizedLinkProps} | ||
href={url} | ||
target='_blank' | ||
rel='noopener noreferrer' | ||
key={offset} | ||
> | ||
{linkText} | ||
</a>, | ||
); | ||
lastIndex = offset + match.length; | ||
return match; | ||
}); | ||
|
||
if (lastIndex < text.length) { | ||
parts.push(text.slice(lastIndex)); | ||
} | ||
|
||
return parts; | ||
}, [text, memoizedLinkProps]); | ||
|
||
return renderedContent; | ||
}); |
Oops, something went wrong.