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.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
feat: ensure theme & component mounting points are within `mountContainer` control (#386)
白熱e33581d20a6e
diff --git a/packages/base-sheets/tsconfig.json b/packages/base-sheets/tsconfig.jsonindex b37297a740..7b6893afd0 100644--- a/packages/base-sheets/tsconfig.json+++ b/packages/base-sheets/tsconfig.json@@ -12,5 +12,5 @@"esModuleInterop": true, // auto allowSyntheticDefaultImports true"strictPropertyInitialization": false},- "include": ["src/**/*", "test/**/*"]-}\ No newline at end of file+ "include": ["src/**/*"]+}diff --git a/packages/base-ui/src/components/menu/Menu.tsx b/packages/base-ui/src/components/menu/Menu.tsxindex d9f47b6bd2..edc49cfd06 100644--- a/packages/base-ui/src/components/menu/Menu.tsx+++ b/packages/base-ui/src/components/menu/Menu.tsx@@ -179,7 +179,9 @@ function MenuItem({ menuItem, onClick }: IMenuItemProps) {onClick({ value: inputValue, id: item.id }); // merge cell}}>- <CustomLabel value={inputValue} title={title} label={label} icon={item.icon} onChange={onChange} />+ <span className={styles.menuItemContent}>+ <CustomLabel value={inputValue} title={title} label={label} icon={item.icon} onChange={onChange} />+ </span></RcMenuItem>);};diff --git a/packages/base-ui/src/components/notification/Notification.tsx b/packages/base-ui/src/components/notification/Notification.tsxindex 028c221a55..f85fe4f916 100644--- a/packages/base-ui/src/components/notification/Notification.tsx+++ b/packages/base-ui/src/components/notification/Notification.tsx@@ -1,9 +1,9 @@+import { ConfigContext } from '@univerjs/design';import { CloseSingle, ErrorSingle, SuccessSingle, WarningSingle } from '@univerjs/icons';import clsx from 'clsx';import { useNotification } from 'rc-notification';import { Placement } from 'rc-notification/es/interface';-import { useEffect, useRef } from 'react';-import { createRoot } from 'react-dom/client';+import { useContext, useEffect, useRef } from 'react';import { Subject } from 'rxjs';import styles from './index.module.less';@@ -48,18 +48,6 @@ export interface INotificationMethodOptions {lines?: number;}-interface INotificationProps extends INotificationMethodOptions {- /**- * close button icon- */- closeIcon?: React.ReactNode;-- /**- * The maximum number of displays. When the limit is exceeded, the oldest message will be automatically closed.- */- maxCount?: number;-}-export const notificationObserver = new Subject<INotificationMethodOptions>();export const PureContent = (props: INotificationMethodOptions) => {@@ -82,12 +70,14 @@ export const PureContent = (props: INotificationMethodOptions) => {);};-export function Notification(props: INotificationProps) {- const { maxCount = 3 } = props;+export function Notification() {+ const { mountContainer } = useContext(ConfigContext);+const [api, contextHolder] = useNotification({prefixCls: styles.notification,- maxCount,+ maxCount: 3,closeIcon: <CloseSingle />,+ getContainer: () => mountContainer,motion: {motionName: styles.notificationFade,motionAppear: true,@@ -123,29 +113,8 @@ export function Notification(props: INotificationProps) {return <>{contextHolder}</>;}-class NotificationInstance {- div: HTMLDivElement;- root: ReturnType<typeof createRoot>;-- constructor() {- this.div = document.createElement('div');- document.body.appendChild(this.div);- this.root = createRoot(this.div);-- this.render();- }-- show(options: INotificationMethodOptions) {- notificationObserver.next(options);- }-- render() {- this.root.render(<Notification content="" title="" type="success" />);- }-}--const instance = new NotificationInstance();-export const notification = {- show: (options: INotificationMethodOptions) => instance.show(options),+ show: (options: INotificationMethodOptions) => {+ notificationObserver.next(options);+ },};diff --git a/packages/base-ui/src/controllers/ui/ui-desktop.controller.tsx b/packages/base-ui/src/controllers/ui/ui-desktop.controller.tsxindex 293573f03a..eda058d2a0 100644--- a/packages/base-ui/src/controllers/ui/ui-desktop.controller.tsx+++ b/packages/base-ui/src/controllers/ui/ui-desktop.controller.tsx@@ -187,6 +187,7 @@ function bootStrap(root.render(<ConnectedApp{...options}+ mountContainer={mountContainer}headerComponents={headerComponents}headerMenuComponents={headerMenuComponents}contentComponents={contentComponents}diff --git a/packages/base-ui/src/services/menu/menu.service.ts b/packages/base-ui/src/services/menu/menu.service.tsindex 166c0e3527..0e59d5ea5d 100644--- a/packages/base-ui/src/services/menu/menu.service.ts+++ b/packages/base-ui/src/services/menu/menu.service.ts@@ -51,7 +51,6 @@ export class DesktopMenuService extends Disposable implements IMenuService {this._menuChanged$.next();return toDisposable(() => {- console.log(item.id);this._menuItemMap.delete(item.id);if (Array.isArray(item.positions)) {diff --git a/packages/base-ui/src/views/App.tsx b/packages/base-ui/src/views/App.tsxindex 7bd9c70970..daca50d4ac 100644--- a/packages/base-ui/src/views/App.tsx+++ b/packages/base-ui/src/views/App.tsx@@ -21,6 +21,7 @@ import { DocBars } from './components/doc-bars/DocBars';import { Parts } from './Parts';export interface IUniverAppProps extends IWorkbenchOptions {+ mountContainer: HTMLElement;headerComponents?: Set<() => ComponentType>;contentComponents?: Set<() => ComponentType>;footerComponents?: Set<() => ComponentType>;@@ -37,6 +38,7 @@ export function App(props: IUniverAppProps) {const containerRef = useRef<HTMLDivElement>(null);const {+ mountContainer,headerComponents,headerMenuComponents,contentComponents,@@ -59,13 +61,18 @@ export function App(props: IUniverAppProps) {const [locale, setLocale] = useState<ILocale>(localeService.getLocales() as unknown as ILocale);+ // Create a portal container for injecting global component themes.+ const portalContainer: HTMLElement = document.createElement('div');+ document.body.appendChild(portalContainer);+useEffect(() => {const subscriptions = [localeService.getLocale().locale$.subscribe((locale) => {locale && setLocale(localeService.getLocales() as unknown as ILocale);}),themeService.currentTheme$.subscribe((theme) => {- themeInstance.setTheme(theme);+ themeInstance.setTheme(mountContainer, theme);+ themeInstance.setTheme(portalContainer, theme);}),];@@ -76,7 +83,7 @@ export function App(props: IUniverAppProps) {}, []);return (- <ConfigProvider locale={locale}>+ <ConfigProvider locale={locale} mountContainer={portalContainer}><Container className={style.layoutContainer}><Layout>{/* outer sidebar */}diff --git a/packages/base-ui/src/views/Parts.tsx b/packages/base-ui/src/views/Parts.tsxindex 3b09f208b1..b921a2345b 100644--- a/packages/base-ui/src/views/Parts.tsx+++ b/packages/base-ui/src/views/Parts.tsx@@ -1,3 +1,4 @@+import { Notification } from '../components/notification/Notification';import { ConfirmPart } from './components/confirm-part/ConfirmPart';import { DialogPart } from './components/dialog-part/DialogPart';@@ -6,6 +7,7 @@ export function Parts() {<><DialogPart /><ConfirmPart />+ <Notification /></>);}diff --git a/packages/design/.storybook/preview.tsx b/packages/design/.storybook/preview.tsxindex 4af784f4b2..82c451c698 100644--- a/packages/design/.storybook/preview.tsx+++ b/packages/design/.storybook/preview.tsx@@ -32,7 +32,7 @@ const preview: Preview = {},decorators: [(Story, context) => {- themeInstance.setTheme(themes[context.globals.theme]);+ themeInstance.setTheme(document.body, themes[context.globals.theme]);return (<Story />diff --git a/packages/design/src/components/config-provider/ColorPicker.stories.tsx b/packages/design/src/components/config-provider/ColorPicker.stories.tsxindex ae3d38e682..23afe4dbb0 100644--- a/packages/design/src/components/config-provider/ColorPicker.stories.tsx+++ b/packages/design/src/components/config-provider/ColorPicker.stories.tsx@@ -29,7 +29,7 @@ export const Playground = {<><Button onClick={() => handleChangeLocale(enUS)}>enUS</Button><Button onClick={() => handleChangeLocale(zhCN)}>zhCN</Button>- <ConfigProvider locale={locale}>+ <ConfigProvider locale={locale} mountContainer={document.body}><ColorPicker /></ConfigProvider></>diff --git a/packages/design/src/components/config-provider/ConfigProvider.tsx b/packages/design/src/components/config-provider/ConfigProvider.tsxindex 5a11512825..99d6a3fdc4 100644--- a/packages/design/src/components/config-provider/ConfigProvider.tsx+++ b/packages/design/src/components/config-provider/ConfigProvider.tsx@@ -4,17 +4,17 @@ import { enUS, ILocale } from '../../locale';export interface IConfigProviderProps {children: React.ReactNode;- locale?: ILocale | unknown;+ locale: ILocale;+ mountContainer: HTMLElement;}-export const ConfigContext = createContext<{- locale: ILocale;-}>({+export const ConfigContext = createContext<Omit<IConfigProviderProps, 'children'>>({locale: enUS,+ mountContainer: document.body,});export function ConfigProvider(props: IConfigProviderProps) {- const { children, locale = {} } = props;+ const { children, locale = {}, mountContainer } = props;// set default locale to enUSlet _locale: ILocale;@@ -24,5 +24,5 @@ export function ConfigProvider(props: IConfigProviderProps) {_locale = enUS;}- return <ConfigContext.Provider value={{ locale: _locale }}>{children}</ConfigContext.Provider>;+ return <ConfigContext.Provider value={{ locale: _locale, mountContainer }}>{children}</ConfigContext.Provider>;}diff --git a/packages/design/src/components/dialog/Dialog.tsx b/packages/design/src/components/dialog/Dialog.tsxindex 2de74ba2b1..fa1292d070 100644--- a/packages/design/src/components/dialog/Dialog.tsx+++ b/packages/design/src/components/dialog/Dialog.tsx@@ -1,7 +1,8 @@import RcDialog from 'rc-dialog';-import { useState } from 'react';+import { useContext, useState } from 'react';import Draggable from 'react-draggable';+import { ConfigContext } from '../config-provider/ConfigProvider';import styles from './index.module.less';export interface IDialogProps {@@ -44,6 +45,8 @@ export function Dialog(props: IDialogProps) {const { children, visible = false, title, draggable = false, closeIcon, footer, onClose } = props;const [dragDisabled, setDragDisabled] = useState(false);+ const { mountContainer } = useContext(ConfigContext);+const TitleIfDraggable = draggable ? (<divstyle={{@@ -73,6 +76,7 @@ export function Dialog(props: IDialogProps) {return (<RcDialogprefixCls={styles.dialog}+ getContainer={() => mountContainer}visible={visible}title={TitleIfDraggable}modalRender={modalRender}diff --git a/packages/design/src/components/dropdown/Dropdown.tsx b/packages/design/src/components/dropdown/Dropdown.tsxindex 03160df91d..69e03b4367 100644--- a/packages/design/src/components/dropdown/Dropdown.tsx+++ b/packages/design/src/components/dropdown/Dropdown.tsx@@ -1,7 +1,9 @@import type { ActionType, AlignType } from '@rc-component/trigger';import RcDropdown from 'rc-dropdown';import Placements from 'rc-dropdown/lib/placements';+import { useContext } from 'react';+import { ConfigContext } from '../config-provider/ConfigProvider';import styles from './index.module.less';// TODO: @jikkai Need to limit width@@ -53,10 +55,13 @@ export interface IDropdownProps {export function Dropdown(props: IDropdownProps) {const { trigger = ['click'], placement, children, overlay, alignPoint = false, align, onVisibleChange } = props;+ const { mountContainer } = useContext(ConfigContext);+return (<RcDropdown{...props}prefixCls={styles.dropdown}+ getPopupContainer={() => mountContainer}trigger={trigger}animation="slide-up"placement={placement}diff --git a/packages/design/src/components/tooltip/Tooltip.tsx b/packages/design/src/components/tooltip/Tooltip.tsxindex 032c4c9e94..ffd2b96aec 100644--- a/packages/design/src/components/tooltip/Tooltip.tsx+++ b/packages/design/src/components/tooltip/Tooltip.tsx@@ -1,5 +1,7 @@import RcTooltip from 'rc-tooltip';+import { useContext } from 'react';+import { ConfigContext } from '../config-provider/ConfigProvider';import styles from './index.module.less';import { placements } from './placements';@@ -12,9 +14,12 @@ export interface ITooltipProps {export const Tooltip = (props: ITooltipProps) => {const { children, placement = 'top', title } = props;+ const { mountContainer } = useContext(ConfigContext);+return (<RcTooltipprefixCls={styles.tooltip}+ getTooltipContainer={() => mountContainer}overlay={<div className={styles.tooltipContent}>{typeof title === 'function' ? title() : title}</div>}builtinPlacements={placements}placement={placement}diff --git a/packages/design/src/themes/theme.ts b/packages/design/src/themes/theme.tsindex f719c473cb..54a77d1b79 100644--- a/packages/design/src/themes/theme.ts+++ b/packages/design/src/themes/theme.ts@@ -28,7 +28,7 @@ class Theme {this.styleSheet = document.styleSheets[index];}- setTheme(theme: Record<string, string>) {+ setTheme(root: HTMLElement, theme: Record<string, string>) {// 1. remove old themeif (this.styleSheet) {let index = 0;@@ -50,17 +50,11 @@ class Theme {* before: {--primary-color:"#0188fb",--primary-color-hover:"#5391ff"}* after: {--primary-color:#0188fb;--primary-color-hover:#5391ff;}*/- const currentTheme = Object.fromEntries(- Object.keys(theme).map((item) => [convertToDashCase(item), convertHexToRgb(theme[item])])- );-- // 3. insert new theme- // TODO: CSS selector should be configurable- this.styleSheet.insertRule(- `:root ${JSON.stringify(currentTheme)- .replace(/"/g, '')- .replace(/,(?=--)/g, ';')}`- );+ Object.keys(theme).forEach((key) => {+ const property = convertToDashCase(key);+ const value = convertHexToRgb(theme[key]);+ root.style.setProperty(property, value);+ });}}