forked from rodrigobendia/font-picker-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FontPicker.vue
329 lines (292 loc) · 9.56 KB
/
FontPicker.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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<template>
<div :id="`font-picker${pickerSuffix}`" class="font-picker" :title="state.errorText">
<button class="dropdown-button" type="button"
:class="{expanded: state.expanded}"
@click="toggleExpanded"
@keypress="toggleExpanded">
<p class="dropdown-font-name">{{state.activeFont}}</p>
<p class="dropdown-icon" :class="state.loadingStatus"></p>
</button>
<ul v-if="state.loadingStatus === 'finished' && fontManager.fonts"
:class="{expanded: state.expanded}"
@scroll="onScroll">
<li v-for="font in fontManager.fonts" :key="font.family">
<button type="button"
:class="`font-${snakeCase(font.family)}${pickerSuffix} ${font.family === state.activeFont ? 'active-font' : ''}`"
@click="itemClick(font)"
@keypress="itemClick(font)">{{font.family}}</button>
</li>
</ul>
</div>
</template>
<script>
import { FontManager } from 'font-picker';
/**
* Vue interface for the font picker
* @prop {string} apiKey (required) - Google API key
* @prop {string} activeFont - Font that should be selected in the font picker and applied to the
* text (default: 'Open Sans'). Must be stored in component state, and be updated using an onChange
* listener. See README.md for an example.
* @prop {Object} options - Object with additional (optional) parameters:
* @prop {string} name - If you have multiple font pickers on your site, you need to give them
* unique names (which may only consist of letters and digits). These names must also be appended
* to the font picker's ID and the .apply-font class name.
* Example: If { name: 'main' }, use #font-picker-main and .apply-font-main
* @prop {string[]} families - If only specific fonts shall appear in the list, specify their
* names in an array
* @prop {string[]} categories - Array of font categories
* Possible values: 'sans-serif', 'serif', 'display', 'handwriting', 'monospace' (default: all
* categories)
* @prop {string[]} variants - Array of variants which the fonts must include and which will be
* downloaded; the first variant in the array will become the default variant (and will be used
* in the font picker and the .apply-font class)
* Example: ['regular', 'italic', '700', '700italic'] (default: ['regular'])
* @prop {number} limit - Maximum number of fonts to be displayed in the list (the least popular
* fonts will be omitted; default: 100)
* @prop {string} sort - Sorting attribute for the font list
* Possible values: 'alphabetical' (default), 'popularity'
* @prop {function} onChange - Function which is executed whenever the user changes the active font
* and its stylesheet finishes downloading
*/
export default {
props: ['activeFont', 'apiKey', 'options'],
data() {
return {
state: {
activeFont: this.activeFont,
errorText: '',
expanded: false,
loadingStatus: 'loading' // possible values: 'loading', 'finished', 'error'
},
pickerSuffix: '',
fontManager: null,
};
},
mounted() {
// Determine selector suffix from font picker's name
if (this.options && this.options.name) {
this.pickerSuffix = `-${this.options.name}`;
} else {
this.pickerSuffix = '';
}
// Initialize FontManager object and generate the font list
this.fontManager = new FontManager(
this.apiKey,
this.activeFont,
this.options
);
this.fontManager.init()
.then(() => {
// font list has finished loading
this.setState({
errorText: '',
loadingStatus: 'finished'
});
})
.catch((err) => {
// error while loading font list
this.setState({
errorText: 'Error trying to fetch the list of available fonts',
loadingStatus: 'error'
});
console.error(this.state.errorText);
console.error(err);
});
},
watch: {
activeFont() {
if (this.state.activeFont !== this.activeFont) {
this.setActiveFont(this.activeFont);
}
},
},
methods: {
/**
* Set state object
*/
setState(state) {
this.state = Object.assign(this.state, state);
},
/**
* EventListener for closing the font picker when clicking anywhere outside it
*/
onClose(e) {
let targetElement = e.target; // clicked element
do {
if (targetElement === document.getElementById('font-picker')) {
// click inside font picker
return;
}
// move up the DOM
targetElement = targetElement.parentNode;
} while (targetElement);
// click outside font picker
this.toggleExpanded();
},
/**
* Download the font previews for all visible font entries and the five after them
*/
onScroll(e) {
const elementHeight = e.target.scrollHeight / this.fontManager.fonts.length;
const downloadIndex = Math.ceil((e.target.scrollTop + e.target.clientHeight) / elementHeight);
this.fontManager.downloadPreviews(downloadIndex + 5);
},
/**
* Set the font with the given font list index as the active one
*/
setActiveFont(fontFamily) {
const activeFontIndex = this.fontManager.setActiveFont(fontFamily);
if (activeFontIndex === -1) {
// error trying to change font
this.setState({
activeFont: fontFamily,
errorText: `Cannot update activeFont: The font "${fontFamily}" is not in the font list`,
loadingStatus: 'error'
});
console.error(this.state.errorText);
} else {
// font change successful
this.setState({
activeFont: fontFamily,
errorText: '',
loadingStatus: 'finished'
});
}
},
/**
* Expand/collapse the picker's font list
*/
toggleExpanded() {
this.setState({
expanded: !this.state.expanded
});
},
snakeCase(text) {
return text.replace(/\s+/g, '-').toLowerCase();
},
itemClick(font) {
this.toggleExpanded();
this.$emit('change', font);
}
},
}
</script>
<style lang="scss">
.font-picker {
position: relative;
display: inline-block;
width: 200px;
box-shadow: 1px 1px 3px rgba(0, 0, 0, 0.2);
* {
box-sizing: border-box;
}
p {
margin: 0;
padding: 0;
}
button {
background: none;
border: 0;
color: inherit;
cursor: pointer;
font-size: inherit;
outline: none;
}
.dropdown-button {
height: 35px;
width: 100%;
display: flex;
align-items: center;
justify-content: space-between;
padding: 0 10px;
background: #CBCBCB;
&:hover, &.expanded, &:focus {
background: #bebebe;
}
.dropdown-font-name {
overflow: hidden;
white-space: nowrap;
}
&.expanded .dropdown-icon.finished:before {
-webkit-transform: rotate(-180deg);
transform: rotate(-180deg);
}
}
.dropdown-icon {
margin-left: 10px;
&.loading:before {
content: '';
display: block;
height: 10px;
width: 10px;
border-radius: 50%;
border: 2px solid #b2b2b2;
border-top-color: black;
-webkit-animation: spinner 0.6s linear infinite;
animation: spinner 0.6s linear infinite;
}
&.finished:before {
content: '';
display: block;
height: 0;
width: 0;
border-left: 5px solid transparent;
border-right: 5px solid transparent;
border-top: 6px solid black;
transition: -webkit-transform 0.3s;
transition: transform 0.3s, -webkit-transform 0.3s;
margin: 0 2px;
}
&.error:before {
content: '⚠';
}
}
ul {
position: absolute;
z-index: 1;
max-height: 0;
width: 100%;
overflow-x: hidden;
overflow-y: auto;
-webkit-overflow-scrolling: touch;
margin: 0;
padding: 0;
background: #EAEAEA;
box-shadow: 1px 1px 3px rgba(0, 0, 0, 0.2);
transition: 0.3s;
&.expanded {
max-height: 200px;
}
li {
height: 35px;
list-style: none;
button {
height: 100%;
width: 100%;
display: flex;
align-items: center;
padding: 0 10px;
white-space: nowrap;
&:hover, &:focus {
background: #dddddd;
}
&.active-font {
background: #d1d1d1;
}
}
}
}
}
@-webkit-keyframes spinner {
to {
-webkit-transform: rotate(360deg);
transform: rotate(360deg);
}
}
@keyframes spinner {
to {
-webkit-transform: rotate(360deg);
transform: rotate(360deg);
}
}
</style>