-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomDropdown.jsx
More file actions
212 lines (199 loc) · 7.53 KB
/
Copy pathCustomDropdown.jsx
File metadata and controls
212 lines (199 loc) · 7.53 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
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
import React, { useState, useRef, useEffect } from 'react';
import ReactDOM from 'react-dom';
import { CaretDown, Check } from '@phosphor-icons/react';
import { motion } from 'framer-motion';
const CustomDropdown = ({
options,
value,
onChange,
icon: Icon,
label,
className = '',
variant = 'default',
fullWidth = false,
}) => {
const [isOpen, setIsOpen] = useState(false);
const dropdownRef = useRef(null);
const menuRef = useRef(null);
const [dropdownMenuPosition, setDropdownMenuPosition] = useState({});
const isBrutalist = variant === 'brutalist';
const isPaper = variant === 'paper';
const isTerracotta = variant === 'terracotta';
useEffect(() => {
const handleClickOutside = (event) => {
const isClickInsideButton =
dropdownRef.current && dropdownRef.current.contains(event.target);
const isClickInsideMenu =
menuRef.current && menuRef.current.contains(event.target);
if (!isClickInsideButton && !isClickInsideMenu) {
setIsOpen(false);
}
};
document.addEventListener('mousedown', handleClickOutside);
return () => {
document.removeEventListener('mousedown', handleClickOutside);
};
}, []);
useEffect(() => {
if (isOpen && dropdownRef.current) {
const rect = dropdownRef.current.getBoundingClientRect();
setDropdownMenuPosition({
top: rect.bottom + window.scrollY + 8,
left: rect.left + window.scrollX,
width: rect.width,
});
}
}, [isOpen]);
const handleSelect = (optionValue) => {
onChange(optionValue);
setIsOpen(false);
};
const selectedOption = options.find((opt) => opt.value === value);
const renderDropdownMenu = () => {
if (!isOpen) return null;
return ReactDOM.createPortal(
<motion.div
ref={menuRef}
initial={{ opacity: 0, y: -10, scale: 0.95 }}
animate={{ opacity: 1, y: 0, scale: 1 }}
exit={{ opacity: 0, y: -10, scale: 0.95 }}
transition={{ duration: 0.1 }}
className={`${
isBrutalist
? 'bg-[#050505] border border-white/10 rounded-sm'
: isPaper
? 'bg-[#e9e4d0] border-[#1a1a1a] rounded-sm'
: isTerracotta
? 'bg-[#F3ECE0] border border-[#1A161320] shadow-[0_20px_40px_-20px_rgba(26,22,19,0.25)]'
: 'bg-gray-800 border border-gray-700 rounded-md shadow-lg'
} z-[1000] origin-top-left max-h-80 overflow-y-auto`}
style={{
position: 'absolute',
top: dropdownMenuPosition.top,
left: dropdownMenuPosition.left,
minWidth: dropdownMenuPosition.width,
width: 'max-content',
}}
>
<div
className={isBrutalist || isPaper || isTerracotta ? 'p-1' : 'py-1'}
>
{options.map((option) => {
const isSelected = value === option.value;
return (
<button
key={option.value}
onClick={() => handleSelect(option.value)}
className={`flex items-center justify-between w-full px-4 py-2 text-left transition-colors ${
isBrutalist
? `text-xs font-mono uppercase tracking-widest ${
isSelected
? 'bg-white/10 text-emerald-400'
: 'text-gray-400 hover:bg-white/5 hover:text-white'
}`
: isPaper
? `text-xs font-mono font-black uppercase tracking-widest ${
isSelected
? 'bg-[#1a1a1a] text-[#e9e4d0]'
: 'text-[#1a1a1a]/60 hover:bg-[#1a1a1a]/5 hover:text-[#1a1a1a]'
}`
: isTerracotta
? `font-fraunces text-[15px] tracking-tight ${
isSelected
? 'bg-[#E8DECE] text-[#9E4A2F] italic'
: 'text-[#1A1613] hover:bg-[#E8DECE]/60 hover:italic'
}`
: `text-sm ${
isSelected
? 'bg-primary-500/10 text-primary-400'
: 'text-gray-300 hover:bg-gray-700 hover:text-white'
}`
}`}
style={
isTerracotta
? {
fontVariationSettings: isSelected
? '"opsz" 18, "SOFT" 80, "WONK" 1, "wght" 440'
: '"opsz" 18, "SOFT" 30, "WONK" 0, "wght" 420',
}
: undefined
}
>
<span>{option.label}</span>
{isSelected && (
<Check
size={isBrutalist || isPaper || isTerracotta ? 12 : 16}
className={
isBrutalist
? 'text-emerald-400'
: isPaper
? 'text-[#e9e4d0]'
: isTerracotta
? 'text-[#C96442]'
: 'text-primary-400'
}
/>
)}
</button>
);
})}
</div>
</motion.div>,
document.body,
);
};
return (
<div
className={`relative ${fullWidth ? 'w-full block' : 'inline-block'} text-left ${className}`}
>
<button
type="button"
ref={dropdownRef}
onClick={() => setIsOpen(!isOpen)}
className={`flex items-center justify-between w-full gap-2 px-4 py-2 transition-all focus:outline-none ${
isBrutalist
? 'bg-transparent border border-gray-800 rounded-sm text-xs font-mono uppercase tracking-widest text-gray-400 hover:border-gray-600 hover:text-white'
: isPaper
? 'bg-transparent border border-[#1a1a1a] rounded-sm text-xs font-mono font-black uppercase tracking-widest text-[#1a1a1a]/60 hover:bg-[#1a1a1a]/5 hover:text-[#1a1a1a]'
: isTerracotta
? 'bg-[#F3ECE0] border border-[#1A161320] text-[#1A1613] font-fraunces italic text-[15px] hover:border-[#1A1613] hover:bg-[#E8DECE]/50'
: 'bg-gray-800 hover:bg-gray-700 border border-gray-700 rounded-md text-sm font-medium text-gray-200 focus:ring-2 focus:ring-offset-2 focus:ring-offset-gray-900 focus:ring-primary-500'
}`}
style={
isTerracotta
? {
fontVariationSettings:
'"opsz" 18, "SOFT" 80, "WONK" 1, "wght" 420',
}
: undefined
}
>
<div className="flex items-center gap-2">
{Icon && (
<Icon
size={isBrutalist || isPaper || isTerracotta ? 16 : 20}
className={
isBrutalist
? 'text-emerald-500'
: isPaper
? 'text-[#1a1a1a]'
: isTerracotta
? 'text-[#C96442]'
: 'text-gray-400'
}
/>
)}
<span>{selectedOption ? selectedOption.label : label}</span>
</div>
<CaretDown
size={isBrutalist || isPaper || isTerracotta ? 12 : 16}
className={`ml-2 transition-transform duration-200 ${isOpen ? 'rotate-180' : ''} ${
isTerracotta ? 'text-[#9E4A2F]' : ''
}`}
/>
</button>
{renderDropdownMenu()}
</div>
);
};
export default CustomDropdown;