From 8f1430d12b7b6bbc9d60cd707383a346dc1a1d52 Mon Sep 17 00:00:00 2001 From: Gavin McDonald Date: Tue, 15 Apr 2025 16:15:05 -0400 Subject: [PATCH] standalone ToolTip component --- components/Card.tsx | 50 ++------------------------------ components/ToolTip.tsx | 66 ++++++++++++++++++++++++++++++++++++++++++ tools/getCardInfo.ts | 2 +- 3 files changed, 70 insertions(+), 48 deletions(-) create mode 100644 components/ToolTip.tsx diff --git a/components/Card.tsx b/components/Card.tsx index c27900b..cff57c2 100644 --- a/components/Card.tsx +++ b/components/Card.tsx @@ -1,6 +1,7 @@ 'use client'; import { useRef, useState } from 'react'; +import ToolTip from '@/components/ToolTip'; import tarokkaCards from '@/constants/tarokkaCards'; import getCardInfo from '@/tools/getCardInfo'; @@ -18,37 +19,6 @@ type CardProps = { export default function Card({ dm, card, position, flipAction }: CardProps) { const { aria, card: cardName, description, flipped, url } = card; - const [showTooltip, setShowTooltip] = useState(false); - const [tooltipPos, setTooltipPos] = useState({ x: 0, y: 0 }); - const longPressTimeout = useRef(null); - const delayTimeout = useRef(null); - - const handleMouseMove = (e: React.MouseEvent) => { - setTooltipPos({ x: e.clientX, y: e.clientY }); - }; - - const handleMouseEnter = () => { - delayTimeout.current = setTimeout(() => { - setShowTooltip(true); - }, 1000); - }; - - const handleMouseLeave = () => { - clearTimeout(delayTimeout.current!); - setShowTooltip(false); - }; - - const handleTouchStart = () => { - longPressTimeout.current = setTimeout(() => { - setShowTooltip(true); - }, 1000); - }; - - const handleTouchEnd = () => { - clearTimeout(longPressTimeout.current!); - setShowTooltip(false); - }; - const handleClick = () => { if (dm) { flipAction(); @@ -71,15 +41,10 @@ export default function Card({ dm, card, position, flipAction }: CardProps) { }; return ( - <> +
-
- {getTooltip()} -
- +
); } diff --git a/components/ToolTip.tsx b/components/ToolTip.tsx new file mode 100644 index 0000000..97e48a3 --- /dev/null +++ b/components/ToolTip.tsx @@ -0,0 +1,66 @@ +'use client'; +import React, { useRef, useState } from 'react'; + +type TooltipProps = { + children: React.ReactNode; + content: React.ReactNode; + delay?: number; + mobileDelay?: number; +}; + +export default function Tooltip({ + children, + content, + delay = 500, + mobileDelay = 500, +}: TooltipProps) { + const [show, setShow] = useState(false); + const [pos, setPos] = useState({ x: 0, y: 0 }); + const delayTimeout = useRef(null); + const longPressTimeout = useRef(null); + + const handleMouseEnter = () => { + delayTimeout.current = setTimeout(() => setShow(true), delay); + }; + + const handleMouseLeave = () => { + if (delayTimeout.current) clearTimeout(delayTimeout.current); + setShow(false); + }; + + const handleMouseMove = (e: React.MouseEvent) => { + setPos({ x: e.clientX, y: e.clientY }); + }; + + const handleTouchStart = () => { + longPressTimeout.current = setTimeout(() => setShow(true), mobileDelay); + }; + + const handleTouchEnd = () => { + if (longPressTimeout.current) clearTimeout(longPressTimeout.current); + setShow(false); + }; + + return ( + <> +
+ {children} +
+
+ {content} +
+ + ); +} diff --git a/tools/getCardInfo.ts b/tools/getCardInfo.ts index b611567..68e9fd7 100644 --- a/tools/getCardInfo.ts +++ b/tools/getCardInfo.ts @@ -24,7 +24,7 @@ export default function getTooltip(card: TarokkaGameCard, position: Layout, dm: } } - // Low deck Tome, Ravenkind, or Sunsword + // Low deck: Tome, Ravenkind, or Sunsword if (isLowCard(card)) { if (dm) text.push(card.prophecy.dmText); text.push(card.prophecy.playerText);