Skip to content

bug: modal text color is set incorrectly when app and system dark mode settings aren't the same #26060

Closed

Description

@Marius-Romanus

Prerequisites

Ionic Framework Version

  • v4.x
  • v5.x
  • v6.x
  • Nightly

Current Behavior

Hi!,

The CCS has strange behavior when you play with the dark and light theme of ionic and the operating system.

dark ionic, dark windows OK
dark-ionic-dark-windows-ok

dark ionic, light windows KO
dark-ionic-light-windows-ko1
dark-ionic-light-windows-ko2

light ionic, light windows OK
light-ionic-light-windows-ok

light ionic, dark windows KO
light-ionic-dark-windows-ko1
light-ionic-dark-windows-ko2

Expected Behavior

It is expected that the elements be seen, and also pass the contrast color and have a good visibility.

Steps to Reproduce

Create a new project, follow the dark theme guide: https://ionicframework.com/docs/theming/dark-mode#ionic-dark-theme. Remove the media query to use only the body.dark and change the theme both in ionic and in the operating system.

Code Reproduction URL

https://github.com/Marius-Romanus/ionic-bug-css

Ionic Info

@ionic/angular 6.2.9

Additional Information

No response

Activity

  1. Marius-Romanus commented on Oct 17, 2022

    @Marius-Romanus
    Author

    Hello @liamdebeasi, I just wanted to know if you are looking at this, or if I make a solution in my project. Thank you!

  2. liamdebeasi commented on Oct 17, 2022

    @liamdebeasi
    Contributor

    Hey there, apologies for the delay. Yes we will look at this soon.

  3. averyrousseau commented on Oct 20, 2022

    @averyrousseau
    Contributor

    Hello! I'm still trying to isolate the cause of this, but I wanted to give a quick update. The issue seems to be specific to ion-modal, and also shows with components other than ion-datetime. (For example, a random ion-text placed underneath the datetime also exhibits the incorrect text color.)

    As a workaround, you could use ion-popover instead of ion-modal; I haven't been able to replicate the issue that way.

  4. averyrousseau commented on Oct 21, 2022

    @averyrousseau
    Contributor

    It looks like the issue here comes down to ion-modal overwriting the text color with --ion-color-primary-contrast when it shouldn't, or not overwriting it with --ion-text-color when it should. Normal modals don't show this problem because wrapping the modal content with an ion-content sets the text color to what it should be, but including an ion-content isn't the intended usage with ion-datetime-button.

    We should probably move some of the text color responsibility out of ion-content so it isn't required for situations like this.

    There are a few possible workarounds here:

    1. Manually set the color CSS property on either the ion-modal or the ion-datetime, updating the value depending on whether dark mode is toggled or not.
    2. Keep the app's dark mode setting in sync with the device's by leveraging the prefers-color-scheme media query from the default variables.scss, rather than commenting that block out.
    3. Use ion-popover instead of ion-modal, as mentioned previously.
  5. changed the title [-]bug: CSS dark and light, datetime and picker[/-] [+]bug: modal text color is set incorrectly when app and system dark mode settings aren't the same[/+] on Oct 21, 2022
  6. added
    package: core
    type: bug
    on Oct 21, 2022
  7. removed their assignment
    on Oct 24, 2022
  8. riderx commented on Dec 19, 2022

    @riderx

    How to override any ionic element with ease

    I had the same problem, the shadow dom was killing my vibe.

    Because ionic elements are not always present, so even patch them in JS is not easy.

    I found a beautiful and horrible solution in same time:

    Todo

    add this in your global css to detect components addition in the dom

    @keyframes nodeInserted {
      from {
          outline-color: #fff;
      }
      to {
          outline-color: #000;
      }
    }
    
    @-moz-keyframes nodeInserted {
      from {
          outline-color: #fff;
      }
      to {
          outline-color: #000;
      }
    }
    
    @-webkit-keyframes nodeInserted {
      from {
          outline-color: #fff;
      }
      to {
          outline-color: #000;
      }
    }
    
    @-ms-keyframes nodeInserted {
      from {
          outline-color: #fff;
      }
      to {
          outline-color: #000;
      }
    }
    
    @-o-keyframes nodeInserted {
      from {
          outline-color: #fff;
      }
      to {
          outline-color: #000;
      }
    }
    
    .shadow-override {
      animation-duration: 0.01s;
      -o-animation-duration: 0.01s;
      -ms-animation-duration: 0.01s;
      -moz-animation-duration: 0.01s;
      -webkit-animation-duration: 0.01s;
      animation-name: nodeInserted;
      -o-animation-name: nodeInserted;
      -ms-animation-name: nodeInserted;
      -moz-animation-name: nodeInserted;
      -webkit-animation-name: nodeInserted;
    }

    Then add to your datetime the class shadow-override

        <ion-item class="ion-justify-content-between form-row">
          <ion-label position="fixed">Date de début</ion-label>
          <ion-datetime-button slot="end" datetime="ACH_DATE_DEBUT"></ion-datetime-button>
          <ion-popover [keepContentsMounted]="true">
            <ng-template>
              <ion-datetime class="shadow-override"
              id="ACH_DATE_DEBUT" formControlName="ACH_DATE_DEBUT"></ion-datetime>
            </ng-template>
          </ion-popover>
        </ion-item>

    Now add where you need the detection and the patch:

      ionViewDidEnter() {
        document.addEventListener('animationstart', (event) => {
            const myElement = event.target as HTMLElement;
            if (event.animationName === 'nodeInserted' && myElement.tagName === 'ION-DATETIME') {
            const sheet = new CSSStyleSheet();
            sheet.replaceSync(`:host .calendar-day.calendar-day-active::after { background: none; }`);
            const elemStyleSheets                   = myElement.shadowRoot.adoptedStyleSheets;
            myElement.shadowRoot.adoptedStyleSheets = [...elemStyleSheets, sheet];
          }
        }, false);
      }

    My issue was the day-active::after was white and my text was white also, no clue why, impossible to use a part for that.
    It's inspired by http://www.backalleycoder.com/2012/04/25/i-want-a-damnodeinserted/

    Took me 2 hours, I hate shadow dom as much as I like it.

    Bonus

    if you like to have the datetime button be only a date or time button, add this to the watcher:

      if (event.animationName === 'nodeInserted' && myElement.tagName === 'ION-DATETIME-BUTTON' && myElement.classList.contains('no-date')) {
        const sheet = new CSSStyleSheet();
        sheet.replaceSync(`#date-button { display: none; }`);
        const elemStyleSheets                   = myElement.shadowRoot.adoptedStyleSheets;
        myElement.shadowRoot.adoptedStyleSheets = [...elemStyleSheets, sheet];
      }
      if (event.animationName === 'nodeInserted' && myElement.tagName === 'ION-DATETIME-BUTTON' && myElement.classList.contains('no-time')) {
        const sheet = new CSSStyleSheet();
        sheet.replaceSync(`#time-button { display: none; }`);
        const elemStyleSheets                   = myElement.shadowRoot.adoptedStyleSheets;
        myElement.shadowRoot.adoptedStyleSheets = [...elemStyleSheets, sheet];
      }

    And add to ion-datetime-button this:

    • class="shadow-override no-time" to remove button for time picker

    CleanShot 2022-12-19 at 17 16 56@2x

    • class="shadow-override no-date" to remove button date picker

    CleanShot 2022-12-19 at 17 16 30@2x

  9. iAbhinav commented on Feb 1, 2023

    @iAbhinav

    This is similarly happening with me, exclusively on chrome.

    Some components are not rendering correctly in dark mode on chrome, though they are working fine in safari.

    Screenshot 2023-02-01 at 5 06 58 PM

    Screenshot 2023-02-01 at 5 06 48 PM

  10. madmacc commented on Feb 13, 2023

    @madmacc

    I think I'm getting this issue when I first open an ion-datetime in calendar view on Android and also in Chrome.
    If I open the calendar and then close and reopen then the dates are in black if I use this in my global.scss but it does not fix the first open:
    ion-datetime {

    &:not(.datetime-placeholder) {
    color: black;
    }
    }

    Screenshot_20230213-164805

  11. madmacc commented on Feb 13, 2023

    @madmacc

    How to override any ionic element with ease

    I had the same problem, the shadow dom was killing my vibe.

    Because ionic elements are not always present, so even patch them in JS is not easy.

    I found a beautiful and horrible solution in same time:

    Todo

    add this in your global css to detect components addition in the dom

    @keyframes nodeInserted {
      from {
          outline-color: #fff;
      }
      to {
          outline-color: #000;
      }
    }
    
    @-moz-keyframes nodeInserted {
      from {
          outline-color: #fff;
      }
      to {
          outline-color: #000;
      }
    }
    
    @-webkit-keyframes nodeInserted {
      from {
          outline-color: #fff;
      }
      to {
          outline-color: #000;
      }
    }
    
    @-ms-keyframes nodeInserted {
      from {
          outline-color: #fff;
      }
      to {
          outline-color: #000;
      }
    }
    
    @-o-keyframes nodeInserted {
      from {
          outline-color: #fff;
      }
      to {
          outline-color: #000;
      }
    }
    
    .shadow-override {
      animation-duration: 0.01s;
      -o-animation-duration: 0.01s;
      -ms-animation-duration: 0.01s;
      -moz-animation-duration: 0.01s;
      -webkit-animation-duration: 0.01s;
      animation-name: nodeInserted;
      -o-animation-name: nodeInserted;
      -ms-animation-name: nodeInserted;
      -moz-animation-name: nodeInserted;
      -webkit-animation-name: nodeInserted;
    }

    Then add to your datetime the class shadow-override

        <ion-item class="ion-justify-content-between form-row">
          <ion-label position="fixed">Date de début</ion-label>
          <ion-datetime-button slot="end" datetime="ACH_DATE_DEBUT"></ion-datetime-button>
          <ion-popover [keepContentsMounted]="true">
            <ng-template>
              <ion-datetime class="shadow-override"
              id="ACH_DATE_DEBUT" formControlName="ACH_DATE_DEBUT"></ion-datetime>
            </ng-template>
          </ion-popover>
        </ion-item>
    

    Now add where you need the detection and the patch:

      ionViewDidEnter() {
        document.addEventListener('animationstart', (event) => {
            const myElement = event.target as HTMLElement;
            if (event.animationName === 'nodeInserted' && myElement.tagName === 'ION-DATETIME') {
            const sheet = new CSSStyleSheet();
            sheet.replaceSync(`:host .calendar-day.calendar-day-active::after { background: none; }`);
            const elemStyleSheets                   = myElement.shadowRoot.adoptedStyleSheets;
            myElement.shadowRoot.adoptedStyleSheets = [...elemStyleSheets, sheet];
          }
        }, false);
      }

    My issue was the day-active::after was white and my text was white also, no clue why, impossible to use a part for that. It's inspired by http://www.backalleycoder.com/2012/04/25/i-want-a-damnodeinserted/

    Took me 2 hours, I hate shadow dom as much as I like it.

    Bonus

    if you like to have the datetime button be only a date or time button, add this to the watcher:

      if (event.animationName === 'nodeInserted' && myElement.tagName === 'ION-DATETIME-BUTTON' && myElement.classList.contains('no-date')) {
        const sheet = new CSSStyleSheet();
        sheet.replaceSync(`#date-button { display: none; }`);
        const elemStyleSheets                   = myElement.shadowRoot.adoptedStyleSheets;
        myElement.shadowRoot.adoptedStyleSheets = [...elemStyleSheets, sheet];
      }
      if (event.animationName === 'nodeInserted' && myElement.tagName === 'ION-DATETIME-BUTTON' && myElement.classList.contains('no-time')) {
        const sheet = new CSSStyleSheet();
        sheet.replaceSync(`#time-button { display: none; }`);
        const elemStyleSheets                   = myElement.shadowRoot.adoptedStyleSheets;
        myElement.shadowRoot.adoptedStyleSheets = [...elemStyleSheets, sheet];
      }

    And add to ion-datetime-button this:

    • class="shadow-override no-time" to remove button for time picker
    CleanShot 2022-12-19 at 17 16 56@2x
    • class="shadow-override no-date" to remove button date picker
    CleanShot 2022-12-19 at 17 16 30@2x

    This did not work for me.

  12. riderx commented on Feb 13, 2023

    @riderx

    @madmacc are you sure you include the css in root ?

    on my side, I did remove all from ionic, shadow dom is more a pain than a gain.

    I use now https://konstaui.com/ who is less powerful, but just a simple tailwindcss theme

  13. added a commit that references this issue on Apr 24, 2023
    c267b43
  14. thetaPC commented on Apr 24, 2023

    @thetaPC
    Contributor

    Thanks for the issue! This has been resolved via #27207 and will be available in an upcoming release of Ionic.

  15. locked as resolved and limited conversation to collaborators on Apr 24, 2023
  16. thetaPC commented on Apr 26, 2023

    @thetaPC
    Contributor

    The fix has been released in v7.0.4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    package: coretype: bug

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions