Skip to content

Commit

Permalink
fix: antd弹层等组件切换主题后 样式不正确
Browse files Browse the repository at this point in the history
  • Loading branch information
汪航洋 authored and cnwhy committed Jun 2, 2024
1 parent 2cb8acf commit 2fad3c3
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview",
"init:monaco": "cpx ./node_modules/monaco-editor/min/** ./public/monaco-editor/min",
"prebuild": "npm run init:monaco"
"predev": "npm run init:monaco"
},
"dependencies": {
"@ant-design/icons": "^5.2.6",
Expand Down
36 changes: 31 additions & 5 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import React, { useState, useEffect, useRef, useMemo } from "react";
import { ConfigProvider, message, theme } from "antd";
import React, {
useState,
useEffect,
useRef,
useMemo,
useLayoutEffect,
useContext,
} from "react";
import { ConfigProvider, message, theme, App as AntdApp } from "antd";
import {
init,
logout,
Expand All @@ -19,6 +26,23 @@ import { useIsDark } from "./uitls/useTheme";

const Manage = React.lazy(() => import("./Pages/Manage"));

const AntdGlobal: React.FC<{ children: React.ReactNode }> = (props) => {
const { locale, theme } = useContext(ConfigProvider.ConfigContext);
useLayoutEffect(() => {
// 处理切换主题后 弹层主题不正常的问题
ConfigProvider.config({
theme: theme,
holderRender: (children) => (
<ConfigProvider theme={theme}>
{children}
</ConfigProvider>
),
});
}, [locale, theme]);

return <AntdApp>{props.children}</AntdApp>;
};

function App() {
const info = useInfo();
// const [gostConfig, setGostConfig] = useState<any>(null);
Expand Down Expand Up @@ -125,9 +149,11 @@ function App() {
theme={{ algorithm: isDark ? theme.darkAlgorithm : undefined }}
locale={zhCN}
>
<React.Suspense fallback="loading...">
{info ? <Manage /> : <Home />}
</React.Suspense>
<AntdGlobal>
<React.Suspense fallback="loading...">
{info ? <Manage /> : <Home />}
</React.Suspense>
</AntdGlobal>
</ConfigProvider>
</Ctx.Provider>
);
Expand Down
11 changes: 8 additions & 3 deletions src/Pages/Manage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
Modal,
Radio,
Row,
Select,
Space,
Switch,
} from "antd";
Expand Down Expand Up @@ -58,7 +57,6 @@ const Manage = () => {
const [loading, setLoading] = useState(false);
const [isSaved, setIsSaved] = useState(true);
const [locals, setLocals] = useState<any[]>([]);

const ref = useRef<any>({});

const updateList = useCallback(async () => {
Expand Down Expand Up @@ -235,7 +233,14 @@ const Manage = () => {
</Form>
</Modal>
</Layout.Header>
<Layout.Content style={{ height: "100%", padding: 16, boxSizing:'border-box', overflow: "auto" }}>
<Layout.Content
style={{
height: "100%",
padding: 16,
boxSizing: "border-box",
overflow: "auto",
}}
>
<Row gutter={[16, 16]} style={{ overflow: "hidden" }}>
{/* <Col {...colSpan} xxl={16}> */}
<ServiceCard colSpan={colSpan}></ServiceCard>
Expand Down
7 changes: 5 additions & 2 deletions src/components/Forms/Json.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import React, { useRef } from "react";
import ReactDOM from "react-dom/client";
import { Button, Dropdown, Form, FormInstance, Space } from "antd";
import { Button, Dropdown, Form, FormInstance, Space, ConfigProvider } from "antd";
import { DownOutlined } from "@ant-design/icons";
import { jsonFormat, jsonStringFormat, jsonParse, jsonEdit } from "../../uitls";
import { Template } from "../../templates";
// import { render } from "react-dom";
// import { useServerConfig } from "../../uitls/server";
import ModalForm, { ModalFormProps } from "../ModalForm";
import { CodeEditor } from "../../uitls/useMonacoEdit";
import { globalConfig } from "antd/es/config-provider";
import { HookAPI } from "antd/es/modal/useModal";

const template2menu: any = (template: Template) => {
const { children, ...other } = template;
Expand Down Expand Up @@ -206,9 +208,10 @@ export const showJsonForm = (props: JsonFromProps) => {
const root = ReactDOM.createRoot(container);
function render({ ...props }: JsonFromProps) {
clearTimeout(timeoutId);
const config = globalConfig();
timeoutId = setTimeout(() => {
document.body.append(container);
root.render(<JsonForm {...props} />);
root.render(<ConfigProvider theme={config.getTheme()}><JsonForm {...props} /></ConfigProvider>);
}, 100);
}

Expand Down
12 changes: 6 additions & 6 deletions src/components/List/Public.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import React, {
useMemo,
useRef,
} from "react";
import { Button, Popconfirm, Space, Table } from "antd";
import { Button, Popconfirm, Space, Table, App } from "antd";
import { red, green } from "@ant-design/colors";
import { getRESTfulApi } from "../../api";
import JsonForm, { showJsonForm } from "../Forms/Json";
import { showJsonForm } from "../Forms/Json";
import { jsonFormatValue, jsonParse } from "../../uitls";
import {
CheckCircleOutlined,
Expand All @@ -17,7 +17,7 @@ import {
EditOutlined,
StopOutlined,
} from "@ant-design/icons";
import { GostCommit, fixOldCacheConfig } from "../../api/local";
import { GostCommit } from "../../api/local";
import Ctx, { CardCtx } from "../../uitls/ctx";
import { useListData1, UseTemplates } from "../ListCard/hooks";
import { configEvent } from "../../uitls/events";
Expand All @@ -31,7 +31,7 @@ export type PublicListProps = {
rowKey?: string;
keyword?: string;
renderConfig?: (v: any, r: any, i: number) => React.ReactNode;
filter?: (item: any, keyord: string) => boolean;
filter?: (item: any, keyord: string) => boolean;
};

export const UpdateCtx = React.createContext<{ update?: (v?: any) => any }>({});
Expand All @@ -55,7 +55,6 @@ const PublicList: React.FC<PublicListProps> = (props) => {
keyword,
renderConfig = defaultRenderConfig,
filter = defFilter,

} = props;
const { localList, comm } = useContext(CardCtx);
const { gostConfig, localConfig } = useContext(Ctx);
Expand All @@ -66,6 +65,7 @@ const PublicList: React.FC<PublicListProps> = (props) => {
localConfig,
});
const templates = UseTemplates({ name: keyName });
const { modal, message, notification } = App.useApp();
const {
deleteValue,
updateValue,
Expand Down Expand Up @@ -172,7 +172,7 @@ const PublicList: React.FC<PublicListProps> = (props) => {
render: (value, record, index) => {
// console.log("render", record);
const isEnable = dataList.includes(record);
const content = {...record};
const content = { ...record };
delete content.status;
return (
<Space size={2}>
Expand Down
2 changes: 1 addition & 1 deletion src/uitls/useTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const useIsDark = ()=> {
const sysIsDark = useSysIsDark();
const { theme: userTheme } = useSettings();
const isDark = useMemo(() => {
console.log('useIsDark')
// console.log('useIsDark')
if (!userTheme || userTheme === "system") {
return sysIsDark;
} else {
Expand Down

0 comments on commit 2fad3c3

Please sign in to comment.