2020-02-27 10:53:30 +08:00
|
|
|
import React from 'react';
|
|
|
|
import { FormattedMessage, injectIntl } from 'react-intl';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import { UnorderedListOutlined } from '@ant-design/icons';
|
|
|
|
import { Select, Row, Col, Popover, Button } from 'antd';
|
2021-06-15 14:51:11 +08:00
|
|
|
import canUseDom from 'rc-util/lib/Dom/canUseDom';
|
2020-02-27 10:53:30 +08:00
|
|
|
import * as utils from '../../utils';
|
2021-04-09 13:13:06 +08:00
|
|
|
import packageJson from '../../../../../package.json';
|
2020-02-27 10:53:30 +08:00
|
|
|
import Logo from './Logo';
|
2021-06-18 10:02:31 +08:00
|
|
|
import SearchBar from './SearchBar';
|
2020-02-27 10:53:30 +08:00
|
|
|
import More from './More';
|
|
|
|
import Navigation from './Navigation';
|
|
|
|
import Github from './Github';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { SiteContextProps } from '../SiteContext';
|
|
|
|
import SiteContext from '../SiteContext';
|
2020-10-30 20:02:43 +08:00
|
|
|
import { ping } from '../../utils';
|
2021-06-15 14:51:11 +08:00
|
|
|
import { AlgoliaConfig } from './algolia-config';
|
2020-02-27 10:53:30 +08:00
|
|
|
|
|
|
|
import './index.less';
|
|
|
|
|
|
|
|
const RESPONSIVE_XS = 1120;
|
|
|
|
const RESPONSIVE_SM = 1200;
|
|
|
|
|
|
|
|
const { Option } = Select;
|
|
|
|
|
2021-04-09 13:13:06 +08:00
|
|
|
const antdVersion: string = packageJson.version;
|
|
|
|
|
2020-02-27 10:53:30 +08:00
|
|
|
export interface HeaderProps {
|
|
|
|
intl: {
|
|
|
|
locale: string;
|
|
|
|
};
|
2020-09-14 10:48:08 +08:00
|
|
|
location: { pathname: string; query: any };
|
2020-05-29 18:05:58 +08:00
|
|
|
router: any;
|
2020-02-27 10:53:30 +08:00
|
|
|
themeConfig: { docVersions: Record<string, string> };
|
|
|
|
changeDirection: (direction: string) => void;
|
|
|
|
}
|
|
|
|
|
2021-06-15 14:51:11 +08:00
|
|
|
let docsearch: any;
|
|
|
|
const triggerDocSearchImport = () => {
|
|
|
|
if (docsearch) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
return import('docsearch.js').then(ds => {
|
|
|
|
docsearch = ds.default;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-06-18 10:02:31 +08:00
|
|
|
function initDocSearch({ isZhCN, router }: { isZhCN: boolean; router: any }) {
|
2021-06-15 14:51:11 +08:00
|
|
|
if (!canUseDom()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
triggerDocSearchImport().then(() => {
|
|
|
|
docsearch({
|
|
|
|
appId: AlgoliaConfig.appId,
|
|
|
|
apiKey: AlgoliaConfig.apiKey,
|
|
|
|
indexName: AlgoliaConfig.indexName,
|
|
|
|
inputSelector: '#search-box input',
|
|
|
|
algoliaOptions: AlgoliaConfig.getSearchParams(isZhCN),
|
|
|
|
transformData: AlgoliaConfig.transformData,
|
|
|
|
debug: AlgoliaConfig.debug,
|
|
|
|
// https://docsearch.algolia.com/docs/behavior#handleselected
|
2021-06-18 10:02:31 +08:00
|
|
|
handleSelected: (input: any, _$1: unknown, suggestion: any) => {
|
|
|
|
router.push(suggestion.url);
|
|
|
|
setTimeout(() => {
|
|
|
|
input.setVal('');
|
|
|
|
});
|
|
|
|
},
|
2021-06-15 14:51:11 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-27 10:53:30 +08:00
|
|
|
interface HeaderState {
|
|
|
|
menuVisible: boolean;
|
|
|
|
windowWidth: number;
|
|
|
|
searching: boolean;
|
2020-10-30 20:02:43 +08:00
|
|
|
showTechUIButton: boolean;
|
2020-02-27 10:53:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
class Header extends React.Component<HeaderProps, HeaderState> {
|
2020-05-29 18:05:58 +08:00
|
|
|
static contextType = SiteContext;
|
2020-02-27 10:53:30 +08:00
|
|
|
|
2020-10-30 20:02:43 +08:00
|
|
|
pingTimer: NodeJS.Timeout;
|
|
|
|
|
2020-02-27 10:53:30 +08:00
|
|
|
state = {
|
|
|
|
menuVisible: false,
|
|
|
|
windowWidth: 1400,
|
|
|
|
searching: false,
|
2020-10-30 20:02:43 +08:00
|
|
|
showTechUIButton: false,
|
2020-02-27 10:53:30 +08:00
|
|
|
};
|
|
|
|
|
2022-04-08 22:55:42 +08:00
|
|
|
context: SiteContextProps;
|
|
|
|
|
2020-02-27 10:53:30 +08:00
|
|
|
componentDidMount() {
|
2020-05-29 18:05:58 +08:00
|
|
|
const { intl, router } = this.props;
|
2020-02-27 10:53:30 +08:00
|
|
|
router.listen(this.handleHideMenu);
|
2021-06-15 14:51:11 +08:00
|
|
|
|
2021-06-18 10:02:31 +08:00
|
|
|
initDocSearch({
|
2021-06-25 15:11:23 +08:00
|
|
|
isZhCN: intl.locale === 'zh-CN',
|
2021-06-18 10:02:31 +08:00
|
|
|
router,
|
|
|
|
});
|
2020-02-27 10:53:30 +08:00
|
|
|
|
|
|
|
window.addEventListener('resize', this.onWindowResize);
|
|
|
|
this.onWindowResize();
|
2020-10-30 20:02:43 +08:00
|
|
|
|
|
|
|
this.pingTimer = ping(status => {
|
|
|
|
if (status !== 'timeout' && status !== 'error') {
|
|
|
|
this.setState({
|
|
|
|
showTechUIButton: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2020-02-27 10:53:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
window.removeEventListener('resize', this.onWindowResize);
|
2020-10-30 20:02:43 +08:00
|
|
|
clearTimeout(this.pingTimer);
|
2020-02-27 10:53:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
onWindowResize = () => {
|
|
|
|
this.setState({
|
|
|
|
windowWidth: window.innerWidth,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
onTriggerSearching = (searching: boolean) => {
|
|
|
|
this.setState({ searching });
|
|
|
|
};
|
|
|
|
|
|
|
|
handleShowMenu = () => {
|
|
|
|
this.setState({
|
|
|
|
menuVisible: true,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
handleHideMenu = () => {
|
|
|
|
this.setState({
|
|
|
|
menuVisible: false,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
onDirectionChange = () => {
|
|
|
|
const { changeDirection } = this.props;
|
|
|
|
const { direction } = this.context;
|
|
|
|
if (direction !== 'rtl') {
|
|
|
|
changeDirection('rtl');
|
|
|
|
} else {
|
|
|
|
changeDirection('ltr');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
getNextDirectionText = () => {
|
|
|
|
const { direction } = this.context;
|
|
|
|
|
|
|
|
if (direction !== 'rtl') {
|
|
|
|
return 'RTL';
|
|
|
|
}
|
|
|
|
return 'LTR';
|
|
|
|
};
|
|
|
|
|
2020-04-14 16:42:39 +08:00
|
|
|
getDropdownStyle = (): React.CSSProperties => {
|
|
|
|
const { direction } = this.context;
|
|
|
|
if (direction === 'rtl') {
|
|
|
|
return {
|
|
|
|
direction: 'ltr',
|
|
|
|
textAlign: 'right',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
};
|
|
|
|
|
2020-02-27 10:53:30 +08:00
|
|
|
onMenuVisibleChange = (visible: boolean) => {
|
|
|
|
this.setState({
|
|
|
|
menuVisible: visible,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-10-15 11:19:14 +08:00
|
|
|
// eslint-disable-next-line class-methods-use-this
|
2020-02-27 10:53:30 +08:00
|
|
|
handleVersionChange = (url: string) => {
|
|
|
|
const currentUrl = window.location.href;
|
|
|
|
const currentPathname = window.location.pathname;
|
2020-07-01 11:11:58 +08:00
|
|
|
if (/overview/.test(currentPathname) && /0?[1-39][0-3]?x/.test(url)) {
|
|
|
|
window.location.href = currentUrl
|
|
|
|
.replace(window.location.origin, url)
|
|
|
|
.replace(/\/components\/overview/, `/docs${/0(9|10)x/.test(url) ? '' : '/react'}/introduce`)
|
|
|
|
.replace(/\/$/, '');
|
|
|
|
return;
|
|
|
|
}
|
2020-07-29 11:11:11 +08:00
|
|
|
window.location.href = currentUrl.replace(window.location.origin, url);
|
2020-02-27 10:53:30 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
onLangChange = () => {
|
|
|
|
const {
|
2020-09-14 10:48:08 +08:00
|
|
|
location: { pathname, query },
|
2020-02-27 10:53:30 +08:00
|
|
|
} = this.props;
|
|
|
|
const currentProtocol = `${window.location.protocol}//`;
|
2022-03-15 10:41:12 +08:00
|
|
|
const currentHref = window.location.href.slice(currentProtocol.length);
|
2020-02-27 10:53:30 +08:00
|
|
|
|
|
|
|
if (utils.isLocalStorageNameSupported()) {
|
|
|
|
localStorage.setItem('locale', utils.isZhCN(pathname) ? 'en-US' : 'zh-CN');
|
|
|
|
}
|
|
|
|
|
|
|
|
window.location.href =
|
|
|
|
currentProtocol +
|
|
|
|
currentHref.replace(
|
|
|
|
window.location.pathname,
|
2020-09-14 10:48:08 +08:00
|
|
|
utils.getLocalizedPathname(pathname, !utils.isZhCN(pathname), query).pathname,
|
2020-02-27 10:53:30 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<SiteContext.Consumer>
|
|
|
|
{({ isMobile }) => {
|
2020-10-30 20:02:43 +08:00
|
|
|
const { menuVisible, windowWidth, searching, showTechUIButton } = this.state;
|
2020-04-21 10:49:59 +08:00
|
|
|
const { direction } = this.context;
|
2020-02-27 10:53:30 +08:00
|
|
|
const {
|
|
|
|
location,
|
|
|
|
themeConfig,
|
|
|
|
intl: { locale },
|
2021-06-18 10:02:31 +08:00
|
|
|
router,
|
2020-02-27 10:53:30 +08:00
|
|
|
} = this.props;
|
2021-04-09 13:13:06 +08:00
|
|
|
const docVersions: Record<string, string> = {
|
|
|
|
[antdVersion]: antdVersion,
|
|
|
|
...themeConfig.docVersions,
|
|
|
|
};
|
2020-02-27 10:53:30 +08:00
|
|
|
const versionOptions = Object.keys(docVersions).map(version => (
|
|
|
|
<Option value={docVersions[version]} key={version}>
|
|
|
|
{version}
|
|
|
|
</Option>
|
|
|
|
));
|
|
|
|
|
|
|
|
const pathname = location.pathname.replace(/(^\/|\/$)/g, '');
|
|
|
|
|
|
|
|
const isHome = ['', 'index', 'index-cn'].includes(pathname);
|
|
|
|
|
|
|
|
const isZhCN = locale === 'zh-CN';
|
2020-04-21 10:49:59 +08:00
|
|
|
const isRTL = direction === 'rtl';
|
2020-02-27 10:53:30 +08:00
|
|
|
let responsive: null | 'narrow' | 'crowded' = null;
|
|
|
|
if (windowWidth < RESPONSIVE_XS) {
|
|
|
|
responsive = 'crowded';
|
|
|
|
} else if (windowWidth < RESPONSIVE_SM) {
|
|
|
|
responsive = 'narrow';
|
|
|
|
}
|
|
|
|
|
|
|
|
const headerClassName = classNames({
|
|
|
|
clearfix: true,
|
|
|
|
'home-header': isHome,
|
|
|
|
});
|
|
|
|
|
|
|
|
const sharedProps = {
|
|
|
|
isZhCN,
|
2020-04-21 10:49:59 +08:00
|
|
|
isRTL,
|
2020-02-27 10:53:30 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const navigationNode = (
|
|
|
|
<Navigation
|
|
|
|
key="nav"
|
|
|
|
{...sharedProps}
|
|
|
|
location={location}
|
|
|
|
responsive={responsive}
|
|
|
|
isMobile={isMobile}
|
2020-10-30 20:02:43 +08:00
|
|
|
showTechUIButton={showTechUIButton}
|
2020-02-27 10:53:30 +08:00
|
|
|
pathname={pathname}
|
|
|
|
directionText={this.getNextDirectionText()}
|
|
|
|
onLangChange={this.onLangChange}
|
|
|
|
onDirectionChange={this.onDirectionChange}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
let menu: (React.ReactElement | null)[] = [
|
|
|
|
navigationNode,
|
|
|
|
<Select
|
|
|
|
key="version"
|
|
|
|
className="version"
|
|
|
|
size="small"
|
|
|
|
defaultValue={antdVersion}
|
|
|
|
onChange={this.handleVersionChange}
|
2020-04-14 16:42:39 +08:00
|
|
|
dropdownStyle={this.getDropdownStyle()}
|
2020-02-27 10:53:30 +08:00
|
|
|
getPopupContainer={trigger => trigger.parentNode}
|
|
|
|
>
|
|
|
|
{versionOptions}
|
|
|
|
</Select>,
|
|
|
|
<Button
|
|
|
|
size="small"
|
|
|
|
onClick={this.onLangChange}
|
|
|
|
className="header-button header-lang-button"
|
|
|
|
key="lang-button"
|
|
|
|
>
|
|
|
|
<FormattedMessage id="app.header.lang" />
|
|
|
|
</Button>,
|
|
|
|
<Button
|
|
|
|
size="small"
|
|
|
|
onClick={this.onDirectionChange}
|
|
|
|
className="header-button header-direction-button"
|
|
|
|
key="direction-button"
|
|
|
|
>
|
|
|
|
{this.getNextDirectionText()}
|
|
|
|
</Button>,
|
|
|
|
<More key="more" {...sharedProps} />,
|
|
|
|
<Github key="github" responsive={responsive} />,
|
|
|
|
];
|
|
|
|
|
|
|
|
if (windowWidth < RESPONSIVE_XS) {
|
|
|
|
menu = searching ? [] : [navigationNode];
|
|
|
|
} else if (windowWidth < RESPONSIVE_SM) {
|
|
|
|
menu = searching ? [] : menu;
|
|
|
|
}
|
|
|
|
|
|
|
|
const colProps = isHome
|
|
|
|
? [{ flex: 'none' }, { flex: 'auto' }]
|
|
|
|
: [
|
|
|
|
{
|
|
|
|
xxl: 4,
|
|
|
|
xl: 5,
|
|
|
|
lg: 6,
|
|
|
|
md: 6,
|
|
|
|
sm: 24,
|
|
|
|
xs: 24,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
xxl: 20,
|
|
|
|
xl: 19,
|
|
|
|
lg: 18,
|
|
|
|
md: 18,
|
|
|
|
sm: 0,
|
|
|
|
xs: 0,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
return (
|
|
|
|
<header id="header" className={headerClassName}>
|
|
|
|
{isMobile && (
|
|
|
|
<Popover
|
|
|
|
overlayClassName="popover-menu"
|
|
|
|
placement="bottomRight"
|
|
|
|
content={menu}
|
|
|
|
trigger="click"
|
|
|
|
visible={menuVisible}
|
|
|
|
arrowPointAtCenter
|
|
|
|
onVisibleChange={this.onMenuVisibleChange}
|
|
|
|
>
|
|
|
|
<UnorderedListOutlined className="nav-phone-icon" onClick={this.handleShowMenu} />
|
|
|
|
</Popover>
|
|
|
|
)}
|
2020-03-29 10:39:46 +08:00
|
|
|
<Row style={{ flexFlow: 'nowrap', height: 64 }}>
|
2020-02-27 10:53:30 +08:00
|
|
|
<Col {...colProps[0]}>
|
2020-09-14 10:48:08 +08:00
|
|
|
<Logo {...sharedProps} location={location} />
|
2020-02-27 10:53:30 +08:00
|
|
|
</Col>
|
|
|
|
<Col {...colProps[1]} className="menu-row">
|
2021-06-15 14:51:11 +08:00
|
|
|
<SearchBar
|
|
|
|
key="search"
|
|
|
|
{...sharedProps}
|
2021-06-18 10:02:31 +08:00
|
|
|
router={router}
|
2021-06-15 14:51:11 +08:00
|
|
|
algoliaConfig={AlgoliaConfig}
|
|
|
|
responsive={responsive}
|
|
|
|
onTriggerFocus={this.onTriggerSearching}
|
|
|
|
/>
|
2020-02-27 10:53:30 +08:00
|
|
|
{!isMobile && menu}
|
|
|
|
</Col>
|
|
|
|
</Row>
|
|
|
|
</header>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</SiteContext.Consumer>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default injectIntl(Header as any);
|