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

fix(web): resolve encryption glitch in footer text #267

Merged
merged 8 commits into from
Jun 15, 2024
32 changes: 22 additions & 10 deletions apps/web/src/components/ui/encrypt-text.tsx
kriptonian1 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use client'
import React, { useRef, useState } from 'react'
import React, { useRef, useState, useEffect } from 'react'
import { motion } from 'framer-motion'

function EncryptText({
Expand All @@ -10,12 +10,18 @@ function EncryptText({
// const TARGET_TEXT = 'Join Waitlist'
const CYCLES_PER_LETTER = 2
const SHUFFLE_TIME = 50

const CHARS = '!@#$%^&*():{};|,.<>/?'

const intervalRef = useRef<ReturnType<typeof setInterval> | null>(null)

const [text, setText] = useState(TARGET_TEXT)
const [width, setWidth] = useState<number | null>(null)
const textRef = useRef<HTMLSpanElement>(null)

useEffect(() => {
if (textRef.current) {
setWidth(textRef.current.offsetWidth)
}
}, [TARGET_TEXT])

const scramble = (): void => {
let pos = 0
Expand Down Expand Up @@ -45,20 +51,26 @@ function EncryptText({

const stopScramble = (): void => {
clearInterval(intervalRef.current || undefined)

setText(TARGET_TEXT)
}

return (
<motion.button
className=" group relative w-full overflow-hidden text-white/60 transition-colors hover:text-white"
<motion.div
kriptonian1 marked this conversation as resolved.
Show resolved Hide resolved
className="group relative w-full overflow-hidden text-white/60 transition-colors hover:text-white"
onMouseEnter={scramble}
onMouseLeave={stopScramble}
style={{ width: width ? `${width}px` : 'auto' }}
>
<div className="relative z-10 flex items-center gap-2">
<span className="flex">{text}</span>
</div>
</motion.button>
<span className="invisible absolute" ref={textRef}>
{TARGET_TEXT}
</span>
<motion.button
className="relative z-10 flex items-center gap-2"
role="link"
>
<span className="flex whitespace-nowrap">{text}</span>
</motion.button>
</motion.div>
)
}

Expand Down