-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
57 lines (49 loc) · 1.37 KB
/
index.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
52
53
54
55
56
57
/*
* ES6 FlowType.JS without jQuery.
* Ported by Fritz Lekschas. Based on:
*
* FlowType.JS v1.1
* Copyright 2013-2014, Simple Focus http://simplefocus.com/
*
* FlowType.JS by Simple Focus (http://simplefocus.com/)
* is licensed under the MIT License. Read a copy of the
* license in the LICENSE.txt file or at
* http://choosealicense.com/licenses/mit
*
* Thanks to Giovanni Difeterici (http://www.gdifeterici.com/)
*/
import withRaf from "with-raf";
const createFlowtype = (
element,
{
maximum = 1280,
minimum = 500,
maxFont = 40,
minFont = 12,
fontRatio = 30
} = {}
) => {
const calculateFontSize = () => {
const elw = element.clientWidth;
const width = elw > maximum ? maximum : elw < minimum ? minimum : elw;
const fontBase = width / fontRatio;
const fontSize =
fontBase > maxFont ? maxFont : fontBase < minFont ? minFont : fontBase;
element.style.fontSize = fontSize + "px";
};
const update = withRaf(calculateFontSize);
const destroy = () => {
window.removeEventListener("resize", update, false);
window.removeEventListener("orientationchange", update, false);
};
// Make changes upon resize
window.addEventListener("resize", update, false);
window.addEventListener("orientationchange", update, false);
// Initialize
update();
return {
update,
destroy
};
};
export default createFlowtype;