11// https://ant.design/docs/react/customize-theme
22
33import { Utils , RenderSize , Config , ThemeMode , CssVars } from "@react-awesome-query-builder/ui" ;
4+ // @ts -ignore antd v4 doesn't have theme
45import { theme as antdTheme , ConfigProviderProps , GlobalToken } from "antd" ;
56import omitBy from "lodash/omitBy" ;
67import pickBy from "lodash/pickBy" ;
@@ -15,6 +16,10 @@ const { logger, isTruthy } = Utils.OtherUtils;
1516const { setColorOpacity, generateCssVarsForLevels, chroma, isDarkColor, isColor } = Utils . ColorUtils ;
1617
1718const buildAlgorithms = ( darkMode : boolean , compactMode : boolean ) => {
19+ if ( ! antdTheme ) {
20+ // antd v4 doesn't have theme
21+ return { } ;
22+ }
1823 const algorithms : Algorithm [ ] = [
1924 darkMode && antdTheme . darkAlgorithm ,
2025 compactMode && antdTheme . compactAlgorithm ,
@@ -49,22 +54,23 @@ const detectTokenThemeMode = (token: GlobalToken | undefined): ThemeMode | undef
4954const filterBasicTokens = ( token : GlobalToken ) => {
5055 return pickBy ( token , ( tokenValue : string | number | boolean , tokenName : keyof GlobalToken ) => {
5156 return (
52- // preserve primary color!
53- tokenName === "colorPrimary"
54- // seed color tokens - seems fine to reuse
55- || [
56- "colorInfo" ,
57- "colorSuccess" ,
58- "colorWarning" ,
59- "colorError" ,
60- "colorLink" ,
61- ] . includes ( tokenName )
62- // non-color tokens
63- || tokenName . startsWith ( "boxShadow" )
64- || tokenName . startsWith ( "font" )
65- || tokenName . startsWith ( "lineType" )
66- || tokenName . startsWith ( "motion" )
67- || typeof tokenValue !== "string"
57+ typeof tokenName === "string" && (
58+ // preserve primary color!
59+ tokenName === "colorPrimary"
60+ // seed color tokens - seems fine to reuse
61+ || [
62+ "colorInfo" ,
63+ "colorSuccess" ,
64+ "colorWarning" ,
65+ "colorError" ,
66+ "colorLink" ,
67+ ] . includes ( tokenName )
68+ // non-color tokens
69+ || tokenName . startsWith ( "boxShadow" )
70+ || tokenName . startsWith ( "font" )
71+ || tokenName . startsWith ( "lineType" )
72+ || tokenName . startsWith ( "motion" )
73+ ) || typeof tokenValue !== "string"
6874 ) ;
6975 } ) as GlobalToken ;
7076} ;
@@ -75,12 +81,12 @@ const filterTokens = (token: GlobalToken) => {
7581 return filterBasicTokens ( token ) ;
7682} ;
7783
78- export const mergeThemes = ( themeMode : ThemeMode | undefined , existingToken : GlobalToken | undefined , themeConfig : ThemeConfig | undefined , algorithms : Algorithm [ ] ) => {
84+ export const mergeThemes = ( themeMode : ThemeMode | undefined , existingToken : GlobalToken | undefined , themeConfig : ThemeConfig | undefined , algorithms ? : Algorithm [ ] ) => {
7985 const tokenThemeMode = detectTokenThemeMode ( existingToken ) ;
8086 const canInheritToken = ! themeMode || ! existingToken || themeMode == tokenThemeMode ;
8187 const filteredExistingToken = existingToken ? filterTokens ( existingToken ) : undefined ;
8288 const mergedTheme : ThemeConfig = {
83- ...( algorithms . length ? { algorithm : algorithms } : { } ) ,
89+ ...( algorithms ? .length ? { algorithm : algorithms } : { } ) ,
8490 ...( existingToken ? { token : filteredExistingToken } : { } ) ,
8591 inherit : canInheritToken ,
8692 ...( themeConfig ? themeConfig : { } ) ,
@@ -96,24 +102,29 @@ export const mergeThemes = (themeMode: ThemeMode | undefined, existingToken: Glo
96102 return mergedTheme ;
97103} ;
98104
99- const generateCssVars = ( token : GlobalToken , config : Config ) => {
105+ const generateCssVars = ( token : GlobalToken | undefined , config : Config ) => {
100106 // logger.log("generateCssVars - antd token", token);
101- const darkMode = isDarkColor ( token . colorBgBase ) ! ;
107+ const darkMode = isDarkColor ( token ? .colorBgBase ) ! ;
102108 const renderSize = config . settings . renderSize ;
103109 const useThickLeftBorderOnHoverItem = config . settings . designSettings ?. useThickLeftBorderOnHoverItem ?? false ;
104110 const useShadowOnHoverItem = config . settings . designSettings ?. useShadowOnHoverItem ?? false ;
105111
112+ if ( ! token ) {
113+ // antd v4 doesn't have theme
114+ return { } as CssVars ;
115+ }
116+
106117 let sizedBorderRadius ;
107118 switch ( renderSize ) {
108119 case "large" :
109- sizedBorderRadius = token ? .borderRadiusLG ?? token ? .borderRadius ;
120+ sizedBorderRadius = token . borderRadiusLG ?? token . borderRadius ;
110121 break ;
111122 case "small" :
112- sizedBorderRadius = token ? .borderRadiusSM ?? token ? .borderRadius ;
123+ sizedBorderRadius = token . borderRadiusSM ?? token . borderRadius ;
113124 break ;
114125 case "medium" :
115126 default :
116- sizedBorderRadius = token ? .borderRadius ;
127+ sizedBorderRadius = token . borderRadius ;
117128 break ;
118129 }
119130
0 commit comments