Skip to content

Commit

Permalink
fix: q on hover
Browse files Browse the repository at this point in the history
  • Loading branch information
prokopsimek committed Jan 17, 2025
1 parent 9291a9f commit f9b8282
Showing 1 changed file with 5 additions and 39 deletions.
44 changes: 5 additions & 39 deletions src/app/components/TopicsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,10 @@

import Link from 'next/link';
import { topics } from '../constants/topics';
import { useState, useEffect, useRef } from 'react';
import { useState } from 'react';

export default function TopicsList() {
const [hoveredTopic, setHoveredTopic] = useState<string | null>(null);
const tooltipRefs = useRef<{ [key: string]: HTMLDivElement | null }>({});
const buttonRefs = useRef<{ [key: string]: HTMLButtonElement | null }>({});

const setTooltipRef = (id: string) => (el: HTMLDivElement | null) => {
tooltipRefs.current[id] = el;
};

const setButtonRef = (id: string) => (el: HTMLButtonElement | null) => {
buttonRefs.current[id] = el;
};

useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {
if (hoveredTopic) {
const tooltip = tooltipRefs.current[hoveredTopic];
const button = buttonRefs.current[hoveredTopic];
if (tooltip && button &&
!tooltip.contains(event.target as Node) &&
!button.contains(event.target as Node)) {
setHoveredTopic(null);
}
}
};

document.addEventListener('mousedown', handleClickOutside);
return () => {
document.removeEventListener('mousedown', handleClickOutside);
};
}, [hoveredTopic]);

return (
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-6 sm:gap-8">
Expand All @@ -48,22 +19,17 @@ export default function TopicsList() {
<h2 className="text-2xl font-staatliches text-white mb-2 pr-10">
{topic.title}
</h2>
<button
ref={setButtonRef(id)}
type="button"
<div
className="absolute top-6 sm:top-8 right-6 sm:right-8 w-8 h-8 rounded-full bg-white/20 hover:bg-white/30 flex items-center justify-center transition-colors border border-white/40"
onClick={(e) => {
e.stopPropagation();
setHoveredTopic(hoveredTopic === id ? null : id);
}}
onMouseEnter={() => setHoveredTopic(id)}
onMouseLeave={() => setHoveredTopic(null)}
>
<span className="text-white text-sm font-medium">?</span>
</button>
</div>
</div>
</Link>

<div
ref={setTooltipRef(id)}
className={`absolute z-10 w-80 p-5 bg-white rounded-xl shadow-xl top-full mt-2 right-0 text-left border-2 border-purple transition-all duration-200 origin-top-right ${
hoveredTopic === id
? 'opacity-100 scale-100 translate-y-0'
Expand Down

0 comments on commit f9b8282

Please sign in to comment.