Skip to content

Commit

Permalink
fix: site style conflict (ant-design#44899)
Browse files Browse the repository at this point in the history
* fix: site style conflict

* chore: lint

* feat: optimize code
  • Loading branch information
chen201724 authored Sep 15, 2023
1 parent 4d38144 commit 2627cae
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 10 deletions.
1 change: 1 addition & 0 deletions .dumi/theme/SiteThemeProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const SiteThemeProvider: FC<ThemeProviderProps> = ({ children, theme, ...rest })
theme={theme}
customToken={{
headerHeight: 64,
bannerHeight: 38,
menuItemBorder: 2,
mobileMaxWidth: 767.99,
siteMarkdownCodeBg: token.colorFillTertiary,
Expand Down
7 changes: 5 additions & 2 deletions .dumi/theme/builtins/ComponentOverview/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const { Title } = Typography;

const Overview: React.FC = () => {
const { styles } = useStyle();
const { theme } = useContext(SiteContext);
const { theme, bannerVisible } = useContext(SiteContext);

const data = useSidebarData();
const [searchBarAffixed, setSearchBarAffixed] = useState<boolean>(false);
Expand Down Expand Up @@ -143,7 +143,10 @@ const Overview: React.FC = () => {
return (
<section className="markdown" ref={sectionRef}>
<Divider />
<Affix offsetTop={24 + token.headerHeight} onChange={setSearchBarAffixed}>
<Affix
offsetTop={24 + token.headerHeight + (bannerVisible ? token.bannerHeight : 0)}
onChange={setSearchBarAffixed}
>
<div
className={styles.componentsOverviewAffix}
style={searchBarAffixed ? affixedStyle : {}}
Expand Down
9 changes: 7 additions & 2 deletions .dumi/theme/builtins/IconSearch/IconSearch.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useMemo, useState } from 'react';
import React, { useCallback, useContext, useMemo, useState } from 'react';
import type { CSSProperties } from 'react';
import Icon, * as AntdIcons from '@ant-design/icons';
import type { IntlShape } from 'react-intl';
Expand All @@ -11,6 +11,7 @@ import Category from './Category';
import { FilledIcon, OutlinedIcon, TwoToneIcon } from './themeIcons';
import type { CategoriesKeys } from './fields';
import { categories } from './fields';
import SiteContext from '../../slots/SiteContext';

export enum ThemeType {
Filled = 'Filled',
Expand Down Expand Up @@ -59,6 +60,7 @@ const IconSearch: React.FC = () => {
theme: ThemeType.Outlined,
});
const token = useTheme();
const { bannerVisible } = useContext(SiteContext);

const newIconNames: string[] = [];

Expand Down Expand Up @@ -124,7 +126,10 @@ const IconSearch: React.FC = () => {

return (
<div className="markdown">
<Affix offsetTop={24 + token.headerHeight} onChange={setSearchBarAffixed}>
<Affix
offsetTop={24 + token.headerHeight + (bannerVisible ? token.bannerHeight : 0)}
onChange={setSearchBarAffixed}
>
<div className={styles.iconSearchAffix} style={searchBarAffixed ? affixedStyle : {}}>
<Segmented
size="large"
Expand Down
15 changes: 9 additions & 6 deletions .dumi/theme/layouts/GlobalLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,13 @@ const GlobalLayout: React.FC = () => {
const { pathname } = useLocation();
const [searchParams, setSearchParams] = useSearchParams();
const [, , setPrefersColor] = usePrefersColor();
const [{ theme = [], direction, isMobile }, setSiteState] = useLayoutState<SiteState>({
isMobile: false,
direction: 'ltr',
theme: [],
});
const [{ theme = [], direction, isMobile, bannerVisible = true }, setSiteState] =
useLayoutState<SiteState>({
isMobile: false,
direction: 'ltr',
theme: [],
bannerVisible: true,
});

const updateSiteConfig = useCallback(
(props: SiteState) => {
Expand Down Expand Up @@ -121,8 +123,9 @@ const GlobalLayout: React.FC = () => {
updateSiteConfig,
theme: theme!,
isMobile: isMobile!,
bannerVisible,
}),
[isMobile, direction, updateSiteConfig, theme],
[isMobile, direction, updateSiteConfig, theme, bannerVisible],
);

const [styleCache] = React.useState(() => createCache());
Expand Down
14 changes: 14 additions & 0 deletions .dumi/theme/slots/Header/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ const useStyle = createStyles(({ token, css }) => {
margin-left: 0;
}
`,
icon: css`
margin-right: 10px;
width: 22px;
height: 22px;
`,
};
});

Expand Down Expand Up @@ -171,6 +176,9 @@ const Header: React.FC = () => {
const onDirectionChange = () => {
updateSiteConfig({ direction: direction !== 'rtl' ? 'rtl' : 'ltr' });
};
const onBannerClose = () => {
updateSiteConfig({ bannerVisible: false });
};

useEffect(() => {
handleHideMenu();
Expand Down Expand Up @@ -349,6 +357,11 @@ const Header: React.FC = () => {
className={styles.banner}
message={
<>
<img
className={styles.icon}
src="https://gw.alipayobjects.com/zos/rmsportal/XuVpGqBFxXplzvLjJBZB.svg"
alt="yuque"
/>
{isMobile ? locale.shortMessage : locale.message}
<a
className={styles.link}
Expand All @@ -370,6 +383,7 @@ const Header: React.FC = () => {
banner
closable
showIcon={false}
onClose={onBannerClose}
/>
)}
<Row style={{ flexFlow: 'nowrap', height: 64 }}>
Expand Down
2 changes: 2 additions & 0 deletions .dumi/theme/slots/SiteContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import type { ThemeName } from '../common/ThemeSwitch';

export interface SiteContextProps {
isMobile: boolean;
bannerVisible: boolean;
direction: DirectionType;
theme: ThemeName[];
updateSiteConfig: (props: Partial<SiteContextProps>) => void;
}

const SiteContext = React.createContext<SiteContextProps>({
isMobile: false,
bannerVisible: true,
direction: 'ltr',
theme: ['light'],
updateSiteConfig: () => {},
Expand Down

0 comments on commit 2627cae

Please sign in to comment.