-
Notifications
You must be signed in to change notification settings - Fork 2
/
textfit-div.html
executable file
·255 lines (248 loc) · 9.1 KB
/
textfit-div.html
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../iron-resizable-behavior/iron-resizable-behavior.html">
<link rel="import" href="../iron-flex-layout/iron-flex-layout-classes.html">
<link rel="import" href="../shadycss/apply-shim.html">
<script src="../textfit/textFit.js"></script>
<!--
### Example:
<div class="layout horizontal flex wrap">
<div class="horizontal-section layout horizontal flex">
<textfit-div class="fullHeight flex" horizontal-center vertical-center text="Hey!"></textfit-div>
</div>
<div class="horizontal-section layout horizontal flex">
<textfit-div class="fullHeight flex" text="Good Evening!"></textfit-div>
</div>
</div>
Custom property | Description | Default
----------------|---------------------------|----------
`--textfit-div` | Mixin applied to the text | `{}`
@element textfit-div
@demo demo/index.html
-->
<dom-module id="textfit-div">
<template>
<style is="custom-style">
:host {
@apply --layout-vertical;
@apply --textfit-div;
}
.fullHeight {
@apply --layout-flex;
@apply --textfit-div;
}
div > span {
@apply --textfit-div;
}
</style>
<div id="fitMe" class="fullHeight">{{text}}</div>
</template>
<script>
Polymer({
is: 'textfit-div',
behaviors: [
Polymer.IronResizableBehavior
],
properties: {
/**
* The text that will be displayed and fit into its container.
*
* @attribute text
* @type String
* @default ''
*/
text: {
observer: '_textChanged',
type: String,
value: ''
},
/**
* Tracks the attachment status of the textfit-div element.
*
* @attribute isAttached
* @type Boolean
* @default false
*/
isAttached: {
type: Boolean,
value: false
},
/**
* When set to true the content will be centered horizontally.
*
* @attribute horizontalCenter
* @type Boolean
* @default false
*/
horizontalCenter: {
type: Boolean,
value: false
},
/**
* When set to true the content will be centered vertically.
*
* @attribute verticalCenter
* @type Boolean
* @default false
*/
verticalCenter: {
type: Boolean,
value: false
},
/**
* When set to true, textfit's multiline detection will be explicitly turned on.
*
* @attribute multiLine
* @type Boolean
* @default false
*/
multiLine: {
type: Boolean,
value: false
},
/**
* If your text has multiple lines, textFit() will automatically detect that and disable white-space: no-wrap! No changes are necessary.
*
* @attribute detectMultiLine
* @type Boolean
* @default true
*/
detectMultiLine: {
type: Boolean,
value: true
},
/**
* Minimum font-size for textfit. If set the calculated font-size will not be set below the defined value.
*
* @attribute minFontSize
* @type Number
* @default 6
*/
minFontSize: {
type: Number,
value: 6
},
/**
* Maximum font-size for textfit. If set the calculated font-size will not be set above the defined value.
*
* @attribute maxFontSize
* @type Number
* @default 200
*/
maxFontSize: {
type: Number,
value: 200
},
/**
* If true, textFit will re-process already-fit nodes. Set to 'false' for better performance
*
* @attribute reProcess
* @type Boolean
* @default true
*/
reProcess: {
type: Boolean,
value: true
},
/**
* If true, textFit will fit text to element width, regardless of text height
*
* @attribute widthOnly
* @type Boolean
* @default false
*/
widthOnly: {
type: Boolean,
value: false
},
/**
* If set to true, textfit-div will not automatically recalculate the font-size when a resize event is fired or the text content is changed.
* doFitMe() must be called manually to trigger a recalculation.
*
* @attribute manualFire
* @type Boolean
* @default false
*/
manualFire: {
type: Boolean,
value: false
},
/**
* If true, textFit will use flexbox for vertical alignment
*
* @attribute manualFire
* @type Boolean
* @default false
*/
alignVertWithFlexbox: {
type: Boolean,
value: false
}
},
listeners: {
'iron-resize': '_onIronResize'
},
/**
* Triggered when the textfit-div element is attached.
* Initial textfit calculation is triggered.
*/
attached: function () {
this.isAttached = true;
if (this.manualFire)
return;
// Below we run async because there is a known bug with polymer attached callback and clientWidth parameter used in textFit plugin
this.async(this.doFitMe);
},
detached: function () {
this.isAttached = false;
},
/**
* Observer method that is triggered when text content changed.
*/
_textChanged: function (newText, oldText) {
if (newText !== oldText && !this.manualFire) {
var span = this.shadowRoot.querySelector('.textFitted');
if (span) {
span.innerHTML = newText;
}
this.async(this.doFitMe);
}
},
/**
* Triggered on iron-resize event.
*/
_onIronResize: function () {
if (this.manualFire)
return;
this.async(this.doFitMe);
},
/**
* Calls textfit to trigger font-size recalculation.
*/
doFitMe: function () {
if (!this.isAttached)
return;
if (this.$.fitMe.innerHTML.length < 1)
return;
if (this.$.fitMe.clientHeight <= 0 || this.$.fitMe.clientWidth <= 0) {
console.debug('textfit-div has no size yet, therefore the text can not be fitted.');
return;
}
try {
textFit(this.$.fitMe, {
alignHoriz: this.horizontalCenter,
alignVert: this.verticalCenter,
multiLine: this.multiLine,
detectMultiLine: this.detectMultiLine,
minFontSize: this.minFontSize,
maxFontSize: this.maxFontSize,
reProcess: this.reProcess,
widthOnly: this.widthOnly,
alignVertWithFlexbox: this.alignVertWithFlexbox
});
} catch (e) {
console.warn(e);
}
}
});
</script>
</dom-module>