Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add custom cursor #2588

Merged
merged 1 commit into from
Dec 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 95 additions & 7 deletions src/components/Universe/CursorTooltip/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import { useHoveredNode } from '~/stores/useGraphStore'
import { colors } from '~/utils'
import { HoverCard } from './HoverCard/index'

const CURSOR_SIZE = 100

export const CursorTooltip = () => {
const tooltipRef = useRef<HTMLDivElement | null>(null)
const cursorRef = useRef<HTMLDivElement | null>(null)
const node = useHoveredNode()

useEffect(() => {
Expand All @@ -16,22 +19,27 @@ export const CursorTooltip = () => {
}, [node])

useEffect(() => {
const canvasElement = document.getElementById('universe-canvas')
let animationFrameId: number
const tooltip = tooltipRef.current
const cursor = cursorRef.current

const handleMouseMove = (e: MouseEvent) => {
const tooltip = tooltipRef.current

if (!tooltip) {
if (!tooltip || !cursor) {
return
}

// Hide tooltip if not hovering over canvas
const target = e.target as Element

if (target.tagName !== 'CANVAS') {
tooltip.style.display = 'none'
cursor.style.display = 'none'
} else {
if (canvasElement) {
canvasElement.style.cursor = 'none'
}

return
cursor.style.display = 'flex'
}

tooltip.style.display = 'block' // Ensure tooltip is visible if hovering canvas
Expand All @@ -46,6 +54,7 @@ export const CursorTooltip = () => {

animationFrameId = requestAnimationFrame(() => {
tooltip.style.transform = `translate(${x}px, ${y}px)`
cursor.style.transform = `translate(${e.clientX - CURSOR_SIZE / 2}px, ${e.clientY - CURSOR_SIZE / 2}px)`
})
}

Expand All @@ -57,7 +66,30 @@ export const CursorTooltip = () => {
}
}, [])

return <TooltipContainer ref={tooltipRef}>{node && <HoverCard node={node} />}</TooltipContainer>
return (
<>
<CustomCursorWrapper ref={cursorRef}>
<Flex className="inner-circle">
<Flex align="center" className="inner-circle__text" grow={1} justify="space-between">
{node ? (
<>
<span>Click to</span>
<span>Select</span>
</>
) : (
<>
<span>Scroll to</span>
<span>Explore</span>
</>
)}
</Flex>

<span className="inner-circle__center">+</span>
</Flex>
</CustomCursorWrapper>
<TooltipContainer ref={tooltipRef}>{node && <HoverCard node={node} />}</TooltipContainer>
</>
)
}

const TooltipContainer = styled(Flex)`
Expand All @@ -68,7 +100,7 @@ const TooltipContainer = styled(Flex)`
will-change: transform; /* Optimize for transform changes */
background: ${colors.BG1};
color: white;
padding: 5px;
padding: 0;
border-radius: 3px;
pointer-events: none; /* Tooltip won't block mouse events */
z-index: 1000;
Expand All @@ -78,3 +110,59 @@ const TooltipContainer = styled(Flex)`
display: none; /* Initially hidden */
box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.2); /* Optional shadow for better visibility */
`

const CustomCursorWrapper = styled(Flex)`
pointer-events: none;
left: 0;
top: 0;
border: 1px solid ${colors.white};
border-radius: 50%;
border-color: rgba(255, 255, 255, 0.2);
position: fixed;
background: rgba(0, 0, 0, 0.2);
width: ${CURSOR_SIZE}px;
height: ${CURSOR_SIZE}px;
align-items: center;
justify-content: center;

.inner-circle {
width: ${CURSOR_SIZE - 30}px;
height: ${CURSOR_SIZE - 30}px;
border-radius: 50%;
border: 1px solid ${colors.white};
border-color: rgba(255, 255, 255, 0.2);
justify-content: center;
align-items: center;
color: rgba(255, 255, 255, 1);
font-size: 6px;
text-transform: uppercase;
font-weight: 300;
background: radial-gradient(rgba(0, 0, 0, 0.4), rgba(0, 0, 0, 0.1));

&__text {
animation: fadeOpacity 8s infinite;
padding: 12px;

/* Define keyframes */
@keyframes fadeOpacity {
0%,
100% {
opacity: 0.2;
}
50% {
opacity: 0.8;
}
}
}

&__center {
font-size: 20px;
font-weight: 200;
opacity: 0.5;
position: absolute;
top: 50%;
left: 50%;
transform: translateX(-50%) translateY(-50%);
}
}
`
Loading