mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-07 20:31:06 -05:00
74 lines
1.6 KiB
TypeScript
74 lines
1.6 KiB
TypeScript
|
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)
|
||
|
|
||
|
useEffect(() => {
|
||
|
if (!ref.current || !onResize || !('ResizeObserver' in window)) {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
let isFirstResize = true
|
||
|
|
||
|
const resizeObserver = new ResizeObserver(() => {
|
||
|
// Ignore the resize that is triggered when the element is first
|
||
|
// inserted into the DOM
|
||
|
if (isFirstResize) {
|
||
|
isFirstResize = false
|
||
|
} else {
|
||
|
onResize()
|
||
|
}
|
||
|
})
|
||
|
|
||
|
resizeObserver.observe(ref.current)
|
||
|
|
||
|
return () => {
|
||
|
resizeObserver.disconnect()
|
||
|
}
|
||
|
}, [onResize])
|
||
|
|
||
|
return (
|
||
|
<textarea
|
||
|
onChange={callFnsInSequence(onChange, resetHeight)}
|
||
|
{...rest}
|
||
|
ref={ref}
|
||
|
/>
|
||
|
)
|
||
|
}
|
||
|
|
||
|
export default AutoExpandingTextArea
|