├── src ├── App.css ├── assets │ ├── google-logo.png │ ├── profile-200x200.jpg │ ├── google-pagination-logo.png │ ├── mic.svg │ ├── profile-ring.svg │ └── image.svg ├── main.jsx ├── index.css ├── utils │ ├── api.js │ ├── ContextApi.jsx │ └── Constants.jsx ├── components │ ├── HomeHeader.jsx │ ├── ProfileIcon.jsx │ ├── SearchedImageItemTemplate.jsx │ ├── SearchedItemTemplate.jsx │ ├── Home.jsx │ ├── Footer.jsx │ ├── SearchInput.jsx │ ├── SearchResult.jsx │ ├── SearchResultHeader.jsx │ └── Pagination.jsx └── App.jsx ├── postcss.config.cjs ├── vite.config.js ├── tailwind.config.cjs ├── .gitignore ├── index.html ├── package.json ├── public └── vite.svg └── README.md /src/App.css: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/assets/google-logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShariqAnsari88/google-search-clone/HEAD/src/assets/google-logo.png -------------------------------------------------------------------------------- /postcss.config.cjs: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | plugins: { 3 | tailwindcss: {}, 4 | autoprefixer: {}, 5 | }, 6 | } 7 | -------------------------------------------------------------------------------- /src/assets/profile-200x200.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShariqAnsari88/google-search-clone/HEAD/src/assets/profile-200x200.jpg -------------------------------------------------------------------------------- /src/assets/google-pagination-logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ShariqAnsari88/google-search-clone/HEAD/src/assets/google-pagination-logo.png -------------------------------------------------------------------------------- /vite.config.js: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'vite' 2 | import react from '@vitejs/plugin-react' 3 | 4 | // https://vitejs.dev/config/ 5 | export default defineConfig({ 6 | plugins: [react()] 7 | }) 8 | -------------------------------------------------------------------------------- /src/main.jsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import ReactDOM from "react-dom/client"; 3 | import App from "./App"; 4 | import "./index.css"; 5 | 6 | ReactDOM.createRoot(document.getElementById("root")).render(); 7 | -------------------------------------------------------------------------------- /src/index.css: -------------------------------------------------------------------------------- 1 | @tailwind base; 2 | @tailwind components; 3 | @tailwind utilities; 4 | 5 | * { 6 | margin: 0; 7 | padding: 0; 8 | box-sizing: border-box; 9 | } 10 | 11 | :root { 12 | font-family: Arial, sans-serif; 13 | font-size: 16px; 14 | line-height: 24px; 15 | font-weight: 400; 16 | } 17 | -------------------------------------------------------------------------------- /tailwind.config.cjs: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | content: ["./index.html", "./src/**/*.{js,ts,jsx,tsx}"], 3 | theme: { 4 | extend: { 5 | boxShadow: { 6 | c: "0 1px 6px rgb(32 33 36 / 28%)", 7 | c2: "0 1px 1px rgb(0 0 0 / 10%)", 8 | }, 9 | }, 10 | }, 11 | plugins: [], 12 | }; 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | pnpm-debug.log* 8 | lerna-debug.log* 9 | 10 | node_modules 11 | dist 12 | dist-ssr 13 | *.local 14 | 15 | # Editor directories and files 16 | .vscode/* 17 | !.vscode/extensions.json 18 | .idea 19 | .DS_Store 20 | *.suo 21 | *.ntvs* 22 | *.njsproj 23 | *.sln 24 | *.sw? 25 | -------------------------------------------------------------------------------- /src/utils/api.js: -------------------------------------------------------------------------------- 1 | import axios from "axios"; 2 | 3 | const BASE_URL = "https://www.googleapis.com/customsearch/v1"; 4 | 5 | const params = { 6 | key: "YOUR_API_KEY", 7 | cx: "YOUR_CX_KEY", 8 | }; 9 | 10 | export const fetchDataFromApi = async (payload) => { 11 | const { data } = await axios.get(BASE_URL, { 12 | params: { ...params, ...payload }, 13 | }); 14 | return data; 15 | }; 16 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Google Search - JS Dev 8 | 9 | 10 |
11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /src/utils/ContextApi.jsx: -------------------------------------------------------------------------------- 1 | import React, { createContext, useState } from "react"; 2 | 3 | export const Context = createContext(); 4 | 5 | export const AppContext = (props) => { 6 | const [imageSearch, setImageSearch] = useState(false); 7 | 8 | return ( 9 | 15 | {props.children} 16 | 17 | ); 18 | }; 19 | -------------------------------------------------------------------------------- /src/assets/mic.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "my-project", 3 | "private": true, 4 | "version": "0.0.0", 5 | "type": "module", 6 | "scripts": { 7 | "dev": "vite", 8 | "build": "vite build", 9 | "preview": "vite preview" 10 | }, 11 | "dependencies": { 12 | "axios": "^1.2.0", 13 | "react": "^18.2.0", 14 | "react-dom": "^18.2.0", 15 | "react-icons": "^4.7.1", 16 | "react-router-dom": "^6.4.4" 17 | }, 18 | "devDependencies": { 19 | "@types/react": "^18.0.24", 20 | "@types/react-dom": "^18.0.8", 21 | "@vitejs/plugin-react": "^2.2.0", 22 | "autoprefixer": "^10.4.13", 23 | "postcss": "^8.4.19", 24 | "tailwindcss": "^3.2.4", 25 | "vite": "^3.2.3" 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/components/HomeHeader.jsx: -------------------------------------------------------------------------------- 1 | import ProfileIcon from "./ProfileIcon"; 2 | 3 | const HomeHeader = () => { 4 | return ( 5 |
6 |
7 | 8 | Gmail 9 | 10 | 11 | Images 12 | 13 |
14 | 15 | 16 |
17 | ); 18 | }; 19 | 20 | export default HomeHeader; 21 | -------------------------------------------------------------------------------- /src/App.jsx: -------------------------------------------------------------------------------- 1 | import { BrowserRouter, Routes, Route } from "react-router-dom"; 2 | 3 | import Home from "./components/Home"; 4 | import SearchResult from "./components/SearchResult"; 5 | import { AppContext } from "./utils/ContextApi"; 6 | 7 | function App() { 8 | return ( 9 | 10 | 11 | 12 | } /> 13 | } 17 | /> 18 | 19 | 20 | 21 | ); 22 | } 23 | 24 | export default App; 25 | -------------------------------------------------------------------------------- /src/assets/profile-ring.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/components/ProfileIcon.jsx: -------------------------------------------------------------------------------- 1 | import { TbGridDots } from "react-icons/tb"; 2 | 3 | import Profile from "../assets/profile-200x200.jpg"; 4 | import ProfileRing from "../assets/profile-ring.svg"; 5 | 6 | const ProfileIcon = () => { 7 | return ( 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | ); 20 | }; 21 | 22 | export default ProfileIcon; 23 | -------------------------------------------------------------------------------- /src/components/SearchedImageItemTemplate.jsx: -------------------------------------------------------------------------------- 1 | const SearchedImageItemTemplate = ({ data }) => { 2 | return ( 3 |
window.open(data.image.contextLink, "_blank")} 6 | > 7 |
8 | {data.title} 13 |
14 |
15 | {data.displayLink} 16 |
17 |
18 | {data.title} 19 |
20 |
21 | ); 22 | }; 23 | 24 | export default SearchedImageItemTemplate; 25 | -------------------------------------------------------------------------------- /src/components/SearchedItemTemplate.jsx: -------------------------------------------------------------------------------- 1 | const SearchedItemTemplate = ({ data }) => { 2 | function createMarkup(html) { 3 | return { __html: html }; 4 | } 5 | 6 | return ( 7 |
8 |
window.open(data.link, "_blank")} 11 | > 12 |
13 | {data.formattedUrl} 14 |
15 |
16 | {data.title} 17 |
18 |
19 |
23 |
24 | ); 25 | }; 26 | 27 | export default SearchedItemTemplate; 28 | -------------------------------------------------------------------------------- /src/utils/Constants.jsx: -------------------------------------------------------------------------------- 1 | import { GoSearch } from "react-icons/go"; 2 | import { BsImage } from "react-icons/bs"; 3 | import { BiNews } from "react-icons/bi"; 4 | import { RiVideoLine } from "react-icons/ri"; 5 | import { SlTag } from "react-icons/sl"; 6 | 7 | export const menu = [ 8 | { name: "All", icon: }, 9 | { name: "Images", icon: }, 10 | { name: "News", icon: }, 11 | { name: "Videos", icon: }, 12 | { name: "Shopping", icon: }, 13 | ]; 14 | 15 | export const quickLinks = [ 16 | "About", 17 | "Advertising", 18 | "Business", 19 | "How Search works", 20 | ]; 21 | export const settingMenu = ["Privacy", "Terms", "Settings"]; 22 | 23 | export const pagination = [ 24 | { page: 1, startIndex: 1 }, 25 | { page: 2, startIndex: 11 }, 26 | { page: 3, startIndex: 21 }, 27 | { page: 4, startIndex: 31 }, 28 | { page: 5, startIndex: 41 }, 29 | { page: 6, startIndex: 51 }, 30 | { page: 7, startIndex: 61 }, 31 | { page: 8, startIndex: 71 }, 32 | { page: 9, startIndex: 81 }, 33 | { page: 10, startIndex: 91 }, 34 | ]; 35 | -------------------------------------------------------------------------------- /src/components/Home.jsx: -------------------------------------------------------------------------------- 1 | import Logo from "../assets/google-logo.png"; 2 | import HomeHeader from "./HomeHeader"; 3 | import SearchInput from "./SearchInput"; 4 | import Footer from "./Footer"; 5 | const Home = () => { 6 | return ( 7 |
8 | 9 |
10 |
11 | Logo 16 | 17 |
18 | 21 | 24 |
25 |
26 |
27 |
28 |
29 | ); 30 | }; 31 | 32 | export default Home; 33 | -------------------------------------------------------------------------------- /public/vite.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/assets/image.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | 7 | 8 | 12 | 15 | 17 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /src/components/Footer.jsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | 3 | import { quickLinks, settingMenu } from "../utils/constants"; 4 | 5 | const Footer = () => { 6 | return ( 7 |
8 |
9 | 10 | India 11 | 12 |
13 | 14 |
15 |
16 | {quickLinks.map((menu, index) => ( 17 | 21 | {menu} 22 | 23 | ))} 24 |
25 | 26 |
27 | {settingMenu.map((menu, index) => ( 28 | 32 | {menu} 33 | 34 | ))} 35 |
36 |
37 |
38 | ); 39 | }; 40 | 41 | export default Footer; 42 | -------------------------------------------------------------------------------- /src/components/SearchInput.jsx: -------------------------------------------------------------------------------- 1 | import React, { useState } from "react"; 2 | import { AiOutlineSearch } from "react-icons/ai"; 3 | import { IoMdClose } from "react-icons/io"; 4 | import { useNavigate, useParams } from "react-router-dom"; 5 | 6 | import MicIcon from "../assets/mic.svg"; 7 | import ImageIcon from "../assets/image.svg"; 8 | 9 | const SearchInput = () => { 10 | const { query } = useParams(); 11 | const [searchQuery, setSearchQuery] = useState(query || ""); 12 | const navigate = useNavigate(); 13 | 14 | const searchQueryHandler = (event) => { 15 | if (event?.key === "Enter" && searchQuery?.length > 0) { 16 | navigate(`/${searchQuery}/${1}`); 17 | } 18 | }; 19 | return ( 20 | 50 | ); 51 | }; 52 | 53 | export default SearchInput; 54 | -------------------------------------------------------------------------------- /src/components/SearchResult.jsx: -------------------------------------------------------------------------------- 1 | import React, { useState, useEffect, useContext } from "react"; 2 | import { useParams } from "react-router-dom"; 3 | 4 | import { fetchDataFromApi } from "../utils/api"; 5 | import SearchResultHeader from "./SearchResultHeader"; 6 | import Footer from "./Footer"; 7 | import SearchedItemTemplate from "./SearchedItemTemplate"; 8 | import SearchedImageItemTemplate from "./SearchedImageItemTemplate"; 9 | import Pagination from "./Pagination"; 10 | import { Context } from "../utils/ContextApi"; 11 | 12 | const SearchResult = () => { 13 | const [result, setResult] = useState(); 14 | const { query, startIndex } = useParams(); 15 | const { imageSearch } = useContext(Context); 16 | 17 | useEffect(() => { 18 | fetchSearchResults(); 19 | window.scrollTo(0, 0); 20 | }, [query, startIndex, imageSearch]); 21 | 22 | const fetchSearchResults = () => { 23 | let payload = { q: query, start: startIndex }; 24 | if (imageSearch) { 25 | payload.searchType = "image"; 26 | } 27 | fetchDataFromApi(payload).then((res) => { 28 | console.log(res); 29 | setResult(res); 30 | }); 31 | }; 32 | 33 | if (!result) return; 34 | const { items, queries, searchInformation } = result; 35 | 36 | return ( 37 |
38 | 39 |
40 |
{`About ${searchInformation.formattedTotalResults} results in (${searchInformation.formattedSearchTime})`}
41 | {!imageSearch ? ( 42 | <> 43 | {items.map((item, index) => ( 44 | 45 | ))} 46 | 47 | ) : ( 48 |
49 | {items.map((item, index) => ( 50 | 54 | ))} 55 |
56 | )} 57 | 58 |
59 |
60 |
61 | ); 62 | }; 63 | 64 | export default SearchResult; 65 | -------------------------------------------------------------------------------- /src/components/SearchResultHeader.jsx: -------------------------------------------------------------------------------- 1 | import { Link } from "react-router-dom"; 2 | import { useContext, useState, useEffect } from "react"; 3 | import { GoSearch } from "react-icons/go"; 4 | import { BsImage } from "react-icons/bs"; 5 | import { BiNews } from "react-icons/bi"; 6 | import { RiVideoLine } from "react-icons/ri"; 7 | import { SlTag } from "react-icons/sl"; 8 | 9 | import Logo from "../assets/google-logo.png"; 10 | import SearchInput from "./SearchInput"; 11 | import ProfileIcon from "./ProfileIcon"; 12 | import { Context } from "../utils/ContextApi"; 13 | import { menu } from "../utils/Constants"; 14 | 15 | const SearchResultHeader = () => { 16 | const [selectedMenu, setSelectedMenu] = useState("All"); 17 | const { setImageSearch } = useContext(Context); 18 | 19 | useEffect(() => { 20 | return () => setImageSearch(false); 21 | }, []); 22 | 23 | const clickHandler = (menuItem) => { 24 | let isTypeImage = menuItem.name === "Images"; 25 | setImageSearch(isTypeImage ? true : false); 26 | setSelectedMenu(menuItem.name); 27 | }; 28 | 29 | return ( 30 |
31 |
32 |
33 | 34 | Logo 39 | 40 | 41 |
42 |
43 | 44 |
45 |
46 | 47 |
48 | {menu.map((menu, index) => ( 49 | clickHandler(menu)} 55 | > 56 | 57 | {menu.icon} 58 | 59 | {menu.name} 60 | {selectedMenu === menu.name && ( 61 | 62 | )} 63 | 64 | ))} 65 |
66 |
67 | ); 68 | }; 69 | 70 | export default SearchResultHeader; 71 | -------------------------------------------------------------------------------- /src/components/Pagination.jsx: -------------------------------------------------------------------------------- 1 | import { useState, useEffect } from "react"; 2 | import { FiChevronRight, FiChevronLeft } from "react-icons/fi"; 3 | import { useParams, useNavigate } from "react-router-dom"; 4 | 5 | import Logo from "../assets/google-pagination-logo.png"; 6 | import { pagination } from "../utils/Constants"; 7 | 8 | const Pagination = ({ queries }) => { 9 | const { query } = useParams(); 10 | const [page, setPage] = useState(pagination[0].startIndex); 11 | const navigate = useNavigate(); 12 | 13 | useEffect(() => { 14 | setPage(pagination[0].startIndex); 15 | }, [query]); 16 | 17 | const paginationClickHandler = (startIndex) => { 18 | setPage(startIndex); 19 | navigate(`/${query}/${startIndex}`); 20 | }; 21 | 22 | return ( 23 |
24 |
25 | {queries.previousPage && ( 26 |
29 | paginationClickHandler( 30 | queries.previousPage[0].startIndex 31 | ) 32 | } 33 | > 34 | 35 | 36 | Prev 37 | 38 |
39 | )} 40 | 41 | {queries.nextPage && ( 42 |
45 | paginationClickHandler( 46 | queries.nextPage[0].startIndex 47 | ) 48 | } 49 | > 50 | 51 | 52 | Next 53 | 54 |
55 | )} 56 |
57 |
58 | {pagination.map((p) => ( 59 | paginationClickHandler(p.startIndex)} 62 | className={`cursor-pointer ${ 63 | page === p.startIndex ? "text-black" : "" 64 | }`} 65 | > 66 | {p.page} 67 | 68 | ))} 69 |
70 |
71 | ); 72 | }; 73 | 74 | export default Pagination; 75 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Getting Started with Create React App 2 | 3 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 4 | 5 | ## Available Scripts 6 | 7 | In the project directory, you can run: 8 | 9 | ### `npm start` 10 | 11 | Runs the app in the development mode.\ 12 | Open [http://localhost:3000](http://localhost:3000) to view it in your browser. 13 | 14 | The page will reload when you make changes.\ 15 | You may also see any lint errors in the console. 16 | 17 | ### `npm test` 18 | 19 | Launches the test runner in the interactive watch mode.\ 20 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. 21 | 22 | ### `npm run build` 23 | 24 | Builds the app for production to the `build` folder.\ 25 | It correctly bundles React in production mode and optimizes the build for the best performance. 26 | 27 | The build is minified and the filenames include the hashes.\ 28 | Your app is ready to be deployed! 29 | 30 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. 31 | 32 | ### `npm run eject` 33 | 34 | **Note: this is a one-way operation. Once you `eject`, you can't go back!** 35 | 36 | If you aren't satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. 37 | 38 | Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own. 39 | 40 | You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it. 41 | 42 | ## Learn More 43 | 44 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). 45 | 46 | To learn React, check out the [React documentation](https://reactjs.org/). 47 | 48 | ### Code Splitting 49 | 50 | This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting) 51 | 52 | ### Analyzing the Bundle Size 53 | 54 | This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size) 55 | 56 | ### Making a Progressive Web App 57 | 58 | This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app) 59 | 60 | ### Advanced Configuration 61 | 62 | This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration) 63 | 64 | ### Deployment 65 | 66 | This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment) 67 | 68 | ### `npm run build` fails to minify 69 | 70 | This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify) 71 | --------------------------------------------------------------------------------