|
| 1 | +import React, { useMemo } from 'react' |
| 2 | +import { twMerge } from 'tailwind-merge' |
| 3 | + |
| 4 | +import dynamic from 'next/dynamic' |
| 5 | + |
| 6 | +const XButton = dynamic(() => import('./buttons/XButton'), { ssr: false }) |
| 7 | +const FacebookButton = dynamic(() => import('./buttons/FacebookButton'), { ssr: false }) |
| 8 | +const LinkedInButton = dynamic(() => import('./buttons/LinkedinButton'), { ssr: false }) |
| 9 | +const SlackButton = dynamic(() => import('./buttons/SlackButton'), { ssr: false }) |
| 10 | +const WhatsAppButton = dynamic(() => import('./buttons/WhatsappButton'), { ssr: false }) |
| 11 | +const RedditButton = dynamic(() => import('./buttons/RedditButton'), { ssr: false }) |
| 12 | +const PinterestButton = dynamic(() => import('./buttons/PinterestButton'), { ssr: false }) |
| 13 | +const TelegramButton = dynamic(() => import('./buttons/TelegramButton'), { ssr: false }) |
| 14 | +const EmailButton = dynamic(() => import('./buttons/EmailButton'), { ssr: false }) |
| 15 | + |
| 16 | +import { SocialShareProps } from './types' |
| 17 | + |
| 18 | +const buttonComponents: Record<string, React.ComponentType<any>> = { |
| 19 | + X: XButton, |
| 20 | + Facebook: FacebookButton, |
| 21 | + LinkedIn: LinkedInButton, |
| 22 | + Slack: SlackButton, |
| 23 | + WhatsApp: WhatsAppButton, |
| 24 | + Reddit: RedditButton, |
| 25 | + Pinterest: PinterestButton, |
| 26 | + Telegram: TelegramButton, |
| 27 | + Email: EmailButton, |
| 28 | +} |
| 29 | + |
| 30 | +const gapSpacing = { |
| 31 | + none: 'gap-0', |
| 32 | + sm: 'gap-1', |
| 33 | + md: 'gap-2', |
| 34 | + lg: 'gap-3', |
| 35 | + xl: 'gap-4', |
| 36 | +} |
| 37 | + |
| 38 | +const layouts = { |
| 39 | + horizontal: 'flex-row', |
| 40 | + vertical: 'flex-col', |
| 41 | +} |
| 42 | + |
| 43 | +const SocialShare = ({ |
| 44 | + text, |
| 45 | + buttons, |
| 46 | + btnShape = 'rounded', |
| 47 | + size = 'md', |
| 48 | + gap = 'none', |
| 49 | + layout = 'horizontal', |
| 50 | + className = '', |
| 51 | + style, |
| 52 | +}: SocialShareProps) => { |
| 53 | + const gapClasses = useMemo(() => gapSpacing[gap], [gap]) |
| 54 | + const layoutClasses = useMemo(() => layouts[layout], [layout]) |
| 55 | + |
| 56 | + return ( |
| 57 | + <div |
| 58 | + className={twMerge( |
| 59 | + `social-share w-fit flex items-center ${layoutClasses} ${gapClasses}`, |
| 60 | + className |
| 61 | + )} |
| 62 | + style={style} |
| 63 | + > |
| 64 | + {buttons.map((btn) => { |
| 65 | + const BtnComponent = buttonComponents[btn] |
| 66 | + if (!BtnComponent) return null |
| 67 | + |
| 68 | + const needsText = !['Facebook', 'LinkedIn'].includes(btn) |
| 69 | + const props = needsText ? { text } : {} |
| 70 | + |
| 71 | + return ( |
| 72 | + <BtnComponent |
| 73 | + key={btn} |
| 74 | + btnShape={btnShape} |
| 75 | + size={size} |
| 76 | + {...props} |
| 77 | + /> |
| 78 | + ) |
| 79 | + })} |
| 80 | + </div> |
| 81 | + ) |
| 82 | +} |
| 83 | + |
| 84 | +export default SocialShare |
0 commit comments