Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: menu support css variable theme #45750

Merged
merged 16 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion components/menu/MenuDivider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import classNames from 'classnames';
import { Divider } from 'rc-menu';
import * as React from 'react';
import { ConfigContext } from '../config-provider';
import useCSSVar from './style/cssVar';

export interface MenuDividerProps extends React.HTMLAttributes<HTMLLIElement> {
className?: string;
Expand All @@ -15,14 +16,15 @@ const MenuDivider: React.FC<MenuDividerProps> = (props) => {
const { getPrefixCls } = React.useContext(ConfigContext);

const prefixCls = getPrefixCls('menu', customizePrefixCls);
const wrapCSSVar = useCSSVar(prefixCls);
kiner-tang marked this conversation as resolved.
Show resolved Hide resolved
const classString = classNames(
{
[`${prefixCls}-item-divider-dashed`]: !!dashed,
},
className,
);

return <Divider className={classString} {...restProps} />;
return wrapCSSVar(<Divider className={classString} {...restProps} />);
};

export default MenuDivider;
5 changes: 4 additions & 1 deletion components/menu/MenuItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Tooltip from '../tooltip';
import { cloneElement, isValidElement } from '../_util/reactNode';
import type { MenuContextProps } from './MenuContext';
import MenuContext from './MenuContext';
import useCSSVar from './style/cssVar';

export interface MenuItemProps extends Omit<RcMenuItemProps, 'title'> {
icon?: React.ReactNode;
Expand Down Expand Up @@ -56,6 +57,8 @@ const MenuItem: GenericComponent = (props) => {
return wrapNode;
};

const wrapCSSVar = useCSSVar(prefixCls);
kiner-tang marked this conversation as resolved.
Show resolved Hide resolved

const { siderCollapsed } = React.useContext<SiderContextProps>(SiderContext);

let tooltipTitle = title;
Expand Down Expand Up @@ -110,7 +113,7 @@ const MenuItem: GenericComponent = (props) => {
</Tooltip>
);
}
return returnNode;
return wrapCSSVar(returnNode);
};

export default MenuItem;
6 changes: 4 additions & 2 deletions components/menu/SubMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { useZIndex } from '../_util/hooks/useZIndex';
import { cloneElement, isValidElement } from '../_util/reactNode';
import type { MenuContextProps, MenuTheme } from './MenuContext';
import MenuContext from './MenuContext';
import useCSSVar from './style/cssVar';

interface TitleEventEntity {
key: string;
Expand Down Expand Up @@ -34,6 +35,7 @@ const SubMenu: React.FC<SubMenuProps> = (props) => {
const context = React.useContext(MenuContext);
const { prefixCls, inlineCollapsed, theme: contextTheme } = context;

const wrapCSSVar = useCSSVar(prefixCls);
const parentPath = useFullPath();

let titleNode: React.ReactNode;
Expand Down Expand Up @@ -70,7 +72,7 @@ const SubMenu: React.FC<SubMenuProps> = (props) => {
// ============================ zIndex ============================
const [zIndex] = useZIndex('Menu');

return (
return wrapCSSVar(
kiner-tang marked this conversation as resolved.
Show resolved Hide resolved
<MenuContext.Provider value={contextValue}>
<RcSubMenu
{...omit(props, ['icon'])}
Expand All @@ -84,7 +86,7 @@ const SubMenu: React.FC<SubMenuProps> = (props) => {
zIndex,
}}
/>
</MenuContext.Provider>
</MenuContext.Provider>,
);
};

Expand Down
6 changes: 4 additions & 2 deletions components/menu/menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type { MenuContextProps, MenuTheme } from './MenuContext';
import MenuContext from './MenuContext';
import OverrideContext from './OverrideContext';
import useStyle from './style';
import useCSSVar from './style/cssVar';

export interface MenuProps extends Omit<RcMenuProps, 'items'> {
theme?: MenuTheme;
Expand Down Expand Up @@ -120,7 +121,8 @@ const InternalMenu = forwardRef<RcMenuRef, InternalMenuProps>((props, ref) => {
};

const prefixCls = getPrefixCls('menu', customizePrefixCls || overrideObj.prefixCls);
const [wrapSSR, hashId] = useStyle(prefixCls, !override);
const [, hashId] = useStyle(prefixCls, !override);
const wrapCSSVar = useCSSVar(rootPrefixCls);
const menuClassName = classNames(`${prefixCls}-${theme}`, menu?.className, className);

// ====================== Expand Icon ========================
Expand Down Expand Up @@ -156,7 +158,7 @@ const InternalMenu = forwardRef<RcMenuRef, InternalMenuProps>((props, ref) => {
);

// ========================= Render ==========================
return wrapSSR(
return wrapCSSVar(
<OverrideContext.Provider value={null}>
<MenuContext.Provider value={contextValue}>
<RcMenu
Expand Down
8 changes: 8 additions & 0 deletions components/menu/style/cssVar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { genCSSVarRegister } from '../../theme/internal';
import { prepareComponentToken } from '.';

export default genCSSVarRegister('Menu', prepareComponentToken, {
unitless: {
groupTitleLineHeight: true,
},
});
3 changes: 2 additions & 1 deletion components/menu/style/horizontal.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { unit } from '@ant-design/cssinjs';
import type { MenuToken } from '.';
import type { GenerateStyle } from '../../theme/internal';

Expand All @@ -16,7 +17,7 @@ const getHorizontalStyle: GenerateStyle<MenuToken> = (token) => {
[`${componentCls}-horizontal`]: {
lineHeight: horizontalLineHeight,
border: 0,
borderBottom: `${lineWidth}px ${lineType} ${colorSplit}`,
borderBottom: `${unit(lineWidth)} ${lineType} ${colorSplit}`,
boxShadow: 'none',

'&::after': {
Expand Down