Skip to content

Commit

Permalink
fix: Close search form after submitting (#23)
Browse files Browse the repository at this point in the history
* fix: Improve search experience (#18)

* fix: Improve search experience

* Search on icon click

* Add margin top to header when items in bag

* Use cn

* fix: Close search form after submitting

* Make onSubmit optional
  • Loading branch information
soniaklimas authored Oct 3, 2024
1 parent ca27f5b commit a1f1afd
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
13 changes: 8 additions & 5 deletions apps/storefront/src/components/header/mobile-search.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use client";

import { Search } from "lucide-react";
import { useParams, usePathname } from "next/navigation";
import { usePathname } from "next/navigation";
import { useTranslations } from "next-intl";
import { useEffect, useState } from "react";

Expand All @@ -14,12 +14,15 @@ export const MobileSearch = () => {
const t = useTranslations("search");

const pathname = usePathname();
const params = useParams();
const [isOpen, setIsOpen] = useState(false);

const handleCloseSheet = () => {
setIsOpen(false);
};

useEffect(() => {
setIsOpen(false);
}, [pathname, params]);
}, [pathname]);

return (
<>
Expand All @@ -38,8 +41,8 @@ export const MobileSearch = () => {
aria-label={t("search-label")}
modal={true}
>
<SheetContent side="top">
<SearchForm />
<SheetContent side="top" closeClassName="right-2 top-2">
<SearchForm onSubmit={handleCloseSheet} />
</SheetContent>
</Sheet>
</>
Expand Down
10 changes: 9 additions & 1 deletion apps/storefront/src/components/header/mobile-side-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { Sheet, SheetContent } from "@nimara/ui/components/sheet";

import { MobileNavigation } from "@/components/mobile-navigation";
import { paths } from "@/lib/paths";
import { cn } from "@/lib/utils";
import { useCurrentRegion } from "@/regions/client";

import { LocaleSwitch } from "../locale-switch";
Expand Down Expand Up @@ -57,7 +58,14 @@ export const MobileSideMenu = ({
<SheetContent side="left" className="w-screen sm:w-1/2">
<div className="relative flex h-full flex-col justify-between gap-4 overflow-auto">
<div className="flex h-full flex-col">
<div className="flex w-full items-center justify-between gap-4 pb-4 sm:hidden">
<div
className={cn(
"flex w-full items-center justify-between gap-4 pb-4 sm:hidden",
{
"mt-2": checkoutLinesCount,
},
)}
>
<Logo />
<div className="flex justify-end gap-1 align-middle">
<MobileSearch />
Expand Down
17 changes: 15 additions & 2 deletions apps/storefront/src/components/header/search-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const initialSearchState: SearchState = {
showOptions: false,
};

export const SearchForm = () => {
export const SearchForm = ({ onSubmit }: { onSubmit?: () => void }) => {
const ts = useTranslations("search");
const tc = useTranslations("common");

Expand Down Expand Up @@ -85,6 +85,9 @@ export const SearchForm = () => {
if (event.code === keyboardCodes.Enter) {
event.preventDefault();
resetSearchState();
if (onSubmit) {
onSubmit();
}

// Handle query search
if (isNoOptionHighlighted || isLastOptionHighlighted) {
Expand Down Expand Up @@ -127,6 +130,15 @@ export const SearchForm = () => {
}
};

const handleSearchIconClick = () => {
if (isNoOptionHighlighted || isLastOptionHighlighted) {
router.push(paths.search.asPath({ query: { q: inputValue } }));
resetSearchState();

return;
}
};

useClickAnyWhere(() =>
setSearchState((prevState) => ({ ...prevState, showOptions: false })),
);
Expand Down Expand Up @@ -173,7 +185,8 @@ export const SearchForm = () => {
size="icon"
type="submit"
variant="outline"
onClick={() => resetSearchState()}
className="cursor-pointer"
onClick={handleSearchIconClick}
>
{isLoading ? (
<Spinner size={16} />
Expand Down
13 changes: 10 additions & 3 deletions packages/ui/src/components/sheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@ const sheetVariants = cva(

interface SheetContentProps
extends React.ComponentPropsWithoutRef<typeof SheetPrimitive.Content>,
VariantProps<typeof sheetVariants> {}
VariantProps<typeof sheetVariants> {
closeClassName?: string;
}

const SheetContent = React.forwardRef<
React.ElementRef<typeof SheetPrimitive.Content>,
SheetContentProps
>(({ side = "right", className, children, ...props }, ref) => (
>(({ side = "right", className, closeClassName, children, ...props }, ref) => (
<SheetPortal>
<SheetOverlay />
<SheetPrimitive.Content
Expand All @@ -68,7 +70,12 @@ const SheetContent = React.forwardRef<
{...props}
>
{children}
<SheetPrimitive.Close className="ring-offset-background focus:ring-ring data-[state=open]:bg-secondary absolute right-4 top-4 rounded-sm opacity-70 transition-opacity hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-offset-2 disabled:pointer-events-none">
<SheetPrimitive.Close
className={cn(
"ring-offset-background focus:ring-ring data-[state=open]:bg-secondary absolute right-4 top-4 rounded-sm opacity-70 transition-opacity hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-offset-2 disabled:pointer-events-none",
closeClassName,
)}
>
<X className="h-4 w-4" />
<span className="sr-only">Close</span>
</SheetPrimitive.Close>
Expand Down

0 comments on commit a1f1afd

Please sign in to comment.