-
-
Notifications
You must be signed in to change notification settings - Fork 340
Expand file tree
/
Copy pathDocFeedbackFloatingButton.tsx
More file actions
168 lines (157 loc) · 5.3 KB
/
DocFeedbackFloatingButton.tsx
File metadata and controls
168 lines (157 loc) · 5.3 KB
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
import * as React from 'react'
import { twMerge } from 'tailwind-merge'
import { Lightbulb, MessageSquare, Plus } from 'lucide-react'
interface DocFeedbackFloatingButtonProps {
onAddNote: () => void
onAddFeedback: () => void
onMouseEnter?: () => void
onMouseLeave?: () => void
hasNote?: boolean
hasImprovement?: boolean
onShowNote?: () => void
isMenuOpen?: boolean
onMenuOpenChange?: (isOpen: boolean) => void
}
export function DocFeedbackFloatingButton({
onAddNote,
onAddFeedback,
onMouseEnter,
onMouseLeave,
hasNote = false,
hasImprovement = false,
onShowNote,
isMenuOpen: controlledIsMenuOpen,
onMenuOpenChange,
}: DocFeedbackFloatingButtonProps) {
const [internalIsMenuOpen, setInternalIsMenuOpen] = React.useState(false)
const isControlled = controlledIsMenuOpen !== undefined
const isMenuOpen = isControlled ? controlledIsMenuOpen : internalIsMenuOpen
const setIsMenuOpen = (value: boolean) => {
if (isControlled) {
onMenuOpenChange?.(value)
} else {
setInternalIsMenuOpen(value)
}
}
const buttonRef = React.useRef<HTMLDivElement>(null)
// Close menu when clicking outside or pressing Escape
React.useEffect(() => {
if (!isMenuOpen) return
const handleClickOutside = (e: MouseEvent) => {
if (buttonRef.current && !buttonRef.current.contains(e.target as Node)) {
setIsMenuOpen(false)
}
}
const handleEscape = (e: KeyboardEvent) => {
if (e.key === 'Escape') {
setIsMenuOpen(false)
}
}
document.addEventListener('mousedown', handleClickOutside)
document.addEventListener('keydown', handleEscape)
return () => {
document.removeEventListener('mousedown', handleClickOutside)
document.removeEventListener('keydown', handleEscape)
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isMenuOpen])
const handleButtonClick = (e: React.MouseEvent) => {
e.preventDefault()
e.stopPropagation()
if (hasNote && onShowNote) {
// If there's a note, expand it and show menu
onShowNote()
}
setIsMenuOpen(!isMenuOpen)
}
const handleNoteClick = (e: React.MouseEvent) => {
e.preventDefault()
e.stopPropagation()
setIsMenuOpen(false)
onAddNote()
}
const handleFeedbackClick = (e: React.MouseEvent) => {
e.preventDefault()
e.stopPropagation()
setIsMenuOpen(false)
onAddFeedback()
}
return (
// eslint-disable-next-line jsx-a11y/click-events-have-key-events,jsx-a11y/no-static-element-interactions
<div
ref={buttonRef}
className="doc-feedback-floating-btn absolute top-0 right-0 -translate-y-full z-[100]"
onMouseEnter={onMouseEnter}
onMouseLeave={onMouseLeave}
onClick={(e) => e.stopPropagation()}
onMouseDown={(e) => e.stopPropagation()}
>
{/* Button - neutral with shadow */}
<button
onClick={handleButtonClick}
className={twMerge(
'flex items-center justify-center',
'w-6 h-6 rounded',
'bg-white dark:bg-gray-800',
'text-gray-600 dark:text-gray-400',
'border border-gray-200 dark:border-gray-700',
'shadow-md hover:shadow-lg',
'hover:bg-gray-50 dark:hover:bg-gray-700',
'transition-all duration-200',
'focus:outline-none focus:ring-2 focus:ring-offset-1 focus:ring-blue-500',
isMenuOpen && 'shadow-lg bg-gray-50 dark:bg-gray-700',
)}
title="Add feedback"
>
<Plus
className={twMerge(
'text-[10px] transition-transform duration-200',
isMenuOpen && 'rotate-45',
)}
/>
</button>
{/* Context Menu */}
{isMenuOpen && (
<div className="absolute right-0 top-full mt-2 w-48 bg-white dark:bg-gray-800 rounded-lg shadow-xl border border-gray-200 dark:border-gray-700 overflow-hidden">
{!hasNote && (
<>
<button
onClick={handleNoteClick}
className="w-full px-4 py-3 flex items-center gap-3 hover:bg-gray-50 dark:hover:bg-gray-700 transition-colors text-left"
>
<MessageSquare className="text-blue-500" />
<div>
<div className="font-medium text-sm text-gray-900 dark:text-white">
Add Note
</div>
<div className="text-xs text-gray-500 dark:text-gray-400">
Personal annotation
</div>
</div>
</button>
{!hasImprovement && (
<div className="h-px bg-gray-200 dark:bg-gray-700" />
)}
</>
)}
{!hasImprovement && (
<button
onClick={handleFeedbackClick}
className="w-full px-4 py-3 flex items-center gap-3 hover:bg-gray-50 dark:hover:bg-gray-700 transition-colors text-left"
>
<Lightbulb className="text-yellow-500" />
<div>
<div className="font-medium text-sm text-gray-900 dark:text-white">
Suggest Improvement
</div>
<div className="text-xs text-gray-500 dark:text-gray-400">
Help improve docs
</div>
</div>
</button>
)}
</div>
)}
</div>
)
}