feat(TypeaheadInput): implement dark mode detection and theme-aware styling
This commit is contained in:
parent
c31b195b5e
commit
99a0d221ba
1 changed files with 82 additions and 15 deletions
|
@ -36,11 +36,47 @@ export function TypeaheadInput({
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
const [isExternalUpdate, setIsExternalUpdate] = useState(false);
|
const [isExternalUpdate, setIsExternalUpdate] = useState(false);
|
||||||
const [dropdownPosition, setDropdownPosition] = useState<DropdownPosition>({ top: 0, left: 0, width: 0 });
|
const [dropdownPosition, setDropdownPosition] = useState<DropdownPosition>({ top: 0, left: 0, width: 0 });
|
||||||
|
const [isDarkMode, setIsDarkMode] = useState(false);
|
||||||
|
|
||||||
const inputRef = useRef<HTMLInputElement>(null);
|
const inputRef = useRef<HTMLInputElement>(null);
|
||||||
const suggestionsRef = useRef<HTMLDivElement>(null);
|
const suggestionsRef = useRef<HTMLDivElement>(null);
|
||||||
const timeoutRef = useRef<NodeJS.Timeout | undefined>(undefined);
|
const timeoutRef = useRef<NodeJS.Timeout | undefined>(undefined);
|
||||||
|
|
||||||
|
// Detect dark mode
|
||||||
|
useEffect(() => {
|
||||||
|
const checkDarkMode = () => {
|
||||||
|
// Check if dark class is on html or body element
|
||||||
|
const isDark = document.documentElement.classList.contains('dark') ||
|
||||||
|
document.body.classList.contains('dark') ||
|
||||||
|
// Fallback: check system preference
|
||||||
|
window.matchMedia('(prefers-color-scheme: dark)').matches;
|
||||||
|
setIsDarkMode(isDark);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Initial check
|
||||||
|
checkDarkMode();
|
||||||
|
|
||||||
|
// Watch for changes to the dark class
|
||||||
|
const observer = new MutationObserver(checkDarkMode);
|
||||||
|
observer.observe(document.documentElement, {
|
||||||
|
attributes: true,
|
||||||
|
attributeFilter: ['class']
|
||||||
|
});
|
||||||
|
observer.observe(document.body, {
|
||||||
|
attributes: true,
|
||||||
|
attributeFilter: ['class']
|
||||||
|
});
|
||||||
|
|
||||||
|
// Watch for system theme changes
|
||||||
|
const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
|
||||||
|
mediaQuery.addEventListener('change', checkDarkMode);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
observer.disconnect();
|
||||||
|
mediaQuery.removeEventListener('change', checkDarkMode);
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
// Calculate dropdown position relative to viewport
|
// Calculate dropdown position relative to viewport
|
||||||
const calculateDropdownPosition = useCallback((): DropdownPosition => {
|
const calculateDropdownPosition = useCallback((): DropdownPosition => {
|
||||||
if (!inputRef.current) {
|
if (!inputRef.current) {
|
||||||
|
@ -297,7 +333,8 @@ export function TypeaheadInput({
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const baseInputClassName = `
|
const baseInputClassName = `
|
||||||
w-full px-3 py-2 border border-gray-300 rounded-lg
|
w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg
|
||||||
|
bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100
|
||||||
focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500
|
focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500
|
||||||
transition-all duration-200 ease-in-out
|
transition-all duration-200 ease-in-out
|
||||||
${className}
|
${className}
|
||||||
|
@ -307,17 +344,24 @@ export function TypeaheadInput({
|
||||||
const DropdownPortal = () => {
|
const DropdownPortal = () => {
|
||||||
if (!showSuggestions || suggestions.length === 0) return null;
|
if (!showSuggestions || suggestions.length === 0) return null;
|
||||||
|
|
||||||
|
// Theme-aware colors
|
||||||
|
const dropdownBgColor = isDarkMode ? 'rgba(31, 41, 55, 1)' : 'rgba(255, 255, 255, 1)'; // gray-800 : white
|
||||||
|
const borderColor = isDarkMode ? 'rgba(75, 85, 99, 1)' : 'rgba(209, 213, 219, 1)'; // gray-600 : gray-300
|
||||||
|
|
||||||
return createPortal(
|
return createPortal(
|
||||||
<div
|
<div
|
||||||
ref={suggestionsRef}
|
ref={suggestionsRef}
|
||||||
className="fixed bg-white border-2 border-gray-300 rounded-xl shadow-2xl max-h-96 overflow-y-auto"
|
className="fixed border-2 rounded-xl shadow-2xl max-h-96 overflow-y-auto"
|
||||||
style={{
|
style={{
|
||||||
top: dropdownPosition.top + 8, // 8px gap below input
|
top: dropdownPosition.top + 8, // 8px gap below input
|
||||||
left: dropdownPosition.left,
|
left: dropdownPosition.left,
|
||||||
width: dropdownPosition.width,
|
width: dropdownPosition.width,
|
||||||
zIndex: 9999, // Higher than table headers (z-index: 10)
|
zIndex: 9999, // Higher than table headers (z-index: 10)
|
||||||
backgroundColor: 'rgba(255, 255, 255, 1)',
|
backgroundColor: dropdownBgColor,
|
||||||
boxShadow: '0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04), 0 0 0 1px rgba(0, 0, 0, 0.05)'
|
borderColor: borderColor,
|
||||||
|
boxShadow: isDarkMode
|
||||||
|
? '0 20px 25px -5px rgba(0, 0, 0, 0.4), 0 10px 10px -5px rgba(0, 0, 0, 0.2), 0 0 0 1px rgba(0, 0, 0, 0.3)'
|
||||||
|
: '0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04), 0 0 0 1px rgba(0, 0, 0, 0.05)'
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<div className="py-2">
|
<div className="py-2">
|
||||||
|
@ -326,6 +370,11 @@ export function TypeaheadInput({
|
||||||
const label = suggestion.label || '';
|
const label = suggestion.label || '';
|
||||||
const description = suggestion.description || '';
|
const description = suggestion.description || '';
|
||||||
|
|
||||||
|
// Theme-aware item colors
|
||||||
|
const itemBgColor = isSelected
|
||||||
|
? (isDarkMode ? 'rgba(30, 58, 138, 0.3)' : 'rgba(239, 246, 255, 1)') // blue-900/30 : blue-50
|
||||||
|
: dropdownBgColor;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
key={suggestion.id}
|
key={suggestion.id}
|
||||||
|
@ -333,21 +382,29 @@ export function TypeaheadInput({
|
||||||
relative px-4 py-4 cursor-pointer transition-all duration-200 ease-in-out
|
relative px-4 py-4 cursor-pointer transition-all duration-200 ease-in-out
|
||||||
border-l-4 mx-2 my-1 rounded-lg
|
border-l-4 mx-2 my-1 rounded-lg
|
||||||
${isSelected
|
${isSelected
|
||||||
? 'bg-blue-50 border-l-blue-500 shadow-md'
|
? 'border-l-blue-500 shadow-md'
|
||||||
: 'border-l-transparent hover:bg-gray-50 hover:border-l-gray-300 hover:shadow-sm'
|
: 'border-l-transparent hover:shadow-sm'
|
||||||
}
|
}
|
||||||
|
${isSelected
|
||||||
|
? ''
|
||||||
|
: (isDarkMode ? 'hover:bg-gray-700' : 'hover:bg-gray-50')
|
||||||
|
}
|
||||||
|
${!isSelected && isDarkMode ? 'hover:border-l-gray-500' : ''}
|
||||||
|
${!isSelected && !isDarkMode ? 'hover:border-l-gray-300' : ''}
|
||||||
`}
|
`}
|
||||||
onClick={() => handleSuggestionClick(suggestion)}
|
onClick={() => handleSuggestionClick(suggestion)}
|
||||||
onMouseEnter={() => setSelectedIndex(index)}
|
onMouseEnter={() => setSelectedIndex(index)}
|
||||||
style={{
|
style={{
|
||||||
backgroundColor: isSelected ? 'rgba(239, 246, 255, 1)' : 'rgba(255, 255, 255, 1)'
|
backgroundColor: itemBgColor
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<div className="flex items-start justify-between">
|
<div className="flex items-start justify-between">
|
||||||
<div className="flex-1 min-w-0 space-y-2">
|
<div className="flex-1 min-w-0 space-y-2">
|
||||||
{/* Main label */}
|
{/* Main label */}
|
||||||
<div className={`font-semibold text-base leading-tight ${
|
<div className={`font-semibold text-base leading-tight ${
|
||||||
isSelected ? 'text-blue-900' : 'text-gray-900'
|
isSelected
|
||||||
|
? (isDarkMode ? 'text-blue-100' : 'text-blue-900')
|
||||||
|
: (isDarkMode ? 'text-gray-100' : 'text-gray-900')
|
||||||
}`}>
|
}`}>
|
||||||
{label}
|
{label}
|
||||||
</div>
|
</div>
|
||||||
|
@ -355,7 +412,9 @@ export function TypeaheadInput({
|
||||||
{/* Description */}
|
{/* Description */}
|
||||||
{description && (
|
{description && (
|
||||||
<div className={`text-sm leading-relaxed ${
|
<div className={`text-sm leading-relaxed ${
|
||||||
isSelected ? 'text-blue-700' : 'text-gray-600'
|
isSelected
|
||||||
|
? (isDarkMode ? 'text-blue-200' : 'text-blue-700')
|
||||||
|
: (isDarkMode ? 'text-gray-300' : 'text-gray-600')
|
||||||
}`}>
|
}`}>
|
||||||
{description}
|
{description}
|
||||||
</div>
|
</div>
|
||||||
|
@ -363,10 +422,16 @@ export function TypeaheadInput({
|
||||||
|
|
||||||
{/* Wikidata ID */}
|
{/* Wikidata ID */}
|
||||||
<div className="flex items-center space-x-2">
|
<div className="flex items-center space-x-2">
|
||||||
<span className={`inline-flex items-center px-2 py-1 rounded-full text-xs font-medium ${
|
<span className={`inline-flex items-center px-2 py-1 rounded-full text-xs font-medium border ${
|
||||||
isSelected
|
isSelected
|
||||||
? 'bg-blue-100 text-blue-800 border border-blue-200'
|
? (isDarkMode
|
||||||
: 'bg-gray-100 text-gray-700 border border-gray-200'
|
? 'bg-blue-800 text-blue-100 border-blue-600'
|
||||||
|
: 'bg-blue-100 text-blue-800 border-blue-200'
|
||||||
|
)
|
||||||
|
: (isDarkMode
|
||||||
|
? 'bg-gray-700 text-gray-300 border-gray-600'
|
||||||
|
: 'bg-gray-100 text-gray-700 border-gray-200'
|
||||||
|
)
|
||||||
}`}>
|
}`}>
|
||||||
<svg className="w-3 h-3 mr-1" fill="currentColor" viewBox="0 0 20 20">
|
<svg className="w-3 h-3 mr-1" fill="currentColor" viewBox="0 0 20 20">
|
||||||
<path fillRule="evenodd" d="M4 4a2 2 0 00-2 2v8a2 2 0 002 2h12a2 2 0 002-2V6a2 2 0 00-2-2H4zm2 6a2 2 0 104 0 2 2 0 00-4 0zm8-2a2 2 0 11-4 0 2 2 0 014 0z" clipRule="evenodd" />
|
<path fillRule="evenodd" d="M4 4a2 2 0 00-2 2v8a2 2 0 002 2h12a2 2 0 002-2V6a2 2 0 00-2-2H4zm2 6a2 2 0 104 0 2 2 0 00-4 0zm8-2a2 2 0 11-4 0 2 2 0 014 0z" clipRule="evenodd" />
|
||||||
|
@ -380,8 +445,8 @@ export function TypeaheadInput({
|
||||||
<div className="ml-4 flex-shrink-0">
|
<div className="ml-4 flex-shrink-0">
|
||||||
<div className={`w-6 h-6 rounded-full flex items-center justify-center transition-all duration-200 ${
|
<div className={`w-6 h-6 rounded-full flex items-center justify-center transition-all duration-200 ${
|
||||||
isSelected
|
isSelected
|
||||||
? 'bg-blue-500 text-white shadow-lg'
|
? (isDarkMode ? 'bg-blue-600 text-white shadow-lg' : 'bg-blue-500 text-white shadow-lg')
|
||||||
: 'bg-gray-200 text-gray-400'
|
: (isDarkMode ? 'bg-gray-600 text-gray-300' : 'bg-gray-200 text-gray-400')
|
||||||
}`}>
|
}`}>
|
||||||
<svg className="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
<svg className="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||||
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 5l7 7-7 7" />
|
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 5l7 7-7 7" />
|
||||||
|
@ -392,7 +457,9 @@ export function TypeaheadInput({
|
||||||
|
|
||||||
{/* Subtle bottom border for separation */}
|
{/* Subtle bottom border for separation */}
|
||||||
{index < suggestions.length - 1 && (
|
{index < suggestions.length - 1 && (
|
||||||
<div className="absolute bottom-0 left-4 right-4 h-px bg-gray-200"></div>
|
<div className={`absolute bottom-0 left-4 right-4 h-px ${
|
||||||
|
isDarkMode ? 'bg-gray-600' : 'bg-gray-200'
|
||||||
|
}`}></div>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|
Loading…
Add table
Reference in a new issue