|
| 1 | +import React, { useEffect, useState, useRef } from 'react' |
| 2 | +import PropTypes from 'prop-types' |
| 3 | + |
| 4 | +function TextRevealOverlay({ |
| 5 | + text, |
| 6 | + isRevealing, |
| 7 | + lastParagraphIndex, |
| 8 | + className, |
| 9 | + textareaRef, |
| 10 | + onAnimationComplete |
| 11 | +}) { |
| 12 | + const [paragraphs, setParagraphs] = useState([]) |
| 13 | + const [isAnimatingLastParagraph, setIsAnimatingLastParagraph] = useState(false) |
| 14 | + const overlayRef = useRef(null) |
| 15 | + |
| 16 | + // Split text into paragraphs whenever text changes |
| 17 | + useEffect(() => { |
| 18 | + if (text) { |
| 19 | + const split = text.split(/\n{2,}|\r\n{2,}/) |
| 20 | + .map(p => p.trim()) |
| 21 | + .filter(p => p.length > 0) |
| 22 | + setParagraphs(split) |
| 23 | + } else { |
| 24 | + setParagraphs([]) |
| 25 | + } |
| 26 | + }, [text]) |
| 27 | + |
| 28 | + useEffect(() => { |
| 29 | + if (isAnimatingLastParagraph && lastParagraphIndex === paragraphs.length - 1) { |
| 30 | + const timer = setTimeout(() => { |
| 31 | + setIsAnimatingLastParagraph(false) |
| 32 | + if (onAnimationComplete) { |
| 33 | + onAnimationComplete() |
| 34 | + } |
| 35 | + }, 500) |
| 36 | + |
| 37 | + return () => clearTimeout(timer) |
| 38 | + } |
| 39 | + }, [isAnimatingLastParagraph, lastParagraphIndex, paragraphs.length, onAnimationComplete]) |
| 40 | + |
| 41 | + |
| 42 | + useEffect(() => { |
| 43 | + if (lastParagraphIndex === paragraphs.length - 1 && paragraphs.length > 0) { |
| 44 | + setIsAnimatingLastParagraph(true) |
| 45 | + } |
| 46 | + }, [lastParagraphIndex, paragraphs.length]) |
| 47 | + |
| 48 | + |
| 49 | + useEffect(() => { |
| 50 | + if (!textareaRef || !textareaRef.current || !overlayRef.current || !isRevealing) return |
| 51 | + |
| 52 | + const textarea = textareaRef.current |
| 53 | + const overlay = overlayRef.current |
| 54 | + |
| 55 | + // Function to update dimensions whenever they change |
| 56 | + const updateDimensions = () => { |
| 57 | + // Position relative to the prompt-input container |
| 58 | + const promptInput = textarea.closest('.ds-prompt-input') |
| 59 | + let paddingLeft = '16px' |
| 60 | + let paddingTop = '16px' |
| 61 | + |
| 62 | + if (promptInput) { |
| 63 | + const promptStyles = window.getComputedStyle(promptInput) |
| 64 | + if (promptStyles.paddingLeft) { |
| 65 | + paddingLeft = promptStyles.paddingLeft |
| 66 | + } |
| 67 | + if (promptStyles.paddingTop) { |
| 68 | + paddingTop = promptStyles.paddingTop |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // Get current styles |
| 73 | + const textareaStyles = window.getComputedStyle(textarea) |
| 74 | + |
| 75 | + // Apply exact dimensions and styling |
| 76 | + overlay.style.width = textareaStyles.width |
| 77 | + overlay.style.fontSize = textareaStyles.fontSize |
| 78 | + overlay.style.lineHeight = textareaStyles.lineHeight |
| 79 | + overlay.style.fontFamily = textareaStyles.fontFamily |
| 80 | + overlay.style.fontWeight = textareaStyles.fontWeight |
| 81 | + overlay.style.color = textareaStyles.color |
| 82 | + |
| 83 | + // Set exact position |
| 84 | + overlay.style.position = 'absolute' |
| 85 | + overlay.style.top = paddingTop |
| 86 | + overlay.style.left = paddingLeft |
| 87 | + overlay.style.width = 'calc(100% - ' + (parseInt(paddingLeft) * 2) + 'px)' |
| 88 | + |
| 89 | + // Set max-height to match textarea max-height |
| 90 | + overlay.style.maxHeight = textareaStyles.maxHeight |
| 91 | + |
| 92 | + // Match the actual height if it's calculated |
| 93 | + if (textarea.style.height) { |
| 94 | + overlay.style.height = textarea.style.height |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + // Initial update |
| 99 | + updateDimensions() |
| 100 | + |
| 101 | + // Set up mutation observer to detect height changes |
| 102 | + let observer = null |
| 103 | + if (typeof MutationObserver !== 'undefined') { |
| 104 | + observer = new MutationObserver((mutations) => { |
| 105 | + mutations.forEach((mutation) => { |
| 106 | + if (mutation.attributeName === 'style') { |
| 107 | + updateDimensions() |
| 108 | + } |
| 109 | + }) |
| 110 | + }) |
| 111 | + |
| 112 | + observer.observe(textarea, { |
| 113 | + attributes: true, |
| 114 | + attributeFilter: ['style'] |
| 115 | + }) |
| 116 | + } |
| 117 | + |
| 118 | + const syncScroll = () => { |
| 119 | + if (overlayRef.current && isRevealing) { |
| 120 | + overlayRef.current.scrollTop = textarea.scrollTop |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + textarea.addEventListener('scroll', syncScroll) |
| 125 | + |
| 126 | + window.addEventListener('resize', updateDimensions) |
| 127 | + |
| 128 | + syncScroll() |
| 129 | + |
| 130 | + return () => { |
| 131 | + textarea.removeEventListener('scroll', syncScroll) |
| 132 | + window.removeEventListener('resize', updateDimensions) |
| 133 | + if (observer) { |
| 134 | + observer.disconnect() |
| 135 | + } |
| 136 | + } |
| 137 | + }, [textareaRef, isRevealing, paragraphs]) |
| 138 | + |
| 139 | + if (!isRevealing || paragraphs.length === 0) { |
| 140 | + return null |
| 141 | + } |
| 142 | + |
| 143 | + return ( |
| 144 | + <div |
| 145 | + ref={overlayRef} |
| 146 | + className={`ds-prompt-input__text-reveal-overlay ${className || ''}`} |
| 147 | + aria-hidden="true" |
| 148 | + > |
| 149 | + {paragraphs.map((paragraph, index) => ( |
| 150 | + <p |
| 151 | + key={index} |
| 152 | + className={`ds-prompt-input__text-reveal-paragraph ${ |
| 153 | + index === lastParagraphIndex ? 'ds-prompt-input__text-reveal-paragraph--animating' : '' |
| 154 | + }`} |
| 155 | + > |
| 156 | + {paragraph} |
| 157 | + </p> |
| 158 | + ))} |
| 159 | + </div> |
| 160 | + ) |
| 161 | +} |
| 162 | + |
| 163 | +TextRevealOverlay.propTypes = { |
| 164 | + text: PropTypes.string, |
| 165 | + isRevealing: PropTypes.bool, |
| 166 | + lastParagraphIndex: PropTypes.number, |
| 167 | + className: PropTypes.string, |
| 168 | + textareaRef: PropTypes.object, |
| 169 | + onAnimationComplete: PropTypes.func |
| 170 | +} |
| 171 | + |
| 172 | +TextRevealOverlay.defaultProps = { |
| 173 | + text: '', |
| 174 | + isRevealing: false, |
| 175 | + lastParagraphIndex: -1, |
| 176 | + className: '', |
| 177 | + textareaRef: null, |
| 178 | + onAnimationComplete: null |
| 179 | +} |
| 180 | + |
| 181 | +export default TextRevealOverlay |
0 commit comments