Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

fix: fix the problem that submenu can not get css vars (#416)

白熱5d1c63f648bb
diff --git a/packages/base-ui/package.json b/packages/base-ui/package.json
index 3358c2a925..c9c7c3f706 100644
--- a/packages/base-ui/package.json
+++ b/packages/base-ui/package.json
@@ -54,7 +54,6 @@
"@wendellhu/redi": "^0.12.10",
"clsx": "^2.0.0",
"localforage": "^1.10.0",
- "rc-menu": "^9.12.2",
"rc-notification": "^5.3.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
diff --git a/packages/base-ui/src/components/menu/Menu.tsx b/packages/base-ui/src/components/menu/Menu.tsx
index edc49cfd06..fed61cdce3 100644
--- a/packages/base-ui/src/components/menu/Menu.tsx
+++ b/packages/base-ui/src/components/menu/Menu.tsx
@@ -1,8 +1,8 @@
import { isRealNum } from '@univerjs/core';
+import { Menu as DesignMenu, MenuItem as DesignMenuItem, SubMenu as DesignSubMenu } from '@univerjs/design';
import { MoreSingle } from '@univerjs/icons';
import { useDependency } from '@wendellhu/redi/react-bindings';
import clsx from 'clsx';
-import RcMenu, { MenuItem as RcMenuItem, SubMenu as RcSubMenu } from 'rc-menu';
import React, { useState } from 'react';
import { isObservable, of } from 'rxjs';
@@ -23,6 +23,8 @@ import { CustomLabel } from '../custom-label/CustomLabel';
import { useObservable } from '../hooks/observable';
import styles from './index.module.less';
+// TODO: @jikkai disabled and hidden are not working
+
export interface IBaseMenuProps {
parentKey?: string | number;
menuType?: string | string[];
@@ -83,11 +85,10 @@ function MenuOptionsWrapper(props: IBaseMenuProps) {
if (isValueOption) {
return (
- <RcMenuItem
+ <DesignMenuItem
key={key}
eventKey={key}
className={clsx(
- styles.menuItem,
option.disabled ? styles.colsMenuitemDisabled : ''
// String(value) === String(option.value) ? styles.selectItemSelected : '' // Set the background color of Item
)}
@@ -106,21 +107,13 @@ function MenuOptionsWrapper(props: IBaseMenuProps) {
icon={option.icon}
/>
</span>
- </RcMenuItem>
+ </DesignMenuItem>
);
}
const CustomComponent = componentManager.get(option.id) as React.ComponentType<any>;
return (
- <RcMenuItem
- key={key}
- eventKey={key}
- className={clsx(
- styles.menuItem,
- styles.menuItemCustom,
- option.disabled ? styles.colsMenuitemDisabled : ''
- )}
- >
+ <DesignMenuItem key={key} eventKey={key} className={clsx(styles.menuItemCustom)}>
<CustomComponent
onValueChange={(v: string | number) => {
onOptionSelect?.({
@@ -129,16 +122,16 @@ function MenuOptionsWrapper(props: IBaseMenuProps) {
});
}}
/>
- </RcMenuItem>
+ </DesignMenuItem>
);
});
}
export const Menu = (props: IBaseMenuProps) => (
- <RcMenu prefixCls={styles.menu} selectable={false}>
+ <DesignMenu selectable={false}>
<MenuOptionsWrapper {...props} />
<MenuWrapper {...props} />
- </RcMenu>
+ </DesignMenu>
);
interface IMenuItemProps {
@@ -170,10 +163,10 @@ function MenuItem({ menuItem, onClick }: IMenuItemProps) {
const { title, label } = item;
return (
- <RcMenuItem
+ <DesignMenuItem
key={item.id}
eventKey={item.id}
- className={clsx(styles.menuItem, disabled ? styles.menuItemDisabled : '')}
+ className={clsx(disabled ? styles.menuItemDisabled : '')}
// disabled={disabled} // FIXME disabled is not working
onClick={() => {
onClick({ value: inputValue, id: item.id }); // merge cell
@@ -182,7 +175,7 @@ function MenuItem({ menuItem, onClick }: IMenuItemProps) {
<span className={styles.menuItemContent}>
<CustomLabel value={inputValue} title={title} label={label} icon={item.icon} onChange={onChange} />
</span>
- </RcMenuItem>
+ </DesignMenuItem>
);
};
@@ -202,10 +195,10 @@ function MenuItem({ menuItem, onClick }: IMenuItemProps) {
if (selections.length > 0) {
return (
- <RcSubMenu
+ <DesignSubMenu
key={item.id}
eventKey={item.id}
- className={clsx(styles.menuItem, disabled ? styles.menuItemDisabled : '')}
+ className={clsx(disabled ? styles.menuItemDisabled : '')}
popupOffset={[18, 0]}
title={
<span className={styles.menuItemContent}>
@@ -231,16 +224,12 @@ function MenuItem({ menuItem, onClick }: IMenuItemProps) {
}}
/>
)}
- </RcSubMenu>
+ </DesignSubMenu>
);
}
return (
- <RcMenuItem
- key={item.id}
- eventKey={item.id}
- className={clsx(styles.menuItem, disabled ? styles.menuItemDisabled : '')}
- >
+ <DesignMenuItem key={item.id} eventKey={item.id} className={clsx(disabled ? styles.menuItemDisabled : '')}>
<span className={styles.menuItemContent}>
<CustomLabel
title={item.title}
@@ -251,7 +240,7 @@ function MenuItem({ menuItem, onClick }: IMenuItemProps) {
/>
{item.shortcut && ` (${item.shortcut})`}
</span>
- </RcMenuItem>
+ </DesignMenuItem>
);
};
@@ -259,10 +248,10 @@ function MenuItem({ menuItem, onClick }: IMenuItemProps) {
const item = menuItem as IDisplayMenuItem<IMenuSelectorItem>;
return (
- <RcSubMenu
+ <DesignSubMenu
key={item.id}
eventKey={item.id}
- className={clsx(styles.menuItem, disabled ? styles.menuItemDisabled : '')}
+ className={clsx(disabled ? styles.menuItemDisabled : '')}
popupOffset={[18, 0]}
title={
<span className={styles.menuItemContent}>
@@ -272,7 +261,7 @@ function MenuItem({ menuItem, onClick }: IMenuItemProps) {
expandIcon={<MoreSingle className={styles.menuItemMoreIcon} />}
>
{menuItems.length && <MenuWrapper menuType={item.id} parentKey={item.id} onOptionSelect={onClick} />}
- </RcSubMenu>
+ </DesignSubMenu>
);
};
diff --git a/packages/base-ui/src/components/menu/index.module.less b/packages/base-ui/src/components/menu/index.module.less
index 3cb09211a3..e9d5688941 100644
--- a/packages/base-ui/src/components/menu/index.module.less
+++ b/packages/base-ui/src/components/menu/index.module.less
@@ -176,108 +176,4 @@
padding-right: 0;
}
}
-
- .effect() {
- animation-duration: 0.3s;
- animation-fill-mode: both;
- transform-origin: 0 0;
- }
-
- &-open {
- &-slide-up-enter,
- &-slide-up-appear {
- .effect();
- opacity: 0;
- animation-timing-function: cubic-bezier(0.08, 0.82, 0.17, 1);
- animation-play-state: paused;
- }
-
- &-slide-up-leave {
- .effect();
- opacity: 1;
- animation-timing-function: cubic-bezier(0.6, 0.04, 0.98, 0.34);
- animation-play-state: paused;
- }
-
- &-slide-up-enter&-slide-up-enter-active,
- &-slide-up-appear&-slide-up-appear-active {
- animation-name: rcMenuOpenSlideUpIn;
- animation-play-state: running;
- }
-
- &-slide-up-leave&-slide-up-leave-active {
- animation-name: rcMenuOpenSlideUpOut;
- animation-play-state: running;
- }
-
- @keyframes rcMenuOpenSlideUpIn {
- 0% {
- opacity: 0;
- transform-origin: 0% 0%;
- transform: scaleY(0);
- }
- 100% {
- opacity: 1;
- transform-origin: 0% 0%;
- transform: scaleY(1);
- }
- }
- @keyframes rcMenuOpenSlideUpOut {
- 0% {
- opacity: 1;
- transform-origin: 0% 0%;
- transform: scaleY(1);
- }
- 100% {
- opacity: 0;
- transform-origin: 0% 0%;
- transform: scaleY(0);
- }
- }
-
- &-zoom-enter,
- &-zoom-appear {
- opacity: 0;
- .effect();
- animation-timing-function: cubic-bezier(0.08, 0.82, 0.17, 1);
- animation-play-state: paused;
- }
-
- &-zoom-leave {
- .effect();
- animation-timing-function: cubic-bezier(0.6, 0.04, 0.98, 0.34);
- animation-play-state: paused;
- }
-
- &-zoom-enter&-zoom-enter-active,
- &-zoom-appear&-zoom-appear-active {
- animation-name: rcMenuOpenZoomIn;
- animation-play-state: running;
- }
-
- &-zoom-leave&-zoom-leave-active {
- animation-name: rcMenuOpenZoomOut;
- animation-play-state: running;
- }
-
- @keyframes rcMenuOpenZoomIn {
- 0% {
- opacity: 0;
- transform: scale(0, 0);
- }
- 100% {
- opacity: 1;
- transform: scale(1, 1);
- }
- }
- @keyframes rcMenuOpenZoomOut {
- 0% {
- transform: scale(1, 1);
- }
- 100% {
- opacity: 0;
- transform: scale(0, 0);
- }
- }
- }
}
diff --git a/packages/design/package.json b/packages/design/package.json
index a3c98c4734..127dcf2247 100644
--- a/packages/design/package.json
+++ b/packages/design/package.json
@@ -55,6 +55,7 @@
"rc-dropdown": "^4.1.0",
"rc-input": "^1.3.5",
"rc-input-number": "^8.5.0",
+ "rc-menu": "^9.12.2",
"rc-select": "^14.10.0",
"rc-tooltip": "^6.1.2",
"react": "^18.2.0",
diff --git a/packages/design/src/components/menu/Menu.tsx b/packages/design/src/components/menu/Menu.tsx
new file mode 100644
index 0000000000..b34ead1455
--- /dev/null
+++ b/packages/design/src/components/menu/Menu.tsx
@@ -0,0 +1,21 @@
+import RcMenu, { MenuItem as RcMenuItem, MenuItemProps, MenuProps, SubMenu as RcSubMenu, SubMenuProps } from 'rc-menu';
+import React, { useContext } from 'react';
+
+import { ConfigContext } from '../config-provider/ConfigProvider';
+import styles from './index.module.less';
+
+export function Menu(props: MenuProps) {
+ const { mountContainer } = useContext(ConfigContext);
+
+ return React.cloneElement(<RcMenu prefixCls={styles.menu} getPopupContainer={() => mountContainer} />, {
+ ...props,
+ });
+}
+
+export function MenuItem(props: MenuItemProps) {
+ return React.cloneElement(<RcMenuItem className={styles.menuItem} />, { ...props });
+}
+
+export function SubMenu(props: SubMenuProps) {
+ return React.cloneElement(<RcSubMenu className={styles.menuItem} />, { ...props });
+}
diff --git a/packages/design/src/components/menu/index.module.less b/packages/design/src/components/menu/index.module.less
new file mode 100644
index 0000000000..3cb09211a3
--- /dev/null
+++ b/packages/design/src/components/menu/index.module.less
@@ -0,0 +1,283 @@
+.menu {
+ padding: var(--padding-base);
+ margin: 0;
+ font-size: var(--font-size-sm);
+ list-style: none;
+ background-color: rgb(var(--bg-color-secondary));
+ border: 1px solid rgb(var(--border-color));
+ border-radius: var(--border-radius-lg);
+ box-shadow: var(--box-shadow-base);
+ box-sizing: border-box;
+ outline: none;
+
+ &-vertical {
+ display: grid;
+ gap: var(--margin-xxs);
+ }
+
+ &-hidden,
+ &-submenu-hidden {
+ display: none;
+ }
+
+ &-item-group-list {
+ margin: 0;
+ padding: 0;
+ }
+
+ &-item-group-title {
+ color: rgb(var(--text-color));
+ line-height: 1.5;
+ padding: 8px 10px;
+ border-bottom: 1px solid #dedede;
+ }
+
+ &-item-active,
+ &-submenu-active > &-submenu-title {
+ background-color: rgb(var(--bg-color-hover));
+ }
+
+ &-item-custom {
+ background: none;
+ }
+
+ &-item-selected {
+ background-color: rgb(var(--bg-color-hover));
+ // fix chrome render bug
+ transform: translateZ(0);
+ }
+
+ &-submenu-selected {
+ background-color: rgb(var(--bg-color-hover));
+ }
+
+ & > li&-submenu {
+ padding: 0;
+ }
+
+ &-horizontal&-sub,
+ &-vertical&-sub,
+ &-vertical-left&-sub,
+ &-vertical-right&-sub {
+ min-width: 160px;
+ margin-top: 0;
+ }
+
+ &-item {
+ &-content {
+ display: inline-flex;
+ align-items: center;
+ gap: var(--margin-xs);
+ }
+ }
+
+ &-item,
+ &-submenu-title {
+ margin: 0;
+ position: relative;
+ display: block;
+ white-space: nowrap;
+ border-radius: var(--border-radius-base);
+ cursor: pointer;
+
+ // Disabled state sets text to gray and nukes hover/tab effects
+ &.menu-item-disabled,
+ &.menu-submenu-disabled {
+ color: rgb(var(--grey-200)) !important;
+ }
+ }
+ &-item-divider {
+ height: 1px;
+ margin: 1px 0;
+ overflow: hidden;
+ padding: 0;
+ line-height: 0;
+ background-color: #e5e5e5;
+ }
+
+ &-submenu {
+ &-popup {
+ position: absolute;
+
+ .submenu-title-wrapper {
+ padding-right: 20px;
+ }
+ }
+ > .menu {
+ background-color: #fff;
+ }
+ }
+
+ .menu-submenu-title,
+ .menu-item {
+ &-more-icon {
+ color: rgb(var(--text-color));
+ font-size: var(--font-size-sm);
+ }
+ }
+
+ &-horizontal {
+ border: none;
+ border-bottom: 1px solid rgb(var(--border-color));
+ box-shadow: none;
+ white-space: nowrap;
+ overflow: hidden;
+
+ & > .menu-item,
+ & > .menu-submenu > .menu-submenu-title {
+ padding: 15px 20px;
+ }
+
+ & > .menu-submenu,
+ & > .menu-item {
+ border-bottom: 2px solid transparent;
+ display: inline-block;
+ vertical-align: bottom;
+ }
+ }
+
+ &-vertical,
+ &-vertical-left,
+ &-vertical-right,
+ &-inline {
+ & > .menu-item,
+ & > .menu-submenu > .menu-submenu-title {
+ width: 100%;
+ min-height: 28px;
+ padding: var(--padding-xs) var(--padding-sm);
+ box-sizing: border-box;
+ display: flex;
+ align-items: center;
+ justify-content: space-between;
+ }
+ .menu-submenu-arrow {
+ display: inline-block;
+ font-size: inherit;
+ vertical-align: baseline;
+ text-align: center;
+ text-transform: none;
+ text-rendering: auto;
+ position: absolute;
+ right: 16px;
+ line-height: 1.5em;
+ }
+ }
+
+ &-sub&-inline {
+ padding: 0;
+ border: none;
+ border-radius: 0;
+ box-shadow: none;
+
+ & > .menu-item,
+ & > .menu-submenu > .menu-submenu-title {
+ padding-top: 8px;
+ padding-bottom: 8px;
+ padding-right: 0;
+ }
+ }
+
+ .effect() {
+ animation-duration: 0.3s;
+ animation-fill-mode: both;
+ transform-origin: 0 0;
+ }
+
+ &-open {
+ &-slide-up-enter,
+ &-slide-up-appear {
+ .effect();
+ opacity: 0;
+ animation-timing-function: cubic-bezier(0.08, 0.82, 0.17, 1);
+ animation-play-state: paused;
+ }
+
+ &-slide-up-leave {
+ .effect();
+ opacity: 1;
+ animation-timing-function: cubic-bezier(0.6, 0.04, 0.98, 0.34);
+ animation-play-state: paused;
+ }
+
+ &-slide-up-enter&-slide-up-enter-active,
+ &-slide-up-appear&-slide-up-appear-active {
+ animation-name: rcMenuOpenSlideUpIn;
+ animation-play-state: running;
+ }
+
+ &-slide-up-leave&-slide-up-leave-active {
+ animation-name: rcMenuOpenSlideUpOut;
+ animation-play-state: running;
+ }
+
+ @keyframes rcMenuOpenSlideUpIn {
+ 0% {
+ opacity: 0;
+ transform-origin: 0% 0%;
+ transform: scaleY(0);
+ }
+ 100% {
+ opacity: 1;
+ transform-origin: 0% 0%;
+ transform: scaleY(1);
+ }
+ }
+ @keyframes rcMenuOpenSlideUpOut {
+ 0% {
+ opacity: 1;
+ transform-origin: 0% 0%;
+ transform: scaleY(1);
+ }
+ 100% {
+ opacity: 0;
+ transform-origin: 0% 0%;
+ transform: scaleY(0);
+ }
+ }
+
+ &-zoom-enter,
+ &-zoom-appear {
+ opacity: 0;
+ .effect();
+ animation-timing-function: cubic-bezier(0.08, 0.82, 0.17, 1);
+ animation-play-state: paused;
+ }
+
+ &-zoom-leave {
+ .effect();
+ animation-timing-function: cubic-bezier(0.6, 0.04, 0.98, 0.34);
+ animation-play-state: paused;
+ }
+
+ &-zoom-enter&-zoom-enter-active,
+ &-zoom-appear&-zoom-appear-active {
+ animation-name: rcMenuOpenZoomIn;
+ animation-play-state: running;
+ }
+
+ &-zoom-leave&-zoom-leave-active {
+ animation-name: rcMenuOpenZoomOut;
+ animation-play-state: running;
+ }
+
+ @keyframes rcMenuOpenZoomIn {
+ 0% {
+ opacity: 0;
+ transform: scale(0, 0);
+ }
+ 100% {
+ opacity: 1;
+ transform: scale(1, 1);
+ }
+ }
+ @keyframes rcMenuOpenZoomOut {
+ 0% {
+ transform: scale(1, 1);
+ }
+ 100% {
+ opacity: 0;
+ transform: scale(0, 0);
+ }
+ }
+ }
+}
diff --git a/packages/design/src/components/menu/index.ts b/packages/design/src/components/menu/index.ts
new file mode 100644
index 0000000000..4113dbc70a
--- /dev/null
+++ b/packages/design/src/components/menu/index.ts
@@ -0,0 +1 @@
+export { Menu, MenuItem, SubMenu } from './Menu';
diff --git a/packages/design/src/index.ts b/packages/design/src/index.ts
index 3f0709d008..2b387ef5e1 100644
--- a/packages/design/src/index.ts
+++ b/packages/design/src/index.ts
@@ -11,6 +11,7 @@ export { Dropdown, type IDropdownProps } from './components/dropdown';
export { type IInputProps, Input } from './components/input';
export { type IInputNumberProps, InputNumber } from './components/input-number';
export { Content, Footer, Header, type ILayoutProps, Layout, Sider } from './components/layout';
+export { Menu, MenuItem, SubMenu } from './components/menu';
export { type IMessageMethodOptions, type IMessageProps, Message, MessageType } from './components/message';
export { type IRadioProps, Radio } from './components/radio';
export { type IRadioGroupProps, RadioGroup } from './components/radio-group';
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 34bc65996d..6febe7e25b 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -515,9 +515,6 @@ importers:
localforage:
specifier: ^1.10.0
version: 1.10.0
- rc-menu:
- specifier: ^9.12.2
rc-notification:
specifier: ^5.3.0
@@ -726,6 +723,9 @@ importers:
rc-input-number:
specifier: ^8.5.0
+ rc-menu:
+ specifier: ^9.12.2
rc-select:
specifier: ^14.10.0