Skip to content

Commit 82cca69

Browse files
committed
1.2.7 Performance boosts
1 parent d4be98e commit 82cca69

File tree

2 files changed

+22
-36
lines changed

2 files changed

+22
-36
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "emoji-picker-react",
3-
"version": "1.2.6",
3+
"version": "1.2.7",
44
"description": "React emoji-picker component",
55
"main": "./dist/index.js",
66
"scripts": {

src/EmojiPicker/index.js

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ const isFFMac = isFirefoxOnMac();
2323

2424
class EmojiPicker extends Component {
2525

26-
constructor() {
27-
super();
26+
constructor(props) {
27+
super(props);
2828

2929
this.state = {
3030
filter: null,
@@ -39,6 +39,7 @@ class EmojiPicker extends Component {
3939

4040
this.active = null; // this is for updating the category name
4141
this.transformed = [];
42+
this.pickerClassName = `emoji-picker nav-${props.nav ? props.nav : 'top'}`;
4243

4344
this.onScroll = throttle(16, this.onScroll.bind(this));
4445
this.onCategoryClick = this.onCategoryClick.bind(this);
@@ -76,8 +77,9 @@ class EmojiPicker extends Component {
7677

7778
setActiveCategory({index}) {
7879

80+
if (this.state.filter) { return; }
81+
7982
const indexPresent = typeof index === 'number',
80-
classList = this._picker.classList,
8183
prevActive = this.active;
8284

8385
if (index === prevActive) {
@@ -88,51 +90,44 @@ class EmojiPicker extends Component {
8890
index = 0;
8991
}
9092

91-
categories.forEach((category) => {
92-
if (category.name !== categories[index].name && classList.contains(category.name)) {
93-
classList.remove(category.name);
94-
}
95-
});
96-
97-
if (this.state.filter) {
98-
return;
99-
}
93+
this._picker.setAttribute('class', `${this.pickerClassName} ${categories[index].name}`);
10094

101-
classList.add(categories[index].name);
10295
this.active = index;
10396
}
10497

10598
unsetActiveCategory() {
106-
const classList = this._picker.classList;
107-
categories.forEach((category) => {
108-
classList.remove(category.name);
109-
});
99+
this._picker.setAttribute('class', this.pickerClassName);
110100
}
111101

112102
setSeenCategory(index, categories) {
113103

114-
const seenCategories = {...this.state.seenCategories};
104+
const seenCategories = Object.assign({}, this.state.seenCategories, categories);
115105
seenCategories[index] = true;
116106

117-
for (const catIndex in categories) {
118-
if (categories.hasOwnProperty(catIndex)) {
119-
seenCategories[catIndex] = true;
120-
}
107+
if (Object.keys(this.state.seenCategories).length === Object.keys(seenCategories).length) {
108+
return;
121109
}
122110

123111
this.setState({ seenCategories });
124112
}
125113

126114
setSeenInSearch(categories) {
127115
const seenInSearch = {...this.state.seenInSearch};
116+
let counter = 0;
128117

129118
for (const catIndex in categories) {
119+
120+
if (this.state.seenCategories[catIndex] || this.state.seenInSearch[catIndex]) {
121+
continue;
122+
}
123+
130124
if (categories.hasOwnProperty(catIndex)) {
125+
counter++;
131126
seenInSearch[catIndex] = true;
132127
}
133128
}
134129

135-
this.setState({seenInSearch});
130+
counter && this.setState({seenInSearch});
136131
}
137132

138133
onScroll() {
@@ -157,13 +152,9 @@ class EmojiPicker extends Component {
157152
if (this.state.filter) {
158153
this.setSeenInSearch(inViewPort);
159154
return this.transformed = clearTransform(this.transformed);
160-
} else {
161-
this.setSeenCategory(0, inViewPort);
162155
}
163156

164-
if (activeCategory !== active) {
165-
this.setSeenCategory(activeCategory);
166-
}
157+
this.setSeenCategory(activeCategory, inViewPort);
167158

168159
// this block deals with mismatches that are caused by fast scrolling
169160
if (typeof proximityIndex !== 'number') {
@@ -209,11 +200,7 @@ class EmojiPicker extends Component {
209200
onSearch(filter) {
210201

211202
this.setState({ filter }, () => {
212-
const positions = getOffsets(this._list);
213-
this.offsets = positions.offsets;
214-
this.listHeight = positions.listHeight;
215203
this._list.scrollTop = 0;
216-
this.proximity = getProximity(this.offsets, 0, this.listHeight);
217204
if (!filter) { return this.setActiveCategory(0); }
218205
this.setSeenInSearch(this.proximity.inViewPort);
219206
this.unsetActiveCategory();
@@ -279,15 +266,14 @@ class EmojiPicker extends Component {
279266

280267
render() {
281268

282-
const { nav = 'top', assetPath, emojiResolution } = this.props;
269+
const { assetPath, emojiResolution } = this.props;
283270
const { filter, activeModifier, seenCategories, seenInSearch, diversityPicker, modifiersSpread } = this.state;
284-
const navClass = `nav-${nav}`;
285271
const { openDiversitiesMenu, closeDiversitiesMenu, _emojiName } = this;
286272
const emojiProps = { onEmojiClick: this.onEmojiClick, assetPath, activeModifier, emojiResolution, _emojiName, openDiversitiesMenu },
287273
visibleCategories = Object.assign({}, seenCategories, seenInSearch);
288274

289275
return (
290-
<aside className={`emoji-picker ${navClass}`} ref={(picker) => this._picker = picker}>
276+
<aside className={this.pickerClassName} ref={(picker) => this._picker = picker}>
291277
<CategoriesNav onClick={this.onCategoryClick}/>
292278
<div className="bar-wrapper">
293279
<SkinTones onModifierClick={this.onModifierClick} activeModifier={activeModifier} spread={modifiersSpread}/>

0 commit comments

Comments
 (0)