Skip to content

Commit 5cd57f1

Browse files
authored
Merge pull request #13 from egargan/box-sizing-height-fix
Account for 'border-box' box sizing in textarea height
2 parents a3273d9 + ebb6df6 commit 5cd57f1

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/index.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,23 @@ export default function autosize(textarea) {
4848
return
4949
}
5050

51-
const maxHeight = Number(getComputedStyle(textarea).height.replace(/px/, '')) + bottom
51+
const textareaStyle = getComputedStyle(textarea)
52+
53+
const topBorderWidth = Number(textareaStyle.borderTopWidth.replace(/px/, ''))
54+
const bottomBorderWidth = Number(textareaStyle.borderBottomWidth.replace(/px/, ''))
55+
56+
const isBorderBox = textareaStyle.boxSizing === 'border-box'
57+
const borderAddOn = isBorderBox ? topBorderWidth + bottomBorderWidth : 0
58+
59+
const maxHeight = Number(textareaStyle.height.replace(/px/, '')) + bottom
5260
textarea.style.maxHeight = `${maxHeight - 100}px`
5361

5462
const container = textarea.parentElement
5563
if (container instanceof HTMLElement) {
5664
const containerHeight = container.style.height
5765
container.style.height = getComputedStyle(container).height
5866
textarea.style.height = 'auto'
59-
textarea.style.height = `${textarea.scrollHeight}px`
67+
textarea.style.height = `${textarea.scrollHeight + borderAddOn}px`
6068
container.style.height = containerHeight
6169
height = textarea.style.height
6270
}

0 commit comments

Comments
 (0)