diff --git a/public/r/DecryptedText-JS-CSS.json b/public/r/DecryptedText-JS-CSS.json index 69b4880d..23cfe85a 100644 --- a/public/r/DecryptedText-JS-CSS.json +++ b/public/r/DecryptedText-JS-CSS.json @@ -8,7 +8,7 @@ { "type": "registry:component", "path": "DecryptedText/DecryptedText.jsx", - "content": "import { useEffect, useState, useRef } from 'react';\nimport { motion } from 'motion/react';\n\nconst styles = {\n wrapper: {\n display: 'inline-block',\n whiteSpace: 'pre-wrap'\n },\n srOnly: {\n position: 'absolute',\n width: '1px',\n height: '1px',\n padding: 0,\n margin: '-1px',\n overflow: 'hidden',\n clip: 'rect(0,0,0,0)',\n border: 0\n }\n};\n\nexport default function DecryptedText({\n text,\n speed = 50,\n maxIterations = 10,\n sequential = false,\n revealDirection = 'start',\n useOriginalCharsOnly = false,\n characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*()_+',\n className = '',\n parentClassName = '',\n encryptedClassName = '',\n animateOn = 'hover',\n ...props\n}) {\n const [displayText, setDisplayText] = useState(text);\n const [isHovering, setIsHovering] = useState(false);\n const [isScrambling, setIsScrambling] = useState(false);\n const [revealedIndices, setRevealedIndices] = useState(new Set());\n const [hasAnimated, setHasAnimated] = useState(false);\n const containerRef = useRef(null);\n\n useEffect(() => {\n let interval;\n let currentIteration = 0;\n\n const getNextIndex = revealedSet => {\n const textLength = text.length;\n switch (revealDirection) {\n case 'start':\n return revealedSet.size;\n case 'end':\n return textLength - 1 - revealedSet.size;\n case 'center': {\n const middle = Math.floor(textLength / 2);\n const offset = Math.floor(revealedSet.size / 2);\n const nextIndex = revealedSet.size % 2 === 0 ? middle + offset : middle - offset - 1;\n\n if (nextIndex >= 0 && nextIndex < textLength && !revealedSet.has(nextIndex)) {\n return nextIndex;\n }\n\n for (let i = 0; i < textLength; i++) {\n if (!revealedSet.has(i)) return i;\n }\n return 0;\n }\n default:\n return revealedSet.size;\n }\n };\n\n const availableChars = useOriginalCharsOnly\n ? Array.from(new Set(text.split(''))).filter(char => char !== ' ')\n : characters.split('');\n\n const shuffleText = (originalText, currentRevealed) => {\n if (useOriginalCharsOnly) {\n const positions = originalText.split('').map((char, i) => ({\n char,\n isSpace: char === ' ',\n index: i,\n isRevealed: currentRevealed.has(i)\n }));\n\n const nonSpaceChars = positions.filter(p => !p.isSpace && !p.isRevealed).map(p => p.char);\n\n for (let i = nonSpaceChars.length - 1; i > 0; i--) {\n const j = Math.floor(Math.random() * (i + 1));\n [nonSpaceChars[i], nonSpaceChars[j]] = [nonSpaceChars[j], nonSpaceChars[i]];\n }\n\n let charIndex = 0;\n return positions\n .map(p => {\n if (p.isSpace) return ' ';\n if (p.isRevealed) return originalText[p.index];\n return nonSpaceChars[charIndex++];\n })\n .join('');\n } else {\n return originalText\n .split('')\n .map((char, i) => {\n if (char === ' ') return ' ';\n if (currentRevealed.has(i)) return originalText[i];\n return availableChars[Math.floor(Math.random() * availableChars.length)];\n })\n .join('');\n }\n };\n\n if (isHovering) {\n setIsScrambling(true);\n interval = setInterval(() => {\n setRevealedIndices(prevRevealed => {\n if (sequential) {\n if (prevRevealed.size < text.length) {\n const nextIndex = getNextIndex(prevRevealed);\n const newRevealed = new Set(prevRevealed);\n newRevealed.add(nextIndex);\n setDisplayText(shuffleText(text, newRevealed));\n return newRevealed;\n } else {\n clearInterval(interval);\n setIsScrambling(false);\n return prevRevealed;\n }\n } else {\n setDisplayText(shuffleText(text, prevRevealed));\n currentIteration++;\n if (currentIteration >= maxIterations) {\n clearInterval(interval);\n setIsScrambling(false);\n setDisplayText(text);\n }\n return prevRevealed;\n }\n });\n }, speed);\n } else {\n setDisplayText(text);\n setRevealedIndices(new Set());\n setIsScrambling(false);\n }\n\n return () => {\n if (interval) clearInterval(interval);\n };\n }, [isHovering, text, speed, maxIterations, sequential, revealDirection, characters, useOriginalCharsOnly]);\n\n useEffect(() => {\n if (animateOn !== 'view' && animateOn !== 'both') return;\n\n const observerCallback = entries => {\n entries.forEach(entry => {\n if (entry.isIntersecting && !hasAnimated) {\n setIsHovering(true);\n setHasAnimated(true);\n }\n });\n };\n\n const observerOptions = {\n root: null,\n rootMargin: '0px',\n threshold: 0.1\n };\n\n const observer = new IntersectionObserver(observerCallback, observerOptions);\n const currentRef = containerRef.current;\n if (currentRef) {\n observer.observe(currentRef);\n }\n\n return () => {\n if (currentRef) {\n observer.unobserve(currentRef);\n }\n };\n }, [animateOn, hasAnimated]);\n\n const hoverProps =\n animateOn === 'hover' || animateOn === 'both'\n ? {\n onMouseEnter: () => setIsHovering(true),\n onMouseLeave: () => setIsHovering(false)\n }\n : {};\n\n return (\n \n {displayText}\n\n \n {displayText.split('').map((char, index) => {\n const isRevealedOrDone = revealedIndices.has(index) || !isScrambling || !isHovering;\n\n return (\n \n {char}\n \n );\n })}\n \n \n );\n}\n" + "content": "import { useEffect, useState, useRef, useMemo, useCallback } from 'react';\nimport { motion } from 'motion/react';\n\nconst styles = {\n wrapper: {\n display: 'inline-block',\n whiteSpace: 'pre-wrap'\n },\n srOnly: {\n position: 'absolute',\n width: '1px',\n height: '1px',\n padding: 0,\n margin: '-1px',\n overflow: 'hidden',\n clip: 'rect(0,0,0,0)',\n border: 0\n }\n};\n\nexport default function DecryptedText({\n text,\n speed = 50,\n maxIterations = 10,\n sequential = false,\n revealDirection = 'start',\n useOriginalCharsOnly = false,\n characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*()_+',\n className = '',\n parentClassName = '',\n encryptedClassName = '',\n animateOn = 'hover',\n clickMode = 'once',\n ...props\n}) {\n const [displayText, setDisplayText] = useState(text);\n const [isAnimating, setIsAnimating] = useState(false);\n const [revealedIndices, setRevealedIndices] = useState(new Set());\n const [hasAnimated, setHasAnimated] = useState(false);\n const [isDecrypted, setIsDecrypted] = useState(animateOn !== 'click');\n const [direction, setDirection] = useState('forward');\n\n const containerRef = useRef(null);\n const orderRef = useRef([]);\n const pointerRef = useRef(0);\n\n const availableChars = useMemo(() => {\n return useOriginalCharsOnly\n ? Array.from(new Set(text.split(''))).filter(char => char !== ' ')\n : characters.split('');\n }, [useOriginalCharsOnly, text, characters]);\n\n const shuffleText = useCallback(\n (originalText, currentRevealed) => {\n return originalText\n .split('')\n .map((char, i) => {\n if (char === ' ') return ' ';\n if (currentRevealed.has(i)) return originalText[i];\n return availableChars[Math.floor(Math.random() * availableChars.length)];\n })\n .join('');\n },\n [availableChars]\n );\n\n const computeOrder = useCallback(\n len => {\n const order = [];\n if (len <= 0) return order;\n if (revealDirection === 'start') {\n for (let i = 0; i < len; i++) order.push(i);\n return order;\n }\n if (revealDirection === 'end') {\n for (let i = len - 1; i >= 0; i--) order.push(i);\n return order;\n }\n // center\n const middle = Math.floor(len / 2);\n let offset = 0;\n while (order.length < len) {\n if (offset % 2 === 0) {\n const idx = middle + offset / 2;\n if (idx >= 0 && idx < len) order.push(idx);\n } else {\n const idx = middle - Math.ceil(offset / 2);\n if (idx >= 0 && idx < len) order.push(idx);\n }\n offset++;\n }\n return order.slice(0, len);\n },\n [revealDirection]\n );\n\n const fillAllIndices = useCallback(() => {\n const s = new Set();\n for (let i = 0; i < text.length; i++) s.add(i);\n return s;\n }, [text]);\n\n const removeRandomIndices = useCallback((set, count) => {\n const arr = Array.from(set);\n for (let i = 0; i < count && arr.length > 0; i++) {\n const idx = Math.floor(Math.random() * arr.length);\n arr.splice(idx, 1);\n }\n return new Set(arr);\n }, []);\n\n const encryptInstantly = useCallback(() => {\n const emptySet = new Set();\n setRevealedIndices(emptySet);\n setDisplayText(shuffleText(text, emptySet));\n setIsDecrypted(false);\n }, [text, shuffleText]);\n\n const triggerDecrypt = useCallback(() => {\n if (sequential) {\n orderRef.current = computeOrder(text.length);\n pointerRef.current = 0;\n setRevealedIndices(new Set());\n } else {\n setRevealedIndices(new Set());\n }\n setDirection('forward');\n setIsAnimating(true);\n }, [sequential, computeOrder, text.length]);\n\n const triggerReverse = useCallback(() => {\n if (sequential) {\n // compute forward order then reverse it: we'll remove indices in that order\n orderRef.current = computeOrder(text.length).slice().reverse();\n pointerRef.current = 0;\n setRevealedIndices(fillAllIndices()); // start fully revealed\n setDisplayText(shuffleText(text, fillAllIndices()));\n } else {\n // non-seq: start from fully revealed as well\n setRevealedIndices(fillAllIndices());\n setDisplayText(shuffleText(text, fillAllIndices()));\n }\n setDirection('reverse');\n setIsAnimating(true);\n }, [sequential, computeOrder, fillAllIndices, shuffleText, text]);\n\n useEffect(() => {\n if (!isAnimating) return;\n\n let interval;\n let currentIteration = 0;\n\n const getNextIndex = revealedSet => {\n const textLength = text.length;\n switch (revealDirection) {\n case 'start':\n return revealedSet.size;\n case 'end':\n return textLength - 1 - revealedSet.size;\n case 'center': {\n const middle = Math.floor(textLength / 2);\n const offset = Math.floor(revealedSet.size / 2);\n const nextIndex = revealedSet.size % 2 === 0 ? middle + offset : middle - offset - 1;\n\n if (nextIndex >= 0 && nextIndex < textLength && !revealedSet.has(nextIndex)) {\n return nextIndex;\n }\n\n for (let i = 0; i < textLength; i++) {\n if (!revealedSet.has(i)) return i;\n }\n return 0;\n }\n default:\n return revealedSet.size;\n }\n };\n\n interval = setInterval(() => {\n setRevealedIndices(prevRevealed => {\n if (sequential) {\n // Forward\n if (direction === 'forward') {\n if (prevRevealed.size < text.length) {\n const nextIndex = getNextIndex(prevRevealed);\n const newRevealed = new Set(prevRevealed);\n newRevealed.add(nextIndex);\n setDisplayText(shuffleText(text, newRevealed));\n return newRevealed;\n } else {\n clearInterval(interval);\n setIsAnimating(false);\n setIsDecrypted(true);\n return prevRevealed;\n }\n }\n // Reverse\n if (direction === 'reverse') {\n if (pointerRef.current < orderRef.current.length) {\n const idxToRemove = orderRef.current[pointerRef.current++];\n const newRevealed = new Set(prevRevealed);\n newRevealed.delete(idxToRemove);\n setDisplayText(shuffleText(text, newRevealed));\n if (newRevealed.size === 0) {\n clearInterval(interval);\n setIsAnimating(false);\n setIsDecrypted(false);\n }\n return newRevealed;\n } else {\n clearInterval(interval);\n setIsAnimating(false);\n setIsDecrypted(false);\n return prevRevealed;\n }\n }\n } else {\n // Non-Sequential\n if (direction === 'forward') {\n setDisplayText(shuffleText(text, prevRevealed));\n currentIteration++;\n if (currentIteration >= maxIterations) {\n clearInterval(interval);\n setIsAnimating(false);\n setDisplayText(text);\n setIsDecrypted(true);\n }\n return prevRevealed;\n }\n\n // Non-Sequential Reverse\n if (direction === 'reverse') {\n let currentSet = prevRevealed;\n if (currentSet.size === 0) {\n currentSet = fillAllIndices();\n }\n const removeCount = Math.max(1, Math.ceil(text.length / Math.max(1, maxIterations)));\n const nextSet = removeRandomIndices(currentSet, removeCount);\n setDisplayText(shuffleText(text, nextSet));\n currentIteration++;\n if (nextSet.size === 0 || currentIteration >= maxIterations) {\n clearInterval(interval);\n setIsAnimating(false);\n setIsDecrypted(false);\n // ensure final scrambled state\n setDisplayText(shuffleText(text, new Set()));\n return new Set();\n }\n return nextSet;\n }\n }\n return prevRevealed;\n });\n }, speed);\n\n return () => clearInterval(interval);\n }, [\n isAnimating,\n text,\n speed,\n maxIterations,\n sequential,\n revealDirection,\n shuffleText,\n direction,\n fillAllIndices,\n removeRandomIndices,\n characters,\n useOriginalCharsOnly\n ]);\n\n /* Click Behaviour */\n const handleClick = () => {\n if (animateOn !== 'click') return;\n\n if (clickMode === 'once') {\n if (isDecrypted) return;\n setDirection('forward');\n triggerDecrypt();\n }\n\n if (clickMode === 'toggle') {\n if (isDecrypted) {\n triggerReverse();\n } else {\n setDirection('forward');\n triggerDecrypt();\n }\n }\n };\n\n /* Hover Behaviour */\n const triggerHoverDecrypt = useCallback(() => {\n if (isAnimating) return;\n\n // Reset animation state cleanly\n setRevealedIndices(new Set());\n setIsDecrypted(false);\n setDisplayText(text);\n\n setDirection('forward');\n setIsAnimating(true);\n }, [isAnimating, text]);\n\n const resetToPlainText = useCallback(() => {\n setIsAnimating(false);\n setRevealedIndices(new Set());\n setDisplayText(text);\n setIsDecrypted(true);\n setDirection('forward');\n }, [text]);\n\n /* View Observer */\n useEffect(() => {\n if (animateOn !== 'view' && animateOn !== 'inViewHover') return;\n\n const observerCallback = entries => {\n entries.forEach(entry => {\n if (entry.isIntersecting && !hasAnimated) {\n triggerDecrypt();\n setHasAnimated(true);\n }\n });\n };\n\n const observerOptions = {\n root: null,\n rootMargin: '0px',\n threshold: 0.1\n };\n\n const observer = new IntersectionObserver(observerCallback, observerOptions);\n const currentRef = containerRef.current;\n if (currentRef) {\n observer.observe(currentRef);\n }\n\n return () => {\n if (currentRef) {\n observer.unobserve(currentRef);\n }\n };\n }, [animateOn, hasAnimated, triggerDecrypt]);\n\n useEffect(() => {\n if (animateOn === 'click') {\n encryptInstantly();\n } else {\n setDisplayText(text);\n setIsDecrypted(true);\n }\n setRevealedIndices(new Set());\n setDirection('forward');\n }, [animateOn, text, encryptInstantly]);\n\n const animateProps =\n animateOn === 'hover' || animateOn === 'inViewHover'\n ? {\n onMouseEnter: triggerHoverDecrypt,\n onMouseLeave: resetToPlainText\n }\n : animateOn === 'click'\n ? {\n onClick: handleClick\n }\n : {};\n\n return (\n \n {displayText}\n\n \n {displayText.split('').map((char, index) => {\n const isRevealedOrDone = revealedIndices.has(index) || (!isAnimating && isDecrypted);\n\n return (\n \n {char}\n \n );\n })}\n \n \n );\n}\n" } ], "registryDependencies": [], diff --git a/public/r/DecryptedText-JS-TW.json b/public/r/DecryptedText-JS-TW.json index 059722bc..e360243e 100644 --- a/public/r/DecryptedText-JS-TW.json +++ b/public/r/DecryptedText-JS-TW.json @@ -8,7 +8,7 @@ { "type": "registry:component", "path": "DecryptedText/DecryptedText.jsx", - "content": "import { useEffect, useState, useRef } from 'react';\nimport { motion } from 'motion/react';\n\nexport default function DecryptedText({\n text,\n speed = 50,\n maxIterations = 10,\n sequential = false,\n revealDirection = 'start',\n useOriginalCharsOnly = false,\n characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*()_+',\n className = '',\n parentClassName = '',\n encryptedClassName = '',\n animateOn = 'hover',\n ...props\n}) {\n const [displayText, setDisplayText] = useState(text);\n const [isHovering, setIsHovering] = useState(false);\n const [isScrambling, setIsScrambling] = useState(false);\n const [revealedIndices, setRevealedIndices] = useState(new Set());\n const [hasAnimated, setHasAnimated] = useState(false);\n const containerRef = useRef(null);\n\n useEffect(() => {\n let interval;\n let currentIteration = 0;\n\n const getNextIndex = revealedSet => {\n const textLength = text.length;\n switch (revealDirection) {\n case 'start':\n return revealedSet.size;\n case 'end':\n return textLength - 1 - revealedSet.size;\n case 'center': {\n const middle = Math.floor(textLength / 2);\n const offset = Math.floor(revealedSet.size / 2);\n const nextIndex = revealedSet.size % 2 === 0 ? middle + offset : middle - offset - 1;\n\n if (nextIndex >= 0 && nextIndex < textLength && !revealedSet.has(nextIndex)) {\n return nextIndex;\n }\n for (let i = 0; i < textLength; i++) {\n if (!revealedSet.has(i)) return i;\n }\n return 0;\n }\n default:\n return revealedSet.size;\n }\n };\n\n const availableChars = useOriginalCharsOnly\n ? Array.from(new Set(text.split(''))).filter(char => char !== ' ')\n : characters.split('');\n\n const shuffleText = (originalText, currentRevealed) => {\n if (useOriginalCharsOnly) {\n const positions = originalText.split('').map((char, i) => ({\n char,\n isSpace: char === ' ',\n index: i,\n isRevealed: currentRevealed.has(i)\n }));\n\n const nonSpaceChars = positions.filter(p => !p.isSpace && !p.isRevealed).map(p => p.char);\n\n for (let i = nonSpaceChars.length - 1; i > 0; i--) {\n const j = Math.floor(Math.random() * (i + 1));\n [nonSpaceChars[i], nonSpaceChars[j]] = [nonSpaceChars[j], nonSpaceChars[i]];\n }\n\n let charIndex = 0;\n return positions\n .map(p => {\n if (p.isSpace) return ' ';\n if (p.isRevealed) return originalText[p.index];\n return nonSpaceChars[charIndex++];\n })\n .join('');\n } else {\n return originalText\n .split('')\n .map((char, i) => {\n if (char === ' ') return ' ';\n if (currentRevealed.has(i)) return originalText[i];\n return availableChars[Math.floor(Math.random() * availableChars.length)];\n })\n .join('');\n }\n };\n\n if (isHovering) {\n setIsScrambling(true);\n interval = setInterval(() => {\n setRevealedIndices(prevRevealed => {\n if (sequential) {\n if (prevRevealed.size < text.length) {\n const nextIndex = getNextIndex(prevRevealed);\n const newRevealed = new Set(prevRevealed);\n newRevealed.add(nextIndex);\n setDisplayText(shuffleText(text, newRevealed));\n return newRevealed;\n } else {\n clearInterval(interval);\n setIsScrambling(false);\n return prevRevealed;\n }\n } else {\n setDisplayText(shuffleText(text, prevRevealed));\n currentIteration++;\n if (currentIteration >= maxIterations) {\n clearInterval(interval);\n setIsScrambling(false);\n setDisplayText(text);\n }\n return prevRevealed;\n }\n });\n }, speed);\n } else {\n setDisplayText(text);\n setRevealedIndices(new Set());\n setIsScrambling(false);\n }\n\n return () => {\n if (interval) clearInterval(interval);\n };\n }, [isHovering, text, speed, maxIterations, sequential, revealDirection, characters, useOriginalCharsOnly]);\n\n useEffect(() => {\n if (animateOn !== 'view' && animateOn !== 'both') return;\n\n const observerCallback = entries => {\n entries.forEach(entry => {\n if (entry.isIntersecting && !hasAnimated) {\n setIsHovering(true);\n setHasAnimated(true);\n }\n });\n };\n\n const observerOptions = {\n root: null,\n rootMargin: '0px',\n threshold: 0.1\n };\n\n const observer = new IntersectionObserver(observerCallback, observerOptions);\n const currentRef = containerRef.current;\n if (currentRef) {\n observer.observe(currentRef);\n }\n\n return () => {\n if (currentRef) observer.unobserve(currentRef);\n };\n }, [animateOn, hasAnimated]);\n\n const hoverProps =\n animateOn === 'hover' || animateOn === 'both'\n ? {\n onMouseEnter: () => setIsHovering(true),\n onMouseLeave: () => setIsHovering(false)\n }\n : {};\n\n return (\n \n {displayText}\n\n \n {displayText.split('').map((char, index) => {\n const isRevealedOrDone = revealedIndices.has(index) || !isScrambling || !isHovering;\n\n return (\n \n {char}\n \n );\n })}\n \n \n );\n}\n" + "content": "import { useEffect, useState, useRef, useMemo, useCallback } from 'react';\nimport { motion } from 'motion/react';\n\nexport default function DecryptedText({\n text,\n speed = 50,\n maxIterations = 10,\n sequential = false,\n revealDirection = 'start',\n useOriginalCharsOnly = false,\n characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*()_+',\n className = '',\n parentClassName = '',\n encryptedClassName = '',\n animateOn = 'hover',\n clickMode = 'once',\n ...props\n}) {\n const [displayText, setDisplayText] = useState(text);\n const [isAnimating, setIsAnimating] = useState(false);\n const [revealedIndices, setRevealedIndices] = useState(new Set());\n const [hasAnimated, setHasAnimated] = useState(false);\n const [isDecrypted, setIsDecrypted] = useState(animateOn !== 'click');\n const [direction, setDirection] = useState('forward');\n\n const containerRef = useRef(null);\n const orderRef = useRef([]);\n const pointerRef = useRef(0);\n\n const availableChars = useMemo(() => {\n return useOriginalCharsOnly\n ? Array.from(new Set(text.split(''))).filter(char => char !== ' ')\n : characters.split('');\n }, [useOriginalCharsOnly, text, characters]);\n\n const shuffleText = useCallback(\n (originalText, currentRevealed) => {\n return originalText\n .split('')\n .map((char, i) => {\n if (char === ' ') return ' ';\n if (currentRevealed.has(i)) return originalText[i];\n return availableChars[Math.floor(Math.random() * availableChars.length)];\n })\n .join('');\n },\n [availableChars]\n );\n\n const computeOrder = useCallback(\n len => {\n const order = [];\n if (len <= 0) return order;\n if (revealDirection === 'start') {\n for (let i = 0; i < len; i++) order.push(i);\n return order;\n }\n if (revealDirection === 'end') {\n for (let i = len - 1; i >= 0; i--) order.push(i);\n return order;\n }\n // center\n const middle = Math.floor(len / 2);\n let offset = 0;\n while (order.length < len) {\n if (offset % 2 === 0) {\n const idx = middle + offset / 2;\n if (idx >= 0 && idx < len) order.push(idx);\n } else {\n const idx = middle - Math.ceil(offset / 2);\n if (idx >= 0 && idx < len) order.push(idx);\n }\n offset++;\n }\n return order.slice(0, len);\n },\n [revealDirection]\n );\n\n const fillAllIndices = useCallback(() => {\n const s = new Set();\n for (let i = 0; i < text.length; i++) s.add(i);\n return s;\n }, [text]);\n\n const removeRandomIndices = useCallback((set, count) => {\n const arr = Array.from(set);\n for (let i = 0; i < count && arr.length > 0; i++) {\n const idx = Math.floor(Math.random() * arr.length);\n arr.splice(idx, 1);\n }\n return new Set(arr);\n }, []);\n\n const encryptInstantly = useCallback(() => {\n const emptySet = new Set();\n setRevealedIndices(emptySet);\n setDisplayText(shuffleText(text, emptySet));\n setIsDecrypted(false);\n }, [text, shuffleText]);\n\n const triggerDecrypt = useCallback(() => {\n if (sequential) {\n orderRef.current = computeOrder(text.length);\n pointerRef.current = 0;\n setRevealedIndices(new Set());\n } else {\n setRevealedIndices(new Set());\n }\n setDirection('forward');\n setIsAnimating(true);\n }, [sequential, computeOrder, text.length]);\n\n const triggerReverse = useCallback(() => {\n if (sequential) {\n // compute forward order then reverse it: we'll remove indices in that order\n orderRef.current = computeOrder(text.length).slice().reverse();\n pointerRef.current = 0;\n setRevealedIndices(fillAllIndices()); // start fully revealed\n setDisplayText(shuffleText(text, fillAllIndices()));\n } else {\n // non-seq: start from fully revealed as well\n setRevealedIndices(fillAllIndices());\n setDisplayText(shuffleText(text, fillAllIndices()));\n }\n setDirection('reverse');\n setIsAnimating(true);\n }, [sequential, computeOrder, fillAllIndices, shuffleText, text]);\n\n useEffect(() => {\n if (!isAnimating) return;\n\n let interval;\n let currentIteration = 0;\n\n const getNextIndex = revealedSet => {\n const textLength = text.length;\n switch (revealDirection) {\n case 'start':\n return revealedSet.size;\n case 'end':\n return textLength - 1 - revealedSet.size;\n case 'center': {\n const middle = Math.floor(textLength / 2);\n const offset = Math.floor(revealedSet.size / 2);\n const nextIndex = revealedSet.size % 2 === 0 ? middle + offset : middle - offset - 1;\n\n if (nextIndex >= 0 && nextIndex < textLength && !revealedSet.has(nextIndex)) {\n return nextIndex;\n }\n for (let i = 0; i < textLength; i++) {\n if (!revealedSet.has(i)) return i;\n }\n return 0;\n }\n default:\n return revealedSet.size;\n }\n };\n\n interval = setInterval(() => {\n setRevealedIndices(prevRevealed => {\n if (sequential) {\n // Forward\n if (direction === 'forward') {\n if (prevRevealed.size < text.length) {\n const nextIndex = getNextIndex(prevRevealed);\n const newRevealed = new Set(prevRevealed);\n newRevealed.add(nextIndex);\n setDisplayText(shuffleText(text, newRevealed));\n return newRevealed;\n } else {\n clearInterval(interval);\n setIsAnimating(false);\n setIsDecrypted(true);\n return prevRevealed;\n }\n }\n\n // Reverse\n if (direction === 'reverse') {\n if (pointerRef.current < orderRef.current.length) {\n const idxToRemove = orderRef.current[pointerRef.current++];\n const newRevealed = new Set(prevRevealed);\n newRevealed.delete(idxToRemove);\n setDisplayText(shuffleText(text, newRevealed));\n if (newRevealed.size === 0) {\n clearInterval(interval);\n setIsAnimating(false);\n setIsDecrypted(false);\n }\n return newRevealed;\n } else {\n clearInterval(interval);\n setIsAnimating(false);\n setIsDecrypted(false);\n return prevRevealed;\n }\n }\n } else {\n // Non-Sequential\n if (direction === 'forward') {\n setDisplayText(shuffleText(text, prevRevealed));\n currentIteration++;\n if (currentIteration >= maxIterations) {\n clearInterval(interval);\n setIsAnimating(false);\n setDisplayText(text);\n setIsDecrypted(true);\n }\n return prevRevealed;\n }\n\n // Non-Sequential Reverse\n if (direction === 'reverse') {\n let currentSet = prevRevealed;\n if (currentSet.size === 0) {\n currentSet = fillAllIndices();\n }\n const removeCount = Math.max(1, Math.ceil(text.length / Math.max(1, maxIterations)));\n const nextSet = removeRandomIndices(currentSet, removeCount);\n setDisplayText(shuffleText(text, nextSet));\n currentIteration++;\n if (nextSet.size === 0 || currentIteration >= maxIterations) {\n clearInterval(interval);\n setIsAnimating(false);\n setIsDecrypted(false);\n // ensure final scrambled state\n setDisplayText(shuffleText(text, new Set()));\n return new Set();\n }\n return nextSet;\n }\n }\n return prevRevealed;\n });\n }, speed);\n\n return () => clearInterval(interval);\n }, [\n isAnimating,\n text,\n speed,\n maxIterations,\n sequential,\n revealDirection,\n shuffleText,\n direction,\n fillAllIndices,\n removeRandomIndices,\n characters,\n useOriginalCharsOnly\n ]);\n\n /* Click Behaviour */\n const handleClick = () => {\n if (animateOn !== 'click') return;\n\n if (clickMode === 'once') {\n if (isDecrypted) return;\n setDirection('forward');\n triggerDecrypt();\n }\n\n if (clickMode === 'toggle') {\n if (isDecrypted) {\n triggerReverse();\n } else {\n setDirection('forward');\n triggerDecrypt();\n }\n }\n };\n\n /* Hover Behaviour */\n const triggerHoverDecrypt = useCallback(() => {\n if (isAnimating) return;\n\n // Reset animation state cleanly\n setRevealedIndices(new Set());\n setIsDecrypted(false);\n setDisplayText(text);\n\n setDirection('forward');\n setIsAnimating(true);\n }, [isAnimating, text]);\n\n const resetToPlainText = useCallback(() => {\n setIsAnimating(false);\n setRevealedIndices(new Set());\n setDisplayText(text);\n setIsDecrypted(true);\n setDirection('forward');\n }, [text]);\n\n useEffect(() => {\n if (animateOn !== 'view' && animateOn !== 'inViewHover') return;\n\n const observerCallback = entries => {\n entries.forEach(entry => {\n if (entry.isIntersecting && !hasAnimated) {\n triggerDecrypt();\n setHasAnimated(true);\n }\n });\n };\n\n const observerOptions = {\n root: null,\n rootMargin: '0px',\n threshold: 0.1\n };\n\n const observer = new IntersectionObserver(observerCallback, observerOptions);\n const currentRef = containerRef.current;\n if (currentRef) {\n observer.observe(currentRef);\n }\n\n return () => {\n if (currentRef) observer.unobserve(currentRef);\n };\n }, [animateOn, hasAnimated, triggerDecrypt]);\n\n useEffect(() => {\n if (animateOn === 'click') {\n encryptInstantly();\n } else {\n setDisplayText(text);\n setIsDecrypted(true);\n }\n setRevealedIndices(new Set());\n setDirection('forward');\n }, [animateOn, text, encryptInstantly]);\n\n const animateProps =\n animateOn === 'hover' || animateOn === 'inViewHover'\n ? {\n onMouseEnter: triggerHoverDecrypt,\n onMouseLeave: resetToPlainText\n }\n : animateOn === 'click'\n ? {\n onClick: handleClick\n }\n : {};\n\n return (\n \n {displayText}\n\n \n {displayText.split('').map((char, index) => {\n const isRevealedOrDone = revealedIndices.has(index) || (!isAnimating && isDecrypted);\n\n return (\n \n {char}\n \n );\n })}\n \n \n );\n}\n" } ], "registryDependencies": [], diff --git a/public/r/DecryptedText-TS-CSS.json b/public/r/DecryptedText-TS-CSS.json index 04c072ce..47f4cd6a 100644 --- a/public/r/DecryptedText-TS-CSS.json +++ b/public/r/DecryptedText-TS-CSS.json @@ -8,7 +8,7 @@ { "type": "registry:component", "path": "DecryptedText/DecryptedText.tsx", - "content": "import { useEffect, useState, useRef } from 'react';\nimport { motion } from 'motion/react';\nimport type { HTMLMotionProps } from 'motion/react';\n\nconst styles = {\n wrapper: {\n display: 'inline-block',\n whiteSpace: 'pre-wrap'\n },\n srOnly: {\n position: 'absolute' as const,\n width: '1px',\n height: '1px',\n padding: 0,\n margin: '-1px',\n overflow: 'hidden',\n clip: 'rect(0,0,0,0)',\n border: 0\n }\n};\n\ninterface DecryptedTextProps extends HTMLMotionProps<'span'> {\n text: string;\n speed?: number;\n maxIterations?: number;\n sequential?: boolean;\n revealDirection?: 'start' | 'end' | 'center';\n useOriginalCharsOnly?: boolean;\n characters?: string;\n className?: string;\n parentClassName?: string;\n encryptedClassName?: string;\n animateOn?: 'view' | 'hover' | 'both';\n}\n\nexport default function DecryptedText({\n text,\n speed = 50,\n maxIterations = 10,\n sequential = false,\n revealDirection = 'start',\n useOriginalCharsOnly = false,\n characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*()_+',\n className = '',\n parentClassName = '',\n encryptedClassName = '',\n animateOn = 'hover',\n ...props\n}: DecryptedTextProps) {\n const [displayText, setDisplayText] = useState(text);\n const [isHovering, setIsHovering] = useState(false);\n const [isScrambling, setIsScrambling] = useState(false);\n const [revealedIndices, setRevealedIndices] = useState>(new Set());\n const [hasAnimated, setHasAnimated] = useState(false);\n const containerRef = useRef(null);\n\n useEffect(() => {\n let interval: ReturnType;\n let currentIteration = 0;\n\n const getNextIndex = (revealedSet: Set): number => {\n const textLength = text.length;\n switch (revealDirection) {\n case 'start':\n return revealedSet.size;\n case 'end':\n return textLength - 1 - revealedSet.size;\n case 'center': {\n const middle = Math.floor(textLength / 2);\n const offset = Math.floor(revealedSet.size / 2);\n const nextIndex = revealedSet.size % 2 === 0 ? middle + offset : middle - offset - 1;\n\n if (nextIndex >= 0 && nextIndex < textLength && !revealedSet.has(nextIndex)) {\n return nextIndex;\n }\n\n for (let i = 0; i < textLength; i++) {\n if (!revealedSet.has(i)) return i;\n }\n return 0;\n }\n default:\n return revealedSet.size;\n }\n };\n\n const availableChars = useOriginalCharsOnly\n ? Array.from(new Set(text.split(''))).filter(char => char !== ' ')\n : characters.split('');\n\n const shuffleText = (originalText: string, currentRevealed: Set): string => {\n if (useOriginalCharsOnly) {\n const positions = originalText.split('').map((char, i) => ({\n char,\n isSpace: char === ' ',\n index: i,\n isRevealed: currentRevealed.has(i)\n }));\n\n const nonSpaceChars = positions.filter(p => !p.isSpace && !p.isRevealed).map(p => p.char);\n\n for (let i = nonSpaceChars.length - 1; i > 0; i--) {\n const j = Math.floor(Math.random() * (i + 1));\n [nonSpaceChars[i], nonSpaceChars[j]] = [nonSpaceChars[j], nonSpaceChars[i]];\n }\n\n let charIndex = 0;\n return positions\n .map(p => {\n if (p.isSpace) return ' ';\n if (p.isRevealed) return originalText[p.index];\n return nonSpaceChars[charIndex++];\n })\n .join('');\n } else {\n return originalText\n .split('')\n .map((char, i) => {\n if (char === ' ') return ' ';\n if (currentRevealed.has(i)) return originalText[i];\n return availableChars[Math.floor(Math.random() * availableChars.length)];\n })\n .join('');\n }\n };\n\n if (isHovering) {\n setIsScrambling(true);\n interval = setInterval(() => {\n setRevealedIndices(prevRevealed => {\n if (sequential) {\n if (prevRevealed.size < text.length) {\n const nextIndex = getNextIndex(prevRevealed);\n const newRevealed = new Set(prevRevealed);\n newRevealed.add(nextIndex);\n setDisplayText(shuffleText(text, newRevealed));\n return newRevealed;\n } else {\n clearInterval(interval);\n setIsScrambling(false);\n return prevRevealed;\n }\n } else {\n setDisplayText(shuffleText(text, prevRevealed));\n currentIteration++;\n if (currentIteration >= maxIterations) {\n clearInterval(interval);\n setIsScrambling(false);\n setDisplayText(text);\n }\n return prevRevealed;\n }\n });\n }, speed);\n } else {\n setDisplayText(text);\n setRevealedIndices(new Set());\n setIsScrambling(false);\n }\n\n return () => {\n if (interval) clearInterval(interval);\n };\n }, [isHovering, text, speed, maxIterations, sequential, revealDirection, characters, useOriginalCharsOnly]);\n\n useEffect(() => {\n if (animateOn !== 'view' && animateOn !== 'both') return;\n\n const observerCallback = (entries: IntersectionObserverEntry[]) => {\n entries.forEach(entry => {\n if (entry.isIntersecting && !hasAnimated) {\n setIsHovering(true);\n setHasAnimated(true);\n }\n });\n };\n\n const observerOptions = {\n root: null,\n rootMargin: '0px',\n threshold: 0.1\n };\n\n const observer = new IntersectionObserver(observerCallback, observerOptions);\n const currentRef = containerRef.current;\n if (currentRef) {\n observer.observe(currentRef);\n }\n\n return () => {\n if (currentRef) {\n observer.unobserve(currentRef);\n }\n };\n }, [animateOn, hasAnimated]);\n\n const hoverProps =\n animateOn === 'hover' || animateOn === 'both'\n ? {\n onMouseEnter: () => setIsHovering(true),\n onMouseLeave: () => setIsHovering(false)\n }\n : {};\n\n return (\n \n {displayText}\n\n \n {displayText.split('').map((char, index) => {\n const isRevealedOrDone = revealedIndices.has(index) || !isScrambling || !isHovering;\n\n return (\n \n {char}\n \n );\n })}\n \n \n );\n}\n" + "content": "import { useEffect, useState, useRef, useMemo, useCallback } from 'react';\nimport { motion } from 'motion/react';\nimport type { HTMLMotionProps } from 'motion/react';\n\nconst styles = {\n wrapper: {\n display: 'inline-block',\n whiteSpace: 'pre-wrap'\n },\n srOnly: {\n position: 'absolute' as const,\n width: '1px',\n height: '1px',\n padding: 0,\n margin: '-1px',\n overflow: 'hidden',\n clip: 'rect(0,0,0,0)',\n border: 0\n }\n};\n\ninterface DecryptedTextProps extends HTMLMotionProps<'span'> {\n text: string;\n speed?: number;\n maxIterations?: number;\n sequential?: boolean;\n revealDirection?: 'start' | 'end' | 'center';\n useOriginalCharsOnly?: boolean;\n characters?: string;\n className?: string;\n parentClassName?: string;\n encryptedClassName?: string;\n animateOn?: 'view' | 'hover' | 'inViewHover' | 'click';\n clickMode?: 'once' | 'toggle';\n}\n\ntype Direction = 'forward' | 'reverse';\n\nexport default function DecryptedText({\n text,\n speed = 50,\n maxIterations = 10,\n sequential = false,\n revealDirection = 'start',\n useOriginalCharsOnly = false,\n characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*()_+',\n className = '',\n parentClassName = '',\n encryptedClassName = '',\n animateOn = 'hover',\n clickMode = 'once',\n ...props\n}: DecryptedTextProps) {\n const [displayText, setDisplayText] = useState(text);\n const [isAnimating, setIsAnimating] = useState(false);\n const [revealedIndices, setRevealedIndices] = useState>(new Set());\n const [hasAnimated, setHasAnimated] = useState(false);\n const [isDecrypted, setIsDecrypted] = useState(animateOn !== 'click');\n const [direction, setDirection] = useState('forward');\n\n const containerRef = useRef(null);\n const orderRef = useRef([]);\n const pointerRef = useRef(0);\n\n const availableChars = useMemo(() => {\n return useOriginalCharsOnly\n ? Array.from(new Set(text.split(''))).filter(char => char !== ' ')\n : characters.split('');\n }, [useOriginalCharsOnly, text, characters]);\n\n const shuffleText = useCallback(\n (originalText: string, currentRevealed: Set) => {\n return originalText\n .split('')\n .map((char, i) => {\n if (char === ' ') return ' ';\n if (currentRevealed.has(i)) return originalText[i];\n return availableChars[Math.floor(Math.random() * availableChars.length)];\n })\n .join('');\n },\n [availableChars]\n );\n\n const computeOrder = useCallback(\n (len: number): number[] => {\n const order: number[] = [];\n if (len <= 0) return order;\n if (revealDirection === 'start') {\n for (let i = 0; i < len; i++) order.push(i);\n return order;\n }\n if (revealDirection === 'end') {\n for (let i = len - 1; i >= 0; i--) order.push(i);\n return order;\n }\n // center\n const middle = Math.floor(len / 2);\n let offset = 0;\n while (order.length < len) {\n if (offset % 2 === 0) {\n const idx = middle + offset / 2;\n if (idx >= 0 && idx < len) order.push(idx);\n } else {\n const idx = middle - Math.ceil(offset / 2);\n if (idx >= 0 && idx < len) order.push(idx);\n }\n offset++;\n }\n return order.slice(0, len);\n },\n [revealDirection]\n );\n\n const fillAllIndices = useCallback((): Set => {\n const s = new Set();\n for (let i = 0; i < text.length; i++) s.add(i);\n return s;\n }, [text]);\n\n const removeRandomIndices = useCallback((set: Set, count: number): Set => {\n const arr = Array.from(set);\n for (let i = 0; i < count && arr.length > 0; i++) {\n const idx = Math.floor(Math.random() * arr.length);\n arr.splice(idx, 1);\n }\n return new Set(arr);\n }, []);\n\n const encryptInstantly = useCallback(() => {\n const emptySet = new Set();\n setRevealedIndices(emptySet);\n setDisplayText(shuffleText(text, emptySet));\n setIsDecrypted(false);\n }, [text, shuffleText]);\n\n const triggerDecrypt = useCallback(() => {\n if (sequential) {\n orderRef.current = computeOrder(text.length);\n pointerRef.current = 0;\n setRevealedIndices(new Set());\n } else {\n setRevealedIndices(new Set());\n }\n setDirection('forward');\n setIsAnimating(true);\n }, [sequential, computeOrder, text.length]);\n\n const triggerReverse = useCallback(() => {\n if (sequential) {\n // compute forward order then reverse it: we'll remove indices in that order\n orderRef.current = computeOrder(text.length).slice().reverse();\n pointerRef.current = 0;\n setRevealedIndices(fillAllIndices()); // start fully revealed\n setDisplayText(shuffleText(text, fillAllIndices()));\n } else {\n // non-seq: start from fully revealed as well\n setRevealedIndices(fillAllIndices());\n setDisplayText(shuffleText(text, fillAllIndices()));\n }\n setDirection('reverse');\n setIsAnimating(true);\n }, [sequential, computeOrder, fillAllIndices, shuffleText, text]);\n\n useEffect(() => {\n if (!isAnimating) return;\n\n let interval: ReturnType;\n let currentIteration = 0;\n\n const getNextIndex = (revealedSet: Set): number => {\n const textLength = text.length;\n switch (revealDirection) {\n case 'start':\n return revealedSet.size;\n case 'end':\n return textLength - 1 - revealedSet.size;\n case 'center': {\n const middle = Math.floor(textLength / 2);\n const offset = Math.floor(revealedSet.size / 2);\n const nextIndex = revealedSet.size % 2 === 0 ? middle + offset : middle - offset - 1;\n\n if (nextIndex >= 0 && nextIndex < textLength && !revealedSet.has(nextIndex)) {\n return nextIndex;\n }\n\n for (let i = 0; i < textLength; i++) {\n if (!revealedSet.has(i)) return i;\n }\n return 0;\n }\n default:\n return revealedSet.size;\n }\n };\n\n interval = setInterval(() => {\n setRevealedIndices(prevRevealed => {\n if (sequential) {\n // Forward\n if (direction === 'forward') {\n if (prevRevealed.size < text.length) {\n const nextIndex = getNextIndex(prevRevealed);\n const newRevealed = new Set(prevRevealed);\n newRevealed.add(nextIndex);\n setDisplayText(shuffleText(text, newRevealed));\n return newRevealed;\n } else {\n clearInterval(interval);\n setIsAnimating(false);\n setIsDecrypted(true);\n return prevRevealed;\n }\n }\n // Reverse\n if (direction === 'reverse') {\n if (pointerRef.current < orderRef.current.length) {\n const idxToRemove = orderRef.current[pointerRef.current++];\n const newRevealed = new Set(prevRevealed);\n newRevealed.delete(idxToRemove);\n setDisplayText(shuffleText(text, newRevealed));\n if (newRevealed.size === 0) {\n clearInterval(interval);\n setIsAnimating(false);\n setIsDecrypted(false);\n }\n return newRevealed;\n } else {\n clearInterval(interval);\n setIsAnimating(false);\n setIsDecrypted(false);\n return prevRevealed;\n }\n }\n } else {\n // Non-Sequential\n if (direction === 'forward') {\n setDisplayText(shuffleText(text, prevRevealed));\n currentIteration++;\n if (currentIteration >= maxIterations) {\n clearInterval(interval);\n setIsAnimating(false);\n setDisplayText(text);\n setIsDecrypted(true);\n }\n return prevRevealed;\n }\n\n // Non-Sequential Reverse\n if (direction === 'reverse') {\n let currentSet = prevRevealed;\n if (currentSet.size === 0) {\n currentSet = fillAllIndices();\n }\n const removeCount = Math.max(1, Math.ceil(text.length / Math.max(1, maxIterations)));\n const nextSet = removeRandomIndices(currentSet, removeCount);\n setDisplayText(shuffleText(text, nextSet));\n currentIteration++;\n if (nextSet.size === 0 || currentIteration >= maxIterations) {\n clearInterval(interval);\n setIsAnimating(false);\n setIsDecrypted(false);\n // ensure final scrambled state\n setDisplayText(shuffleText(text, new Set()));\n return new Set();\n }\n return nextSet;\n }\n }\n return prevRevealed;\n });\n }, speed);\n\n return () => clearInterval(interval);\n }, [\n isAnimating,\n text,\n speed,\n maxIterations,\n sequential,\n revealDirection,\n shuffleText,\n direction,\n fillAllIndices,\n removeRandomIndices,\n characters,\n useOriginalCharsOnly\n ]);\n\n /* Click Behaviour */\n const handleClick = () => {\n if (animateOn !== 'click') return;\n\n if (clickMode === 'once') {\n if (isDecrypted) return;\n setDirection('forward');\n triggerDecrypt();\n }\n\n if (clickMode === 'toggle') {\n if (isDecrypted) {\n triggerReverse();\n } else {\n setDirection('forward');\n triggerDecrypt();\n }\n }\n };\n\n /* Hover Behaviour */\n const triggerHoverDecrypt = useCallback(() => {\n if (isAnimating) return;\n\n // Reset animation state cleanly\n setRevealedIndices(new Set());\n setIsDecrypted(false);\n setDisplayText(text);\n\n setDirection('forward');\n setIsAnimating(true);\n }, [isAnimating, text]);\n\n const resetToPlainText = useCallback(() => {\n setIsAnimating(false);\n setRevealedIndices(new Set());\n setDisplayText(text);\n setIsDecrypted(true);\n setDirection('forward');\n }, [text]);\n\n /* View Observer */\n useEffect(() => {\n if (animateOn !== 'view' && animateOn !== 'inViewHover') return;\n\n const observerCallback = (entries: IntersectionObserverEntry[]) => {\n entries.forEach(entry => {\n if (entry.isIntersecting && !hasAnimated) {\n triggerDecrypt();\n setHasAnimated(true);\n }\n });\n };\n\n const observerOptions = {\n root: null,\n rootMargin: '0px',\n threshold: 0.1\n };\n\n const observer = new IntersectionObserver(observerCallback, observerOptions);\n const currentRef = containerRef.current;\n if (currentRef) {\n observer.observe(currentRef);\n }\n\n return () => {\n if (currentRef) {\n observer.unobserve(currentRef);\n }\n };\n }, [animateOn, hasAnimated, triggerDecrypt]);\n\n useEffect(() => {\n if (animateOn === 'click') {\n encryptInstantly();\n } else {\n setDisplayText(text);\n setIsDecrypted(true);\n }\n setRevealedIndices(new Set());\n setDirection('forward');\n }, [animateOn, text, encryptInstantly]);\n\n const animateProps =\n animateOn === 'hover' || animateOn === 'inViewHover'\n ? {\n onMouseEnter: triggerHoverDecrypt,\n onMouseLeave: resetToPlainText\n }\n : animateOn === 'click'\n ? {\n onClick: handleClick\n }\n : {};\n\n return (\n \n {displayText}\n\n \n {displayText.split('').map((char, index) => {\n const isRevealedOrDone = revealedIndices.has(index) || (!isAnimating && isDecrypted);\n\n return (\n \n {char}\n \n );\n })}\n \n \n );\n}\n" } ], "registryDependencies": [], diff --git a/public/r/DecryptedText-TS-TW.json b/public/r/DecryptedText-TS-TW.json index ff30e4be..90646f75 100644 --- a/public/r/DecryptedText-TS-TW.json +++ b/public/r/DecryptedText-TS-TW.json @@ -8,7 +8,7 @@ { "type": "registry:component", "path": "DecryptedText/DecryptedText.tsx", - "content": "import { useEffect, useState, useRef } from 'react';\nimport { motion } from 'motion/react';\nimport type { HTMLMotionProps } from 'motion/react';\n\ninterface DecryptedTextProps extends HTMLMotionProps<'span'> {\n text: string;\n speed?: number;\n maxIterations?: number;\n sequential?: boolean;\n revealDirection?: 'start' | 'end' | 'center';\n useOriginalCharsOnly?: boolean;\n characters?: string;\n className?: string;\n encryptedClassName?: string;\n parentClassName?: string;\n animateOn?: 'view' | 'hover' | 'both';\n}\n\nexport default function DecryptedText({\n text,\n speed = 50,\n maxIterations = 10,\n sequential = false,\n revealDirection = 'start',\n useOriginalCharsOnly = false,\n characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*()_+',\n className = '',\n parentClassName = '',\n encryptedClassName = '',\n animateOn = 'hover',\n ...props\n}: DecryptedTextProps) {\n const [displayText, setDisplayText] = useState(text);\n const [isHovering, setIsHovering] = useState(false);\n const [isScrambling, setIsScrambling] = useState(false);\n const [revealedIndices, setRevealedIndices] = useState>(new Set());\n const [hasAnimated, setHasAnimated] = useState(false);\n const containerRef = useRef(null);\n\n useEffect(() => {\n let interval: ReturnType;\n let currentIteration = 0;\n\n const getNextIndex = (revealedSet: Set): number => {\n const textLength = text.length;\n switch (revealDirection) {\n case 'start':\n return revealedSet.size;\n case 'end':\n return textLength - 1 - revealedSet.size;\n case 'center': {\n const middle = Math.floor(textLength / 2);\n const offset = Math.floor(revealedSet.size / 2);\n const nextIndex = revealedSet.size % 2 === 0 ? middle + offset : middle - offset - 1;\n\n if (nextIndex >= 0 && nextIndex < textLength && !revealedSet.has(nextIndex)) {\n return nextIndex;\n }\n for (let i = 0; i < textLength; i++) {\n if (!revealedSet.has(i)) return i;\n }\n return 0;\n }\n default:\n return revealedSet.size;\n }\n };\n\n const availableChars = useOriginalCharsOnly\n ? Array.from(new Set(text.split(''))).filter(char => char !== ' ')\n : characters.split('');\n\n const shuffleText = (originalText: string, currentRevealed: Set): string => {\n if (useOriginalCharsOnly) {\n const positions = originalText.split('').map((char, i) => ({\n char,\n isSpace: char === ' ',\n index: i,\n isRevealed: currentRevealed.has(i)\n }));\n\n const nonSpaceChars = positions.filter(p => !p.isSpace && !p.isRevealed).map(p => p.char);\n\n for (let i = nonSpaceChars.length - 1; i > 0; i--) {\n const j = Math.floor(Math.random() * (i + 1));\n [nonSpaceChars[i], nonSpaceChars[j]] = [nonSpaceChars[j], nonSpaceChars[i]];\n }\n\n let charIndex = 0;\n return positions\n .map(p => {\n if (p.isSpace) return ' ';\n if (p.isRevealed) return originalText[p.index];\n return nonSpaceChars[charIndex++];\n })\n .join('');\n } else {\n return originalText\n .split('')\n .map((char, i) => {\n if (char === ' ') return ' ';\n if (currentRevealed.has(i)) return originalText[i];\n return availableChars[Math.floor(Math.random() * availableChars.length)];\n })\n .join('');\n }\n };\n\n if (isHovering) {\n setIsScrambling(true);\n interval = setInterval(() => {\n setRevealedIndices(prevRevealed => {\n if (sequential) {\n if (prevRevealed.size < text.length) {\n const nextIndex = getNextIndex(prevRevealed);\n const newRevealed = new Set(prevRevealed);\n newRevealed.add(nextIndex);\n setDisplayText(shuffleText(text, newRevealed));\n return newRevealed;\n } else {\n clearInterval(interval);\n setIsScrambling(false);\n return prevRevealed;\n }\n } else {\n setDisplayText(shuffleText(text, prevRevealed));\n currentIteration++;\n if (currentIteration >= maxIterations) {\n clearInterval(interval);\n setIsScrambling(false);\n setDisplayText(text);\n }\n return prevRevealed;\n }\n });\n }, speed);\n } else {\n setDisplayText(text);\n setRevealedIndices(new Set());\n setIsScrambling(false);\n }\n\n return () => {\n if (interval) clearInterval(interval);\n };\n }, [isHovering, text, speed, maxIterations, sequential, revealDirection, characters, useOriginalCharsOnly]);\n\n useEffect(() => {\n if (animateOn !== 'view' && animateOn !== 'both') return;\n\n const observerCallback = (entries: IntersectionObserverEntry[]) => {\n entries.forEach(entry => {\n if (entry.isIntersecting && !hasAnimated) {\n setIsHovering(true);\n setHasAnimated(true);\n }\n });\n };\n\n const observerOptions = {\n root: null,\n rootMargin: '0px',\n threshold: 0.1\n };\n\n const observer = new IntersectionObserver(observerCallback, observerOptions);\n const currentRef = containerRef.current;\n if (currentRef) {\n observer.observe(currentRef);\n }\n\n return () => {\n if (currentRef) observer.unobserve(currentRef);\n };\n }, [animateOn, hasAnimated]);\n\n const hoverProps =\n animateOn === 'hover' || animateOn === 'both'\n ? {\n onMouseEnter: () => setIsHovering(true),\n onMouseLeave: () => setIsHovering(false)\n }\n : {};\n\n return (\n \n {displayText}\n\n \n {displayText.split('').map((char, index) => {\n const isRevealedOrDone = revealedIndices.has(index) || !isScrambling || !isHovering;\n\n return (\n \n {char}\n \n );\n })}\n \n \n );\n}\n" + "content": "import { useEffect, useState, useRef, useMemo, useCallback } from 'react';\nimport { motion } from 'motion/react';\nimport type { HTMLMotionProps } from 'motion/react';\n\ninterface DecryptedTextProps extends HTMLMotionProps<'span'> {\n text: string;\n speed?: number;\n maxIterations?: number;\n sequential?: boolean;\n revealDirection?: 'start' | 'end' | 'center';\n useOriginalCharsOnly?: boolean;\n characters?: string;\n className?: string;\n encryptedClassName?: string;\n parentClassName?: string;\n animateOn?: 'view' | 'hover' | 'inViewHover' | 'click';\n clickMode?: 'once' | 'toggle';\n}\n\ntype Direction = 'forward' | 'reverse';\n\nexport default function DecryptedText({\n text,\n speed = 50,\n maxIterations = 10,\n sequential = false,\n revealDirection = 'start',\n useOriginalCharsOnly = false,\n characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*()_+',\n className = '',\n parentClassName = '',\n encryptedClassName = '',\n animateOn = 'hover',\n clickMode = 'once',\n ...props\n}: DecryptedTextProps) {\n const [displayText, setDisplayText] = useState(text);\n const [isAnimating, setIsAnimating] = useState(false);\n const [revealedIndices, setRevealedIndices] = useState>(new Set());\n const [hasAnimated, setHasAnimated] = useState(false);\n const [isDecrypted, setIsDecrypted] = useState(animateOn !== 'click');\n const [direction, setDirection] = useState('forward');\n\n const containerRef = useRef(null);\n const orderRef = useRef([]);\n const pointerRef = useRef(0);\n\n const availableChars = useMemo(() => {\n return useOriginalCharsOnly\n ? Array.from(new Set(text.split(''))).filter(char => char !== ' ')\n : characters.split('');\n }, [useOriginalCharsOnly, text, characters]);\n\n const shuffleText = useCallback(\n (originalText: string, currentRevealed: Set) => {\n return originalText\n .split('')\n .map((char, i) => {\n if (char === ' ') return ' ';\n if (currentRevealed.has(i)) return originalText[i];\n return availableChars[Math.floor(Math.random() * availableChars.length)];\n })\n .join('');\n },\n [availableChars]\n );\n\n const computeOrder = useCallback(\n (len: number): number[] => {\n const order: number[] = [];\n if (len <= 0) return order;\n if (revealDirection === 'start') {\n for (let i = 0; i < len; i++) order.push(i);\n return order;\n }\n if (revealDirection === 'end') {\n for (let i = len - 1; i >= 0; i--) order.push(i);\n return order;\n }\n // center\n const middle = Math.floor(len / 2);\n let offset = 0;\n while (order.length < len) {\n if (offset % 2 === 0) {\n const idx = middle + offset / 2;\n if (idx >= 0 && idx < len) order.push(idx);\n } else {\n const idx = middle - Math.ceil(offset / 2);\n if (idx >= 0 && idx < len) order.push(idx);\n }\n offset++;\n }\n return order.slice(0, len);\n },\n [revealDirection]\n );\n\n const fillAllIndices = useCallback((): Set => {\n const s = new Set();\n for (let i = 0; i < text.length; i++) s.add(i);\n return s;\n }, [text]);\n\n const removeRandomIndices = useCallback((set: Set, count: number): Set => {\n const arr = Array.from(set);\n for (let i = 0; i < count && arr.length > 0; i++) {\n const idx = Math.floor(Math.random() * arr.length);\n arr.splice(idx, 1);\n }\n return new Set(arr);\n }, []);\n\n const encryptInstantly = useCallback(() => {\n const emptySet = new Set();\n setRevealedIndices(emptySet);\n setDisplayText(shuffleText(text, emptySet));\n setIsDecrypted(false);\n }, [text, shuffleText]);\n\n const triggerDecrypt = useCallback(() => {\n if (sequential) {\n orderRef.current = computeOrder(text.length);\n pointerRef.current = 0;\n setRevealedIndices(new Set());\n } else {\n setRevealedIndices(new Set());\n }\n setDirection('forward');\n setIsAnimating(true);\n }, [sequential, computeOrder, text.length]);\n\n const triggerReverse = useCallback(() => {\n if (sequential) {\n // compute forward order then reverse it: we'll remove indices in that order\n orderRef.current = computeOrder(text.length).slice().reverse();\n pointerRef.current = 0;\n setRevealedIndices(fillAllIndices()); // start fully revealed\n setDisplayText(shuffleText(text, fillAllIndices()));\n } else {\n // non-seq: start from fully revealed as well\n setRevealedIndices(fillAllIndices());\n setDisplayText(shuffleText(text, fillAllIndices()));\n }\n setDirection('reverse');\n setIsAnimating(true);\n }, [sequential, computeOrder, fillAllIndices, shuffleText, text]);\n\n useEffect(() => {\n if (!isAnimating) return;\n\n let interval: ReturnType;\n let currentIteration = 0;\n\n const getNextIndex = (revealedSet: Set): number => {\n const textLength = text.length;\n switch (revealDirection) {\n case 'start':\n return revealedSet.size;\n case 'end':\n return textLength - 1 - revealedSet.size;\n case 'center': {\n const middle = Math.floor(textLength / 2);\n const offset = Math.floor(revealedSet.size / 2);\n const nextIndex = revealedSet.size % 2 === 0 ? middle + offset : middle - offset - 1;\n\n if (nextIndex >= 0 && nextIndex < textLength && !revealedSet.has(nextIndex)) {\n return nextIndex;\n }\n for (let i = 0; i < textLength; i++) {\n if (!revealedSet.has(i)) return i;\n }\n return 0;\n }\n default:\n return revealedSet.size;\n }\n };\n\n interval = setInterval(() => {\n setRevealedIndices(prevRevealed => {\n if (sequential) {\n // Forward\n if (direction === 'forward') {\n if (prevRevealed.size < text.length) {\n const nextIndex = getNextIndex(prevRevealed);\n const newRevealed = new Set(prevRevealed);\n newRevealed.add(nextIndex);\n setDisplayText(shuffleText(text, newRevealed));\n return newRevealed;\n } else {\n clearInterval(interval);\n setIsAnimating(false);\n setIsDecrypted(true);\n return prevRevealed;\n }\n }\n // Reverse\n if (direction === 'reverse') {\n if (pointerRef.current < orderRef.current.length) {\n const idxToRemove = orderRef.current[pointerRef.current++];\n const newRevealed = new Set(prevRevealed);\n newRevealed.delete(idxToRemove);\n setDisplayText(shuffleText(text, newRevealed));\n if (newRevealed.size === 0) {\n clearInterval(interval);\n setIsAnimating(false);\n setIsDecrypted(false);\n }\n return newRevealed;\n } else {\n clearInterval(interval);\n setIsAnimating(false);\n setIsDecrypted(false);\n return prevRevealed;\n }\n }\n } else {\n // Non-Sequential\n if (direction === 'forward') {\n setDisplayText(shuffleText(text, prevRevealed));\n currentIteration++;\n if (currentIteration >= maxIterations) {\n clearInterval(interval);\n setIsAnimating(false);\n setDisplayText(text);\n setIsDecrypted(true);\n }\n return prevRevealed;\n }\n\n // Non-Sequential Reverse\n if (direction === 'reverse') {\n let currentSet = prevRevealed;\n if (currentSet.size === 0) {\n currentSet = fillAllIndices();\n }\n const removeCount = Math.max(1, Math.ceil(text.length / Math.max(1, maxIterations)));\n const nextSet = removeRandomIndices(currentSet, removeCount);\n setDisplayText(shuffleText(text, nextSet));\n currentIteration++;\n if (nextSet.size === 0 || currentIteration >= maxIterations) {\n clearInterval(interval);\n setIsAnimating(false);\n setIsDecrypted(false);\n // ensure final scrambled state\n setDisplayText(shuffleText(text, new Set()));\n return new Set();\n }\n return nextSet;\n }\n }\n return prevRevealed;\n });\n }, speed);\n return () => clearInterval(interval);\n }, [\n isAnimating,\n text,\n speed,\n maxIterations,\n sequential,\n revealDirection,\n shuffleText,\n direction,\n fillAllIndices,\n removeRandomIndices,\n characters,\n useOriginalCharsOnly\n ]);\n\n /* Click Behaviour */\n const handleClick = () => {\n if (animateOn !== 'click') return;\n\n if (clickMode === 'once') {\n if (isDecrypted) return;\n setDirection('forward');\n triggerDecrypt();\n }\n\n if (clickMode === 'toggle') {\n if (isDecrypted) {\n triggerReverse();\n } else {\n setDirection('forward');\n triggerDecrypt();\n }\n }\n };\n\n /* Hover Behaviour */\n const triggerHoverDecrypt = useCallback(() => {\n if (isAnimating) return;\n\n // Reset animation state cleanly\n setRevealedIndices(new Set());\n setIsDecrypted(false);\n setDisplayText(text);\n\n setDirection('forward');\n setIsAnimating(true);\n }, [isAnimating, text]);\n\n const resetToPlainText = useCallback(() => {\n setIsAnimating(false);\n setRevealedIndices(new Set());\n setDisplayText(text);\n setIsDecrypted(true);\n setDirection('forward');\n }, [text]);\n\n /* View Observer */\n useEffect(() => {\n if (animateOn !== 'view' && animateOn !== 'inViewHover') return;\n\n const observerCallback = (entries: IntersectionObserverEntry[]) => {\n entries.forEach(entry => {\n if (entry.isIntersecting && !hasAnimated) {\n triggerDecrypt();\n setHasAnimated(true);\n }\n });\n };\n\n const observerOptions = {\n root: null,\n rootMargin: '0px',\n threshold: 0.1\n };\n\n const observer = new IntersectionObserver(observerCallback, observerOptions);\n const currentRef = containerRef.current;\n if (currentRef) {\n observer.observe(currentRef);\n }\n\n return () => {\n if (currentRef) observer.unobserve(currentRef);\n };\n }, [animateOn, hasAnimated, triggerDecrypt]);\n\n useEffect(() => {\n if (animateOn === 'click') {\n encryptInstantly();\n } else {\n setDisplayText(text);\n setIsDecrypted(true);\n }\n setRevealedIndices(new Set());\n setDirection('forward');\n }, [animateOn, text, encryptInstantly]);\n\n const animateProps =\n animateOn === 'hover' || animateOn === 'inViewHover'\n ? {\n onMouseEnter: triggerHoverDecrypt,\n onMouseLeave: resetToPlainText\n }\n : animateOn === 'click'\n ? {\n onClick: handleClick\n }\n : {};\n\n return (\n \n {displayText}\n\n \n {displayText.split('').map((char, index) => {\n const isRevealedOrDone = revealedIndices.has(index) || (!isAnimating && isDecrypted);\n\n return (\n \n {char}\n \n );\n })}\n \n \n );\n}\n" } ], "registryDependencies": [], diff --git a/src/constants/code/TextAnimations/decryptedTextCode.js b/src/constants/code/TextAnimations/decryptedTextCode.js index 9b36fc4d..6add37df 100644 --- a/src/constants/code/TextAnimations/decryptedTextCode.js +++ b/src/constants/code/TextAnimations/decryptedTextCode.js @@ -21,13 +21,20 @@ parentClassName="all-letters" encryptedClassName="encrypted" /> -{/* Example 3: Animate on view (runs once) */} -
+{/* Example 3: Click to decrypt (toggle mode) */} + +{/* Example 4: Animate on view (runs once) */} +
+ + />
`, code, tailwind, diff --git a/src/content/TextAnimations/DecryptedText/DecryptedText.jsx b/src/content/TextAnimations/DecryptedText/DecryptedText.jsx index bc611dac..6eab473b 100644 --- a/src/content/TextAnimations/DecryptedText/DecryptedText.jsx +++ b/src/content/TextAnimations/DecryptedText/DecryptedText.jsx @@ -1,4 +1,4 @@ -import { useEffect, useState, useRef } from 'react'; +import { useEffect, useState, useRef, useMemo, useCallback } from 'react'; import { motion } from 'motion/react'; const styles = { @@ -30,16 +30,123 @@ export default function DecryptedText({ parentClassName = '', encryptedClassName = '', animateOn = 'hover', + clickMode = 'once', ...props }) { const [displayText, setDisplayText] = useState(text); - const [isHovering, setIsHovering] = useState(false); - const [isScrambling, setIsScrambling] = useState(false); + const [isAnimating, setIsAnimating] = useState(false); const [revealedIndices, setRevealedIndices] = useState(new Set()); const [hasAnimated, setHasAnimated] = useState(false); + const [isDecrypted, setIsDecrypted] = useState(animateOn !== 'click'); + const [direction, setDirection] = useState('forward'); + const containerRef = useRef(null); + const orderRef = useRef([]); + const pointerRef = useRef(0); + + const availableChars = useMemo(() => { + return useOriginalCharsOnly + ? Array.from(new Set(text.split(''))).filter(char => char !== ' ') + : characters.split(''); + }, [useOriginalCharsOnly, text, characters]); + + const shuffleText = useCallback( + (originalText, currentRevealed) => { + return originalText + .split('') + .map((char, i) => { + if (char === ' ') return ' '; + if (currentRevealed.has(i)) return originalText[i]; + return availableChars[Math.floor(Math.random() * availableChars.length)]; + }) + .join(''); + }, + [availableChars] + ); + + const computeOrder = useCallback( + len => { + const order = []; + if (len <= 0) return order; + if (revealDirection === 'start') { + for (let i = 0; i < len; i++) order.push(i); + return order; + } + if (revealDirection === 'end') { + for (let i = len - 1; i >= 0; i--) order.push(i); + return order; + } + // center + const middle = Math.floor(len / 2); + let offset = 0; + while (order.length < len) { + if (offset % 2 === 0) { + const idx = middle + offset / 2; + if (idx >= 0 && idx < len) order.push(idx); + } else { + const idx = middle - Math.ceil(offset / 2); + if (idx >= 0 && idx < len) order.push(idx); + } + offset++; + } + return order.slice(0, len); + }, + [revealDirection] + ); + + const fillAllIndices = useCallback(() => { + const s = new Set(); + for (let i = 0; i < text.length; i++) s.add(i); + return s; + }, [text]); + + const removeRandomIndices = useCallback((set, count) => { + const arr = Array.from(set); + for (let i = 0; i < count && arr.length > 0; i++) { + const idx = Math.floor(Math.random() * arr.length); + arr.splice(idx, 1); + } + return new Set(arr); + }, []); + + const encryptInstantly = useCallback(() => { + const emptySet = new Set(); + setRevealedIndices(emptySet); + setDisplayText(shuffleText(text, emptySet)); + setIsDecrypted(false); + }, [text, shuffleText]); + + const triggerDecrypt = useCallback(() => { + if (sequential) { + orderRef.current = computeOrder(text.length); + pointerRef.current = 0; + setRevealedIndices(new Set()); + } else { + setRevealedIndices(new Set()); + } + setDirection('forward'); + setIsAnimating(true); + }, [sequential, computeOrder, text.length]); + + const triggerReverse = useCallback(() => { + if (sequential) { + // compute forward order then reverse it: we'll remove indices in that order + orderRef.current = computeOrder(text.length).slice().reverse(); + pointerRef.current = 0; + setRevealedIndices(fillAllIndices()); // start fully revealed + setDisplayText(shuffleText(text, fillAllIndices())); + } else { + // non-seq: start from fully revealed as well + setRevealedIndices(fillAllIndices()); + setDisplayText(shuffleText(text, fillAllIndices())); + } + setDirection('reverse'); + setIsAnimating(true); + }, [sequential, computeOrder, fillAllIndices, shuffleText, text]); useEffect(() => { + if (!isAnimating) return; + let interval; let currentIteration = 0; @@ -69,51 +176,11 @@ export default function DecryptedText({ } }; - const availableChars = useOriginalCharsOnly - ? Array.from(new Set(text.split(''))).filter(char => char !== ' ') - : characters.split(''); - - const shuffleText = (originalText, currentRevealed) => { - if (useOriginalCharsOnly) { - const positions = originalText.split('').map((char, i) => ({ - char, - isSpace: char === ' ', - index: i, - isRevealed: currentRevealed.has(i) - })); - - const nonSpaceChars = positions.filter(p => !p.isSpace && !p.isRevealed).map(p => p.char); - - for (let i = nonSpaceChars.length - 1; i > 0; i--) { - const j = Math.floor(Math.random() * (i + 1)); - [nonSpaceChars[i], nonSpaceChars[j]] = [nonSpaceChars[j], nonSpaceChars[i]]; - } - - let charIndex = 0; - return positions - .map(p => { - if (p.isSpace) return ' '; - if (p.isRevealed) return originalText[p.index]; - return nonSpaceChars[charIndex++]; - }) - .join(''); - } else { - return originalText - .split('') - .map((char, i) => { - if (char === ' ') return ' '; - if (currentRevealed.has(i)) return originalText[i]; - return availableChars[Math.floor(Math.random() * availableChars.length)]; - }) - .join(''); - } - }; - - if (isHovering) { - setIsScrambling(true); - interval = setInterval(() => { - setRevealedIndices(prevRevealed => { - if (sequential) { + interval = setInterval(() => { + setRevealedIndices(prevRevealed => { + if (sequential) { + // Forward + if (direction === 'forward') { if (prevRevealed.size < text.length) { const nextIndex = getNextIndex(prevRevealed); const newRevealed = new Set(prevRevealed); @@ -122,39 +189,135 @@ export default function DecryptedText({ return newRevealed; } else { clearInterval(interval); - setIsScrambling(false); + setIsAnimating(false); + setIsDecrypted(true); + return prevRevealed; + } + } + // Reverse + if (direction === 'reverse') { + if (pointerRef.current < orderRef.current.length) { + const idxToRemove = orderRef.current[pointerRef.current++]; + const newRevealed = new Set(prevRevealed); + newRevealed.delete(idxToRemove); + setDisplayText(shuffleText(text, newRevealed)); + if (newRevealed.size === 0) { + clearInterval(interval); + setIsAnimating(false); + setIsDecrypted(false); + } + return newRevealed; + } else { + clearInterval(interval); + setIsAnimating(false); + setIsDecrypted(false); return prevRevealed; } - } else { + } + } else { + // Non-Sequential + if (direction === 'forward') { setDisplayText(shuffleText(text, prevRevealed)); currentIteration++; if (currentIteration >= maxIterations) { clearInterval(interval); - setIsScrambling(false); + setIsAnimating(false); setDisplayText(text); + setIsDecrypted(true); } return prevRevealed; } - }); - }, speed); - } else { - setDisplayText(text); - setRevealedIndices(new Set()); - setIsScrambling(false); + + // Non-Sequential Reverse + if (direction === 'reverse') { + let currentSet = prevRevealed; + if (currentSet.size === 0) { + currentSet = fillAllIndices(); + } + const removeCount = Math.max(1, Math.ceil(text.length / Math.max(1, maxIterations))); + const nextSet = removeRandomIndices(currentSet, removeCount); + setDisplayText(shuffleText(text, nextSet)); + currentIteration++; + if (nextSet.size === 0 || currentIteration >= maxIterations) { + clearInterval(interval); + setIsAnimating(false); + setIsDecrypted(false); + // ensure final scrambled state + setDisplayText(shuffleText(text, new Set())); + return new Set(); + } + return nextSet; + } + } + return prevRevealed; + }); + }, speed); + + return () => clearInterval(interval); + }, [ + isAnimating, + text, + speed, + maxIterations, + sequential, + revealDirection, + shuffleText, + direction, + fillAllIndices, + removeRandomIndices, + characters, + useOriginalCharsOnly + ]); + + /* Click Behaviour */ + const handleClick = () => { + if (animateOn !== 'click') return; + + if (clickMode === 'once') { + if (isDecrypted) return; + setDirection('forward'); + triggerDecrypt(); } - return () => { - if (interval) clearInterval(interval); - }; - }, [isHovering, text, speed, maxIterations, sequential, revealDirection, characters, useOriginalCharsOnly]); + if (clickMode === 'toggle') { + if (isDecrypted) { + triggerReverse(); + } else { + setDirection('forward'); + triggerDecrypt(); + } + } + }; + + /* Hover Behaviour */ + const triggerHoverDecrypt = useCallback(() => { + if (isAnimating) return; + // Reset animation state cleanly + setRevealedIndices(new Set()); + setIsDecrypted(false); + setDisplayText(text); + + setDirection('forward'); + setIsAnimating(true); + }, [isAnimating, text]); + + const resetToPlainText = useCallback(() => { + setIsAnimating(false); + setRevealedIndices(new Set()); + setDisplayText(text); + setIsDecrypted(true); + setDirection('forward'); + }, [text]); + + /* View Observer */ useEffect(() => { - if (animateOn !== 'view' && animateOn !== 'both') return; + if (animateOn !== 'view' && animateOn !== 'inViewHover') return; const observerCallback = entries => { entries.forEach(entry => { if (entry.isIntersecting && !hasAnimated) { - setIsHovering(true); + triggerDecrypt(); setHasAnimated(true); } }); @@ -177,23 +340,38 @@ export default function DecryptedText({ observer.unobserve(currentRef); } }; - }, [animateOn, hasAnimated]); + }, [animateOn, hasAnimated, triggerDecrypt]); - const hoverProps = - animateOn === 'hover' || animateOn === 'both' + useEffect(() => { + if (animateOn === 'click') { + encryptInstantly(); + } else { + setDisplayText(text); + setIsDecrypted(true); + } + setRevealedIndices(new Set()); + setDirection('forward'); + }, [animateOn, text, encryptInstantly]); + + const animateProps = + animateOn === 'hover' || animateOn === 'inViewHover' ? { - onMouseEnter: () => setIsHovering(true), - onMouseLeave: () => setIsHovering(false) + onMouseEnter: triggerHoverDecrypt, + onMouseLeave: resetToPlainText } - : {}; + : animateOn === 'click' + ? { + onClick: handleClick + } + : {}; return ( - + {displayText}