Skip to content

New Tailwind theme #2961

Open
Open

Description

@jaredpalmer

💥 Proposal

This may be belong in the Infima repo, but I strongly suggest Docusaurus consider Tailwind. Tailwind already solves for theming out of the box. It also gives developers much more freedom. You can still re-implement Infima's core classes with tailwind, so this actually wouldn't need to be a breaking change.

The benefits:

  • Smaller CSS footprint
  • Better theming
  • Less custom CSS needed on other pages

Next Steps

  • Start on a new Tailwind preset
  • Get it to feature-parity with classic theme

Have you read the Contributing Guidelines on issues?

Yes

Activity

  1. added
    status: needs triage
    proposal
    on Jun 18, 2020
  2. changed the title [-]Move to Tailwind[/-] [+]Move to Tailwind / Tailwind Preset[/+] on Jun 18, 2020
  3. slorber commented on Jun 18, 2020

    @slorber
    Collaborator

    Hi,

    I'm not very familar with Tailwind yet.

    Currently Infima is a css lib with a few postcss. But it also includes some lightweight vanilla JS components

    If I understand correctly, you mean we could implement current Infima CSS classes as a Tailwind preset, instead of the existing css/postcss ?

    So we would publish that preset and enable users to extend / customize it more easily?


    I see the value of Tailwind, and a similar goal of styling co-located to the markup.

    Its scope is larger than current Infima:

    • People having complex D2 sites will probably need additional one-of CSS styles that's not provided by Infima (afaik it also happens to Tailwind users). Atomic/utility CSS will grow slower than hand-made CSS.
    • There are also simple D2 sites that do not require additional styles. Can we use Tailwind without adding unnecessary CSS to these sites?
  4. added
    status: needs more information
    and removed
    status: needs triage
    on Jun 18, 2020
  5. jaredpalmer commented on Jun 18, 2020

    @jaredpalmer
    Author

    Yes you can and should always purge unused tailwind css. Only the classes actually used in the codebase are kept.

  6. kvnxiao commented on Nov 29, 2020

    @kvnxiao

    Hoping this is helpful information, as I got TailwindCSS v2 to properly work with the PostCSS config that Docusaurus v2 already uses.

    I'm using a custom configureWebpack plugin in a file plugins/my-loaders/index.js like so:

    module.exports = function (context, options) {
      return {
        name: 'postcss-tailwindcss-loader',
        configureWebpack(config, isServer, utils) {
          return {
            module: {
              rules: [
                {
                  test: /\.css$/,
                  use: [
                    {
                      loader: require.resolve('postcss-loader'),
                      options: {
                        ident: 'postcss',
                        plugins: () => [
                          require('postcss-import'),
                          require('tailwindcss'),
                          require('postcss-preset-env')({
                            autoprefixer: {
                              flexbox: 'no-2009',
                            },
                            stage: 4,
                          }),
                        ],
                      },
                    },
                  ],
                },
              ],
            },
          }
        },
      }
    }

    with package.json referencing a local module dependency "my-loaders": "file:plugins/my-loaders", and docusaurus.config.js plugins entry set up as plugins: ['my-loaders'],

    And a tailwind.config.js like so:

    module.exports = {
      purge: ['./src/**/*.html', './src/**/*.js', './src/**/*.tsx'],
      darkMode: false, // or 'media' or 'class'
      theme: {
        extend: {},
      },
      variants: {
        extend: {},
      },
      plugins: [],
    }

    A reference commit summarizing the above info: Discord4J/documentation@2019279

    Note that the PostCSS version in which Docusaurus v2 currently uses is 7.0.XX, while the latest version of PostCSS is 8.0.X and TailwindCSS v2 by default uses PostCSS V8. I had to use a v7 compatibility install of TailwindCSS v2 for this to all work: "tailwindcss": "npm:@tailwindcss/postcss7-compat" in package.json dependencies.

    I'm thinking that maybe tailwindcss can be added directly to the https://github.com/facebook/docusaurus/blob/master/packages/docusaurus/src/webpack/utils.ts#L64-L71 plugin block for getStyleLoaders with PurgeCSS configured for first class support. Since tailwindcss has PurgeCSS enabled, users who don't use tailwindcss will never see it or even know because all the tailwindcss related css rules will be removed. And there should be no performance penalties in terms of time spent compiling for users who don't import tailwindcss into their .css files or index.js/index.tsx.


    EDIT: With the newer versions of Docusaurus v2 supporting a configurePostCss option in a plugin, the above code snippet should be changed to the following:

    module.exports = function (context, options) {
      return {
        name: 'postcss-tailwindcss-loader',
        configurePostCss(postcssOptions) {
          postcssOptions.plugins.push(
            require('postcss-import'),
            require('tailwindcss'),
            require('postcss-preset-env')({
              autoprefixer: {
                flexbox: 'no-2009',
              },
              stage: 4,
            }),
          )
          return postcssOptions
        },
      }
    }
  7. slorber commented on Nov 30, 2020

    @slorber
    Collaborator

    Thanks for these infos.

    We'll look at this more in depth soon, as we'll work on our theme gallery (#3522) and Tailwind support is part of it

  8. clairefro commented on Dec 20, 2020

    @clairefro

    @kvnxiao I tried your config for tailwind and can't seem to get past this error (looks like it's still looking for postcss 8):

    Error: Cannot find module 'postcss-import'
    

    Do you have a working repo I could take a look at?

  9. kvnxiao commented on Dec 20, 2020

    @kvnxiao
  10. nahtnam commented on Dec 24, 2020

    @nahtnam

    @kvnxiao's solution is great! I just want to add an improvement to it. If you add the following to your tailwind.config.js: important: '#tailwind', then it will not affect the docusaurus pages. Then all you need to do is add <div id="tailwind"> around the pages that you want to use tailwind on!

  11. wibed commented on Dec 27, 2020

    @wibed

    @kvnxiao
    according to postcss/postcss-import#435
    postcss-import@^12 (current ^14) is based on postcss8

    also the install guide on tailwindcss does not mention the postcss-import module
    https://tailwindcss.com/docs/installation#post-css-7-compatibility-build

    why did you made it a dependency on your module?

  12. 49 remaining items

  13. Karkunow commented on Sep 12, 2024

    @Karkunow

    Have this one been resolved?

  14. slorber commented on Sep 13, 2024

    @slorber
    Collaborator

    This hasn't been resolved and is not currently being worked on.

    I'm currently working on making Docusaurus much faster, notably by adopting Rust tools like Rspack, SWC, and LightningCSS.

    For that, we need to get rid of PostCSS, which is currently needed for Tailwind. In v4 with Oxide Tailwind will also adopt LightningCSS (details).

    We'll look into adding first-class Tailwind support, and a Tailwind theme, once we both successfully adopt LightningCSS.

  15. cemerick commented on Sep 26, 2024

    @cemerick
    Contributor

    @kvnxiao's (revised/edited) recipe in #2961 (comment) works quite well (better to snoop around in the Discord4J project they linked IMO to see how everything is laid out), with the addition that @tailwind entries need to be added to one's custom.css file, e.g.

    @tailwind base;
    @tailwind components;
    @tailwind utilities;
    

    This is standard fare for a tailwind project ofc, but I neglected to do this at first (I guess I thought postcss would somehow roll things up without those rules), and so thought the tailwind+docusaurus integration wasn't working for a while.

  16. rjvim commented on Oct 9, 2024

    @rjvim

    I have setup Tailwind recently on Docusaurus 3.5, here is the commit: betalectic/betalectic.github.io@55a5c53

    Thanks to various contributors above.

    • Preflight is loaded using Injection
    • Dark theme is working

    cc @Abhishek-Beta


    Change on 16th Oct, need to add following before for before/after of tailwindcss to work

    In plugin use the latest tailwind preflight file:

    injectHtmlTags() {
      return {
        headTags: [
          {
            tagName: "link",
            attributes: {
              rel: "stylesheet",
              href: "https://unpkg.com/[email protected]/src/css/preflight.css",
            },
          },
        ],
      };
    },
  17. clicktodev commented on Feb 1, 2025

    @clicktodev
    Contributor

    Looks like this is approachable now that tw v4 is officially out.

    Nextra uses tw under the hood so it might a good idea to take a look at their implementation

  18. artemkovalyov commented on Apr 2, 2025

    @artemkovalyov
    Contributor

    Tailwind 4 would be a great variant or even replacement for utility first approach. Having it as an option would be a killer. Infima is good but feels underweight for the range of use-cases Docusaurus has seen use for. Some well-positioned framework should work just fine and ensure so needed extensibility.

  19. shirakaba commented on Apr 17, 2025

    @shirakaba

    So desparate for an official Tailwind theme. Infima overrides everything. I can't even do something as simple as expressing m-0 for resetting an <h1> to zero margin, because Infima's invasive styles are on a higher-priority layer than Tailwind. The only way to outprioritise Infima is to use inline styles, which means just throwing away Tailwind.

    Image Image
  20. clicktodev commented on Apr 17, 2025

    @clicktodev
    Contributor

    So desparate for an official Tailwind theme. Infima overrides everything. I can't even do something as simple as expressing m-0 for resetting an <h1> to zero margin, because Infima's invasive styles are on a higher-priority layer than Tailwind. The only way to outprioritise Infima is to use inline styles, which means just throwing away Tailwind.

    Image Image

    Try setting important to true in the tw js config

  21. slorber commented on Apr 17, 2025

    @slorber
    Collaborator

    Creating a new Tailwind theme and overriding Infima things are 2 separate problems.

    If Infima default/global styles are annoying, the issue to track is #6032

    We may find a CSS solution that doesn't require introducing Tailwind. Creating a Tailwind theme remains a goal, but people who don't want Tailwind should also be able to customize things without being annoyed by Infima.

  22. clicktodev commented on Apr 17, 2025

    @clicktodev
    Contributor

    So desparate for an official Tailwind theme. Infima overrides everything. I can't even do something as simple as expressing m-0 for resetting an <h1> to zero margin, because Infima's invasive styles are on a higher-priority layer than Tailwind. The only way to outprioritise Infima is to use inline styles, which means just throwing away Tailwind.

    Image Image

    set important to true and disable tailwind preflight

    Image

  23. clicktodev commented on Apr 17, 2025

    @clicktodev
    Contributor

    Also add a prefix in the config to avoid conflicting class names

  24. slorber commented on Feb 19, 2026

    @slorber
    Collaborator

    FYI Tailwind v4.2 just released a new webpack plugin (actually, it's a webpack loader): https://github.com/tailwindlabs/tailwindcss/releases/tag/v4.2.0

    Apparently, using this integration is much faster than using postcss for frameworks like Next.js (with both webpack and Turbopack)

    If anyone gives this plugin a try, please share how to set it up and if it works well.

  25. webbertakken commented on Feb 19, 2026

    @webbertakken
    Contributor

    Will give it a try after work

  26. webbertakken commented on Feb 19, 2026

    @webbertakken
    Contributor

    Had a quick look. Seems to work fine, without any warnings.

    There's a couple of postcss related plugins configured in docusaurus webpack that need to be skipped in favour of using the new plugin (including lightning CSS, which is the core engine for Tailwind 4).

    Docusaurus doesn't offer a hook to swap out those plugins, so a small custom plugin is required.

    The approach I used is to use configureWebpack() (instead of the current configurePostCss()) to walk the webpack rules and replace postcss-loader with @tailwindcss/webpack.

    Details

    Replace the PostCSS-based Tailwind integration with the new dedicated webpack loader, eliminating PostCSS as a middleman.

    Deps changed
    yarn add -D @tailwindcss/webpack@latest tailwindcss@latest
    yarn remove @tailwindcss/postcss postcss-import autoprefixer postcss @types/postcss-import
    Configuring the plugin
    export default {
      // ...other config options
    
      plugins: [
        tailwindPlugin,
    
        // ... other plugins
      ],
    }
    The plugin (before)
    function tailwindPlugin() {
      return {
        name: 'tailwind-plugin',
        configurePostCss(postcssOptions) {
          postcssOptions.plugins = ['postcss-import', '@tailwindcss/postcss', 'autoprefixer']
          return postcssOptions
        },
      }
    }
    
    module.exports = tailwindPlugin
    The plugin (after)

    src/plugins/tailwind-config.cjs — replace configurePostCss() with configureWebpack()

    /**
     * Docusaurus plugin that replaces postcss-loader with @tailwindcss/webpack
     * in the CSS loader chain. The webpack loader handles Tailwind compilation
     * directly (via the Rust-based Oxide scanner) and uses Lightning CSS for
     * optimisation, eliminating the need for PostCSS, postcss-import, and autoprefixer.
     *
     * Non-Tailwind CSS files pass through unchanged (the loader bails out early
     * when no Tailwind directives are detected).
     */
    function tailwindPlugin() {
      return {
        name: 'tailwind-plugin',
        configureWebpack(config) {
          replacePostcssLoader(config.module?.rules)
          return {}
        },
      }
    }
    
    /** Recursively walk webpack rules and replace postcss-loader with @tailwindcss/webpack. */
    function replacePostcssLoader(rules) {
      if (!Array.isArray(rules)) return
    
      for (const rule of rules) {
        if (!rule) continue
        if (rule.oneOf) replacePostcssLoader(rule.oneOf)
        if (rule.rules) replacePostcssLoader(rule.rules)
    
        if (Array.isArray(rule.use)) {
          for (let i = 0; i < rule.use.length; i++) {
            const loader = rule.use[i]
            const loaderPath = typeof loader === 'string' ? loader : loader?.loader
            if (loaderPath && loaderPath.includes('postcss-loader')) {
              rule.use[i] = '@tailwindcss/webpack'
            }
          }
        }
      }
    }
    
    module.exports = tailwindPlugin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    proposal

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions