Skip to content

Commit

Permalink
fix: search ability in mobile, no results found, right chevron
Browse files Browse the repository at this point in the history
  • Loading branch information
Cayla Hamann committed Jun 25, 2020
1 parent 6621851 commit cb683ac
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
21 changes: 19 additions & 2 deletions src/components/MobileHeader.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import React from 'react';
import React, { useState } from 'react';
import PropTypes from 'prop-types';
import { Link } from 'gatsby';
import cx from 'classnames';
import Logo from './Logo';
import Navigation from './Navigation';
import HamburgerMenu from './HamburgerMenu';
import SearchInput from './SearchInput';
import styles from './MobileHeader.module.scss';

const MobileHeader = ({ className, isOpen, toggle }) => {
const [searchTerm, setSearchTerm] = useState('');

return (
<header className={cx(styles.container, className)}>
<div className={styles.menuBar}>
Expand All @@ -22,7 +25,21 @@ const MobileHeader = ({ className, isOpen, toggle }) => {
/>
</div>

{isOpen && <Navigation className={styles.navigation} />}
{isOpen && (
<>
<SearchInput
className={styles.searchInput}
placeholder="Search developer docs"
onChange={(e) => setSearchTerm(e.target.value)}
value={searchTerm}
/>
<Navigation
mobile
searchTerm={searchTerm}
className={styles.navigation}
/>
</>
)}
</header>
);
};
Expand Down
10 changes: 8 additions & 2 deletions src/components/Navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ const filterPageNames = (pages, searchTerm, parent = []) => {
}
})
),
];
].filter((el) => el !== undefined);
};

const Navigation = ({ className, searchTerm }) => {
const Navigation = ({ className, searchTerm, mobile }) => {
const searchTermSanitized = searchTerm?.replace(
/[.*+?^${}()|[\]\\]/g,
'\\$&'
Expand All @@ -35,6 +35,10 @@ const Navigation = ({ className, searchTerm }) => {
? filterPageNames(pages, searchTermSanitized)
: undefined;

if (filteredPageNames?.length === 0) {
return <div className={styles.emptyResults}>No results found.</div>;
}

return (
<nav
className={cx(styles.container, className)}
Expand All @@ -46,6 +50,7 @@ const Navigation = ({ className, searchTerm }) => {
searchTerm={searchTermSanitized}
pages={pages}
filteredPageNames={filteredPageNames}
mobile={mobile}
/>
</ul>
</nav>
Expand All @@ -55,6 +60,7 @@ const Navigation = ({ className, searchTerm }) => {
Navigation.propTypes = {
className: PropTypes.string,
searchTerm: PropTypes.string,
mobile: PropTypes.bool,
};

export default Navigation;
4 changes: 4 additions & 0 deletions src/components/Navigation.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@
list-style: none;
padding-left: 0;
}

.emptyResults {
font-size: 0.875rem;
}
17 changes: 14 additions & 3 deletions src/components/NavigationItems.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const NavigationItems = ({
filteredPageNames,
searchTerm,
depthLevel = 0,
mobile,
}) => {
const groupedPages = pages.reduce((groups, page) => {
const { group = '' } = page;
Expand Down Expand Up @@ -52,14 +53,21 @@ const NavigationItems = ({
searchTerm={searchTerm}
filteredPageNames={filteredPageNames}
key={index}
mobile={mobile}
/>
))}
</Fragment>
);
});
};

const NavItem = ({ page, depthLevel, searchTerm, filteredPageNames }) => {
const NavItem = ({
page,
depthLevel,
searchTerm,
filteredPageNames,
mobile,
}) => {
const crumbs = useContext(BreadcrumbContext).flatMap((x) => x.displayName);
const isHomePage = crumbs.length === 0 && depthLevel === 0;

Expand Down Expand Up @@ -108,7 +116,7 @@ const NavItem = ({ page, depthLevel, searchTerm, filteredPageNames }) => {
{headerIcon}
{display}
</span>
{isCurrentPage && (
{!mobile && isCurrentPage && (
<FeatherIcon
className={styles.currentPageIndicator}
name="chevron-right"
Expand All @@ -123,7 +131,7 @@ const NavItem = ({ page, depthLevel, searchTerm, filteredPageNames }) => {
onKeyPress={() => setIsExpanded(!isExpanded)}
tabIndex={0}
>
{depthLevel > 0 && (
{!mobile && depthLevel > 0 && (
<FeatherIcon
className={cx(
{ [styles.isExpanded]: isExpanded },
Expand All @@ -147,6 +155,7 @@ const NavItem = ({ page, depthLevel, searchTerm, filteredPageNames }) => {
filteredPageNames={filteredPageNames}
depthLevel={depthLevel + 1}
searchTerm={searchTerm}
mobile={mobile}
/>
</ul>
)}
Expand All @@ -159,13 +168,15 @@ NavigationItems.propTypes = {
filteredPageNames: PropTypes.array,
searchTerm: PropTypes.string,
depthLevel: PropTypes.number,
mobile: PropTypes.bool,
};

NavItem.propTypes = {
page: link,
filteredPageNames: PropTypes.array,
searchTerm: PropTypes.string,
depthLevel: PropTypes.number.isRequired,
mobile: PropTypes.bool,
};

export default NavigationItems;

0 comments on commit cb683ac

Please sign in to comment.