|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { forwardRef, useState } from 'react' |
| 4 | +import { ArrowRight, ChevronRight, Loader2 } from 'lucide-react' |
| 5 | +import { Button, type ButtonProps as EmcnButtonProps } from '@/components/emcn' |
| 6 | +import { cn } from '@/lib/core/utils/cn' |
| 7 | +import { useCtaButtonClass } from '@/hooks/use-cta-button-class' |
| 8 | + |
| 9 | +export interface CTAButtonProps extends Omit<EmcnButtonProps, 'variant' | 'size'> { |
| 10 | + /** Shows loading spinner and disables button */ |
| 11 | + loading?: boolean |
| 12 | + /** Text to show when loading (appends "..." automatically) */ |
| 13 | + loadingText?: string |
| 14 | + /** Show arrow animation on hover (default: true) */ |
| 15 | + showArrow?: boolean |
| 16 | + /** Make button full width (default: true) */ |
| 17 | + fullWidth?: boolean |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * Branded CTA button for auth and status pages. |
| 22 | + * Automatically detects whitelabel customization and applies appropriate styling. |
| 23 | + * |
| 24 | + * @example |
| 25 | + * ```tsx |
| 26 | + * // Primary branded button with arrow |
| 27 | + * <CTAButton onClick={handleSubmit}>Sign In</CTAButton> |
| 28 | + * |
| 29 | + * // Loading state |
| 30 | + * <CTAButton loading loadingText="Signing in">Sign In</CTAButton> |
| 31 | + * |
| 32 | + * // Without arrow animation |
| 33 | + * <CTAButton showArrow={false}>Continue</CTAButton> |
| 34 | + * ``` |
| 35 | + */ |
| 36 | +export const CTAButton = forwardRef<HTMLButtonElement, CTAButtonProps>( |
| 37 | + ( |
| 38 | + { |
| 39 | + children, |
| 40 | + loading = false, |
| 41 | + loadingText, |
| 42 | + showArrow = true, |
| 43 | + fullWidth = true, |
| 44 | + className, |
| 45 | + disabled, |
| 46 | + onMouseEnter, |
| 47 | + onMouseLeave, |
| 48 | + ...props |
| 49 | + }, |
| 50 | + ref |
| 51 | + ) => { |
| 52 | + const buttonClass = useCtaButtonClass() |
| 53 | + const [isHovered, setIsHovered] = useState(false) |
| 54 | + |
| 55 | + const handleMouseEnter = (e: React.MouseEvent<HTMLButtonElement>) => { |
| 56 | + setIsHovered(true) |
| 57 | + onMouseEnter?.(e) |
| 58 | + } |
| 59 | + |
| 60 | + const handleMouseLeave = (e: React.MouseEvent<HTMLButtonElement>) => { |
| 61 | + setIsHovered(false) |
| 62 | + onMouseLeave?.(e) |
| 63 | + } |
| 64 | + |
| 65 | + return ( |
| 66 | + <Button |
| 67 | + ref={ref} |
| 68 | + variant='cta' |
| 69 | + size='cta' |
| 70 | + disabled={disabled || loading} |
| 71 | + onMouseEnter={handleMouseEnter} |
| 72 | + onMouseLeave={handleMouseLeave} |
| 73 | + className={cn(buttonClass, 'group', fullWidth && 'w-full', className)} |
| 74 | + {...props} |
| 75 | + > |
| 76 | + {loading ? ( |
| 77 | + <span className='flex items-center gap-2'> |
| 78 | + <Loader2 className='h-4 w-4 animate-spin' /> |
| 79 | + {loadingText ? `${loadingText}...` : children} |
| 80 | + </span> |
| 81 | + ) : showArrow ? ( |
| 82 | + <span className='flex items-center gap-1'> |
| 83 | + {children} |
| 84 | + <span className='inline-flex transition-transform duration-200 group-hover:translate-x-0.5'> |
| 85 | + {isHovered ? ( |
| 86 | + <ArrowRight className='h-4 w-4' aria-hidden='true' /> |
| 87 | + ) : ( |
| 88 | + <ChevronRight className='h-4 w-4' aria-hidden='true' /> |
| 89 | + )} |
| 90 | + </span> |
| 91 | + </span> |
| 92 | + ) : ( |
| 93 | + children |
| 94 | + )} |
| 95 | + </Button> |
| 96 | + ) |
| 97 | + } |
| 98 | +) |
| 99 | + |
| 100 | +CTAButton.displayName = 'CTAButton' |
0 commit comments