Theme color changes are very easy with webpack.mix.js <theme>.libraries.yml

Created on 5 April 2023, about 1 year ago
Updated 20 April 2023, about 1 year ago

Problem/Motivation

Changing out the CSS file for different distributions is often what we desire rather than building an entirely new theme.

Projects like Multisite, Domain and Domain Library Attach leverage a single codebase for use in multiple sites. It would be nice to have an easy way to change theme colors based on Domain, page or even on the Radix Appearance Settings page.

Domain Libraries Attach, which is almost ready for Drupal 10, allows you to load or choose different libraries via an admin interface, meaning two different sets of CSS or JS files. It does seem to have a dependency on the Domain module, which isn't ready for Drupal 10.

Switch Page provides theme switching for different pages, rules or roles. Much of the time a landing page needs a CSS or color change rather than a complete new theme.

Proposed resolution

It is possible to make a very small set of changes to the Radix theme that will produce multiple compiled theme CSS files that could be used with Domain Libraries Attach.

(1) Modify webpack.mix.js as follows:

mix.sass('src/scss/main.style.scss', 'css');
mix.sass('src/scss/secondary.style.scss', 'css');

(2) Add new secondary.style.scss file

Modify your bootstrap variables.scss file, or go crazy changing your other scss files.

(3) Modify custom.libraries.yml as follows:

style:
  version: VERSION
  css:
    theme:
      build/css/main.style.css: {}
      build/css/secondary.style.css: {}
  js:
    build/js/main.script.js: {}
  dependencies:
    - core/drupal
    - core/jquery

Update: A little more logic is needed in order for the theme to choose which style to display. If you don't do anything, the theme will choose the last css above, namely the "/build/css/secondary.style.css"

Executing "run npm watch" compiles without errors and produces new files in the builld/css:
secondary.style.css
secondary.style.css.map

Remaining tasks

(1) Wait for the domain ecosystem to catch up with Drupal 10. It looks like Domain Libraries is almost ready, but that it depends on the Domain module, which is not D10 ready.

(2) Modify the Appearance Settings page interface to permit selecting any available library.

I think offering a Theme Color Selector would be easy to do within Radix, eliminating any other module dependencies.

User interface changes

Add Theme Color Selector to Radix Theme Appearance Settings page until Domain Libaries Attach matures.

✨ Feature request
Status

Closed: won't fix

Version

5.0

Component

User interface

Created by

πŸ‡ΊπŸ‡ΈUnited States tstermitz

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

Comments & Activities

  • Issue created by @tstermitz
  • πŸ‡ΊπŸ‡ΈUnited States tstermitz
  • πŸ‡ΊπŸ‡ΈUnited States tstermitz
  • πŸ‡ΊπŸ‡ΈUnited States tstermitz
  • πŸ‡ΊπŸ‡ΈUnited States tstermitz
  • πŸ‡ΊπŸ‡ΈUnited States tstermitz
  • Thanks for the work/idea Tom, I'm not sure if this is a general enough use case that needs to go into the base theme, this might rather confuse people if we add it there, it could be useful to have a workflow of how it should be done in this issue queue or a blog post

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

    The better title for this suggestion is that webpack.mix.js easily compiles multiple CSS files. Then the theme.library.xml file declares which one to be used. Or else an Appearance Page admin button could choose, so that the CSS template choice is in the DB, while the code base remains in git.

    The Bootstrap file structure and philosophy encourages you to make a few finite changes to a variable override file. If you set up your files logically you can "re-skin" a website via some very minor changes to three or four color variables.

    I have two immediate use cases.

    (1) I have three websites with different colors and logos, but the code base is identical but for the theming. I'm happy with everything else: structure, page-sizes, layout, views, menus. I could maintain three completely different themes, but my life will be infinitely easier if I mange updates on one local, and do a git pull to each remote site. I become a "yeoman wordpress developer" and sell ten or fifteen websites in 15 different brand colors, that I could maintain with minimal effort.

    (2) Another typical use case would be to have a landing page or site sub-section, let's say members vs anonymous. In my case I was looking for a way to brand a subsection of my website for a separate business purpose.

    This led me to several modules in the domain ecosystem, but as I mentioned, they are not ready for Drupal 10. Maybe Domain Library Attach almost does what I suggested, providing an admin interface that selects Primary, Secondary or Tertiary CSS library.

    (I'm not that guy, as I'm a mere "front-end" developer, and only mess occasionally with twig templates.)

  • πŸ‡²πŸ‡ΎMalaysia ckng

    Agreed this is an "extension" to what the theme currently support. Only worth as a documentation.

    As for wildcard support in Laravel Mix, this is still not supported.
    See https://github.com/laravel-mix/laravel-mix/issues/3300, https://github.com/laravel-mix/laravel-mix/issues/982.

    A workaround is proposed https://github.com/laravel-mix/laravel-mix/issues/982#issuecomment-45485..., reproduced here

    var fs = require('fs');
    var path = require('path');
    var files = fs.readdirSync('./resources/sass/pages');
    
    for (var i=0; i<files.length; i++) {
      if(path.extname(files[i]) == '.scss') {
        mix.sass('resources/sass/pages/' + files[i], 'public/css/pages');
      }
    }
  • Status changed to Closed: won't fix about 1 year ago
  • Closing this ticket as "Won't fix" but would be nice if you can document a workaround here or a blog post / etc...
    but thanks

Production build 0.69.0 2024