Library gin_custom_css should depend on gin/gin for higher priority

Created on 25 September 2024, 7 months ago

Problem/Motivation

The gin theme allows to inject user defined CSS overrides via the gin_custom.css file. But due loading order the overrides might be overwritten by Gin's own styles. IMHO custom specified styles are inherently designed to override base styles and therefore - given the same specifity - custom CSS should win over base CSS.

Steps to reproduce

Create a gin_custom.css with h3 {font-weight: 900;}. It will be overwritten by Gin's own h3 { font-size: var(--gin-font-size-h3); }

Proposed resolution

Make libarary gin/gin_custom_css dependant on libarary gin/gin to enforce a later loading and higher priority.

Alternatively, we could also try to use the new native CSS @layer gin_base, gin_custom_css; at-rule to get a similar effect.

Not a solution: specifity wars.

🐛 Bug report
Status

Active

Version

3.0

Component

Code

Created by

🇦🇹Austria hudri Austria

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @hudri
  • Merge request !506Make library gin/gin_custom_css depend on gin/gin → (Merged) created by hudri
  • Pipeline finished with Success
    7 months ago
    Total: 224s
    #292163
  • 🇧🇷Brazil stephencamilo curitiba

    #2 patch still not being able to override gin/gin.

  • 🇧🇪Belgium Selfirian

    Yes, I've been trying to fix this. I don't think that the proposed solution it the right way to tackle this. The issue is that the weight of the attached css file get's ignored. This might actually be a drupal core bug.

  • 🇬🇷Greece vensires

    If it's a Drupal core issue, it should be caused by the AssetResolver::getCssAssets() method somewhere, though it seems to follow the exact same logic with the ::getJsAssets() method which - from what I know - hasn't caused any issues.

    There might exist a logic that not-processed CSS files get loaded first or at least before the rest of the aggregated files of the same module.

    Check the attached screenshot. The red arrow points to our gin-custom.css file whereas the blue button points to the aggregated CSS file containing the rest of Gin's code.

    I suggest we read CSS File Organization and 🐛 LibraryDiscoveryParser overwrites theme CSS group ordering Active before we come up with another issue in Drupal Core.

    Notice: Setting as "Needs Review" in order to attract more people for review of how we should go on from here.

  • 🇧🇪Belgium Selfirian

    Ok I found a fix, but I'm unable to commit the modification to the MR.

    Here's what I did and works.

    in the library file,

    # Custom CSS
    
    gin_custom_css:
      css:
        theme:
          public://gin-custom.css: { preprocess: true, minified: true, weight: 50 }

    Because the maximum weight for css is 50 according to the documentation

    and then I changed the the gin.theme

    /**
     * Set Gin CSS on top of all other CSS files.
     */
    function gin_css_alter(&$css, $assets) {
      // Use anything greater than 100 to have it load after the theme
      // as CSS_AGGREGATE_THEME is set to 100.
      // Let's be on the safe side and assign a high number to it.
      $base_css = \Drupal::service('extension.list.theme')->getPath('gin') . '/dist/css/base/gin.css';
    
      if (isset($css[$base_css])) {
        $css[$base_css]['group'] = 0;
      }
    }
    

    Because it was set to 200 so it came after everything else.

  • Pipeline finished with Failed
    7 months ago
    Total: 205s
    #295715
  • 🇬🇷Greece vensires

    @selfirian, you suggestion was great, thanks a lot!

    Though, since gin.css had this group for a reason I supposed, instead of setting it to 0, I preferred to do the following:

    // The gin-custom.css file should be loaded just after our gin.css file.
    $custom_css = 'public://gin-custom.css';
    if (isset($css[$custom_css])) {
      $css[$custom_css]['group'] = 201;
    }
    

    This change has the following result:

    Please review.

  • 🇮🇪Ireland lostcarpark

    There are a couple of minor issues reported by PHPCS, on the gin.theme file.

    Just remove the spaces at the end of line 23, and the extra blank line at the end of the file, and push, and everything should be fine.

  • Pipeline finished with Success
    7 months ago
    Total: 213s
    #297837
  • 🇬🇷Greece vensires

    Thank you @lostcarpark. PHPCS issues are now fixed too.

  • 🇮🇪Ireland lostcarpark

    I carried out a test in DrupalPod, and created a gin-custom.css file in my files directory, and verified that styles from it took priority over Gin default styles:

    This seems to fix the problems, and as tests are passing, I think this can go to RTBC.

  • 🇧🇪Belgium Selfirian

    Wouldn't it be better if the custom css related code is all grouped in the inlcude/page.theme?

  • 🇬🇷Greece vensires

    I think that's a preference issue. I left it where I found it: https://git.drupalcode.org/project/gin/-/blob/8.x-3.x/gin.theme?ref_type.... I don't honestly think this kind of change should be addressed in this issue - seems out of scope. But whatever the maintainers decide.

  • 🇧🇪Belgium Selfirian

    Yes, the bug is fixed that's the most important part. Thanks for the help.

  • 🇨🇭Switzerland saschaeggi Zurich

    Thanks y'all 👏

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024