Skip to content

Commit

Permalink
feat: <EmailProtection />
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Jan 10, 2025
1 parent 59414b6 commit 6e4dd5d
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/email-protection/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use client';

import { memo, useState } from 'react';
import { useIsomorphicLayoutEffect } from '../use-isomorphic-layout-effect';

interface EmailProtectionProps {
/**
* The mailbox name.
* If the desired E-Mail addresses are "[email protected]", then the mailbox name is "hello".
*
* By passing the mailbox name and domain separately, the scrapper won't be able to find the
* mail address even if they scan JavaScript files.
*/
mailbox: string,
/**
* The domain name.
* If the desired E-Mail addresses are "[email protected]", then the domain name is "example.com".
*
* By passing the mailbox name and domain separately, the scrapper won't be able to find the
* mail address even if they scan JavaScript files.
*/
domain: string
}

/**
* @see https://foxact.skk.moe/email-protection
*
*/
export const EmailProtection = memo(({ mailbox, domain }: Readonly<EmailProtectionProps>): React.ReactNode => {
// eslint-disable-next-line sukka/unicorn/prefer-string-replace-all -- target lib es2018
const [text, setText] = useState(() => Math.random().toString(36).slice(2) + '[at]' + domain.replace(/\./g, '[dot]'));
useIsomorphicLayoutEffect(() => {
// This is only allowed because it won't trigger infinite re-render and double render is intentional
setText(mailbox + '@' + domain);
}, [domain, mailbox]);

return text;
});

if (process.env.NODE_ENV !== 'production') {
EmailProtection.displayName = 'EmailProtection';
}

0 comments on commit 6e4dd5d

Please sign in to comment.