Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(v2): show search icon only on mobiles #2791

Merged
merged 1 commit into from
May 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
}
}

@media (max-width: 360px) {
@media (max-width: 768px) {
.hideLogoText {
display: none;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
import {useHistory} from '@docusaurus/router';
import useSearchQuery from '@theme/hooks/useSearchQuery';

import './styles.css';
import styles from './styles.module.css';

const Search = (props) => {
const Search = ({handleSearchBarToggle, isSearchBarExpanded}) => {
const [algoliaLoaded, setAlgoliaLoaded] = useState(false);
const searchBarRef = useRef(null);
const {siteConfig = {}} = useDocusaurusContext();
Expand Down Expand Up @@ -76,19 +76,19 @@ const Search = (props) => {
);
};

const handleSearchIcon = useCallback(() => {
const toggleSearchInput = useCallback(() => {
loadAlgolia();

if (algoliaLoaded) {
searchBarRef.current.focus();
}

props.handleSearchBarToggle(!props.isSearchBarExpanded);
}, [props.isSearchBarExpanded]);
handleSearchBarToggle(!isSearchBarExpanded);
}, [isSearchBarExpanded]);

const handleSearchInputBlur = useCallback(() => {
props.handleSearchBarToggle(!props.isSearchBarExpanded);
}, [props.isSearchBarExpanded]);
handleSearchBarToggle(!isSearchBarExpanded);
}, [isSearchBarExpanded]);

const handleSearchInput = useCallback((e) => {
const needFocus = e.type !== 'mouseover';
Expand All @@ -104,32 +104,33 @@ const Search = (props) => {

return (
<div className="navbar__search" key="search-box">
<span
aria-label="expand searchbar"
role="button"
className={classnames('search-icon', {
'search-icon-hidden': props.isSearchBarExpanded,
})}
onClick={handleSearchIcon}
onKeyDown={handleSearchIcon}
tabIndex={0}
/>
<input
id="search_input_react"
type="search"
placeholder="Search"
aria-label="Search"
className={classnames(
'navbar__search-input',
{'search-bar-expanded': props.isSearchBarExpanded},
{'search-bar': !props.isSearchBarExpanded},
)}
onMouseOver={handleSearchInput}
onFocus={handleSearchInput}
onBlur={handleSearchInputBlur}
onKeyDown={handleSearchInputPressEnter}
ref={searchBarRef}
/>
<div className={styles.searchWrapper}>
<span
aria-label="expand searchbar"
role="button"
className={classnames(styles.searchIconButton, {
[styles.searchIconButtonHidden]: isSearchBarExpanded,
})}
onClick={toggleSearchInput}
onKeyDown={toggleSearchInput}
tabIndex={0}
/>

<input
id="search_input_react"
type="search"
placeholder="Search"
aria-label="Search"
className={classnames('navbar__search-input', styles.searchInput, {
[styles.searchInputExpanded]: isSearchBarExpanded,
})}
onMouseOver={handleSearchInput}
onFocus={handleSearchInput}
onBlur={handleSearchInputBlur}
onKeyDown={handleSearchInputPressEnter}
ref={searchBarRef}
/>
</div>
</div>
);
};
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

.searchIconButton {
display: none;
}

@media (max-width: 768px) {
.searchIconButton {
display: block;
position: absolute;
right: 0;
width: 2.75rem;
height: 2rem;
z-index: 1;
}

.searchIconButtonHidden {
visibility: hidden;
}

:global(.navbar__items:first-of-type) {
flex: 0 1 auto;
}

:global(.navbar__inner) {
position: relative;
}

:global(.navbar__search) {
position: relative;
flex: 1 1 auto;
padding-left: 0;
}

.searchWrapper {
position: absolute;
top: calc(var(--ifm-navbar-padding-vertical) * 2 * -1);
width: 100%;
display: flex;
justify-content: flex-end;
}

.searchInput {
width: 0;
transition: width 0.3s ease-in-out;
}

.searchInputExpanded {
width: 100%;
}

:global(.algolia-autocomplete) {
width: 100%;
display: flex !important;
justify-content: flex-end;
}
}