Guidance on dynamically changing the primary color in Ionic 5

Currently, my app-component routes to a split-layout which then navigates to other pages. When I want to change the accent color in the settings page, I use the following workaround:

changeAccentColor() {
    console.log('Accent-color changed to: ', this.selectedAccentColor);
    document.body.style.setProperty('--accentColor', this.selectedAccentColor);
    document.body.style.setProperty('--toggleHead', '#ffffff');
  }

The accent color is applied like this:

<ion-toggle style='--handle-background-checked:var(--toggleHead); --background-checked:var(--accentColor)'>Toggle</ion-toggle>

and in other places like this:

.active-link {
    --ion-text-color: var(--accentColor);
    font-weight: bold;
}

While this method somewhat works, it feels like a hack to me because I am unable to utilize Ionic's standard color handling methods. Is there a way to directly change the primary color during runtime, and if not, what would be the best alternative?

Answer №1

To dynamically change styles in Angular, the most common method is to utilize ngStyle

If ngStyle doesn't work with ion-toggle in your situation, you can directly bind the color variables to the style and modify them as needed throughout your code like so:

In your .ts file:

ngOnInit() {
   this.selectedAccentColor = 'blue';
   this.toggleHeadColor = 'red';
}

changeAccentColor() {
    this.selectedAccentColor = 'green';
    this.toggleHeadColor = 'yellow';
}

And in your .html file:

<ion-toggle style='--handle-background-checked:{{toggleHeadColor}}; --background-checked:{{selectedAccentColor}}'>Toggle</ion-toggle>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for obtaining results from a File Reader

I am currently facing an issue with an onchange event in my HTML. The event is intended to retrieve an image from the user, convert it to a data URL, and then send it over socket.io to store in the database. However, I am struggling to figure out how to ac ...

Exploring the Benefits of Incorporating Multiple Navigations into a JQuery Slider

I'm looking to enhance the functionality of the main slider on this particular page. Currently, there are left and right arrows as well as grey and blue boxes underneath that allow users to navigate between four slides (which repeat using only two ima ...

The dynamic tabs in Angular Material only appear to have a functioning Sidenav on the initial tab

My web application features dynamically created tabs using *ngFor, each tab containing a sidenav-container with the sidenav and content. Each tab should have the ability to show and toggle the sidebar. The components involved are: app: includes tabs and ...

Display the div element only if the router is active and the navbar is visible

I am attempting to display a div underneath each link in a navbar when the page is active. I am looping through each link using *ngFor. What I am trying to achieve looks like this: https://i.sstatic.net/PpY66.png However, what I am currently getting is ...

What steps can I take to ensure that Bootstrap components are compatible with my mobile device?

[![This is the result I see when accessing my page on a mobile device <head> <title>Login Page</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=devic ...

Checkboxes display on a separate line when rendered inlines

I'm having an issue with the rendering of my checkbox in CSS. The checkbox is currently displaying on a separate line from the text, but I would like it to be inline with the rest of the content. For reference, here is the full CSS and HTML code: htt ...

Creating a dynamic background image with automatic cropping techniques: How to do it?

I'm currently working on a custom wordpress theme and I've encountered an issue that I need help with. I want to have a wide background image on my homepage with text centered on it, but I also want the height to remain the same as the browser w ...

What is the best way to set up a condition that only runs if an element does not contain a certain class, such as "active"?

Click here for the picture I have a list of items: a, b, c, d, e, f. Whenever I click on item b, it becomes active and the other elements lose the "active" class. When an element has the "active" class, I want the background to change accordingly. If it ...

Angular code for opening a PDF from an API response in a new tab

It's been nearly three days and I could really use some assistance with this issue I'm facing. Inside my Node application, there is an API call that utilizes HTTPS to connect to an external API and retrieve a PDF file. The PDF code snippet looks ...

Is the 3D cube spinning with a slight perspective glitch?

I've successfully created a spinning cube using html and css. However, I'm facing an issue where pressing the up and down arrow keys causes the cube to rotate in a larger space rather than around its center. I've attempted using margin: 0 a ...

What is the process for incorporating linear-gradient coloring into the background of a Material UI Chip component?

Is it possible to incorporate a linear-gradient below color as a background for Material UI Chip? linear-gradient(to right bottom, #430089, #82ffa1) The version of Material UI I am working with is v0.18.7. <Chip backgroundColor={indigo400} style={{widt ...

What is the best way to customize the interval time for my specific situation?

I'm working on setting an interval in my app and I have the following code: HTML <div class="text"> {{currentItem.name}} </div> <ul> <li ng-repeat="item in items" ng-click="pickItem($index)">{{item.type}}</li> ...

Exploring the SetCustomValidity Function in Angular

I am trying to utilize the html setCustomValidity method for setting up business validation, however the form is not blocking the custom validity. How can I implement this function in Angular? Here is the code snippet: export class AutoComponent { au ...

Adding or changing class in Angular 2 based on specific conditions

Currently, my focus is on building a web application using Angular and the Semantic-UI framework. In order to add a single class to an element based on a specific condition, I have successfully implemented the following syntax: [class.className]='co ...

How to align a list item to the right using CSS

I'm having trouble aligning items in a list to the right and adjusting their width based on content length. Unfortunately, I haven't been able to make it work despite multiple attempts. Take a look at my code snippet below: .messages { over ...

using jquery to trap anchor in unordered list items

This section is dedicated to the menu on the website. The code for this menu can be found in the _Layout.cshtml() page. When the 'Neu Gesellschaft' page loads, I want to change the CSS of the anchor tag a. I attempted the following jQuery code: ...

The issue of binding subjects in an Angular share service

I established a shared service with the following Subject: totalCostSource$ = new Subject<number>(); shareCost(cost: number ) { this.totalCostSource$.next(cost); } Within my component, I have the following code: private incomeTax: num ...

Include an iframe with hidden overflow specifically for Safari browsers

I have a situation where I'm using a third-party iframe to display specific content. The iframe is enclosed within a div container that has border-radius and overflow: hidden properties applied. Strangely, the content inside the iframe doesn't se ...

Can you provide me with information on how to indicate the activated menu item by highlighting the triggering button in Angular 14 and Material Design?

Is there a way to highlight the triggering button in an Angular Material menu, while also highlighting a menu item that matches a routerLink? <button mat-button [matMenuTriggerFor]="animals">Animal index</button> <mat-menu #animals ...

Achieving Vertical Tabs on Larger Screens and Horizontal Tabs on Smaller Screens with Css Bootstrap 4

I'm attempting to create tabs that are vertical on wider screens and horizontal on smaller screens, but I'm unsure of how to accomplish this. I have experimented with adding flex-md-column and flex-lg-column to the nav tabs: <ul class="nav na ...