-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor vaTextarea autosize calculations to use scrollHeight instead…
… of newline characters (#4146) * Fix vaTextarea not calculating autosizing properly with wrapped lines * fix: correct textarea autosize jumps and incorrect line height * chore: remove unused code --------- Co-authored-by: Maksim Nedoshev <m0ksem1337@gmail.com>
- Loading branch information
Showing
3 changed files
with
147 additions
and
33 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
51 changes: 51 additions & 0 deletions
51
packages/ui/src/components/va-textarea/composables/useLineHeight.ts
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,51 @@ | ||
import { Ref, ref, watch } from 'vue' | ||
import { useResizeObserver } from '../../../composables' | ||
|
||
const makeTextElement = (textarea: HTMLTextAreaElement) => { | ||
const div = document.createElement('div') | ||
div.style.position = 'absolute' | ||
div.style.top = '0' | ||
div.style.left = '0' | ||
div.style.width = 'auto' | ||
const { font } = window.getComputedStyle(textarea) | ||
div.style.font = font | ||
div.textContent = 'Vuestic' | ||
div.style.zIndex = '-1' | ||
div.style.pointerEvents = 'none' | ||
div.style.opacity = '0' | ||
div.ariaHidden = 'true' | ||
div.innerText = textarea.value | ||
|
||
return div | ||
} | ||
|
||
/** | ||
* Used to get textarea textHeight. Uses resizeObserver and fake div, because | ||
* font family may be loaded after the component is mounted. | ||
*/ | ||
export const useTextHeight = (textarea: Ref<HTMLTextAreaElement | undefined>, text: Ref<string | number>) => { | ||
const textElement = ref<HTMLElement>() | ||
const textHeight = ref<number>() | ||
|
||
watch(textarea, (el) => { | ||
if (el) { | ||
textElement.value = makeTextElement(el) | ||
textarea.value?.parentElement?.appendChild(textElement.value) | ||
} | ||
}) | ||
|
||
useResizeObserver(textElement, (newElement) => { | ||
if (!newElement || !textarea.value) { return } | ||
|
||
textHeight.value = newElement[0].contentRect.height | ||
}) | ||
|
||
watch(text, (newText) => { | ||
if (!textElement.value) { return } | ||
textElement.value.innerText = String(newText) | ||
// Add space to correctly handle new lines with br | ||
textElement.value.innerHTML += ' ;' | ||
}) | ||
|
||
return textHeight | ||
} |
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