-
Notifications
You must be signed in to change notification settings - Fork 0
/
GrowingTextarea.vue
82 lines (71 loc) · 2.1 KB
/
GrowingTextarea.vue
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<template>
<div class="growing-textarea">
<textarea ref="textarea" :placeholder="placeholder" v-model="inputValue" :style="styleObject"></textarea>
<span ref="text">Joghurt</span>
</div>
</template>
<script>
export default {
name: "GrowingTextarea",
data() {
return {
inputValue: '',
firstHeight: null,
lineHeight: null,
padding: null,
numberOfLines: 1,
styleObject: null
}
},
props: {
placeholder: String,
value: String,
fat: Boolean
},
mounted() {
this.firstHeight = this.$refs.textarea.offsetHeight;
this.padding = this.firstHeight - this.$refs.text.offsetHeight;
this.lineHeight = this.firstHeight - this.padding;
if( this.fat ) {
this.styleObject = {'font-weight':'700'};
}
},
watch: {
inputValue() {
this.$emit('input', this.inputValue);
this.$refs.textarea.style.height = (this.lineHeight + this.padding)+'px';
},
value() {
this.inputValue = this.value;
this.numberOfLines = Math.floor( this.$refs.textarea.scrollHeight / this.lineHeight );
this.$refs.textarea.style.height = (this.numberOfLines * this.lineHeight + this.padding)+'px';
}
}
}
</script>
<style lang="scss">
@import '@/styles/theme.scss';
@import '@/styles/shared/variables.scss';
.growing-textarea {
position: relative;
textarea {
display: block;
height: 40px;
}
span {
position: absolute;
left: 0;
top: 0;
visibility: hidden;
display: block;
line-height: 1.5;
}
}
@media only screen and (min-width: $viewport-tablet-portrait) {
.growing-textarea {
textarea {
height: 48px;
}
}
}
</style>