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

Created on 9 August 2024, 4 months ago
Updated 19 August 2024, 4 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
    about 2 months ago
    Total: 135s
    #321817
  • Pipeline finished with Canceled
    6 days ago
    Total: 137s
    #370300
  • Pipeline finished with Canceled
    6 days ago
    Total: 83s
    #370302
  • Pipeline finished with Success
    6 days ago
    Total: 147s
    #370303
Production build 0.71.5 2024