Skip to content

Commit

Permalink
feat: new algolia SearchBar (ant-design#30942)
Browse files Browse the repository at this point in the history
  • Loading branch information
vagusX authored Jun 15, 2021
1 parent fbcbf4d commit 0d61976
Show file tree
Hide file tree
Showing 11 changed files with 388 additions and 103 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
"@ant-design/bisheng-plugin": "^2.3.0",
"@ant-design/hitu": "^0.0.0-alpha.13",
"@ant-design/tools": "^13.4.1-beta.0",
"@docsearch/react": "^3.0.0-alpha.36",
"@qixian.cs/github-contributors-list": "^1.0.3",
"@stackblitz/sdk": "^1.3.0",
"@types/classnames": "^2.2.8",
Expand Down
20 changes: 20 additions & 0 deletions site/theme/static/dark.less
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@import '../template/Layout/Header/DocSearch.less';

[data-theme='dark'] {
/* Change autocomplete styles in WebKit */
input:-webkit-autofill,
Expand Down Expand Up @@ -366,4 +368,22 @@
color: rgba(255, 255, 255, 0.65);
}
}

// customization DocSearch Modal component in dark mode
#antd_algolia_search_modal {
.customDocSearch();

// additional style reset in dark mode
--docsearch-key-gradient: @background-color-light;
--docsearch-logo-color: @white;

.DocSearch-Modal,
.DocSearch-Commands-Key {
box-shadow: none;
}

.DocSearch-Hit a {
box-shadow: none;
}
}
}
1 change: 1 addition & 0 deletions site/theme/static/docsearch.less
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
}

.algolia-docsearch-suggestion--highlight {
padding: 0;
color: @primary-color;
}
}
32 changes: 32 additions & 0 deletions site/theme/template/Layout/Header/DocSearch.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// theme customization for DocSearch Modal
.customDocSearch() {
.DocSearch {
--docsearch-primary-color: @primary-color;
--docsearch-text-color: @text-color;
--docsearch-muted-color: @text-color-secondary;
--docsearch-container-background: @modal-mask-bg;
--docsearch-searchbox-shadow: inset 0 0 0 2px @primary-color;

// Modal
--docsearch-modal-background: @modal-content-bg;

// Search box
--docsearch-searchbox-background: @input-bg;
--docsearch-searchbox-focus-background: @input-bg;

// Footer
--docsearch-footer-background: @modal-content-bg;

// Hits
--docsearch-hit-background: @modal-content-bg;
--docsearch-hit-color: @primary-color;
--docsearch-hit-active-color: @text-color-inverse;
--docsearch-highlight-color: @primary-color;

// apply antd modal style
.DocSearch-Footer {
border-top: @modal-footer-border-width @modal-footer-border-style
@modal-footer-border-color-split;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@import '../../../static/theme.less';
@import '../../../../../components/style/themes/default.less';
@import './index.less';
@import './DocSearch.less';

@search-icon-color: #ced4d9;

Expand All @@ -17,6 +18,23 @@
border-left: 1px solid @site-border-color-split;
transition: width 0.5s;

.keybindings {
cursor: pointer;
}

.keybinding {
color: @search-icon-color;
kbd {
display: inline-block;
width: 20px;
height: 20px;
padding: 0;
// better keybinding font display using `Arial`
font-family: Arial; /* stylelint-disable-line font-family-no-missing-generic-family-keyword */
text-align: center;
}
}

.ant-row-rtl & {
margin: 0 0 0 auto !important;
padding-right: 16px;
Expand All @@ -39,6 +57,12 @@
pointer-events: none;
}

.ant-input-affix-wrapper {
background: transparent;
border: 0;
box-shadow: none;
}

input {
width: 100%;
max-width: 200px;
Expand Down Expand Up @@ -136,3 +160,28 @@
display: none;
}
}

.DocSearch {
font-family: @font-family;
}

#antd_algolia_search_modal {
// avoid the veritcal scrollbar(looks like a white block)
.DocSearch-Hit-title {
overflow-y: hidden;

mark {
padding: 0;
}
}

// reset original `box-shadow` style
.DocSearch-Footer {
box-shadow: none;
}
}

// customization DocSearch Modal component in light mode
body[data-theme='default'] #antd_algolia_search_modal {
.customDocSearch();
}
196 changes: 196 additions & 0 deletions site/theme/template/Layout/Header/SearchBar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { Link, browserHistory } from 'bisheng/router';
import classNames from 'classnames';
import { Helmet } from 'react-helmet-async';
import canUseDom from 'rc-util/lib/Dom/canUseDom';
import { Input, Tooltip, Typography } from 'antd';
import { SearchOutlined } from '@ant-design/icons';
import { DocSearchProps, useDocSearchKeyboardEvents, DocSearchModalProps } from '@docsearch/react';
import '@docsearch/css';

import { SharedProps } from './interface';
import { IAlgoliaConfig, transformHitUrl } from './algolia-config';

import './SearchBar.less';

const { Text } = Typography;

export interface SearchBarProps extends SharedProps {
onTriggerFocus?: (focus: boolean) => void;
responsive: null | 'narrow' | 'crowded';
algoliaConfig: IAlgoliaConfig;
}

let SearchModal: React.FC<DocSearchModalProps> | null = null;

const Hit: DocSearchProps['hitComponent'] = ({ hit, children }) => {
const toUrl = React.useMemo(() => transformHitUrl(hit.url), [hit.url]);
return <Link to={toUrl}>{children}</Link>;
};

const CTRL_KEY = 'Ctrl';
const CMD_KEY = '⌘';

function isAppleDevice() {
return /(mac|iphone|ipod|ipad)/i.test(navigator.platform);
}

/**
* Recompose for algolia DocSearch Component Inspiring by
*
* - [@docusaurus-theme-search-algolia](https://docusaurus.io/docs/api/themes/@docusaurus/theme-search-algolia)
* - [DocSearchModal Docs](https://autocomplete-experimental.netlify.app/docs/DocSearchModal)
*/
export const SearchBar = ({
isZhCN,
responsive,
onTriggerFocus,
algoliaConfig,
}: SearchBarProps) => {
const [isInputFocus, setInputFocus] = React.useState(false);
const [inputSearch, setInputSearch] = React.useState('');

const [isModalOpen, setModalOpen] = React.useState(false);
const [searchModalQuery, setSearchModalQuery] = React.useState('');
const searchPlaceholder = isZhCN ? '在 ant.design 中搜索' : 'Search in ant.design';
const searchInputPlaceholder = isZhCN ? '搜索' : 'Search';

const triggerSearchModalImport = React.useCallback(() => {
if (SearchModal) {
return Promise.resolve();
}

return import('@docsearch/react/modal').then(({ DocSearchModal }) => {
SearchModal = DocSearchModal;
});
}, []);

const handleInputFocus = React.useCallback((focus: boolean) => {
setInputFocus(focus);
onTriggerFocus?.(focus);
}, []);

const handleInputChange = React.useCallback((event: React.ChangeEvent<HTMLInputElement>) => {
triggerSearchModalImport();
setInputSearch(event.target.value);
}, []);

const searchModalContainer = React.useMemo(() => {
if (!canUseDom()) {
return;
}
const id = 'antd_algolia_search_modal';
let searchModalContainer$ = document.querySelector(`#${id}`);
if (!searchModalContainer$) {
const containerDiv = document.createElement('div');
containerDiv.id = id;
document.body.appendChild(containerDiv);
searchModalContainer$ = containerDiv;
}
return searchModalContainer$;
}, []);

const handleModalOpen = React.useCallback(() => {
triggerSearchModalImport().then(() => {
handleInputFocus(true);
setModalOpen(true);
});
}, []);

const handleModalClose = React.useCallback(() => {
// clear search value in SearchModal
setSearchModalQuery('');
setModalOpen(false);
}, []);

useDocSearchKeyboardEvents({
isOpen: isModalOpen,
onOpen: handleModalOpen,
onClose: handleModalClose,
});

const searchParameters = React.useMemo(() => algoliaConfig.getSearchParams(isZhCN), [isZhCN]);

const navigator = React.useRef({
navigate({ itemUrl }: { itemUrl: string }) {
browserHistory.push(itemUrl);
},
}).current;

return (
<div
id="search-box"
className={classNames({
'narrow-mode': responsive,
focused: isInputFocus,
})}
>
<Helmet>
{/* pre-connect to algolia server */}
<link
rel="preconnect"
href={`https://${algoliaConfig.appId}-dsn.algolia.net`}
crossOrigin="anonymous"
/>
</Helmet>

<Input
placeholder={searchInputPlaceholder}
onTouchStart={triggerSearchModalImport}
onMouseOver={triggerSearchModalImport}
value={inputSearch}
onChange={handleInputChange}
onFocus={() => {
triggerSearchModalImport();
handleInputFocus(true);
}}
onBlur={() => {
handleInputFocus(false);
}}
prefix={<SearchOutlined />}
suffix={
typeof window !== 'undefined' && (
<Tooltip placement="right" title={isZhCN ? '唤起搜索窗' : 'Search in doc modal'}>
<span
className="keybindings"
onClick={() => {
// move userSearch to SearchModal
setSearchModalQuery(inputSearch);
setInputSearch('');
handleModalOpen();
}}
>
<Text keyboard className="keybinding">
{isAppleDevice() ? CMD_KEY : CTRL_KEY}
</Text>
<Text keyboard className="keybinding">
K
</Text>
</span>
</Tooltip>
)
}
/>

{SearchModal &&
searchModalContainer &&
isModalOpen &&
ReactDOM.createPortal(
<SearchModal
navigator={navigator}
onClose={handleModalClose}
initialScrollY={window.scrollY}
initialQuery={searchModalQuery}
placeholder={searchPlaceholder}
hitComponent={Hit}
apiKey={algoliaConfig.apiKey}
indexName={algoliaConfig.indexName}
transformItems={algoliaConfig.transformData}
searchParameters={searchParameters}
/>,
searchModalContainer,
)}
</div>
);
};
Loading

0 comments on commit 0d61976

Please sign in to comment.