-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
432 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { ComponentType } from 'react'; | ||
import { BrowserRouter as Router, Switch, Route } from 'react-router-dom'; | ||
|
||
interface RouteData { | ||
path: string | ||
page: JSX.Element | ||
exact?: boolean | ||
} | ||
|
||
interface PageProps { | ||
Layout?: ComponentType<{ page: JSX.Element }> | ||
routes: (RouteData | null)[] | ||
} | ||
|
||
export default function BaseRouter({ Layout, routes }: PageProps) { | ||
const pages = routes.map(routeData => { | ||
if (!routeData) { | ||
return null; | ||
} | ||
const { path, page, exact } = routeData; | ||
if (Layout) { | ||
return ( | ||
<Route key={path} path={path} exact={exact}> | ||
<Layout page={page}/> | ||
</Route> | ||
); | ||
} | ||
return <Route key={path} path={path} exact={exact}>{page}</Route>; | ||
}); | ||
|
||
return ( | ||
<Router> | ||
<Switch> | ||
{pages} | ||
</Switch> | ||
</Router> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import Navigation from './components/Navigation'; | ||
import SearchBar from './components/SearchBar'; | ||
import { useAnswersState } from '@yext/answers-headless-react'; | ||
import { universalResultsConfig } from './universalResultsConfig'; | ||
|
||
const navLinks = [ | ||
{ | ||
to: '/', | ||
label: 'All' | ||
}, | ||
...Object.entries(universalResultsConfig).map(([verticalKey, config]) => ({ | ||
to: verticalKey, | ||
label: config.label | ||
})) | ||
] | ||
|
||
export default function Layout({ page }: { page: JSX.Element }) { | ||
const verticalKey = useAnswersState(state => state.vertical.key); | ||
return ( | ||
<div className='App'> | ||
<SearchBar | ||
placeholder='Search...' | ||
isVertical={!!verticalKey} | ||
/> | ||
<Navigation links={navLinks} /> | ||
{page} | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import classNames from 'classnames'; | ||
import { useCallback, useEffect, useLayoutEffect, useRef, useState } from 'react'; | ||
import { NavLink, useLocation } from 'react-router-dom'; | ||
import { ReactComponent as KebabIcon } from '../icons/kebab.svg'; | ||
import '../sass/Navigation.scss'; | ||
|
||
interface LinkData { | ||
to: string | ||
label: string | ||
} | ||
|
||
interface NavigationProps { | ||
links: LinkData[] | ||
} | ||
|
||
export default function Navigation({ links }: NavigationProps) { | ||
// Close the menu when clicking the document | ||
const [menuOpen, setMenuOpen] = useState<boolean>(false); | ||
const menuRef = useRef<HTMLButtonElement>(null); | ||
const handleDocumentClick = (e: MouseEvent) => { | ||
if (e.target !== menuRef.current) { | ||
setMenuOpen(false); | ||
} | ||
}; | ||
useLayoutEffect(() => { | ||
document.addEventListener('click', handleDocumentClick) | ||
return () => document.removeEventListener('click', handleDocumentClick); | ||
}, []); | ||
|
||
// Responsive tabs | ||
const [numOverflowLinks, setNumOverflowLinks] = useState<number>(0); | ||
const navigationRef = useRef<HTMLDivElement>(null); | ||
const handleResize = useCallback(() => { | ||
const navEl = navigationRef.current; | ||
if (!navEl) { | ||
return; | ||
} | ||
const isOverflowing = navEl.scrollWidth > navEl.offsetWidth; | ||
if (isOverflowing && numOverflowLinks < links.length) { | ||
setNumOverflowLinks(numOverflowLinks + 1); | ||
} | ||
}, [links.length, numOverflowLinks]) | ||
useLayoutEffect(handleResize, [handleResize]); | ||
useEffect(() => { | ||
let timeoutId: NodeJS.Timeout; | ||
function resizeListener() { | ||
clearTimeout(timeoutId); | ||
timeoutId = setTimeout(() => { | ||
setNumOverflowLinks(0); | ||
handleResize() | ||
}, 50) | ||
}; | ||
window.addEventListener('resize', resizeListener); | ||
return () => window.removeEventListener('resize', resizeListener); | ||
}, [handleResize]); | ||
|
||
const { search } = useLocation(); | ||
const visibleLinks = links.slice(0, links.length - numOverflowLinks); | ||
const overflowLinks = links.slice(-numOverflowLinks); | ||
const menuButtonClassNames = classNames('Navigation__menuButton', { | ||
'Navigation__menuButton--open': menuOpen | ||
}); | ||
return ( | ||
<nav className='Navigation' ref={navigationRef}> | ||
<div className='Navigation__links'> | ||
{visibleLinks.map(l => renderLink(l, search))} | ||
</div> | ||
{numOverflowLinks > 0 && | ||
<div className='Navigation__menuWrapper'> | ||
<button | ||
className={menuButtonClassNames} | ||
ref={menuRef} | ||
onClick={() => setMenuOpen(!menuOpen)} | ||
> | ||
<KebabIcon /> More | ||
</button> | ||
<div className='Navigation__menuLinks'> | ||
{menuOpen && overflowLinks.map(l => renderLink(l, search))} | ||
</div> | ||
</div> | ||
} | ||
</nav> | ||
) | ||
} | ||
|
||
function renderLink(linkData: LinkData, search: string) { | ||
const { to, label } = linkData; | ||
return ( | ||
<NavLink | ||
key={to} | ||
className='Navigation__link' | ||
activeClassName='Navigation__link--currentRoute' | ||
to={`${to}${search}`} | ||
exact={true} | ||
> | ||
{label} | ||
</NavLink> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.