Skip to content

Commit

Permalink
feat(v2): allow to change location of search bar
Browse files Browse the repository at this point in the history
  • Loading branch information
lex111 committed Feb 8, 2021
1 parent fc071b0 commit 9857868
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 21 deletions.
13 changes: 3 additions & 10 deletions packages/docusaurus-theme-classic/src/theme/Navbar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@ function Navbar(): JSX.Element {
navbar: {items, hideOnScroll, style},
colorMode: {disableSwitch: disableColorModeSwitch},
} = useThemeConfig();

const [sidebarShown, setSidebarShown] = useState(false);
const [isSearchBarExpanded, setIsSearchBarExpanded] = useState(false);

const {isDarkTheme, setLightTheme, setDarkTheme} = useThemeContext();
const {navbarRef, isNavbarVisible} = useHideableNavbar(hideOnScroll);

Expand All @@ -73,6 +70,7 @@ function Navbar(): JSX.Element {
}
}, [windowSize]);

const hasSearchNavbarItem = items.some((item) => item.type === 'search');
const {leftItems, rightItems} = splitNavItemsByPosition(items);

return (
Expand Down Expand Up @@ -101,9 +99,7 @@ function Navbar(): JSX.Element {
<Logo
className="navbar__brand"
imageClassName="navbar__logo"
titleClassName={clsx('navbar__title', {
[styles.hideLogoText]: isSearchBarExpanded,
})}
titleClassName={clsx('navbar__title')}
/>
{leftItems.map((item, i) => (
<NavbarItem {...item} key={i} />
Expand All @@ -121,10 +117,7 @@ function Navbar(): JSX.Element {
onChange={onToggleChange}
/>
)}
<SearchBar
handleSearchBarToggle={setIsSearchBarExpanded}
isSearchBarExpanded={isSearchBarExpanded}
/>
{!hasSearchNavbarItem && <SearchBar />}
</div>
</div>
<div
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@
}
}

@media (max-width: 768px) {
.hideLogoText {
display: none;
}
}

.navbarHideable {
transition: transform var(--ifm-transition-fast) ease;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* 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.
*/

import React from 'react';
import SearchBar from '@theme/SearchBar';
import styles from './styles.module.css';

export default function SearchNavbarItem({mobile}: Props): JSX.Element {
if (mobile) {
return null;
}

return (
<div className={styles.searchWrapper}>
<SearchBar />
</div>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
import React from 'react';
import DefaultNavbarItem from '@theme/NavbarItem/DefaultNavbarItem';
import LocaleDropdownNavbarItem from '@theme/NavbarItem/LocaleDropdownNavbarItem';
import SearchNavbarItem from '@theme/NavbarItem/SearchNavbarItem';
import type {Props} from '@theme/NavbarItem';

const NavbarItemComponents = {
default: () => DefaultNavbarItem,
localeDropdown: () => LocaleDropdownNavbarItem,
search: () => SearchNavbarItem,

// Need to lazy load these items as we don't know for sure the docs plugin is loaded
// See https://github.com/facebook/docusaurus/issues/3360
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* 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.
*/

@media (max-width: 996px) {
.searchWrapper {
position: absolute;
right: var(--ifm-navbar-padding-horizontal);
}
}
9 changes: 9 additions & 0 deletions packages/docusaurus-theme-classic/src/validateThemeConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ const LocaleDropdownNavbarItemSchema = Joi.object({
className: Joi.string(),
});

const SearchItemSchema = Joi.object({
type: Joi.string().equal('search').required(),
position: NavbarItemPosition,
});

// Can this be made easier? :/
const isOfType = (type) => {
let typeSchema = Joi.string().required();
Expand Down Expand Up @@ -139,6 +144,10 @@ const NavbarItemSchema = Joi.object().when({
is: isOfType('localeDropdown'),
then: LocaleDropdownNavbarItemSchema,
},
{
is: isOfType('search'),
then: SearchItemSchema,
},
{
is: isOfType(undefined),
then: Joi.forbidden().messages({
Expand Down
24 changes: 19 additions & 5 deletions website/docs/api/themes/theme-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -395,12 +395,26 @@ module.exports = {
};
```

````
### Navbar search dropdown

If you use the [search](../../search.md), the search bar will be the rightmost element in the navbar.

However, with this special navbar item type, you can change the default location.

```js {5-8} title="docusaurus.config.js"
module.exports = {
themeConfig: {
navbar: {
items: [
{
type: 'localeDropdown',
position: 'left',
type: 'search',
position: 'right',
},
```
],
},
},
};
```

### Auto-hide sticky navbar

Expand All @@ -416,7 +430,7 @@ module.exports = {
// ...
},
};
````
```

### Navbar style

Expand Down

0 comments on commit 9857868

Please sign in to comment.