Skip to content

Commit

Permalink
Merge pull request #81 from Weaverse/stephen-dev
Browse files Browse the repository at this point in the history
Update drawer and search for two type
  • Loading branch information
hta218 authored Jul 18, 2024
2 parents b316fe1 + 133c8e9 commit 53431e0
Show file tree
Hide file tree
Showing 9 changed files with 277 additions and 89 deletions.
16 changes: 10 additions & 6 deletions app/components/Drawer.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {cn} from '@/lib/utils';
import {Dialog, Transition} from '@headlessui/react';
import {useLocation} from '@remix-run/react';
import {Fragment, useEffect, useState} from 'react';
import {IconArrowLeft, IconClose} from './Icon';
import {Heading} from './Text';
import { useLocation } from '@remix-run/react';

/**
* Drawer component that opens on user click.
Expand Down Expand Up @@ -72,17 +72,21 @@ export function Drawer({
'transform text-left align-middle shadow-xl transition-all',
openFrom === 'left'
? 'h-screen-dynamic w-screen max-w-96 bg-background-subtle-1'
: 'h-screen-dynamic w-screen max-w-96',
: openFrom === 'top'
? 'h-fit w-screen bg-background-subtle-1'
: 'h-screen-dynamic w-screen max-w-96',
isForm === 'cart'
? 'bg-background-basic'
: 'bg-background-subtle-1',
)}
>
<header
className={cn(
'h-nav sticky top-0 flex items-center px-6 py-5',
'sticky top-0 flex h-nav items-center px-6 py-5',
heading ? 'justify-between' : 'justify-end',
openFrom === 'left' && !isBackMenu ? 'flex-row-reverse' : ''
openFrom === 'left' || openFrom === 'top' && !isBackMenu
? 'flex-row-reverse'
: '',
)}
>
{isBackMenu && (
Expand All @@ -93,7 +97,7 @@ export function Drawer({
data-test="close-cart"
>
<IconArrowLeft
viewBox='0 0 32 32'
viewBox="0 0 32 32"
className="h-8 w-8 opacity-50"
aria-label="Close panel"
/>
Expand Down Expand Up @@ -134,7 +138,7 @@ Drawer.Title = Dialog.Title;

export function useDrawer(openDefault = false) {
const [isOpen, setIsOpen] = useState(openDefault);
let { pathname } = useLocation();
let {pathname} = useLocation();
useEffect(() => {
if (isOpen) {
closeDrawer();
Expand Down
29 changes: 3 additions & 26 deletions app/components/Header/MenuDrawerHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Await, useLocation} from '@remix-run/react';
import {Await} from '@remix-run/react';
import {CartForm} from '@shopify/hydrogen';
import {useThemeSettings} from '@weaverse/hydrogen';
import {useCartFetchers} from '~/hooks/useCartFetchers';
Expand All @@ -15,8 +15,8 @@ import {IconAccount, IconLogin, IconSearch} from '../Icon';
import {Link} from '../Link';
import {Logo} from '../Logo';
import {DrawerMenu} from './menu/DrawerMenu';
import {PredictiveSearch} from '../predictive-search/PredictiveSearch';
import {CartCount} from './CartCount';
import { SearchToggle } from './SearchToggle';

export function UseMenuDrawerHeader({
header,
Expand Down Expand Up @@ -112,7 +112,7 @@ function HeaderMenuDrawer({menu}: {menu?: EnhancedMenu | null | undefined}) {
<button className="text-left" onClick={openDrawer}>
<span></span>
</button>
<SearchToggle />
<SearchToggle isOpenDrawerHearder={true} />
<Drawer
open={showMenu}
onClose={closeDrawer}
Expand Down Expand Up @@ -155,26 +155,3 @@ function AccountLink({className}: {className?: string}) {
</Suspense>
);
}

function SearchToggle() {
const {isOpen, closeDrawer, openDrawer} = useDrawer();
return (
<>
<button
onClick={openDrawer}
className="relative flex h-6 w-6 items-center justify-center focus:ring-primary/5"
>
<IconSearch className="h-6 w-6 !font-extralight" />
</button>
<Drawer
open={isOpen}
onClose={closeDrawer}
openFrom="left"
heading="Search"
isForm="search"
>
<PredictiveSearch isOpen={isOpen} />
</Drawer>
</>
);
}
26 changes: 2 additions & 24 deletions app/components/Header/MenuMegaHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Await, useLocation} from '@remix-run/react';
import {Await} from '@remix-run/react';
import {CartForm} from '@shopify/hydrogen';
import {useThemeSettings} from '@weaverse/hydrogen';
import {useCartFetchers} from '~/hooks/useCartFetchers';
Expand All @@ -15,8 +15,8 @@ import {IconAccount, IconLogin, IconSearch} from '../Icon';
import {Link} from '../Link';
import {Logo} from '../Logo';
import {MegaMenu} from './menu/MegaMenu';
import {PredictiveSearch} from '../predictive-search/PredictiveSearch';
import {CartCount} from './CartCount';
import { SearchToggle } from './SearchToggle';

export function UseMenuMegaHeader({
header,
Expand Down Expand Up @@ -126,25 +126,3 @@ function AccountLink({className}: {className?: string}) {
);
}

function SearchToggle() {
const {isOpen, closeDrawer, openDrawer} = useDrawer();
return (
<>
<button
onClick={openDrawer}
className="relative flex h-6 w-6 items-center justify-center focus:ring-primary/5"
>
<IconSearch className="h-6 w-6 !font-extralight" />
</button>
<Drawer
open={isOpen}
onClose={closeDrawer}
openFrom="right"
heading="Search"
isForm="search"
>
<PredictiveSearch isOpen={isOpen} />
</Drawer>
</>
);
}
58 changes: 58 additions & 0 deletions app/components/Header/SearchToggle.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import {useThemeSettings} from '@weaverse/hydrogen';
import {Drawer, useDrawer} from '../Drawer';
import {IconSearch} from '../Icon';
import { SearchTypeHeader } from '../predictive-search/PredictiveSearch/SearchHeader/SearchTypeHeader';
import { SearchTypeDrawer } from '../predictive-search/PredictiveSearch/SearchDrawer/SearchTypeDrawer';
import { useEffect, useState } from 'react';

type TypeOpenFrom = 'top' | 'right' | 'left';

export function SearchToggle({isOpenDrawerHearder}: {isOpenDrawerHearder?: boolean}) {
const {isOpen, closeDrawer, openDrawer} = useDrawer();
let settings = useThemeSettings();
const [searchType, setSearchType] = useState(settings?.searchType);
const [openFrom, setOpenFrom] = useState<TypeOpenFrom>(
searchType === 'headerSearch' ? 'top' : 'right'
);

useEffect(() => {
const handleResize = () => {
if (window.innerWidth <= 1280) {
setSearchType('drawerSearch');
setOpenFrom('left');
} else {
setSearchType(settings?.searchType);
if (settings?.searchType === 'drawerSearch' && isOpenDrawerHearder) {
setOpenFrom('left');
} else {
setOpenFrom(settings?.searchType === 'headerSearch' ? 'top' : 'right');
}
}
};

window.addEventListener('resize', handleResize);
handleResize();

return () => window.removeEventListener('resize', handleResize);
}, [settings?.searchType, isOpenDrawerHearder]);
return (
<>
<button
onClick={openDrawer}
className="relative flex h-6 w-6 items-center justify-center focus:ring-primary/5"
>
<IconSearch className="h-6 w-6 !font-extralight" />
</button>
<Drawer
open={isOpen}
onClose={closeDrawer}
openFrom={openFrom}
heading="Search"
isForm="search"
>
{searchType === 'headerSearch' && <SearchTypeHeader isOpen={isOpen} />}
{searchType === 'drawerSearch' && <SearchTypeDrawer isOpen={isOpen} />}
</Drawer>
</>
);
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {IconSearch} from '../Icon';
import {Input} from '../Input';
import {PredictiveSearchResults} from './PredictiveSearchResults';
import {PredictiveSearchForm} from './SearchForm';
import {IconSearch} from '../../../Icon';
import {Input} from '../../../Input';
import {PredictiveSearchForm} from '../../SearchForm';
import { SearchTypeDrawerResults } from './SearchTypeDrawerResults';

interface PredictiveSearchProps {
// Predictive search props
isOpen?: boolean;
}

export function PredictiveSearch(props: PredictiveSearchProps) {
export function SearchTypeDrawer(props: PredictiveSearchProps) {
let {isOpen} = props;
return (
<div className="border-t border-bar-subtle">
Expand Down Expand Up @@ -36,7 +36,7 @@ export function PredictiveSearch(props: PredictiveSearchProps) {
</div>
)}
</PredictiveSearchForm>
{isOpen && <PredictiveSearchResults />}
{isOpen && <SearchTypeDrawerResults />}
</div>
);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {Link} from '@remix-run/react';
import {PredictiveSearchResult} from './PredictiveSearchResult';
import {usePredictiveSearch} from './usePredictiveSearch';
import {PredictiveSearchResult} from '../../PredictiveSearchResult';
import {usePredictiveSearch} from '../../usePredictiveSearch';

export function PredictiveSearchResults() {
export function SearchTypeDrawerResults() {
const {results, totalResults, searchTerm, searchInputRef} =
usePredictiveSearch();

Expand Down Expand Up @@ -70,29 +70,30 @@ export function PredictiveSearchResults() {
type={products.type}
/>
{/* view all results /search?q=term */}

</div>
)}
{pages && (
<div>
<PredictiveSearchResult
goToSearchResult={goToSearchResult}
items={pages.items}
key={pages.type}
searchTerm={searchTerm}
type={pages.type}
/>
</div>
)}
<div>
<PredictiveSearchResult
goToSearchResult={goToSearchResult}
items={pages.items}
key={pages.type}
searchTerm={searchTerm}
type={pages.type}
/>
</div>
)}
{searchTerm.current && (
<Link
onClick={goToSearchResult}
to={`/search?q=${searchTerm.current}`}
className='flex justify-center'
>
<p className="inline-flex items-center font-normal justify-center rounded-md h-[50px] px-5 py-3 bg-primary text-primary-foreground border border-primary hover:bg-background hover:text-foreground hover:border-bar">Show All Results ({totalResultsCount})</p>
</Link>
)}
<Link
onClick={goToSearchResult}
to={`/search?q=${searchTerm.current}`}
className="flex justify-center"
>
<p className="inline-flex h-[50px] items-center justify-center rounded-md border border-primary bg-primary px-5 py-3 font-normal text-primary-foreground hover:border-bar hover:bg-background hover:text-foreground">
Show All Results ({totalResultsCount})
</p>
</Link>
)}
</div>
</div>
);
Expand All @@ -108,10 +109,8 @@ function NoPredictiveSearchResults({
}
return (
<div className="w-full p-6">
<p className='border-b border-bar-subtle pb-3'>
NO RESULTS
</p>
<p className='pt-5'>
<p className="border-b border-bar-subtle pb-3">NO RESULTS</p>
<p className="pt-5">
No results found for <q>{searchTerm.current}</q>
</p>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {IconSearch} from '~/components/Icon';
import {Input} from '~/components/Input';
import {PredictiveSearchForm} from '../../SearchForm';
import { SearchTypeHeaderResults } from './SearchTypeHeaderResults';

interface PredictiveSearchProps {
// Predictive search props
isOpen?: boolean;
}

export function SearchTypeHeader(props: PredictiveSearchProps) {
let {isOpen} = props;
return (
<div className="relative border-t border-bar-subtle">
<PredictiveSearchForm>
{({fetchResults, inputRef}) => (
<div className="mx-auto w-full max-w-[560px] p-6">
<Input
name="q"
onChange={fetchResults}
onFocus={fetchResults}
onClear={fetchResults}
placeholder="Enter a keyword"
ref={inputRef}
className="rounded border-2"
prefixElement={
<button type="submit" className="cursor-pointer">
<IconSearch
className="h-6 w-6 opacity-55"
viewBox="0 0 24 24"
/>
</button>
}
autoFocus={true}
/>
</div>
)}
</PredictiveSearchForm>
{isOpen && <SearchTypeHeaderResults />}
</div>
);
}
Loading

0 comments on commit 53431e0

Please sign in to comment.