Update module to use CSS variables instead of adding inline CSS.

Created on 9 August 2024, 11 months ago

Problem/Motivation

The current implementation of the Environment Indicator module adds inline CSS to the page to style the environment indicator banner. This approach can be limiting and doesn't leverage modern CSS best practices. Inline CSS can make it harder to customize and override styles, and it increases the overall size of the HTML. Using CSS variables instead of inline CSS would make the module more flexible, easier to maintain, and more compatible with various theming needs.

Steps to reproduce

Search the codebase for background-color: https://git.drupalcode.org/search?search=background-color&nav_source=nav...

  1. Install and enable the Environment Indicator module.
  2. Configure the environment indicator to display a banner for a specific environment (e.g., Development, Staging).
  3. Inspect the HTML elements where the environment indicator is applied.
  4. Observe that the styles for the environment indicator banner are added as inline CSS.

Proposed resolution

Refactor the module to use CSS variables instead of inline CSS for styling the environment indicator banner. This will involve:

  • Defining CSS variables in a global stylesheet that can be overridden by themes.
  • Updating the module’s logic to apply styles using the defined CSS variables rather than injecting inline styles.

Remaining tasks

  1. Identify all instances where inline CSS is used within the module.
  2. Create a global CSS file for the module that defines the necessary CSS variables.
  3. Refactor the code to use CSS variables instead of inline styles.
  4. Test the changes across different environments to ensure consistency and compatibility.
  5. Update documentation to reflect the new approach.

User interface changes

  • Users may notice a slight change in how custom styles are applied to the environment indicator banner, depending on how CSS variables are overridden.
  • Theme developers will have the ability to easily customize the environment indicator banner by overriding CSS variables.

API changes

N/A

Data model changes

N/A

πŸ“Œ Task
Status

Active

Version

4.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States trackleft2 Tucson, AZ πŸ‡ΊπŸ‡Έ

Live updates comments and jobs are added and updated live.
  • CSS

    It involves the content or handling of Cascading Style Sheets.

Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @trackleft2
  • πŸ‡ΊπŸ‡ΈUnited States michaellander

    Thanks for creating the issue! For anyone interested, I put some of this in ✨ Support for core navigation experimental module Needs work .

    Specifically:
    https://git.drupalcode.org/issue/environment_indicator-3457688/-/blob/3457688-support-for-core/environment_indicator.module#L144

    However, I stopped short of adding it for non-active environments. Was looking for more feedback!

  • πŸ‡ΊπŸ‡ΈUnited States trackleft2 Tucson, AZ πŸ‡ΊπŸ‡Έ

    @michaellander I think your idea definitely has merit and should be implemented across the board. +1 from me.

  • πŸ‡ΊπŸ‡ΈUnited States trackleft2 Tucson, AZ πŸ‡ΊπŸ‡Έ

    In my initial merge request I eliminate the need for Javascript to set the background-color and color style properties of the toolbar nav element.

    This is done by adding a data-attribute to the toolbar nav element if the toolbar integration is enabled, and then targeting that element via CSS in a new toolbar specific library.

    This fixes the Flash before js issue.
    It also inadvertently fixes the issue where the toolbar is colored even if the toolbar integration is disabled.

    Next we can work on how those switchers work.

  • πŸ‡ΊπŸ‡ΈUnited States michaellander

    Nice work! For the switchers, we could probably just use Drupal\Component\Utility\Html::getClass() on the environment label to generate a wrapper class name, and use that class to switch the variable values in a given context. Could always wrap the name generation in a utility function/method?

    :root {
      --environment-indicator-bg-color: blue;
    }
    .environment-indicator--production {
      --environment-indicator-bg-color: red;
    }
    .environment-indicator--develop {
      --environment-indicator-bg-color: green;
    }
    

    Thoughts?

  • πŸ‡ΊπŸ‡ΈUnited States trackleft2 Tucson, AZ πŸ‡ΊπŸ‡Έ

    I was able to style the environment switchers using CSS variables in the most recent version of the open merge request.
    I think there is still an issue with the non-toolbar version of the indicator's environment indicator switcher dropdown.

  • πŸ‡ΊπŸ‡ΈUnited States trackleft2 Tucson, AZ πŸ‡ΊπŸ‡Έ
  • Pipeline finished with Success
    8 months ago
    Total: 135s
    #321817
  • Pipeline finished with Canceled
    6 months ago
    Total: 137s
    #370300
  • Pipeline finished with Canceled
    6 months ago
    Total: 83s
    #370302
  • Pipeline finished with Success
    6 months ago
    Total: 147s
    #370303
  • Pipeline finished with Success
    4 months ago
    Total: 146s
    #430947
  • Pipeline finished with Success
    4 months ago
    Total: 152s
    #430950
  • Pipeline finished with Success
    4 months ago
    Total: 151s
    #430964
  • Pipeline finished with Canceled
    4 months ago
    Total: 73s
    #430965
  • Pipeline finished with Success
    4 months ago
    Total: 144s
    #430966
  • Pipeline finished with Success
    4 months ago
    Total: 146s
    #430970
  • Pipeline finished with Success
    4 months ago
    Total: 147s
    #430971
  • Pipeline finished with Success
    4 months ago
    Total: 148s
    #430973
  • Pipeline finished with Success
    4 months ago
    Total: 152s
    #430978
  • Pipeline finished with Success
    4 months ago
    Total: 298s
    #430984
  • Pipeline finished with Success
    4 months ago
    Total: 148s
    #430990
  • Pipeline finished with Failed
    4 months ago
    Total: 149s
    #431027
  • Pipeline finished with Success
    4 months ago
    Total: 244s
    #431035
  • Pipeline finished with Failed
    4 months ago
    Total: 455s
    #431073
  • Pipeline finished with Failed
    4 months ago
    Total: 161s
    #431079
  • Pipeline finished with Canceled
    4 months ago
    Total: 140s
    #431097
  • Pipeline finished with Failed
    4 months ago
    Total: 162s
    #431098
  • Pipeline finished with Failed
    4 months ago
    Total: 182s
    #431106
  • Pipeline finished with Failed
    4 months ago
    Total: 175s
    #431110
  • Pipeline finished with Success
    4 months ago
    Total: 161s
    #431125
  • Status changed to Needs review 4 months ago
  • πŸ‡ΊπŸ‡ΈUnited States trackleft2 Tucson, AZ πŸ‡ΊπŸ‡Έ

    I've added a bunch of PHPUnit Functional tests to ensure the right libraries load at the right time and that some of the selectors we use exist on the page.

  • πŸ‡ΊπŸ‡ΈUnited States trackleft2 Tucson, AZ πŸ‡ΊπŸ‡Έ

    Also resolves πŸ“Œ Fix stylelint issues. Active

  • Pipeline finished with Canceled
    4 months ago
    #431130
  • Pipeline finished with Success
    4 months ago
    Total: 165s
    #431131
  • πŸ‡ΊπŸ‡ΈUnited States trackleft2 Tucson, AZ πŸ‡ΊπŸ‡Έ

    trackleft2 β†’ changed the visibility of the branch 4.x to hidden.

  • πŸ‡ΊπŸ‡ΈUnited States trackleft2 Tucson, AZ πŸ‡ΊπŸ‡Έ
  • πŸ‡ΊπŸ‡ΈUnited States trackleft2 Tucson, AZ πŸ‡ΊπŸ‡Έ

    FYI I've added instructions to this module's .tugboat configuration file to install gin and gin_toolbar module, set gin as the default admin theme, and three environment switchers in order to help us test.

    To view a demo site with a copy of the module installed with this merge request applied, click the view live preview link under this issue's summary.

  • πŸ‡ΊπŸ‡ΈUnited States trackleft2 Tucson, AZ πŸ‡ΊπŸ‡Έ
Production build 0.71.5 2024