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

fix(ui): optimize resize observer and update toolbar item structure of Ribbon #4644

Merged
merged 1 commit into from
Feb 14, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 22 additions & 20 deletions packages/ui/src/views/components/ribbon/Ribbon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import type { IMenuSchema } from '../../../services/menu/menu-manager.service';
import { LocaleService } from '@univerjs/core';
import { clsx } from '@univerjs/design';
import { MoreFunctionSingle } from '@univerjs/icons';
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { IMenuManagerService } from '../../../services/menu/menu-manager.service';
import { MenuManagerPosition, RibbonPosition } from '../../../services/menu/types';
import { useDependency } from '../../../utils/di';
Expand Down Expand Up @@ -69,24 +69,26 @@ export function Ribbon(props: IRibbonProps) {
// resize observer
useEffect(() => {
const observer = new ResizeObserver((entries) => {
const toolbar = entries[0].target;
const toolbarWidth = toolbar.clientWidth;
const toolbarItems = Object.values(toolbarItemRefs.current);
const collapsedIds: string[] = [];
let totalWidth = 0;

const allGroups = ribbon.find((group) => group.key === activatedTab)?.children ?? [];

for (const { el, key } of toolbarItems) {
if (!el) continue;

totalWidth += el?.getBoundingClientRect().width + 8;
if (totalWidth > toolbarWidth - 32 - 8 * (allGroups.length - 1)) {
collapsedIds.push(key);
requestAnimationFrame(() => {
const toolbar = entries[0].target;
const toolbarWidth = toolbar.clientWidth;
const toolbarItems = Object.values(toolbarItemRefs.current);
const collapsedIds: string[] = [];
let totalWidth = 0;

const allGroups = ribbon.find((group) => group.key === activatedTab)?.children ?? [];

for (const { el, key } of toolbarItems) {
if (!el) continue;

totalWidth += el?.getBoundingClientRect().width + 8;
if (totalWidth > toolbarWidth - 32 - 8 * (allGroups.length - 1)) {
collapsedIds.push(key);
}
}
}

setCollapsedIds(collapsedIds);
setCollapsedIds(collapsedIds);
});
});

if (toolbarRef.current) {
Expand Down Expand Up @@ -241,9 +243,9 @@ export function Ribbon(props: IRibbonProps) {
aria-hidden
ref={toolbarRef}
className={`
univer-absolute univer-left-0 univer-right-0 univer-top-[-99999px] univer-mx-auto univer-box-border
univer-flex univer-h-full univer-flex-1 univer-items-center univer-justify-center univer-gap-1
univer-overflow-hidden univer-px-4 univer-opacity-0
univer-invisible univer-absolute univer-left-0 univer-right-0 univer-top-[-99999px] univer-mx-auto
univer-box-border univer-flex univer-h-full univer-flex-1 univer-items-center univer-justify-center
univer-gap-1 univer-overflow-hidden univer-px-4
`}
>
{fakeToolbarContent}
Expand Down