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(ui): avoid nested button markup in context menu custom selectors (#6698)

白熱8ddbb4b86a78
diff --git a/packages/design/src/components/color-picker/__tests__/__snapshots__/index.spec.tsx.snap b/packages/design/src/components/color-picker/__tests__/__snapshots__/index.spec.tsx.snap
index 07ec1e1c28..dec84255fe 100644
--- a/packages/design/src/components/color-picker/__tests__/__snapshots__/index.spec.tsx.snap
+++ b/packages/design/src/components/color-picker/__tests__/__snapshots__/index.spec.tsx.snap
@@ -226,7 +226,7 @@ exports[`ColorPicker > should render correctly 1`] = `
/>
<button
class="univer-box-border univer-size-5 univer-cursor-pointer univer-rounded-full univer-border univer-border-solid univer-border-transparent univer-bg-gray-300 univer-transition-shadow"
- style="background-color: #771D1D;"
+ style="background-color: #8A2C0D;"
type="button"
/>
<button
@@ -497,7 +497,7 @@ exports[`ColorPicker > should render with value 1`] = `
/>
<button
class="univer-box-border univer-size-5 univer-cursor-pointer univer-rounded-full univer-border univer-border-solid univer-border-transparent univer-bg-gray-300 univer-transition-shadow"
- style="background-color: #771D1D;"
+ style="background-color: #8A2C0D;"
type="button"
/>
<button
@@ -768,7 +768,7 @@ exports[`ColorPicker > should support format hex 1`] = `
/>
<button
class="univer-box-border univer-size-5 univer-cursor-pointer univer-rounded-full univer-border univer-border-solid univer-border-transparent univer-bg-gray-300 univer-transition-shadow"
- style="background-color: #771D1D;"
+ style="background-color: #8A2C0D;"
type="button"
/>
<button
diff --git a/packages/design/src/components/color-picker/__tests__/index.spec.tsx b/packages/design/src/components/color-picker/__tests__/index.spec.tsx
index 0742179298..893cb1730a 100644
--- a/packages/design/src/components/color-picker/__tests__/index.spec.tsx
+++ b/packages/design/src/components/color-picker/__tests__/index.spec.tsx
@@ -22,11 +22,18 @@ import { ColorInput } from '../ColorInput';
import { ColorPicker } from '../ColorPicker';
import { ColorSpectrum } from '../ColorSpectrum';
import { HueSlider } from '../HueSlider';
+import { colorPresets } from '../presets';
import '@testing-library/jest-dom/vitest';
afterEach(cleanup);
describe('ColorPicker', () => {
+ it('should not contain duplicate preset colors', () => {
+ const flattenedPresets = colorPresets.flat().map((color) => color.toUpperCase());
+
+ expect(new Set(flattenedPresets).size).toBe(flattenedPresets.length);
+ });
+
it('should render correctly', () => {
const { container } = render(<ColorPicker />);
diff --git a/packages/design/src/components/color-picker/presets.ts b/packages/design/src/components/color-picker/presets.ts
index 6813ba6d98..e26feed647 100644
--- a/packages/design/src/components/color-picker/presets.ts
+++ b/packages/design/src/components/color-picker/presets.ts
@@ -19,5 +19,5 @@ export const colorPresets = [
['#CDD0D8', '#A4CAFE', '#F8B4B4', '#FDBA8C', '#FAC815', '#84E1BC', '#7EDCE2', '#CABFFD', '#F8B4D9'],
['#979DAC', '#3F83F8', '#F05252', '#FF5A1F', '#D49D0F', '#0DA471', '#0694A2', '#9061F9', '#E74694'],
['#414657', '#1A56DB', '#C81E1E', '#B43403', '#9A6D15', '#046C4E', '#036672', '#6C2BD9', '#BF125D'],
- ['#000000', '#233876', '#771D1D', '#771D1D', '#634312', '#014737', '#014451', '#4A1D96', '#751A3D'],
+ ['#000000', '#233876', '#771D1D', '#8A2C0D', '#634312', '#014737', '#014451', '#4A1D96', '#751A3D'],
];
diff --git a/packages/sheets-ui/src/menu/sheet.menu.ts b/packages/sheets-ui/src/menu/sheet.menu.ts
index c5c4630ba8..b247585640 100644
--- a/packages/sheets-ui/src/menu/sheet.menu.ts
+++ b/packages/sheets-ui/src/menu/sheet.menu.ts
@@ -105,6 +105,7 @@ export function ChangeColorSheetMenuItemFactory(accessor: IAccessor): IMenuSelec
label: {
name: COLOR_PICKER_COMPONENT,
hoverable: false,
+ selectable: false,
},
},
],
diff --git a/packages/ui/src/views/components/context-menu/ContextMenuPanel.tsx b/packages/ui/src/views/components/context-menu/ContextMenuPanel.tsx
index 8f8f8557a2..c6dd6e8c3e 100644
--- a/packages/ui/src/views/components/context-menu/ContextMenuPanel.tsx
+++ b/packages/ui/src/views/components/context-menu/ContextMenuPanel.tsx
@@ -65,6 +65,16 @@ const submenuOverlapOffset = 2;
const submenuVisualGap = 20;
export const CONTEXT_MENU_SUBMENU_PORTAL_ATTR = 'data-u-context-menu-submenu';
+type MenuLabel = IMenuItem['label'] | IValueOption['label'];
+
+function isNonSelectableLabel(label?: MenuLabel) {
+ return typeof label === 'object' && label?.selectable === false;
+}
+
+function isNonHoverableLabel(label?: MenuLabel) {
+ return typeof label === 'object' && label?.hoverable === false;
+}
+
export function ContextMenuPanel(props: IContextMenuPanelProps) {
const { menuType, menuSessionVersion = 0, className, onOptionSelect } = props;
const menuManagerService = useDependency(IMenuManagerService);
@@ -395,6 +405,11 @@ function ContextMenuMenuItem(props: IContextMenuMenuItemProps) {
);
const canExecuteItem = menuItem.type === MenuItemType.BUTTON || menuItem.type === MenuItemType.BUTTON_SELECTOR;
+ const renderAsContainer = isNonSelectableLabel(menuItem.label);
+ const interactiveItemClassName = clsx(itemClassName, isNonHoverableLabel(menuItem.label) && `
+ hover:univer-bg-transparent
+ dark:hover:!univer-bg-transparent
+ `);
return (
<div
@@ -416,40 +431,59 @@ function ContextMenuMenuItem(props: IContextMenuMenuItemProps) {
}
}}
>
- <button
- type="button"
- className={itemClassName}
- disabled={disabled}
- onClick={() => {
- if (hasSubmenu) {
- setSubmenuPositionReady(false);
- setSubmenuVisible(true);
- return;
- }
-
- if (!canExecuteItem) {
- return;
- }
-
- const item = menuItem as IDisplayMenuItem<IMenuButtonItem>;
- onOptionSelect?.({
- commandId: item.commandId,
- value: inputValue,
- id: item.id,
- label: menuKey,
- });
- }}
- >
- {contentNode}
- {hasSubmenu && (
- <MoreIcon
- className={`
- univer-size-3.5 univer-text-gray-400
- dark:!univer-text-gray-200
- `}
- />
+ {renderAsContainer
+ ? (
+ <div
+ className={interactiveItemClassName}
+ aria-disabled={disabled}
+ >
+ {contentNode}
+ {hasSubmenu && (
+ <MoreIcon
+ className={`
+ univer-size-3.5 univer-text-gray-400
+ dark:!univer-text-gray-200
+ `}
+ />
+ )}
+ </div>
+ )
+ : (
+ <button
+ type="button"
+ className={interactiveItemClassName}
+ disabled={disabled}
+ onClick={() => {
+ if (hasSubmenu) {
+ setSubmenuPositionReady(false);
+ setSubmenuVisible(true);
+ return;
+ }
+
+ if (!canExecuteItem) {
+ return;
+ }
+
+ const item = menuItem as IDisplayMenuItem<IMenuButtonItem>;
+ onOptionSelect?.({
+ commandId: item.commandId,
+ value: inputValue,
+ id: item.id,
+ label: menuKey,
+ });
+ }}
+ >
+ {contentNode}
+ {hasSubmenu && (
+ <MoreIcon
+ className={`
+ univer-size-3.5 univer-text-gray-400
+ dark:!univer-text-gray-200
+ `}
+ />
+ )}
+ </button>
)}
- </button>
{hasSubmenu && submenuVisible && (
submenuPortalContainer
@@ -497,38 +531,26 @@ function ContextMenuMenuItem(props: IContextMenuMenuItemProps) {
{selections.map((option, index) => {
const optionKey = `${menuItem.id}-${option.label ?? option.id}-${index}`;
const optionSelected = typeof inputValue !== 'undefined' && String(inputValue) === String(option.value);
-
- return (
- <button
- key={optionKey}
- type="button"
- className={clsx(
- `
- univer-relative univer-flex univer-min-h-8 univer-w-full
- univer-items-center univer-rounded-md univer-border-none
- univer-bg-transparent univer-px-2 univer-text-left
- univer-text-sm
- dark:!univer-text-white
- `,
- option.disabled
- ? 'univer-cursor-not-allowed univer-opacity-60'
- : `
- univer-cursor-pointer
- hover:univer-bg-gray-50
- dark:hover:!univer-bg-gray-600
- `
- )}
- disabled={option.disabled}
- onClick={() => {
- onOptionSelect?.({
- ...option,
- id: menuItem.id,
- label: menuKey,
- commandId: option.commandId,
- });
- }}
- >
- {optionSelected && (
+ const optionSelectable = !isNonSelectableLabel(option.label);
+ const optionHoverable = !isNonHoverableLabel(option.label);
+ const optionClassName = clsx(
+ `
+ univer-relative univer-flex univer-min-h-8 univer-w-full
+ univer-items-center univer-rounded-md univer-border-none
+ univer-bg-transparent univer-px-2 univer-text-left univer-text-sm
+ dark:!univer-text-white
+ `,
+ option.disabled
+ ? 'univer-cursor-not-allowed univer-opacity-60'
+ : optionHoverable && `
+ univer-cursor-pointer
+ hover:univer-bg-gray-50
+ dark:hover:!univer-bg-gray-600
+ `
+ );
+ const optionContentNode = (
+ <>
+ {optionSelectable && optionSelected && (
<CheckMarkIcon
className="
univer-absolute univer-left-0 univer-size-4
@@ -537,7 +559,7 @@ function ContextMenuMenuItem(props: IContextMenuMenuItemProps) {
/>
)}
<span
- className={clsx(contentClassName, optionSelected && `
+ className={clsx(contentClassName, optionSelectable && optionSelected && `
univer-pl-4
`)}
>
@@ -557,7 +579,38 @@ function ContextMenuMenuItem(props: IContextMenuMenuItemProps) {
}}
/>
</span>
- </button>
+ </>
+ );
+
+ return (
+ optionSelectable
+ ? (
+ <button
+ key={optionKey}
+ type="button"
+ className={optionClassName}
+ disabled={option.disabled}
+ onClick={() => {
+ onOptionSelect?.({
+ ...option,
+ id: menuItem.id,
+ label: menuKey,
+ commandId: option.commandId,
+ });
+ }}
+ >
+ {optionContentNode}
+ </button>
+ )
+ : (
+ <div
+ key={optionKey}
+ className={optionClassName}
+ aria-disabled={option.disabled}
+ >
+ {optionContentNode}
+ </div>
+ )
);
})}
</div>