Skip to content

Commit

Permalink
feat(theme-classic): allow className as option for type: "search" (#7357
Browse files Browse the repository at this point in the history
)

* feat(theme-classic): allow className as option for type: "search"

* fixup! feat(theme-classic): allow className as option for type: "search"

* refactor

Co-authored-by: Jan Peer Stoecklmair <[email protected]>
Co-authored-by: Joshua Chen <[email protected]>
  • Loading branch information
3 people authored May 25, 2022
1 parent cd21a31 commit 5fcb742
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,12 @@ describe('themeConfig', () => {
position: 'left',
label: 'Current version',
},
// Search with className
{
type: 'search',
position: 'right',
className: 'search-bar-wrapper',
},
],
},
};
Expand Down
2 changes: 2 additions & 0 deletions packages/docusaurus-theme-classic/src/theme-classic.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,7 @@ declare module '@theme/Navbar/Search' {

export interface Props {
readonly children: ReactNode;
readonly className?: string;
}

export default function NavbarSearch(props: Props): JSX.Element;
Expand Down Expand Up @@ -834,6 +835,7 @@ declare module '@theme/NavbarItem/DropdownNavbarItem' {
declare module '@theme/NavbarItem/SearchNavbarItem' {
export interface Props {
readonly mobile?: boolean;
readonly className?: string;
}

export default function SearchNavbarItem(props: Props): JSX.Element;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export default function NavbarContent(): JSX.Element {
const items = useNavbarItems();
const [leftItems, rightItems] = splitNavbarItems(items);

const autoAddSearchBar = !items.some((item) => item.type === 'search');
const searchBarItem = items.find((item) => item.type === 'search');

return (
<NavbarContentLayout
Expand All @@ -74,7 +74,7 @@ export default function NavbarContent(): JSX.Element {
<>
<NavbarItems items={rightItems} />
<NavbarColorModeToggle className={styles.colorModeToggle} />
{autoAddSearchBar && (
{!searchBarItem && (
<NavbarSearch>
<SearchBar />
</NavbarSearch>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
*/

import React from 'react';
import clsx from 'clsx';
import type {Props} from '@theme/Navbar/Search';

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

export default function NavbarSearch({children}: Props): JSX.Element {
return <div className={styles.searchBox}>{children}</div>;
export default function NavbarSearch({
children,
className,
}: Props): JSX.Element {
return <div className={clsx(className, styles.searchBox)}>{children}</div>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ import SearchBar from '@theme/SearchBar';
import NavbarSearch from '@theme/Navbar/Search';
import type {Props} from '@theme/NavbarItem/SearchNavbarItem';

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

return (
<NavbarSearch>
<NavbarSearch className={className}>
<SearchBar />
</NavbarSearch>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ const LocaleDropdownNavbarItemSchema = NavbarItemBaseSchema.append({

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

const NavbarItemSchema = Joi.object({
Expand Down
1 change: 1 addition & 0 deletions website/docs/api/themes/theme-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,7 @@ However, with this special navbar item type, you can change the default location
| --- | --- | --- | --- |
| `type` | `'search'` | **Required** | Sets the type of this item to a search bar. |
| `position` | <code>'left' \| 'right'</code> | `'left'` | The side of the navbar this item should appear on. |
| `className` | `string` | / | Custom CSS class for this navbar item. |

</APITable>

Expand Down

0 comments on commit 5fcb742

Please sign in to comment.