|
| 1 | +import { Button, Text } from '@chakra-ui/react'; |
| 2 | +import { LinkButtonProps } from './types'; |
| 3 | +import { ArrowIcon } from '@assets'; |
| 4 | +import { useCursor } from '../../Cursor'; |
| 5 | +import { useRef, useState } from 'react'; |
| 6 | +import { useMousePositions } from '@hooks'; |
| 7 | + |
| 8 | +const LinkButton = ({ |
| 9 | + text, |
| 10 | + href, |
| 11 | + animationOnHover, |
| 12 | + withUnderline, |
| 13 | + withArrow, |
| 14 | + fontSize, |
| 15 | +}: LinkButtonProps) => { |
| 16 | + const ref = useRef<HTMLButtonElement>(null); |
| 17 | + const { toggle, setCursorInsets } = useCursor(); |
| 18 | + const [isHovered, setIsHovered] = useState(false); |
| 19 | + |
| 20 | + const onMouseEnter = () => { |
| 21 | + const { width, height, top, left } = |
| 22 | + ref.current?.getBoundingClientRect() || { |
| 23 | + width: 56, |
| 24 | + height: 56, |
| 25 | + top: 0, |
| 26 | + left: 0, |
| 27 | + }; |
| 28 | + setCursorInsets(undefined); |
| 29 | + setTimeout(() => { |
| 30 | + setCursorInsets({ height, width, top, left }); |
| 31 | + }, 0); |
| 32 | + toggle(); |
| 33 | + setIsHovered(true); |
| 34 | + }; |
| 35 | + |
| 36 | + const onMouseLeave = () => { |
| 37 | + setCursorInsets(undefined); |
| 38 | + setIsHovered(false); |
| 39 | + toggle(); |
| 40 | + }; |
| 41 | + |
| 42 | + const translationProps = isHovered |
| 43 | + ? { |
| 44 | + transform: 'translateY(15px)', |
| 45 | + transition: 'transform 0.2s', |
| 46 | + } |
| 47 | + : { |
| 48 | + transform: 'translateY(-11px)', |
| 49 | + transition: 'transform 0.2s', |
| 50 | + }; |
| 51 | + |
| 52 | + return ( |
| 53 | + <Button |
| 54 | + ref={ref} |
| 55 | + flexDir={'column'} |
| 56 | + as="a" |
| 57 | + href={href} |
| 58 | + bg={'transparent'} |
| 59 | + color={'white'} |
| 60 | + colorScheme="violet" |
| 61 | + fontSize={fontSize} |
| 62 | + borderRadius={0} |
| 63 | + overflow={'clip'} |
| 64 | + px={2} |
| 65 | + maxHeight={'7'} |
| 66 | + _hover={{ color: 'violet' }} |
| 67 | + borderBottom={withUnderline ? '1px solid white' : 'none'} |
| 68 | + rightIcon={withArrow ? <ArrowIcon /> : undefined} |
| 69 | + onMouseEnter={onMouseEnter} |
| 70 | + onMouseLeave={onMouseLeave} |
| 71 | + justifyItems={'end'} |
| 72 | + > |
| 73 | + <Text |
| 74 | + pointerEvents={'none'} |
| 75 | + {...(animationOnHover ? translationProps : {})} |
| 76 | + > |
| 77 | + {text} |
| 78 | + </Text> |
| 79 | + {animationOnHover && ( |
| 80 | + <Text pointerEvents={'none'} {...translationProps}> |
| 81 | + {text} |
| 82 | + </Text> |
| 83 | + )} |
| 84 | + </Button> |
| 85 | + ); |
| 86 | +}; |
| 87 | + |
| 88 | +export default LinkButton; |
0 commit comments