2023-06-29 08:56:05 -04:00
|
|
|
import { useEffect, useRef } from 'react'
|
|
|
|
import { callFnsInSequence } from '../../utils/functions'
|
|
|
|
import { MergeAndOverride } from '../../../../types/utils'
|
|
|
|
|
|
|
|
export const resetHeight = (
|
|
|
|
e:
|
|
|
|
| React.ChangeEvent<HTMLTextAreaElement>
|
|
|
|
| React.KeyboardEvent<HTMLTextAreaElement>
|
|
|
|
) => {
|
|
|
|
const el = e.target as HTMLTextAreaElement
|
|
|
|
|
|
|
|
window.requestAnimationFrame(() => {
|
|
|
|
const curHeight = el.offsetHeight
|
|
|
|
const fitHeight = el.scrollHeight
|
|
|
|
// clear height if text area is empty
|
|
|
|
if (!el.value.length) {
|
|
|
|
el.style.removeProperty('height')
|
|
|
|
}
|
|
|
|
// otherwise expand to fit text
|
|
|
|
else if (fitHeight > curHeight) {
|
|
|
|
el.style.height = `${fitHeight}px`
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type AutoExpandingTextAreaProps = MergeAndOverride<
|
|
|
|
React.ComponentProps<'textarea'>,
|
|
|
|
{
|
|
|
|
onResize?: () => void
|
|
|
|
}
|
|
|
|
>
|
|
|
|
|
|
|
|
function AutoExpandingTextArea({
|
|
|
|
onChange,
|
|
|
|
onResize,
|
|
|
|
...rest
|
|
|
|
}: AutoExpandingTextAreaProps) {
|
|
|
|
const ref = useRef<HTMLTextAreaElement>(null)
|
2023-09-05 10:46:13 -04:00
|
|
|
const previousHeightRef = useRef<number | null>(null)
|
2023-06-29 08:56:05 -04:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!ref.current || !onResize || !('ResizeObserver' in window)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const resizeObserver = new ResizeObserver(() => {
|
|
|
|
// Ignore the resize that is triggered when the element is first
|
|
|
|
// inserted into the DOM
|
2023-09-05 10:46:13 -04:00
|
|
|
if (!ref.current) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const newHeight = ref.current.offsetHeight
|
|
|
|
const heightChanged = newHeight !== previousHeightRef.current
|
|
|
|
previousHeightRef.current = newHeight
|
|
|
|
if (heightChanged) {
|
2023-07-18 04:35:04 -04:00
|
|
|
// Prevent errors like "ResizeObserver loop completed with undelivered
|
2023-09-05 10:46:13 -04:00
|
|
|
// notifications" that occur if onResize triggers another repaint. The
|
|
|
|
// cost of this is that onResize lags one frame behind, but it's
|
2023-07-18 04:35:04 -04:00
|
|
|
// unlikely to matter.
|
2023-07-27 05:55:10 -04:00
|
|
|
|
|
|
|
// Wrap onResize to prevent extra parameters being passed
|
|
|
|
window.requestAnimationFrame(() => onResize())
|
2023-06-29 08:56:05 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
resizeObserver.observe(ref.current)
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
resizeObserver.disconnect()
|
|
|
|
}
|
|
|
|
}, [onResize])
|
|
|
|
|
|
|
|
return (
|
|
|
|
<textarea
|
|
|
|
onChange={callFnsInSequence(onChange, resetHeight)}
|
|
|
|
{...rest}
|
|
|
|
ref={ref}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default AutoExpandingTextArea
|