From b42afebd9d3f4003181eaf5efd680e6f068b4eed Mon Sep 17 00:00:00 2001 From: madeval Date: Mon, 17 Oct 2022 21:30:36 -0300 Subject: [PATCH 1/2] ref: remove unused imports --- src/layout/header/index.tsx | 103 +++++++++++++++++------------------- 1 file changed, 50 insertions(+), 53 deletions(-) diff --git a/src/layout/header/index.tsx b/src/layout/header/index.tsx index a2e592d..e6eebe4 100644 --- a/src/layout/header/index.tsx +++ b/src/layout/header/index.tsx @@ -1,73 +1,70 @@ -import React from "react"; import { Box, Flex, useColorModeValue, HStack, - Button, Container, Heading, Icon, useDisclosure, Collapse, - Text, -} from "@chakra-ui/react"; -import { ArrowSquareOut, MagnifyingGlass, Sticker, X } from "phosphor-react"; -import Theme from "./theme"; -import Tap from "@/animations/tap"; -import Mobile from "./mobile"; -import { Links } from "./links"; -import CustomLink from "@/common/link"; -import Categories from "./categories"; -import Search from "@/components/search"; -import CustomIconBtn from "@/common/iconBtn"; -import { useHotkeys } from "react-hotkeys-hook"; +} from '@chakra-ui/react' +import { ArrowSquareOut, MagnifyingGlass, Sticker, X } from 'phosphor-react' +import Theme from './theme' +import Tap from '@/animations/tap' +import Mobile from './mobile' +import { Links } from './links' +import CustomLink from '@/common/link' +import Categories from './categories' +import Search from '@/components/search' +import CustomIconBtn from '@/common/iconBtn' +import { useHotkeys } from 'react-hotkeys-hook' const Header = () => { - const bg = useColorModeValue("bg.light", "bg.dark"); - const { isOpen, onToggle } = useDisclosure(); - useHotkeys("ctrl+k", (e) => { - e.preventDefault(); - onToggle(); - }); + const bg = useColorModeValue('bg.light', 'bg.dark') + const { isOpen, onToggle } = useDisclosure() + useHotkeys('ctrl+k', (e) => { + e.preventDefault() + onToggle() + }) return ( <> - - - + + + - - - svgl + + + svgl - - + + {Links.map((link) => ( {link.title} {link.external ? ( - + ) : null} @@ -76,10 +73,10 @@ const Header = () => { : } @@ -87,37 +84,37 @@ const Header = () => { /> - + - + - + - + - ); -}; + ) +} -export default Header; +export default Header From 422af78d445fe5d6961b5d7659009d97d9a9849c Mon Sep 17 00:00:00 2001 From: madeval Date: Mon, 17 Oct 2022 22:35:49 -0300 Subject: [PATCH 2/2] feat: add autofocus when open search box --- src/components/search.tsx | 46 ++++++++++++++++++++++++++++++------ src/interfaces/components.ts | 4 ++++ src/layout/header/index.tsx | 2 +- 3 files changed, 44 insertions(+), 8 deletions(-) diff --git a/src/components/search.tsx b/src/components/search.tsx index 5fc0ab2..aa3c4d2 100644 --- a/src/components/search.tsx +++ b/src/components/search.tsx @@ -1,18 +1,27 @@ -import { useEffect, useState } from "react"; -import { Input, Text, Image, HStack, Box, Center, Spinner } from "@chakra-ui/react"; +import { useEffect, useRef, useState } from "react"; +import { + Input, + Text, + Image, + HStack, + Box, + Center, + Spinner, +} from "@chakra-ui/react"; import useDebounce from "@/hooks/useDebounce"; -import { SVGCardProps } from "@/interfaces/components"; +import { SearchProps, SVGCardProps } from "@/interfaces/components"; import CustomLink from "@/common/link"; import { getSvgByQuery } from "@/services"; import CustomIconBtn from "@/common/iconBtn"; import { Trash } from "phosphor-react"; import Tap from "@/animations/tap"; -const Search = () => { +const Search = ({ availableFocus = false }: SearchProps) => { const [search, setSearch] = useState(""); const [empty, setEmpty] = useState(false); const [results, setResults] = useState([]); const debouncedSearch = useDebounce(search, 500); + const searchRef = useRef(null); useEffect(() => { if (debouncedSearch) { @@ -27,6 +36,18 @@ const Search = () => { } }, [debouncedSearch]); + useEffect(() => { + const isFocusAvailable = availableFocus && searchRef.current; + + if (!isFocusAvailable) return; + + const timeoutId = setTimeout(() => { + searchRef.current?.focus(); + }, 100); + + return () => clearTimeout(timeoutId); + }, [availableFocus]); + const handleFilter = (e: React.ChangeEvent) => { setEmpty(false); setSearch(e.target.value); @@ -46,12 +67,23 @@ const Search = () => { placeholder="Search svgs..." value={search} onChange={handleFilter} + ref={searchRef} /> - {search && !empty && results.length === 0 && ()} - {search && empty && (No results found!)} + {search && !empty && results.length === 0 && ( + + + + )} + {search && empty && No results found!} {results && results.length > 0 && ( <> - + {results.map((item: SVGCardProps) => ( diff --git a/src/interfaces/components.ts b/src/interfaces/components.ts index 31ccdf7..b7c5ea8 100644 --- a/src/interfaces/components.ts +++ b/src/interfaces/components.ts @@ -42,3 +42,7 @@ export interface ErrorProps { title: string; description: string; } + +export interface SearchProps { + availableFocus?: boolean; +} diff --git a/src/layout/header/index.tsx b/src/layout/header/index.tsx index e6eebe4..e8552da 100644 --- a/src/layout/header/index.tsx +++ b/src/layout/header/index.tsx @@ -91,7 +91,7 @@ const Header = () => { - +