-
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.
- Loading branch information
Showing
9 changed files
with
171 additions
and
5 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
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,68 @@ | ||
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(props: Omit<React.ComponentProps<CheckboxField>, '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 gap-2'> | ||
<Common.Input | ||
type='checkbox' | ||
asChild | ||
{...props} | ||
> | ||
<Field.Checkbox /> | ||
</Common.Input> | ||
|
||
<Common.Label asChild> | ||
<Field.Label className='!block'> | ||
<LinkRenderer | ||
text={localizedText || ''} | ||
className='underline underline-offset-2' | ||
/> | ||
</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; | ||
}); |
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