tailwind-nextjs-blog/components/social-icons/index.tsx

41 lines
995 B
TypeScript
Raw Normal View History

2023-08-16 23:28:02 +08:00
import { Mail, Github, Facebook, Youtube, Linkedin, Twitter, Mastodon } from './icons'
2022-07-17 21:40:41 +08:00
const components = {
mail: Mail,
github: Github,
facebook: Facebook,
youtube: Youtube,
linkedin: Linkedin,
twitter: Twitter,
2023-08-16 23:28:02 +08:00
mastodon: Mastodon,
}
2022-07-17 21:40:41 +08:00
2023-08-16 23:28:02 +08:00
type SocialIconProps = {
kind: keyof typeof components
href: string | undefined
size?: number
}
2022-07-17 21:40:41 +08:00
2023-08-16 23:28:02 +08:00
const SocialIcon = ({ kind, href, size = 8 }: SocialIconProps) => {
if (!href || (kind === 'mail' && !/^mailto:\w+([.-]?\w+)@\w+([.-]?\w+)(.\w{2,3})+$/.test(href)))
return null
const SocialSvg = components[kind]
2022-07-17 21:40:41 +08:00
return (
<a
className="text-sm text-gray-500 transition hover:text-gray-600"
target="_blank"
rel="noopener noreferrer"
2023-08-16 23:28:02 +08:00
href={href}
>
2022-07-17 21:40:41 +08:00
<span className="sr-only">{kind}</span>
<SocialSvg
2023-08-16 23:28:02 +08:00
className={`fill-current text-gray-700 hover:text-primary-500 dark:text-gray-200 dark:hover:text-primary-400 h-${size} w-${size}`}
2022-07-17 21:40:41 +08:00
/>
</a>
2023-08-16 23:28:02 +08:00
)
}
2022-07-17 21:40:41 +08:00
2023-08-16 23:28:02 +08:00
export default SocialIcon