0

I have three CSS styles in my global.scss file. Currently, I am using dark mode, but I want to change them conditionally. How can I do this in Ionic 8 and Angular 18?

Here is my global.scss code.

// @import "@ionic/angular/css/palettes/dark.always.css";
// @import "@ionic/angular/css/palettes/dark.class.css";
@import '@ionic/angular/css/palettes/dark.system.css';

I want to add one toggle button in my app so i can witch theme according to that.

3
  • I do stuff like that with css variables like this Commented Sep 1, 2024 at 17:01
  • This question is similar to: Angular Ionic dark mode toggle. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Sep 2, 2024 at 0:13
  • @SwissCodeMen it's not a similar question in ionic 8 and angular 18 there is no theme/variable.scss file and I want to use default mode which is already available in global.scss Commented Sep 3, 2024 at 15:18

2 Answers 2

2

I find the solution for ionic 8 and angular 18.

// @import "@ionic/angular/css/palettes/dark.always.css";
@import "@ionic/angular/css/palettes/dark.class.css"; // uncomment this
// @import '@ionic/angular/css/palettes/dark.system.css'; // comment this

And toggle the class

document.documentElement.classList.toggle('ion-palette-dark', theme === 'dark');

Here is my full code which i implemented

constructor(){
  this.initializeTheme();
}

initializeTheme() {
  let currentTheme = localStorage.getItem('theme');

  if (!currentTheme) {
    // Default to dark theme if no theme is set
    currentTheme = 'dark';
    localStorage.setItem('theme', currentTheme);
  }

  this.applyTheme(currentTheme);
}

toggleTheme() {
  const currentTheme = localStorage.getItem('theme') || 'dark';
  const newTheme = currentTheme === 'dark' ? 'light' : 'dark';

  this.applyTheme(newTheme);
  localStorage.setItem('theme', newTheme);
}

applyTheme(theme: string) {
  // Update theme and toggle the class
  this.theme = theme === 'dark' ? 'dark' : 'light';
  document.documentElement.classList.toggle('ion-palette-dark', theme === 'dark');
}

Here is my hrml for toggle dark and light theme

<ion-menu-toggle auto-hide="false">
  <ion-item lines="none" detail="false" (click)="toggleTheme()">
    <ion-icon aria-hidden="true" slot="start" [name]="theme === 'dark' ? 'sunny' : 'moon'"></ion-icon>
    <ion-label>{{ theme === 'dark' ? 'Light': 'Dark' }} Mode</ion-label>
  </ion-item>
</ion-menu-toggle>
Sign up to request clarification or add additional context in comments.

Comments

0

To make it conditional make below mentioned changes:

1). comment all thee of these in your global.scss:

// @import "@ionic/angular/css/palettes/dark.always.css";
// @import "@ionic/angular/css/palettes/dark.class.css";
// @import '@ionic/angular/css/palettes/dark.system.css';

2). Go to your theme/variable.scss

Take everything out of @media (prefers-color-scheme: dark) media query and pase it out side (simply comment media query open and close tag).

3). On the basis of your condition you can toggle dark mode on and off:

changeDarkMode(darkodetype: boolean){
   document.body.classList.toggle('dark', darkModetype);
}

Note: Disableing dark mode media query will always shows light mode. you have to enable dark mode programatically.

1 Comment

I want to use the default Ionic dark and light modes that are already available in the global.scss file and there is no option of theme/variable.scss in ionic 8 and angular 18.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.