mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
9e8be31bdf
[web] Create comment entries GitOrigin-RevId: 7f3fbe672d18d57a0f5e683e5456ea79ed295e2d
73 lines
1.6 KiB
TypeScript
73 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
|