-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
619 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/** | ||
* Styles | ||
*/ | ||
import './style.scss'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useRef, useEffect, RawHTML, memo } from '@wordpress/element'; | ||
|
||
const AIResponse = memo( | ||
function AIResponse({ response, loading }) { | ||
const responseRef = useRef(); | ||
|
||
useEffect(() => { | ||
if (!responseRef.current) { | ||
return; | ||
} | ||
|
||
const popupContent = responseRef.current.closest( | ||
'.mind-popup-content' | ||
); | ||
|
||
if (!popupContent) { | ||
return; | ||
} | ||
|
||
// Smooth scroll to bottom of response. | ||
const { scrollHeight, clientHeight } = popupContent; | ||
|
||
// Only auto-scroll for shorter contents. | ||
const shouldScroll = scrollHeight - clientHeight < 1000; | ||
|
||
if (shouldScroll) { | ||
popupContent.scrollTo({ | ||
top: scrollHeight, | ||
behavior: 'smooth', | ||
}); | ||
} | ||
}, [response]); | ||
|
||
if (!response && !loading) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<div | ||
ref={responseRef} | ||
className="mind-popup-response" | ||
style={{ | ||
opacity: loading ? 0.85 : 1, | ||
}} | ||
> | ||
<RawHTML>{response}</RawHTML> | ||
{loading && <div className="mind-popup-cursor" />} | ||
</div> | ||
); | ||
}, | ||
(prevProps, nextProps) => { | ||
// Custom memoization to prevent unnecessary rerenders. | ||
return ( | ||
prevProps.renderBuffer.lastUpdate === | ||
nextProps.renderBuffer.lastUpdate && | ||
prevProps.loading === nextProps.loading && | ||
prevProps.progress.isComplete === nextProps.progress.isComplete | ||
); | ||
} | ||
); | ||
|
||
export default AIResponse; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
.mind-popup-response { | ||
/* GPU acceleration */ | ||
transform: translateZ(0); | ||
will-change: transform; | ||
|
||
/* Optimize repaints */ | ||
contain: content; | ||
|
||
/* Smooth typing cursor */ | ||
.mind-popup-cursor { | ||
display: inline-block; | ||
width: 1.5px; | ||
height: 1em; | ||
background: currentColor; | ||
margin-left: 2px; | ||
animation: mind-cursor-blink 1s step-end infinite; | ||
} | ||
} | ||
|
||
@keyframes mind-cursor-blink { | ||
0%, | ||
100% { | ||
opacity: 1; | ||
} | ||
50% { | ||
opacity: 0; | ||
} | ||
} | ||
|
||
/* Optimize for mobile */ | ||
@media (max-width: 768px) { | ||
.mind-popup-response { | ||
contain: strict; | ||
height: 100%; | ||
} | ||
} |
Oops, something went wrong.