diff --git a/apps/audius-client/packages/web/src/components/follow-button/FollowButton.tsx b/apps/audius-client/packages/web/src/components/follow-button/FollowButton.tsx index e6681d01ee9..f5b8df3241a 100644 --- a/apps/audius-client/packages/web/src/components/follow-button/FollowButton.tsx +++ b/apps/audius-client/packages/web/src/components/follow-button/FollowButton.tsx @@ -1,4 +1,4 @@ -import { useState, useCallback, useEffect } from 'react' +import { useState, useCallback, useEffect, forwardRef, MouseEvent } from 'react' import { Button, @@ -38,97 +38,105 @@ const defaultMessages: FollowMessages = { unfollow: 'Unfollow' } -export const FollowButton: React.FC = ({ - color, - className, - following = false, - onUnfollow, - onFollow, - isDisabled, - messages = defaultMessages, - stopPropagation, - showIcon = true, - size = 'medium', - invertedColor = false, - ...buttonProps -}) => { - const [isHovering, setIsHovering] = useState(false) - const [isHoveringClicked, setIsHoveringClicked] = useState(false) - - const onMouseEnter = useCallback(() => { - setIsHovering(true) - }, [setIsHovering]) - const onMouseLeave = useCallback(() => { - setIsHovering(false) - }, [setIsHovering]) - - const style = { - [styles.noIcon]: !showIcon, - [styles.full]: size === 'full', - [styles.medium]: size === 'medium', - [styles.small]: size === 'small' - } +export const FollowButton = forwardRef( + function FollowButton(props, ref) { + const { + color, + className, + following = false, + onUnfollow, + onFollow, + isDisabled, + messages = defaultMessages, + stopPropagation, + showIcon = true, + size = 'medium', + invertedColor = false, + ...buttonProps + } = props + const [isHovering, setIsHovering] = useState(false) + const [isHoveringClicked, setIsHoveringClicked] = useState(false) - const onClick = useCallback( - (e: React.MouseEvent) => { - if (following) { - onUnfollow?.() - } else { - onFollow?.() - } - setIsHoveringClicked(true) - if (stopPropagation) { - e.stopPropagation() - e.nativeEvent.stopImmediatePropagation() - } - }, - [following, onUnfollow, onFollow, setIsHoveringClicked, stopPropagation] - ) - - useEffect(() => { - if (!isHovering && isHoveringClicked) setIsHoveringClicked(false) - }, [isHovering, isHoveringClicked, setIsHoveringClicked]) - - let buttonType - if (color) { - buttonType = ButtonType.PRIMARY - } else { - buttonType = - !following && !isHovering && !isHoveringClicked && !invertedColor - ? ButtonType.SECONDARY - : ButtonType.PRIMARY_ALT - } + const handleMouseEnter = useCallback(() => { + setIsHovering(true) + }, [setIsHovering]) - let icon - let text - if (!following && !isHoveringClicked) { - icon = - text = messages.follow - } else if (!following && isHoveringClicked) { - icon = - text = messages.following - } else { - icon = - text = messages.unfollow - } + const handleMouseLeave = useCallback(() => { + setIsHovering(false) + }, [setIsHovering]) - if (!showIcon) icon = null - - return ( -