File size: 6,023 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
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 (
<div>
<Heading tag="h4" fontStyle="XS" weight="bold" className={heading}>
Found this helpful?
</Heading>
<div className={stack}>
<FeedbackButton
onClick={handleClick('upvote')}
disabled={selected === 'downvote'}
selected={selected === 'upvote'}
pageTitle={pageTitle}
>
<ThumbsUp size={16} weight={isDarkMode ? 'light' : 'regular'} />
</FeedbackButton>
<FeedbackButton
onClick={handleClick('downvote')}
disabled={selected === 'upvote'}
selected={selected === 'downvote'}
variant="downvote"
pageTitle={pageTitle}
>
<ThumbsDown size={16} weight={isDarkMode ? 'light' : 'regular'} />
</FeedbackButton>
</div>
</div>
)
}
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<HTMLFormElement>(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 (
<Popover.Root open={isOpen} onOpenChange={handleOpenChange}>
<Popover.Trigger
className={trigger({
selected,
})}
disabled={disabled}
onClick={handleClick}
>
{children}
</Popover.Trigger>
<Popover.Portal>
<Popover.Content className={popoverContent} sideOffset={10}>
<header className={popoverHeader}>
<Heading tag="h2" fontStyle="XXS">
Thanks for feedback! Was there anything in particular you wanted
to mention?
</Heading>
<Popover.Close className={popoverClose}>
<X />
</Popover.Close>
</header>
<Form ref={formRef} method="post" action="/api/feedback">
<input
className={visuallyHidden}
name="variant"
type="checkbox"
value={variant}
checked
/>
<input
className={visuallyHidden}
name="pageTitle"
type="text"
value={pageTitle}
/>
<Copy className={popoverInputLabel} tag="label">
<span className={visuallyHidden}>Feedback</span>
<input
className={popoverInput}
name="feedback"
placeholder="Type your feedback here"
type="text"
disabled={isLoading}
/>
</Copy>
<div className={popoverFormFooter}>
<GradientButton
className={popoverButton}
aria-disabled={isLoading}
variant="small"
tag="button"
type="submit"
>
Send
</GradientButton>
<GradientButton
className={outbound}
href={
variant === 'upvote'
? `https://twitter.com/intent/tweet?text=${encodeURIComponent(
`I've found this #reactspring doc page helpful! https://react-spring.io/${location.pathname}`
)}`
: 'https://github.com/pmndrs/react-spring/issues/new/choose'
}
variant="small"
>
{variant === 'upvote'
? 'Tweet about react-spring'
: 'Open an issue'}
</GradientButton>
</div>
</Form>
</Popover.Content>
</Popover.Portal>
</Popover.Root>
)
}
|