@arcblock/ux 2.13.21 → 2.13.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/Config/config-provider.d.ts +6 -23
- package/lib/Config/config-provider.js +19 -80
- package/lib/Config/theme-mode-toggle.js +2 -2
- package/lib/NavMenu/images/payment-kit.png +0 -0
- package/lib/NavMenu/products.js +30 -4
- package/lib/NavMenu/style.js +2 -1
- package/lib/PhoneInput/index.js +2 -1
- package/lib/SessionUser/components/quick-login-item.js +4 -4
- package/lib/SessionUser/components/un-login.js +6 -5
- package/lib/Theme/index.d.ts +1 -0
- package/lib/Theme/index.js +1 -0
- package/lib/Theme/theme-provider.d.ts +27 -12
- package/lib/Theme/theme-provider.js +123 -16
- package/lib/Theme/theme.d.ts +5 -4
- package/lib/Theme/theme.js +6 -5
- package/package.json +6 -6
- package/src/Config/config-provider.tsx +21 -103
- package/src/Config/theme-mode-toggle.tsx +2 -2
- package/src/NavMenu/images/payment-kit.png +0 -0
- package/src/NavMenu/products.tsx +21 -4
- package/src/NavMenu/style.ts +2 -1
- package/src/PhoneInput/index.tsx +2 -1
- package/src/SessionUser/components/quick-login-item.tsx +3 -3
- package/src/SessionUser/components/un-login.tsx +5 -4
- package/src/SessionUser/components/user-info.tsx +1 -1
- package/src/Theme/index.ts +1 -0
- package/src/Theme/theme-provider.tsx +144 -16
- package/src/Theme/theme.ts +8 -9
@@ -1,34 +1,18 @@
|
|
1
|
-
import { ReactNode } from 'react';
|
2
|
-
import { type PaletteMode } from '@mui/material';
|
1
|
+
import { type ReactNode } from 'react';
|
3
2
|
import { LocaleProviderProps } from '../Locale/context';
|
4
|
-
import { ThemeProviderProps } from '../Theme/theme-provider';
|
5
|
-
|
6
|
-
type Prefer = 'light' | 'dark' | 'system';
|
7
|
-
export interface ConfigContextType {
|
8
|
-
mode: PaletteMode;
|
9
|
-
prefer?: Prefer;
|
10
|
-
themeOptions: UserThemeOptions;
|
11
|
-
toggleMode: () => void;
|
12
|
-
}
|
13
|
-
export interface ConfigProviderProps extends Omit<LocaleProviderProps, 'translations'>, Omit<ThemeProviderProps, 'theme'> {
|
3
|
+
import { type ThemeProviderProps } from '../Theme/theme-provider';
|
4
|
+
export interface ConfigProviderProps extends Omit<LocaleProviderProps, 'translations'>, ThemeProviderProps {
|
14
5
|
children: ReactNode;
|
15
|
-
prefer?: Prefer;
|
16
|
-
theme?: UserThemeOptions | ((config: ThemeConfig) => UserThemeOptions);
|
17
|
-
disableBlockletTheme?: boolean;
|
18
6
|
translations?: Record<string, any>;
|
19
7
|
}
|
20
8
|
/**
|
21
9
|
* 集中化配置
|
22
10
|
*/
|
23
|
-
export declare function ConfigProvider({ children,
|
24
|
-
export declare namespace ConfigProvider {
|
25
|
-
var useConfig: typeof import("./config-provider").useConfig;
|
26
|
-
}
|
11
|
+
export declare function ConfigProvider({ children, theme, injectFirst, darkSchemeClass, prefer, disableBlockletTheme, enableColorScheme, locale, fallbackLocale, translations, languages, onLoadingTranslation, }: ConfigProviderProps): import("react/jsx-runtime").JSX.Element;
|
27
12
|
export declare function useConfig(): {
|
28
|
-
mode: PaletteMode;
|
29
|
-
prefer?: Prefer;
|
30
|
-
themeOptions: UserThemeOptions;
|
13
|
+
mode: import("@mui/material").PaletteMode;
|
31
14
|
toggleMode: () => void;
|
15
|
+
prefer?: import("../Theme").Prefer;
|
32
16
|
locale: import("../type").Locale;
|
33
17
|
changeLocale: (locale: import("../type").Locale) => void;
|
34
18
|
t: (key: string, data?: Record<string, any>) => string;
|
@@ -38,4 +22,3 @@ export declare function useConfig(): {
|
|
38
22
|
}[];
|
39
23
|
theme: import("@mui/material").Theme;
|
40
24
|
};
|
41
|
-
export {};
|
@@ -1,111 +1,50 @@
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
2
|
-
import { createContext, useContext, useMemo, useState, useCallback, useEffect } from 'react';
|
3
|
-
import { BLOCKLET_THEME_PREFER_KEY } from '@blocklet/theme';
|
4
|
-
import set from 'lodash/set';
|
5
|
-
import { ThemeProvider as EmotionThemeProvider } from '@emotion/react';
|
6
2
|
import { LocaleProvider, useLocaleContext } from '../Locale/context';
|
7
|
-
import ThemeProvider from '../Theme/theme-provider';
|
8
|
-
import {
|
9
|
-
const resolveMode = prefer => {
|
10
|
-
if (prefer) {
|
11
|
-
if (prefer === 'system') {
|
12
|
-
// 取系统默认
|
13
|
-
return getDefaultThemePrefer({
|
14
|
-
theme: {
|
15
|
-
prefer: 'system'
|
16
|
-
}
|
17
|
-
});
|
18
|
-
}
|
19
|
-
return prefer;
|
20
|
-
}
|
21
|
-
return getDefaultThemePrefer();
|
22
|
-
};
|
23
|
-
const ConfigContext = /*#__PURE__*/createContext({});
|
3
|
+
import ThemeProvider, { useColorScheme } from '../Theme/theme-provider';
|
4
|
+
import { useTheme } from '../Theme';
|
24
5
|
/**
|
25
6
|
* 集中化配置
|
26
7
|
*/
|
27
8
|
export function ConfigProvider({
|
28
9
|
children,
|
29
|
-
// theme
|
10
|
+
// theme provider
|
11
|
+
theme,
|
12
|
+
injectFirst,
|
13
|
+
darkSchemeClass,
|
30
14
|
prefer,
|
31
|
-
theme: themeOptions,
|
32
15
|
disableBlockletTheme = false,
|
33
|
-
|
34
|
-
// locale
|
16
|
+
enableColorScheme,
|
17
|
+
// locale provider
|
35
18
|
locale,
|
36
19
|
fallbackLocale,
|
37
20
|
translations = {},
|
38
21
|
languages,
|
39
22
|
onLoadingTranslation
|
40
23
|
}) {
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
} else {
|
49
|
-
result = {
|
50
|
-
...themeOptions
|
51
|
-
};
|
52
|
-
}
|
53
|
-
}
|
54
|
-
set(result, 'palette.mode', mode);
|
55
|
-
set(result, 'mode', mode);
|
56
|
-
return result;
|
57
|
-
}, [mode, themeOptions]);
|
58
|
-
const theme = useMemo(() => {
|
59
|
-
return createTheme({
|
60
|
-
..._themeOptions,
|
61
|
-
disableBlockletTheme
|
62
|
-
});
|
63
|
-
}, [_themeOptions, disableBlockletTheme]);
|
64
|
-
const toggleMode = useCallback(() => {
|
65
|
-
const newMode = mode === 'light' ? 'dark' : 'light';
|
66
|
-
setMode(newMode);
|
67
|
-
localStorage.setItem(BLOCKLET_THEME_PREFER_KEY, newMode);
|
68
|
-
}, [mode, setMode]);
|
69
|
-
const config = useMemo(() => ({
|
70
|
-
mode,
|
71
|
-
themeOptions: _themeOptions,
|
72
|
-
toggleMode,
|
73
|
-
prefer
|
74
|
-
}), [mode, prefer, _themeOptions, toggleMode]);
|
75
|
-
|
76
|
-
// change prefer manually
|
77
|
-
useEffect(() => {
|
78
|
-
if (prefer) {
|
79
|
-
setMode(resolveMode(prefer));
|
80
|
-
}
|
81
|
-
}, [prefer, setMode]);
|
82
|
-
return /*#__PURE__*/_jsx(ConfigContext.Provider, {
|
83
|
-
value: config,
|
24
|
+
return /*#__PURE__*/_jsx(ThemeProvider, {
|
25
|
+
theme: theme,
|
26
|
+
injectFirst: injectFirst,
|
27
|
+
darkSchemeClass: darkSchemeClass,
|
28
|
+
prefer: prefer,
|
29
|
+
disableBlockletTheme: disableBlockletTheme,
|
30
|
+
enableColorScheme: enableColorScheme,
|
84
31
|
children: /*#__PURE__*/_jsx(LocaleProvider, {
|
85
32
|
locale: locale,
|
86
33
|
fallbackLocale: fallbackLocale,
|
87
34
|
translations: translations,
|
88
35
|
onLoadingTranslation: onLoadingTranslation,
|
89
36
|
languages: languages,
|
90
|
-
children:
|
91
|
-
theme: theme,
|
92
|
-
injectFirst: injectFirst,
|
93
|
-
children: /*#__PURE__*/_jsx(EmotionThemeProvider, {
|
94
|
-
theme: theme,
|
95
|
-
children: children
|
96
|
-
})
|
97
|
-
})
|
37
|
+
children: children
|
98
38
|
})
|
99
39
|
});
|
100
40
|
}
|
101
41
|
export function useConfig() {
|
102
42
|
const theme = useTheme();
|
103
43
|
const localeCtx = useLocaleContext();
|
104
|
-
const
|
44
|
+
const colorSchemeCtx = useColorScheme();
|
105
45
|
return {
|
106
46
|
theme,
|
107
47
|
...localeCtx,
|
108
|
-
...
|
48
|
+
...colorSchemeCtx
|
109
49
|
};
|
110
|
-
}
|
111
|
-
ConfigProvider.useConfig = useConfig;
|
50
|
+
}
|
@@ -2,13 +2,13 @@ import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
2
|
import { IconButton } from '@mui/material';
|
3
3
|
import LightModeOutlinedIcon from '@mui/icons-material/LightModeOutlined';
|
4
4
|
import Brightness2OutlinedIcon from '@mui/icons-material/Brightness2Outlined';
|
5
|
-
import {
|
5
|
+
import { useColorScheme } from '../Theme/theme-provider';
|
6
6
|
export default function ThemeModeToggle() {
|
7
7
|
const {
|
8
8
|
mode,
|
9
9
|
toggleMode,
|
10
10
|
prefer
|
11
|
-
} =
|
11
|
+
} = useColorScheme();
|
12
12
|
if (!toggleMode) {
|
13
13
|
if (process.env.NODE_ENV !== 'production') {
|
14
14
|
console.warn('Please ensure the component is wrapped with ConfigProvider context');
|
Binary file
|
package/lib/NavMenu/products.js
CHANGED
@@ -3,7 +3,7 @@ import React from "react";
|
|
3
3
|
import { useRef } from 'react';
|
4
4
|
import { Link } from 'react-router-dom';
|
5
5
|
import { useCreation, useMemoizedFn } from 'ahooks';
|
6
|
-
import { Box, Grid } from '@mui/material';
|
6
|
+
import { Box, Grid, useTheme } from '@mui/material';
|
7
7
|
import SubItemGroup from './sub-item-group';
|
8
8
|
import { Item } from './nav-menu';
|
9
9
|
import { styled } from '../Theme';
|
@@ -1273,6 +1273,7 @@ DidConnectSvg.defaultProps = {
|
|
1273
1273
|
fill: "none",
|
1274
1274
|
xmlns: "http://www.w3.org/2000/svg"
|
1275
1275
|
};
|
1276
|
+
import PaymentKitPng from './images/payment-kit.png';
|
1276
1277
|
const translations = {
|
1277
1278
|
en: {
|
1278
1279
|
groups: {
|
@@ -1300,6 +1301,9 @@ const translations = {
|
|
1300
1301
|
alKit: {
|
1301
1302
|
description: 'Boost apps with AI'
|
1302
1303
|
},
|
1304
|
+
paymentKit: {
|
1305
|
+
description: 'Effortless Crypto & Card Payments'
|
1306
|
+
},
|
1303
1307
|
blockletStore: {
|
1304
1308
|
description: 'Discover & deploy apps'
|
1305
1309
|
},
|
@@ -1364,6 +1368,9 @@ const translations = {
|
|
1364
1368
|
alKit: {
|
1365
1369
|
description: 'AI 赋能应用'
|
1366
1370
|
},
|
1371
|
+
paymentKit: {
|
1372
|
+
description: '便捷的加密货币和银行卡支付'
|
1373
|
+
},
|
1367
1374
|
blockletStore: {
|
1368
1375
|
description: '发现和部署应用程序'
|
1369
1376
|
},
|
@@ -1437,6 +1444,9 @@ export default function Products({
|
|
1437
1444
|
const {
|
1438
1445
|
mode
|
1439
1446
|
} = useNavMenuContext();
|
1447
|
+
const {
|
1448
|
+
palette
|
1449
|
+
} = useTheme();
|
1440
1450
|
const wrapperRef = useRef(null);
|
1441
1451
|
const {
|
1442
1452
|
locale = 'en'
|
@@ -1472,7 +1482,11 @@ export default function Products({
|
|
1472
1482
|
children: "AIGNE"
|
1473
1483
|
}),
|
1474
1484
|
description: t('products.aigne.description'),
|
1475
|
-
icon: /*#__PURE__*/_jsx(AigneSvg, {
|
1485
|
+
icon: /*#__PURE__*/_jsx(AigneSvg, {
|
1486
|
+
style: {
|
1487
|
+
filter: palette.mode === 'dark' ? 'invert(1)' : 'none'
|
1488
|
+
}
|
1489
|
+
})
|
1476
1490
|
}, {
|
1477
1491
|
label: /*#__PURE__*/_jsx(Link, {
|
1478
1492
|
to: `https://www.aistro.io/${locale}`,
|
@@ -1504,6 +1518,18 @@ export default function Products({
|
|
1504
1518
|
}),
|
1505
1519
|
description: t('products.alKit.description'),
|
1506
1520
|
icon: /*#__PURE__*/_jsx(AIKitSvg, {})
|
1521
|
+
}, {
|
1522
|
+
label: /*#__PURE__*/_jsx(Link, {
|
1523
|
+
to: `https://www.blocklet.io/${locale}`,
|
1524
|
+
target: "_blank",
|
1525
|
+
rel: "noreferrer noopener",
|
1526
|
+
children: "Payment Kit"
|
1527
|
+
}),
|
1528
|
+
description: t('products.paymentKit.description'),
|
1529
|
+
icon: /*#__PURE__*/_jsx("img", {
|
1530
|
+
src: PaymentKitPng,
|
1531
|
+
alt: "Payment Kit"
|
1532
|
+
})
|
1507
1533
|
}], [{
|
1508
1534
|
label: /*#__PURE__*/_jsx(Link, {
|
1509
1535
|
to: `https://store.blocklet.dev/?locale=${locale}`,
|
@@ -1555,7 +1581,7 @@ export default function Products({
|
|
1555
1581
|
icon: /*#__PURE__*/_jsx(AbtNetworkSvg, {})
|
1556
1582
|
}], [{
|
1557
1583
|
label: /*#__PURE__*/_jsx(Link, {
|
1558
|
-
to: `https://www.
|
1584
|
+
to: `https://www.blocklet.io/${locale}/blocklet-server`,
|
1559
1585
|
target: "_blank",
|
1560
1586
|
rel: "noreferrer noopener",
|
1561
1587
|
children: "Blocklet Server"
|
@@ -1622,7 +1648,7 @@ export default function Products({
|
|
1622
1648
|
icon: /*#__PURE__*/_jsx(DidConnectSvg, {})
|
1623
1649
|
}]]
|
1624
1650
|
}];
|
1625
|
-
}, [t, locale]);
|
1651
|
+
}, [t, locale, palette]);
|
1626
1652
|
return /*#__PURE__*/_jsx(Wrapper, {
|
1627
1653
|
ref: wrapperRef,
|
1628
1654
|
className: `is-${mode} ${className}`,
|
package/lib/NavMenu/style.js
CHANGED
@@ -126,7 +126,8 @@ export const NavMenuItem = styled('li', {
|
|
126
126
|
width: '32px',
|
127
127
|
height: '32px',
|
128
128
|
marginRight: '16px',
|
129
|
-
border: '1px solid
|
129
|
+
border: '1px solid',
|
130
|
+
borderColor: theme.palette.grey[200],
|
130
131
|
borderRadius: '4px',
|
131
132
|
color: theme.palette.grey[500]
|
132
133
|
},
|
package/lib/PhoneInput/index.js
CHANGED
@@ -178,7 +178,8 @@ export default function PhoneInput({
|
|
178
178
|
|
179
179
|
// 预览模式
|
180
180
|
if (preview) {
|
181
|
-
const
|
181
|
+
const phoneWithCode = addCountryCodeToPhone(phone, getDialCodeByCountry(country));
|
182
|
+
const isValid = phone && validatePhoneNumber(phoneWithCode, country);
|
182
183
|
const canDial = allowDial && isValid;
|
183
184
|
return /*#__PURE__*/_jsxs(Box, {
|
184
185
|
display: "flex",
|
@@ -21,18 +21,18 @@ export default function QuickLoginItem({
|
|
21
21
|
borderRadius: 1,
|
22
22
|
p: 2,
|
23
23
|
transition: 'background-color 0.5s',
|
24
|
+
bgcolor: 'background.paper',
|
24
25
|
'&:hover, &:active': {
|
25
|
-
backgroundColor: '
|
26
|
+
backgroundColor: 'action.hover'
|
26
27
|
},
|
27
28
|
display: 'flex',
|
28
29
|
justifyContent: 'space-between',
|
29
30
|
alignItems: 'center',
|
30
31
|
cursor: 'pointer',
|
31
32
|
'&:hover': {
|
32
|
-
backgroundColor: '
|
33
|
+
backgroundColor: 'action.hover'
|
33
34
|
},
|
34
|
-
width: '100%'
|
35
|
-
backgroundColor: 'white'
|
35
|
+
width: '100%'
|
36
36
|
},
|
37
37
|
onClick: onClick,
|
38
38
|
children: [/*#__PURE__*/_jsxs(Box, {
|
@@ -150,7 +150,7 @@ export default function UnLogin({
|
|
150
150
|
maxWidth: '90vw',
|
151
151
|
borderColor: 'divider',
|
152
152
|
border: '0 !important',
|
153
|
-
boxShadow:
|
153
|
+
boxShadow: 4
|
154
154
|
},
|
155
155
|
children: [/*#__PURE__*/_jsxs(Box, {
|
156
156
|
sx: {
|
@@ -158,7 +158,8 @@ export default function UnLogin({
|
|
158
158
|
alignItems: 'center',
|
159
159
|
gap: 1,
|
160
160
|
p: 2,
|
161
|
-
borderBottom: '1px solid
|
161
|
+
borderBottom: '1px solid',
|
162
|
+
borderColor: 'divider'
|
162
163
|
},
|
163
164
|
children: [loginAppLogo && !currentState.loadAppLogoError ? /*#__PURE__*/_jsx("img", {
|
164
165
|
src: loginAppLogo,
|
@@ -205,8 +206,9 @@ export default function UnLogin({
|
|
205
206
|
overflow: 'hidden',
|
206
207
|
position: 'relative',
|
207
208
|
p: 0,
|
209
|
+
bgcolor: 'background.paper',
|
208
210
|
'&:hover': {
|
209
|
-
|
211
|
+
bgcolor: `${palette.action.hover} !important`
|
210
212
|
}
|
211
213
|
},
|
212
214
|
onClick: () => {
|
@@ -227,8 +229,7 @@ export default function UnLogin({
|
|
227
229
|
}, userSessionItem.id), index < currentState.userSessions.length - 1 ? /*#__PURE__*/_jsx(Divider, {
|
228
230
|
sx: {
|
229
231
|
mx: 2,
|
230
|
-
my: '0px !important'
|
231
|
-
borderColor: '#e4e4e7'
|
232
|
+
my: '0px !important'
|
232
233
|
}
|
233
234
|
}, `${userSessionItem.id}-divider`) : null]
|
234
235
|
}, `${userSessionItem.id}-root`);
|
package/lib/Theme/index.d.ts
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
import type { CreateMUIStyled, Theme } from '@mui/material';
|
2
2
|
import { useTheme } from '@mui/material';
|
3
3
|
export * from './theme';
|
4
|
+
export * from './theme-provider';
|
4
5
|
export { default as ThemeProvider } from './theme-provider';
|
5
6
|
export { useTheme };
|
6
7
|
export declare const styled: CreateMUIStyled<Theme>;
|
package/lib/Theme/index.js
CHANGED
@@ -1,28 +1,43 @@
|
|
1
1
|
import PropTypes from 'prop-types';
|
2
|
+
import { PaletteMode } from '@mui/material';
|
2
3
|
import { Theme } from '@mui/material/styles';
|
3
|
-
|
4
|
+
/** 颜色模式上下文类型 */
|
5
|
+
export interface ColorSchemeContextType {
|
6
|
+
mode: PaletteMode;
|
7
|
+
toggleMode: () => void;
|
8
|
+
prefer?: Prefer;
|
9
|
+
}
|
10
|
+
export declare const ColorSchemeContext: import("react").Context<ColorSchemeContextType>;
|
11
|
+
export declare function useColorScheme(): ColorSchemeContextType;
|
12
|
+
export type UxTheme = Partial<Theme> | ((outerTheme: Partial<Theme>) => Theme);
|
13
|
+
export type Prefer = 'light' | 'dark' | 'system';
|
14
|
+
interface BaseThemeProviderProps {
|
4
15
|
children?: React.ReactNode;
|
5
|
-
theme
|
16
|
+
theme?: UxTheme;
|
6
17
|
injectFirst?: boolean;
|
7
18
|
/** 指定一个类名,DarkSchemeStyles 只会作用于带有该类的元素及其后代 */
|
8
19
|
darkSchemeClass?: string;
|
9
20
|
}
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
21
|
+
interface ColorSchemeProviderProps extends BaseThemeProviderProps {
|
22
|
+
prefer?: Prefer;
|
23
|
+
disableBlockletTheme?: boolean;
|
24
|
+
}
|
25
|
+
export interface ThemeProviderProps extends ColorSchemeProviderProps {
|
26
|
+
/** 下列情况会启用 ColorScheme 功能(让 theme 支持明暗模式切换)
|
27
|
+
* 1. 显示打开 enableColorScheme
|
28
|
+
* 2. 显示设置 prefer
|
29
|
+
* 3. 顶层 ThemeProvider
|
30
|
+
*/
|
31
|
+
enableColorScheme?: boolean;
|
32
|
+
}
|
33
|
+
declare function ThemeProvider({ children, prefer, enableColorScheme, ...props }: ThemeProviderProps): import("react/jsx-runtime").JSX.Element;
|
14
34
|
declare namespace ThemeProvider {
|
15
35
|
var propTypes: {
|
16
36
|
children: PropTypes.Requireable<any>;
|
17
37
|
theme: PropTypes.Requireable<any>;
|
18
38
|
injectFirst: PropTypes.Requireable<boolean>;
|
19
39
|
darkSchemeClass: PropTypes.Requireable<string>;
|
20
|
-
|
21
|
-
var defaultProps: {
|
22
|
-
children: null;
|
23
|
-
theme: Theme;
|
24
|
-
injectFirst: boolean;
|
25
|
-
darkSchemeClass: string;
|
40
|
+
enableColorScheme: PropTypes.Requireable<boolean>;
|
26
41
|
};
|
27
42
|
}
|
28
43
|
export default ThemeProvider;
|
@@ -1,11 +1,39 @@
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
2
|
+
import { createContext, useCallback, useContext, useEffect, useMemo, useState } from 'react';
|
2
3
|
import PropTypes from 'prop-types';
|
3
4
|
import { GlobalStyles } from '@mui/material';
|
4
5
|
import { ThemeProvider as MuiThemeProvider, useTheme } from '@mui/material/styles';
|
5
6
|
import StyledEngineProvider from '@mui/material/StyledEngineProvider';
|
6
7
|
import CssBaseline from '@mui/material/CssBaseline';
|
7
|
-
import
|
8
|
+
import set from 'lodash/set';
|
9
|
+
import { BLOCKLET_THEME_PREFER_KEY } from '@blocklet/theme';
|
10
|
+
import { createTheme, getDefaultThemePrefer, isTheme, lazyThemeConfig } from './theme';
|
8
11
|
const defaultTheme = createTheme();
|
12
|
+
|
13
|
+
/** 颜色模式上下文类型 */
|
14
|
+
|
15
|
+
export const ColorSchemeContext = /*#__PURE__*/createContext({});
|
16
|
+
export function useColorScheme() {
|
17
|
+
return useContext(ColorSchemeContext);
|
18
|
+
}
|
19
|
+
|
20
|
+
/** 根据偏好获取颜色模式 */
|
21
|
+
const resolveMode = prefer => {
|
22
|
+
if (prefer) {
|
23
|
+
if (prefer === 'system') {
|
24
|
+
// 取系统默认
|
25
|
+
return getDefaultThemePrefer({
|
26
|
+
theme: {
|
27
|
+
prefer: 'system'
|
28
|
+
}
|
29
|
+
});
|
30
|
+
}
|
31
|
+
return prefer;
|
32
|
+
}
|
33
|
+
return getDefaultThemePrefer();
|
34
|
+
};
|
35
|
+
|
36
|
+
/** 深色模式全局样式 */
|
9
37
|
function DarkSchemeStyles({
|
10
38
|
className
|
11
39
|
}) {
|
@@ -55,22 +83,26 @@ function DarkSchemeStyles({
|
|
55
83
|
}
|
56
84
|
return null;
|
57
85
|
}
|
58
|
-
/**
|
59
|
-
|
60
|
-
*/
|
61
|
-
export default function ThemeProvider({
|
86
|
+
/** 基础的 theme provider, 可以为 webapp/blocklet 快捷的配置好 mui theme provider */
|
87
|
+
function BaseThemeProvider({
|
62
88
|
children,
|
63
|
-
theme,
|
64
|
-
injectFirst,
|
65
|
-
darkSchemeClass
|
89
|
+
theme = defaultTheme,
|
90
|
+
injectFirst = true,
|
91
|
+
darkSchemeClass = ''
|
66
92
|
}) {
|
93
|
+
const _theme = useMemo(() => {
|
94
|
+
if (isTheme(theme)) return theme;
|
95
|
+
|
96
|
+
// 是 ThemeOptions 则创建一个 theme
|
97
|
+
return createTheme(theme);
|
98
|
+
}, [theme]);
|
67
99
|
return (
|
68
100
|
/*#__PURE__*/
|
69
101
|
// injectFirst 会影响 makeStyles 自定义样式和 mui styles 覆盖问题
|
70
102
|
_jsx(StyledEngineProvider, {
|
71
103
|
injectFirst: injectFirst,
|
72
104
|
children: /*#__PURE__*/_jsxs(MuiThemeProvider, {
|
73
|
-
theme:
|
105
|
+
theme: _theme,
|
74
106
|
children: [/*#__PURE__*/_jsx(CssBaseline, {}), /*#__PURE__*/_jsx(DarkSchemeStyles, {
|
75
107
|
className: darkSchemeClass
|
76
108
|
}), children]
|
@@ -78,15 +110,90 @@ export default function ThemeProvider({
|
|
78
110
|
})
|
79
111
|
);
|
80
112
|
}
|
113
|
+
/** 带颜色模式切换功能的 theme provider */
|
114
|
+
function ColorSchemeProvider({
|
115
|
+
children,
|
116
|
+
theme: themeInput,
|
117
|
+
prefer,
|
118
|
+
disableBlockletTheme = false,
|
119
|
+
...rest
|
120
|
+
}) {
|
121
|
+
const [mode, setMode] = useState(() => resolveMode(prefer));
|
122
|
+
const _themeInput = useMemo(() => {
|
123
|
+
let result = {};
|
124
|
+
const getThemeConfig = lazyThemeConfig(mode);
|
125
|
+
if (themeInput) {
|
126
|
+
if (typeof themeInput === 'function') {
|
127
|
+
result = {
|
128
|
+
...themeInput(getThemeConfig())
|
129
|
+
};
|
130
|
+
} else {
|
131
|
+
result = {
|
132
|
+
...themeInput
|
133
|
+
};
|
134
|
+
}
|
135
|
+
}
|
136
|
+
set(result, 'palette.mode', mode);
|
137
|
+
set(result, 'mode', mode);
|
138
|
+
return result;
|
139
|
+
}, [mode, themeInput]);
|
140
|
+
const theme = useMemo(() => {
|
141
|
+
return createTheme({
|
142
|
+
..._themeInput,
|
143
|
+
disableBlockletTheme
|
144
|
+
});
|
145
|
+
}, [_themeInput, disableBlockletTheme]);
|
146
|
+
|
147
|
+
// 切换明/暗模式
|
148
|
+
const toggleMode = useCallback(() => {
|
149
|
+
const newMode = mode === 'light' ? 'dark' : 'light';
|
150
|
+
setMode(newMode);
|
151
|
+
localStorage.setItem(BLOCKLET_THEME_PREFER_KEY, newMode);
|
152
|
+
}, [mode, setMode]);
|
153
|
+
const colorSchemeValue = useMemo(() => ({
|
154
|
+
mode,
|
155
|
+
toggleMode,
|
156
|
+
prefer
|
157
|
+
}), [mode, prefer, toggleMode]);
|
158
|
+
useEffect(() => {
|
159
|
+
if (prefer) {
|
160
|
+
setMode(resolveMode(prefer));
|
161
|
+
}
|
162
|
+
}, [prefer, setMode]);
|
163
|
+
return /*#__PURE__*/_jsx(ColorSchemeContext.Provider, {
|
164
|
+
value: colorSchemeValue,
|
165
|
+
children: /*#__PURE__*/_jsx(BaseThemeProvider, {
|
166
|
+
theme: theme,
|
167
|
+
...rest,
|
168
|
+
children: children
|
169
|
+
})
|
170
|
+
});
|
171
|
+
}
|
172
|
+
export default function ThemeProvider({
|
173
|
+
children,
|
174
|
+
prefer,
|
175
|
+
enableColorScheme = false,
|
176
|
+
...props
|
177
|
+
}) {
|
178
|
+
const {
|
179
|
+
toggleMode
|
180
|
+
} = useColorScheme();
|
181
|
+
if (enableColorScheme || prefer || !toggleMode) {
|
182
|
+
return /*#__PURE__*/_jsx(ColorSchemeProvider, {
|
183
|
+
prefer: prefer,
|
184
|
+
...props,
|
185
|
+
children: children
|
186
|
+
});
|
187
|
+
}
|
188
|
+
return /*#__PURE__*/_jsx(BaseThemeProvider, {
|
189
|
+
...props,
|
190
|
+
children: children
|
191
|
+
});
|
192
|
+
}
|
81
193
|
ThemeProvider.propTypes = {
|
82
194
|
children: PropTypes.any,
|
83
195
|
theme: PropTypes.any,
|
84
196
|
injectFirst: PropTypes.bool,
|
85
|
-
darkSchemeClass: PropTypes.string
|
86
|
-
|
87
|
-
ThemeProvider.defaultProps = {
|
88
|
-
children: null,
|
89
|
-
theme: defaultTheme,
|
90
|
-
injectFirst: true,
|
91
|
-
darkSchemeClass: ''
|
197
|
+
darkSchemeClass: PropTypes.string,
|
198
|
+
enableColorScheme: PropTypes.bool
|
92
199
|
};
|
package/lib/Theme/theme.d.ts
CHANGED
@@ -7,6 +7,8 @@ import '@fontsource/roboto/latin-700.css';
|
|
7
7
|
import '@fontsource/roboto/latin-ext-400.css';
|
8
8
|
import '@fontsource/roboto/latin-ext-500.css';
|
9
9
|
import '@fontsource/roboto/latin-ext-700.css';
|
10
|
+
/** 是否是 MUI Theme 对象 */
|
11
|
+
export declare function isTheme(obj: any): obj is Theme;
|
10
12
|
export declare function collectFontFamilies(obj?: {
|
11
13
|
fontFamily?: string;
|
12
14
|
}, fontSet?: Set<string>): Set<string>;
|
@@ -20,8 +22,7 @@ export declare function createDefaultThemeOptions(mode?: PaletteMode): ThemeOpti
|
|
20
22
|
export interface UserThemeOptions extends ThemeOptions {
|
21
23
|
disableBlockletTheme?: boolean;
|
22
24
|
}
|
23
|
-
export
|
24
|
-
export declare
|
25
|
-
export declare const
|
26
|
-
export declare const createTheme: (...args: Array<UserThemeOptions | ((config: ThemeConfig) => UserThemeOptions)>) => Theme;
|
25
|
+
export declare function lazyThemeConfig(mode: PaletteMode): () => Partial<Theme>;
|
26
|
+
export declare const create: (...args: Array<UserThemeOptions | ((config: Partial<Theme>) => UserThemeOptions)>) => Theme;
|
27
|
+
export declare const createTheme: (...args: Array<UserThemeOptions | ((config: Partial<Theme>) => UserThemeOptions)>) => Theme;
|
27
28
|
export { deepmerge };
|