/
auto-expand.js
executable file
·51 lines (28 loc) · 957 Bytes
/
auto-expand.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
# AutoExpand Mixin for reproCSS
## version 0.0.9
Automatically expand an HTML element's `width` or `height` to match its `scrollWidth`, `scrollHeight`, or both.
### Syntax
autoExpand(selector, direction)
- `selector` is a CSS selector
- `direction` is a string matching `width`, `height`, or `both`
### Example
autoExpand('textarea', 'height')
- https://github.com/tomhodgins/reprocss
Author: Tommy Hodgins
License: MIT
*/
function autoExpand(selector, direction) {
var tag = document.querySelectorAll(selector)
for (var i=0; i < tag.length; i++) {
if (direction == 'width' || direction == 'both') {
tag[i].style.width = ''
tag[i].style.width = tag[i].scrollWidth + 'px'
}
if (direction == 'height' || direction == 'both') {
tag[i].style.height = ''
tag[i].style.height = tag[i].scrollHeight + 'px'
}
}
return '\n/* ' + selector + ' { ' + direction +': auto-expand; } */\n'
}