Git Product home page Git Product logo

Comments (5)

skolmer avatar skolmer commented on September 2, 2024 1

There is a way to build multiple themes in one project using less-loader:

This is the minimum configuration required for webpack:

{
  resolve: {
    alias: {
      '../../theme.config$': path.join(paths.appSrc, '/semantic-ui/theme.config')
    }
  },
  module: {
    rules: [
      {
        test: /\.less$/,
        use: [
          {
            loader: "less-loader",
            options: {
              additionalData: (content, loaderContext) => {
                const { resourceQuery } = loaderContext;
                const theme = /[?&]theme=([^?&]+)/.exec(resourceQuery);
                if (theme) {
                  console.log("building custom theme: " + theme[1]);
                  return "@custom_theme: " + theme[1] + ";" + content;
                }

                return content;
              },
            },
          },
        ],
      },
    ],
  },
}

or use craco config:

const CracoLessPlugin = require("craco-less");

module.exports = {
  plugins: [{ 
    plugin: CracoLessPlugin,
    options: {
      lessLoaderOptions: {
        additionalData: (content, loaderContext) => {
          const { resourceQuery } = loaderContext;
          const theme = /[?&]theme=([^?&]+)/.exec(resourceQuery);
          if (theme) {
            console.log("building custom theme: " + theme[1]);
            return "@custom_theme: " + theme[1] + ";" + content;
          }

          return content;
        }
     }
   }
  }]
};

In your theme.config you can now use multiple site directories or do other customizations based on the loaded theme:

/* Path to site override folder */
@siteFolder  : '../../src/semantic-ui/@{custom_theme}/site';

Now you are able to lazy load your theme at runtime by adding a JS module for each theme like this one (./src/semantic-ui/dark.js):

import 'semantic-ui-less/semantic.less?theme=dark';

To load the theme based on a query string for example you could do this in your main module:

const urlParams = new URLSearchParams(window.location.search);
const theme = urlParams.get('theme');

if (theme === 'dark') {
  import('./semantic-ui/dark.js').then(render).catch(console.error);
} else {
  import('./semantic-ui/light.js').then(render).catch(console.error);
}

const render = () => {
  // theme is now loaded. render your app...
}

from semantic-ui-less.

 avatar commented on September 2, 2024

Thanks for the question. Have the same problem here... How build many themes using Webpack in order to use theme with a Theme selector in a React app ?

from semantic-ui-less.

aautem avatar aautem commented on September 2, 2024

@andrewplummer Were you ever able to find a solution for this?

from semantic-ui-less.

andrewplummer avatar andrewplummer commented on September 2, 2024

@aautem not really.. I find overriding the theming cumbersome anyway so I've been manually importing the specific styles I need for "functional styles" (like hiding/showing modals) and writing everything else from scratch

from semantic-ui-less.

alolis avatar alolis commented on September 2, 2024

@skolmer, in case you are still watching this, any insights on how to make your proposed solution work dynamically on re-render (based on some component prop for example) instead of page reload?

from semantic-ui-less.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.