-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.tsx
190 lines (180 loc) · 4.57 KB
/
index.tsx
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
import Head from "next/head";
import React, { useCallback, useState } from "react";
import useFitText from "use-fit-text";
/**
* Example1 - basic example
*/
function Example1() {
const { fontSize, ref } = useFitText();
return (
<>
<b>Example 1 - basic example</b>
<div
ref={ref}
style={{ fontSize, height: 40, width: 120, border: "1px solid #ccc" }}
>
Lorem ipsum dolor sit amet, consectetur Lorem ipsum dolor sit amet,
consectetur
</div>
<hr />
</>
);
}
/**
* Example2 - non-wrapping text
*/
function Example2() {
const { fontSize, ref } = useFitText();
return (
<>
<b>Example 2 - non-wrapping text</b>
<div
ref={ref}
style={{ fontSize, height: 40, width: 120, border: "1px solid #ccc" }}
>
<div style={{ whiteSpace: "nowrap" }}>
Lorem ipsum dolor sit amet, consectetur
</div>
</div>
<hr />
</>
);
}
/**
* Example3 - demonstrates:
* - recalculating font size on window resize
* - `onStart` and `onFinish` callbacks
*/
function Example3() {
const onStart = useCallback(() => {
console.log("Example 3 resizing started");
}, []);
const onFinish = useCallback((fontSize: number) => {
console.log("Example 3 resizing finished", fontSize);
}, []);
const { fontSize, ref } = useFitText({ maxFontSize: 500, onStart, onFinish });
return (
<>
<b>Example 3 - recalculating on container change</b>
<p>Demonstrates:</p>
<ul>
<li>recalculating font size on window resize</li>
<li>
<code className="text-dark">onStart</code> and{" "}
<code className="text-dark">onFinish</code> callbacks (see log message
in the console)
</li>
</ul>
<div
ref={ref}
style={{
fontSize,
height: 100,
width: "100%",
}}
>
Lorem ipsum dolor sit amet, consectetur
</div>
<hr />
</>
);
}
/**
* Example4 - recalculating on content change
*/
function Example4() {
const [text, setText] = useState("Lorem ipsum dolor sit amet, consectetur");
const { fontSize, ref } = useFitText();
return (
<>
<b>Example 4 - recalculating on content change</b>
<p>
Change the input text and the font size will be updated to fit the text
in the div
</p>
<div>
<textarea
onChange={(event) => setText(event.target.value)}
value={text}
/>
<div
ref={ref}
style={{ fontSize, height: 40, width: 120, border: "1px solid #ccc" }}
>
{text}
</div>
</div>
<hr />
</>
);
}
/**
* Example5 - fails to fit text because `fontSizeMin` is too big
*/
function Example5() {
const { fontSize, ref } = useFitText({
maxFontSize: 285.7142857142857,
minFontSize: 125.7142857142857,
// Note: with `v2.3.0` and earlier, adding this non-referentially equal
// `onFinish` callback caused a "Maximum update depth exceeded" error.
// See https://github.com/saltycrane/use-fit-text/issues/9
onFinish: () => {},
});
return (
<>
<b>
Example 5 - fails to fit text because <code>fontSizeMin</code> is too
big. Shows a message in the console.
</b>
<div
ref={ref}
style={{ fontSize, height: 40, width: 120, border: "1px solid red" }}
>
Lorem ipsum dolor sit amet, consectetur Lorem ipsum dolor sit amet,
consectetur
</div>
<hr />
</>
);
}
/**
* Page
*/
function Page() {
return (
<>
<Head>
<link
rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"
integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z"
crossOrigin="anonymous"
/>
</Head>
<nav className="navbar navbar-light bg-light">
<div className="container">
<span className="navbar-brand">use-fit-text</span>
<ul className="navbar-nav">
<li className="nav-item">
<a
className="nav-link"
href="https://github.com/saltycrane/use-fit-text"
>
Github
</a>
</li>
</ul>
</div>
</nav>
<div className="container" style={{ fontSize: 16 }}>
<h1 className="my-4">Examples</h1>
<Example1 />
<Example2 />
<Example3 />
<Example4 />
<Example5 />
</div>
</>
);
}
export default Page;