Skip to content

Commit 172be23

Browse files
sync style property views with applied comp styles + added useThemeStyles hook
1 parent 9541784 commit 172be23

File tree

1 file changed

+63
-39
lines changed

1 file changed

+63
-39
lines changed

client/packages/lowcoder/src/comps/controls/styleControl.tsx

Lines changed: 63 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,48 @@ const ResetIcon = styled(IconReset)`
829829
}
830830
`;
831831

832+
const useThemeStyles = (
833+
styleKey: string,
834+
props: Record<string, string | undefined>,
835+
) => {
836+
const editorState = useContext(EditorContext);
837+
const {comp, compType} = useContext(CompContext);
838+
const theme = useContext(ThemeContext);
839+
const bgColor = useContext(BackgroundColorContext);
840+
const { themeId } = theme || {};
841+
const isPreviewTheme = themeId === 'preview-theme';
842+
const isDefaultTheme = themeId === 'default-theme-id';
843+
844+
const appSettingsComp = editorState?.getAppSettingsComp();
845+
const preventAppStylesOverwriting = appSettingsComp?.getView()?.preventAppStylesOverwriting;
846+
const { appliedThemeId, preventStyleOverwriting } = (comp?.comp || {});
847+
const appTheme = isPreviewTheme || isDefaultTheme || (!preventStyleOverwriting && !preventAppStylesOverwriting)
848+
? theme?.theme
849+
: defaultTheme;
850+
let compTheme: JSONValue|undefined = {};
851+
if (appliedThemeId !== themeId) {
852+
compTheme = isPreviewTheme || isDefaultTheme || (compType && !preventStyleOverwriting && !preventAppStylesOverwriting)
853+
? {
854+
...(omit(defaultTheme, 'components', 'chart')),
855+
...defaultTheme.components?.[compType]?.[styleKey] as unknown as Record<string, string>,
856+
...(omit(theme?.theme, 'components', 'chart')),
857+
...theme?.theme?.components?.[compType]?.[styleKey] as unknown as Record<string, string>,
858+
}
859+
: defaultTheme.components?.[compType]?.[styleKey];
860+
}
861+
const styleProps = (!comp && !compType) || preventStyleOverwriting || preventAppStylesOverwriting || appliedThemeId === themeId
862+
? props
863+
: {};
864+
865+
return {
866+
appTheme,
867+
styleProps,
868+
bgColor,
869+
compTheme,
870+
compType,
871+
}
872+
};
873+
832874
export function styleControl<T extends readonly SingleColorConfig[]>(
833875
colorConfigs: T,
834876
styleKey: string = '',
@@ -888,39 +930,16 @@ export function styleControl<T extends readonly SingleColorConfig[]>(
888930
return new ControlItemCompBuilder(
889931
childrenMap as ToConstructor<{ [K in Names<T>]: ColorControl }>,
890932
(props) => {
891-
// const compType = useContext(CompTypeContext);
892-
const editorState = useContext(EditorContext);
893-
const {comp, compType} = useContext(CompContext);
894-
const theme = useContext(ThemeContext);
895-
const bgColor = useContext(BackgroundColorContext);
896-
const { themeId } = theme || {};
897-
const isPreviewTheme = themeId === 'preview-theme';
898-
const isDefaultTheme = themeId === 'default-theme-id';
899-
900-
901-
const appSettingsComp = editorState?.getAppSettingsComp();
902-
const preventAppStylesOverwriting = appSettingsComp?.getView()?.preventAppStylesOverwriting;
903-
const { appliedThemeId, preventStyleOverwriting } = (comp?.comp || {});
904-
const appTheme = isPreviewTheme || isDefaultTheme || (!preventStyleOverwriting && !preventAppStylesOverwriting)
905-
? theme?.theme
906-
: defaultTheme;
907-
let compTheme: JSONValue|undefined = {};
908-
if (appliedThemeId !== themeId) {
909-
compTheme = isPreviewTheme || isDefaultTheme || (compType && !preventStyleOverwriting && !preventAppStylesOverwriting)
910-
? {
911-
...(omit(defaultTheme, 'components', 'chart')),
912-
...defaultTheme.components?.[compType]?.[styleKey] as unknown as Record<string, string>,
913-
...(omit(theme?.theme, 'components', 'chart')),
914-
...theme?.theme?.components?.[compType]?.[styleKey] as unknown as Record<string, string>,
915-
}
916-
: defaultTheme.components?.[compType]?.[styleKey];
917-
}
918-
const styleProps = (!comp && !compType) || preventStyleOverwriting || preventAppStylesOverwriting || appliedThemeId === themeId
919-
? props as ColorMap
920-
: {} as ColorMap;
933+
const {
934+
styleProps,
935+
appTheme,
936+
bgColor,
937+
compTheme,
938+
compType,
939+
} = useThemeStyles(styleKey, props as Record<string, string>);
921940

922941
return calcColors(
923-
styleProps,
942+
styleProps as ColorMap,
924943
colorConfigs,
925944
appTheme,
926945
bgColor,
@@ -932,21 +951,26 @@ export function styleControl<T extends readonly SingleColorConfig[]>(
932951
)
933952
.setControlItemData({ filterText: label, searchChild: true })
934953
.setPropertyViewFn((children) => {
935-
const theme = useContext(ThemeContext);
936-
const compType = useContext(CompTypeContext);
937-
const bgColor = useContext(BackgroundColorContext);
938954
const isMobile = useIsMobile();
939-
const compTheme = compType
940-
? theme?.theme?.components?.[compType]?.[styleKey]
941-
: undefined;
955+
const childrenProps = childrenToProps(children) as Record<string, string>;
956+
const {
957+
styleProps,
958+
appTheme,
959+
bgColor,
960+
compTheme,
961+
compType,
962+
} = useThemeStyles(styleKey, childrenProps);
942963

943964
const props = calcColors(
944-
childrenToProps(children) as ColorMap,
965+
styleProps as ColorMap,
945966
colorConfigs,
946-
theme?.theme,
967+
appTheme,
947968
bgColor,
948969
compTheme as Record<string, string> | undefined,
970+
compType,
971+
styleKey,
949972
);
973+
950974
const showReset = Object.values(childrenToProps(children)).findIndex((item) => item) > -1;
951975
return (
952976
<>

0 commit comments

Comments
 (0)