Skip to content

Commit

Permalink
JNG-6046 sub themes (#496)
Browse files Browse the repository at this point in the history
  • Loading branch information
noherczeg authored Dec 11, 2024
1 parent 17ef3da commit 6aee3b8
Show file tree
Hide file tree
Showing 32 changed files with 512 additions and 187 deletions.
59 changes: 59 additions & 0 deletions docs/pages/01_ui_react.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,65 @@ const redirectHook: RedirectServiceHook = () => {
If for whatever reason the data's owning View fails it's refresh request, the `getLatestViewData()` method return `null`
====

== Theming

=== Implementing sub-themes

Modelers may define custom "sub-themes" for every visual elements. These sub-theme names can be assigned to any number
of elements on any type of screen.

> Regardless of where and how many we define, the same corresponding hook will be used

Each sub-theme will bring a hook, which we can implement.

*Example:*

__src/custom/hooks/sub-themes/registerTestSubThemeHook.tsx__
[source,typescriptjsx]
----
import { type Theme, alpha } from '@mui/material';
import type { BundleContext } from '@pandino/pandino-api';
import { type SubThemeHook, SUB_THEME_HOOK_INTERFACE_KEY } from '~/theme/SubThemeWrapper';
/**
* Sub Theme Name: nested
*
* Notes:
* Don't forget to remove unused hooks, and related imports afterwards to reduce build time and bundle size!
*/
export function registerNestedSubThemeHook(context: BundleContext) {
context.registerService<SubThemeHook>(SUB_THEME_HOOK_INTERFACE_KEY, nestedSubTheme, {
name: 'nested',
});
}
const nestedSubTheme = (parentTheme: Theme, data?: any) => {
// You can calculate styling variables here, or use other hooks as well.
return {
components: {
// Define component styling for components here, e.g.:
MuiPaper: {
styleOverrides: {
root: {
background: alpha(parentTheme.palette.secondary.main, 0.97),
color: parentTheme.palette.common.white,
},
},
},
},
};
};
----

In case we define a sub-theme for a Group or tabs, the theme properties will propagate down the
component hierarchy.

The deeper we define a sub-theme, the higher its precedence will be.

Any number of any nesting level is possible with sub-themes, but keep in mind if you use calculated
settings based on e.g. page data, it may have a negative impact on performance.

== Opt-in features

=== Generate pro+ MUI code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,33 @@ public static String getApplicationIcon(Application application) {
public static EObject eContainer(EObject eObject) {
return eObject.eContainer();
}

public static String toSafeCamelCase(String input) {
if (input == null || input.isEmpty()) {
return input;
}

StringBuilder result = new StringBuilder();
boolean toUpperCase = false;
boolean lastWasDelimiter = true;

for (char c : input.toCharArray()) {
if (c == '-' || c == '_' || c == ' ') {
if (!lastWasDelimiter) {
toUpperCase = true;
lastWasDelimiter = true;
}
} else {
if (toUpperCase) {
result.append(Character.toUpperCase(c));
toUpperCase = false;
} else {
result.append(Character.toLowerCase(c));
}
lastWasDelimiter = false;
}
}

return result.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,11 @@ public static boolean containerHasCustomComponent(PageContainer container) {
return acc.stream().anyMatch(VisualElement::isCustomImplementation);
}

public static boolean containerHasSubTheme(PageContainer container) {
List<VisualElement> acc = collectElementsOfType(container, new ArrayList<>(), VisualElement.class);
return acc.stream().anyMatch(e -> e.getSubTheme() != null && !e.getSubTheme().isBlank());
}

public static boolean containerHasAssociationButton(PageContainer container) {
List<Button> acc = collectElementsOfType(container, new ArrayList<>(), Button.class);
return acc.stream().anyMatch(b -> b.getActionDefinition().getIsOpenPageAction());
Expand Down Expand Up @@ -543,6 +548,16 @@ public static Set<PageContainer> getPageContainersWithCustomImplementations(Appl
return containers;
}

public static Set<String> getSubThemes(Application app) {
Set<String> subThemes = new HashSet<>();
for (PageContainer container : app.getPageContainers()) {
Set<VisualElement> elements = new HashSet<>();
collectVisualElementsMatchingCondition(container, (v) -> v.getSubTheme() != null && !v.getSubTheme().isBlank(), elements);
subThemes.addAll(elements.stream().map(VisualElement::getSubTheme).collect(Collectors.toSet()));
}
return subThemes;
}

public static String getProxyPropsForCustomImplementation(VisualElement element) {
PageContainer container = element.getPageContainer();
String actionType = pageContainerActionDefinitionTypeName(container);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"@mui/material": "5.15.15",
"@mui/system": "5.15.15",
"@mui/types": "7.2.14",
"@mui/utils": "5.15.14",
"@mui/x-data-grid{{ getMUIDataGridPlanSuffix }}": "7.3.1",
"@mui/x-date-pickers{{ getMUIPickersPlanSuffix }}": "7.3.1",
"@mui/x-tree-view": "7.3.1",
Expand Down
Loading

0 comments on commit 6aee3b8

Please sign in to comment.