A way to access colours of theme for custom CSS

So I created a docked sidebar for navigation, mainly using the tutorial available in the documentation.
However I used NavLinks for the navigation as I couldn't seem to find a Syncfusion alternative to using NavLinks.
Because of this, some custom styling was needed to get the NavLinks to look right in the Sidebar.
The issue I now have is I have used a base colour for the text/icons and a different colour to specify which page is currently selected using the .nav-link.active class, but these colours obviously don't change with the themes. It would be great to specify these colours as "primary", "secondary" etc. as per the current theme, so they change accordingly as I change the theme. But I can't seem to find any documentation pointing to how to access these values.

Is there such a way or am I SOL?

10 Replies 1 reply marked as answer

MS Muthukumar Settu Syncfusion Team January 13, 2021 02:42 PM UTC

Hi Scott Taylor, 
 
Greetings from Syncfusion support.  
 
We have validated your query with the Sidebar component. We would like to let you know that our theme studio doesn’t have support to access the colors of custom CSS apart from Syncfusion component and these custom styles are not customizable with theme studio. So, we suggest you to add manual styles for custom elements in your sample. Please refer to the following links for more details on Sidebar component.  
 
 

Please let us know, if you need any further assistance. 
 
 
Regards,
Muthukumar S 



ST Scott Taylor January 13, 2021 03:00 PM UTC

I'm not using a custom theme, I want a way to access the colours in the built in themes so I can use them elsewhere with elements that are not a part of syncfusion, stuff like the standard NavLinks (since I can't find a Syncfusion alternative), so I can make these elements correspond to the Syncfusion components.

Example of what I would like to do...

<style>
.custom-class {
     color: @Syncfusion.Blazor.Themes.CurrentTheme.PrimaryColour;
}
</style>

or something along those lines. This feature would be ABSOLUTELY invaluable and ascend the usefulness of using Syncfusion for an app absolutely invaluable. If this is not possible yet, you NEED to get this feature in.


SA Shameer Ali Baig Sulaiman Ali Baig Syncfusion Team January 14, 2021 03:42 PM UTC

Hi Scott, 
  
We can customize the controls by using the SCSS file format and compilation. For further details please refer our Blazor UG below.  
   
  
Based on this, we can use theme styles into custom styles. 
  
Please find sample for this below.  
 
  
Please let us know if you need any further assistance.  
  
Regards,  
Shameer Ali Baig S. 



ST Scott Taylor February 12, 2021 11:44 AM UTC

I feel like I'm repeating myself now. I don't want to change a theme or create a theme, I want to use one of the default themes. I want to be able to access the Hex code values (or however they're stored) of the colours used in the theme I'm currently using. 

Once again as an example. Say I want to keep something consistent with the SF components BUT IS NOT AN SF COMPONENT (you know like the <body> element), I want it to be a colour that makes sense for the current theme. The main issue I'm having is that using a dark theme with SF makes the whole app look inconsistent, since things such as the <body> do not change colour to correspond with the theme so you have white text on a white background for example. It would be great to pick out the background colour (IN CODE, NOT BY LOOKING AT A LIST IN A DOC OF THE COLOURS - So it can be dynamic when changing the SF theme) and apply it to the element.

I don't know how many times I can rewrite this request.


MK Muthukumar Kannan Syncfusion Team February 23, 2021 01:27 PM UTC

Hi Scott,

Sorry for the delayed response.

Currently, we are preparing the workaround for your requirement. However, we will update the further details regarding this within two business days(February 25th, 2021). Until then we appreciate your patience.

Please let us know if you have any concerns.
 
Regards,
Muthukumar K



MK Muthukumar Kannan Syncfusion Team February 26, 2021 01:45 PM UTC

Hi Scott,

Thanks for your patience.

We have validated your requirement and created a simple Blazor Server application. In that we have implemented switching dark and light theme by clicking Syncfusion button component. While changing dark mode, the body background color will change to corresponding HEX code value which is mapped from C# property and used in <style> tag.


MainLayout.Razor

              Here, we have created a SfTheme class and added the properties for adding styles. Added the light theme value in OnInitialized method and dark theme value in OnThemeModeChange method by creating SfTheme Instance and append these value to the <style> tag. Please refer the below code snippet.


 
@inherits LayoutComponentBase 
@inject IJSRuntime JSRuntime 
@using Syncfusion.Blazor.Buttons; 
  
<div class="page"> 
    <div class="sidebar"> 
        <NavMenu /> 
    </div> 
  
    <div class="main"> 
        <div class="top-row px-4"> 
            <div class="theme-switcher"> 
                @*Theme mode switcher*@ 
                <SfButton @ref="ThemeMode" IsToggle="true" IsPrimary="true" Content="@themeMode" @onclick="OnThemeModeChange"></SfButton> 
            </div> 
            <a rel='nofollow' href="https://docs.microsoft.com/aspnet/" target="_blank">About</a> 
        </div> 
        <div class="content px-4"> 
            @Body 
        </div> 
    </div> 
</div> 
  
<style> 
.theme-switcher { 
    margin-right: 24px; 
} 
  
.content.px-4 { 
    background-color: @SfThemeRef.GreyLight; 
}   
</style> 
@code { 
    protected SfTheme SfThemeRef { get; set; } 
    private string themeMode = "Dark"; 
    protected SfButton ThemeMode { get; set; } 
  
    public class SfTheme 
    { 
        public string Primary { get; set; } 
        public string GreyLight { get; set; } 
        public string GreyWhite { get; set; } 
  
        public SfTheme(string primary, string greyLight, string greyWhite) 
        { 
            Primary = primary; 
            GreyLight = greyLight; 
            GreyWhite = greyWhite; 
        } 
    } 
  
    /// <summary> 
    /// The mode switcher OnClick event handler. 
    /// </summary> 
    protected async Task OnThemeModeChange(Microsoft.AspNetCore.Components.Web.MouseEventArgs args) 
    { 
        this.themeMode = ThemeMode.Content == "Dark" ? "Light" : "Dark"; 
        // Update the SfTheme properties based on theme mode. 
        SfThemeRef = GetThemeStyle(ThemeMode.Content); 
        // Change Syncfusion theme from JavaScript based on selected theme mode. 
        await JSRuntime.InvokeAsync<object>("ChangeTheme", themeMode); 
    } 
  
    protected SfTheme GetThemeStyle(string themeMode = "Light") 
    { 
        return themeMode == "Dark" ? new SfTheme("#0070f0", "#acacac", "#6e6e6e") : new SfTheme("#317ab9", "#cccccc", "#ffffff"); 
    } 
  
  
    protected override void OnInitialized() 
    { 
        SfThemeRef = GetThemeStyle(); 
    } 
} 


Added JavaScript call to change the themes in ‘wwwroot/script/theme.js’ file.


function ChangeTheme(theme) { 
    var themeMode = theme !== 'Dark' ? '-dark' : ''; 
    var newlink = document.createElement('link'); 
    newlink.setAttribute('id', 'sf-theme'); 
    newlink.setAttribute('rel', 'stylesheet'); 
    newlink.setAttribute('rel='nofollow' href', `_content/Syncfusion.Blazor/styles/bootstrap${themeMode}.css`); 
  
    var prevLink = document.getElementById('sf-theme'); 
  
    var head = document.getElementsByTagName('head')[0]; 
    head.appendChild(newlink); 
    newlink.onload = () => { 
        prevLink.parentElement.removeChild(prevLink); 
    } 
} 
  

Set the ID field to Syncusion Themes link tag and refer the ‘theme.js’ file in “_Host.cshtml” file.


Here, we have documented the list of common variables is used in the Syncfusion Blazor library themes. You can use these variables to customize the corresponding styles.


Also, we have prepared a sample for your reference and the same can be download from the below link.


Please let us know if you need any further assistance on this.

Regards,
Muthukumar K


Marked as answer

AS ashimaz November 22, 2023 09:15 AM UTC

Hi,


I have tried the above code, and I am able to switch between Light and Dark, but its not changing the primary color from the theme mode.


SfTheme("#ff3a43", "#d0ff00", "#d0ff00") > from the second color I am able to change background color, but other two not working, so how do I change primary color using code?


Regards,

Shimaz



BH BharatRam Harikrishnan Syncfusion Team November 25, 2023 09:53 AM UTC

Hi Shimaz,

 

The other two are not working because those values are not set to a property as we did for the working one. You can apply the primary color to a property as shown below,

 

 

Please find the following sample for reference: https://www.syncfusion.com/downloads/support/directtrac/general/ze/BLAZOR~12083129909

 

Additionally, to customize our Syncfusion Blazor component theme you can follow any of the following solutions:

 

Customize using Web Compiler: Customize using Web Compiler: You can customize our existing themes by overriding theme variables using the npm  blazor-themes package directly or through the LibMan Library Manager. For more details, please check out the following UG documentation links:

https://blazor.syncfusion.com/documentation/appearance/themes#npm-package-reference

https://blazor.syncfusion.com/documentation/appearance/themes#libman

 

Customization and Theme Generation: You can customize the styles and generate a new theme that suits your preferences. Please refer to the following link for reference: https://blazor.syncfusion.com/documentation/appearance/theme-studio

 

CSS Overrides: Alternatively, you can override specific CSS styles. You can find the details regarding this at styles and appearance section. Please refer to the following some links for reference:

https://blazor.syncfusion.com/documentation/radio-button/style-and-appearance

https://blazor.syncfusion.com/documentation/check-box/style-and-appearance

 

If we misunderstood your query, could you please provide us with more details regarding your requirement?

 

Regards,

Bharat Ram H



AS ashimaz November 26, 2023 05:04 AM UTC

Hi, is there any way to apply primary color to all components?



BH BharatRam Harikrishnan Syncfusion Team November 27, 2023 09:29 AM UTC

Hi Shimaz,

 

Yes, as mentioned in our previous update, you can customize our existing themes by overriding theme variables using the npm  blazor-themes package. For more details, please check out the following UG documentation links: https://blazor.syncfusion.com/documentation/appearance/themes#npm-package-reference

 

In your case, if you need to customize the primary color of all components in Bootstrap theme, you have to override the variable of our theme, as shown below:

custom.scss

$brand-primary: pink !default;

 

/* @import 'blazor-themes/SCSS-Themes/<Theme name>.scss'; */

@import 'blazor-themes/SCSS-Themes/bootstrap.scss';

 

Please refer to the following image and sample for reference:

 

Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/Sample1119714456

 

If we misunderstood your query, could you please provide us with your specific use case?

 

Regards,

Bharat Ram H


Loader.
Up arrow icon