Skip to content

Commit

Permalink
Scroll to Top Button addition
Browse files Browse the repository at this point in the history
  • Loading branch information
harsh07bharvada committed Oct 1, 2021
1 parent 54729b6 commit 4dd56b3
Show file tree
Hide file tree
Showing 5 changed files with 4,590 additions and 2,813 deletions.
1 change: 1 addition & 0 deletions src/assets/up.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 24 additions & 24 deletions src/components/Container.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { useState, useEffect } from 'react';
import Header from './Header.js'
import Search from './Search.js'
import Results from './Results.js'
import data from '../json/data.json'
import './Container.css'
import { useState, useEffect } from "react";
import Header from "./Header.js";
import Search from "./Search.js";
import Results from "./Results.js";
import ScrollToTopButton from "./ScrollToTopButton";
import data from "../json/data.json";
import "./Container.css";

function Container() {
const [emojiData, setEmojiData] = useState([]);
Expand All @@ -12,49 +13,48 @@ function Container() {

useEffect(() => {
setEmojiData(data);
}, [])
const onChange = val => {
}, []);
const onChange = (val) => {
setSearchQuery(val.toLowerCase());

let queryKeywords = val.toLowerCase().trim().split(" ");

let newEmojis = []
let newEmojis = [];

let queryLength = queryKeywords.length;

let queryLengthSum = 0;

console.log("\n\n\n NEW LINE \n\n\n")

console.log("\n\n\n NEW LINE \n\n\n");

if (val.toLowerCase() !== "") {
emojiData.forEach((item) => {
let removeDuplicates = [...new Set(item.keywords.trim().split(" "))];
queryLengthSum = 0;
queryKeywords.forEach((query) => {
removeDuplicates.forEach(keyword => {
removeDuplicates.forEach((keyword) => {
if (keyword.indexOf(query) >= 0) {
queryLengthSum++;
}
})
})
});
});

if (queryLength <= queryLengthSum) {
newEmojis.push(item)
newEmojis.push(item);
}

})
});
}

setNewEmojiData(newEmojis)
}
setNewEmojiData(newEmojis);
};
return (
<div className="container">
<Header />
<Search onChange={onChange} />
<Results emojiFiltered={searchQuery === "" ? emojiData : newEmojiData} />
<Search onChange={onChange} />{" "}
<Results emojiFiltered={searchQuery === "" ? emojiData : newEmojiData} />{" "}
<ScrollToTopButton />
</div>
)
);
}

export default Container
export default Container;
26 changes: 26 additions & 0 deletions src/components/ScrollToTopButton.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
.scroll-to-top-wrapper {
position: fixed;
bottom: 1rem;
right: 1rem;
}

.scroll-top-button {
padding: 1rem;
border-radius: 1rem;
background-color: white;
box-shadow: -5px -5px 15px #ffffff99, 5px 5px 15px #152b4711;
display: flex;
align-items: center;
justify-items: center;
cursor: pointer;
animation: fadeIn 0.5s;
}

@keyframes fadeIn {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
42 changes: 42 additions & 0 deletions src/components/ScrollToTopButton.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { useState, useEffect } from "react";
import "./ScrollToTopButton.css";
import { ReactComponent as UpIcon } from "../assets/up.svg";

const ScrollToTopButton = () => {
const [showScrollToTop, setScrollToTop] = useState(false);

const scrollToTop = () => {
window.scrollTo({
top: 0,
behavior: "smooth",
});
};

useEffect(() => {
const toggleVisibility = () => {
if (window.pageYOffset > 500) {
setScrollToTop(true);
} else {
setScrollToTop(false);
}
};

window.addEventListener("scroll", toggleVisibility);

return () => window.removeEventListener("scroll", toggleVisibility);
}, []);

return (
<div className="scroll-to-top-wrapper">
{showScrollToTop && (
<div onClick={scrollToTop}>
<div className="scroll-top-button">
<UpIcon />
</div>
</div>
)}
</div>
);
};

export default ScrollToTopButton;
Loading

0 comments on commit 4dd56b3

Please sign in to comment.