import * as React from 'react' import * as Popover from '@radix-ui/react-popover' import { ThumbsDown, ThumbsUp, X } from 'phosphor-react' import { useLocation, useFetcher } from '@remix-run/react' import { Heading } from '~/components/Text/Heading' import { Copy } from '~/components/Text/Copy' import { GradientButton } from '~/components/Buttons/GradientButton' import { useIsDarkTheme } from '~/hooks/useIsDarkTheme' import { EventNames, firePlausibleEvent } from '~/helpers/analytics' import { heading, outbound, popoverButton, popoverClose, popoverContent, popoverFormFooter, popoverHeader, popoverInput, popoverInputLabel, stack, trigger, } from './Feedback.css' import { visuallyHidden } from '../../styles/utilities.css' interface FeedbackProps { location?: string } export const Feedback = ({ location }: FeedbackProps) => { const [pageTitle, setPageTitle] = React.useState('') const [selected, setSelected] = React.useState<'upvote' | 'downvote' | null>( null ) const isDarkMode = useIsDarkTheme() const handleClick = (type: 'upvote' | 'downvote') => () => { setSelected(type) const name = type === 'upvote' ? EventNames.DocLiked : EventNames.DocDisliked if (location) { firePlausibleEvent({ name, additionalProps: { location, title: pageTitle, }, }) } } React.useEffect(() => { const element = document.querySelector('h1 > a') if (element) { setPageTitle(element.innerHTML) } }, []) React.useEffect(() => { setSelected(null) /** * if the location changes, reset the selected state * otherwise you vote once somewhere and can never do it again */ }, [location]) return (
Found this helpful?
) } interface FeedbackButtonProps { children: React.ReactNode onClick?: () => void disabled?: boolean selected?: boolean variant?: 'upvote' | 'downvote' pageTitle: string } const FeedbackButton = ({ children, onClick, pageTitle, disabled = false, selected = false, variant = 'upvote', }: FeedbackButtonProps) => { const [isOpen, setIsOpen] = React.useState(false) const formRef = React.useRef(null!) const location = useLocation() const { data, state, Form } = useFetcher<{ success: boolean }>() const handleClick = () => { if (onClick) { onClick() } } const handleOpenChange = (isOpen: boolean) => setIsOpen(isOpen) React.useEffect(() => { if (data?.success) { formRef.current.reset() setIsOpen(false) } }, [data]) const isLoading = state === 'submitting' return ( {children}
Thanks for feedback! Was there anything in particular you wanted to mention?
Feedback
Send {variant === 'upvote' ? 'Tweet about react-spring' : 'Open an issue'}
) }