How to apply global theming to custom modules in Design Manager Preview?
SOLVE
Hi all,
I am struggling to understand how I am supposed to access global theming variables (i.e. font family, styling, sizing, line-height, etc.) while I am developing custom modules. I have seen posts that suggest that you can't actually do this and that to test, you must place your custom module into the Page editor. Yet, even while testing this way I am receiving incorrect styling.
Take this code for example:
My global theme is set to use the 'Acumin Pro' font. The intention of this particular module is to display a number using the same font style as the h1 font set in my global theme settings. I've tried this using css classes but find that I am repeating classes across modules, so am trying `require_css`. This is attempting to use variables from theme-overrides.css, though I have also tried directly {{theme.text.font}} or actually using the h1 tag instead of p, which I thought to try custom styling elsewhere. All of this is to no avail:
The failed attempt
The correct output
So, how can I access this global styling? From a classic web development point of view, I could simply import a shared stylesheet. HubSpot seems to do away with this, much to my confusion.
It ultimately depends on how you have your fields.json set up, but assuming you are building off of the HubSpot boilerplate, in order to access the styling that is set in your theme settings, you would need to use syntax that looks something like this:
You may notice that the h1 is set to be whatever is set in the "secondary" font of the "Global Fonts". If you wish to use the primary font for your h1 in the module, then you can utilize it directly:
It ultimately depends on how you have your fields.json set up, but assuming you are building off of the HubSpot boilerplate, in order to access the styling that is set in your theme settings, you would need to use syntax that looks something like this:
You may notice that the h1 is set to be whatever is set in the "secondary" font of the "Global Fonts". If you wish to use the primary font for your h1 in the module, then you can utilize it directly:
How to apply global theming to custom modules in Design Manager Preview?
SOLVE
Thanks for your response @evaldas ! Sadly, it appears that this did not work for me. I do know that the theme I am using initially was created with the HubSpot boilerplate but has since been extended quite a bit.
Could you point me towards documentation on how I might access theme variables if I am not using strictly the boilerplate template? I see that theme-overrides.css exists and see all these variables set there, but have yet to successfully see any kind of success in using those variables. I must be referencing it incorrectly but am not sure where!
How to apply global theming to custom modules in Design Manager Preview?
SOLVE
@bearons while fields.json is the ultimate "source of truth", you can sometimes use the theme-overrides.css to find the appropriate references to the fields you are trying to get values from.
Since variables can be set at the developer's discretion, there isn't really a straighforward/fool-proof way to access them. However, most commonly the variables are set at the top of the "theme-overrides.css" file.
For example, in the boilerplate, the variables for h1 styling look like this:
{% set h1_font = theme.text.h1.font %}
{% set h1_text_transform = theme.text.h1.transform %}
h1_font.style is the same as theme.text.h1.font.style
because the variable "h1_font" has been set as "theme.text.h1.font"
Since you are trying to access this in a module, it might be better for you to not use the variable you see in the theme-overrides.css (in this case it would be "h1_font") but instead find what the variable is set to and use that instead (i.e. "theme.text.h1.font") - because as far as I know the CSS files do not share variables with HTML files.
✔️ Did this post help answer your query? Help the community by marking it as a solution.
How to apply global theming to custom modules in Design Manager Preview?
SOLVE
Thanks again for your reply. I'm wondering if I am making a mistake in how I am attempting to reference the values from the theme. Thanks for your advice, I did discover something happening in this example. Using a similar but different example module:
I followed your advice to discover the corresponding values in my theme's fields.json:
And when I attempt to access the color property, I actually do see it render in the design preview, as you can see the hex code here:
Yet when I reference the same exact theme variables from fields.json in my module.css, I am met with a blank/null value. You can see that in the browswer dev tools here:
I did rediscover the Themes Overview developer documentation but this suggests referencing these values in the same way you are suggesting, so I'm a bit at a loss.