Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: custom theme detection #10410

Merged
merged 3 commits into from
Dec 19, 2024
Merged

fix: custom theme detection #10410

merged 3 commits into from
Dec 19, 2024

Conversation

vladitasev
Copy link
Contributor

We cannot rely on the Path field ending on css_variables, we have to follow the <framework>.<lib>.<theme>.<field> pattern, therefore the custom theme name is the third part of the value, split by .

@@ -68,7 +68,7 @@ const processThemeMetadata = (metadata: ThemeMetadata): ThemeDescriptor | undefi
let baseThemeName;

try {
themeName = metadata.Path.match(/\.([^.]+)\.css_variables$/)![1];
themeName = metadata.Path.split(".")![2];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the ! will eventually happen to be undefined, and we are back in the scenario where an exception is thrown and no theme is loaded. Can we provide a better fallback?

Also, this was the case before, but what if a custom theme has a . in the name? is it escaped? should we also check this?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Custom themes can't have a . in the theme name, otherwise they couldn't even be saved by the theming engine.

Starting with SAP/theming-base-content@11.23.0, there is --sapSapThemeId, which might be a good fallback (is always an SAP theme, but that might be what you're after anyways):

themeName = getComputedStyle(document.body).getPropertyValue('--sapSapThemeId');
// is "sap_horizon" for SAP Morning Horizon,
// is "sap_horizon" for custom themes based on SAP Morning Horizon

Maybe you don't even need JavaScript for that anymore, because you can target --sapSapThemeId with CSS container style queries – see Theme-specific CSS for your application.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the insight!

I changed the check a bit - if there are 4 parts (after splitting) then this means we're with the "known" format and themeId is guaranteed to be the third part.

However, if this is not the case (future versions of the metadata), we read the CSS var as this is the standard as of last release.

@vladitasev vladitasev merged commit 303344b into main Dec 19, 2024
10 checks passed
@vladitasev vladitasev deleted the better-custom-theme-detection branch December 19, 2024 08:28
@ui5-webcomponents-bot
Copy link
Collaborator

🎉 This PR is included in version v2.6.0-rc.3 🎉

The release is available on v2.6.0-rc.3

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants