Skip to content

Instantly share code, notes, and snippets.

@Spiritbocs
Created September 5, 2024 08:12
Show Gist options
  • Select an option

  • Save Spiritbocs/a3aa1fa16d55f0bf48aa53abbbce48c8 to your computer and use it in GitHub Desktop.

Select an option

Save Spiritbocs/a3aa1fa16d55f0bf48aa53abbbce48c8 to your computer and use it in GitHub Desktop.
How Tailwind Play Works

Understanding the Differences Between Tailwind Play and Next.js + TailwindCSS Setup

Tailwind Play Overview

The website you're referring to — Tailwind Play — is a special, customized environment created by Tailwind Labs to showcase and experiment with TailwindCSS in an in-browser code editor. It allows you to write and test TailwindCSS classes directly in an editor and see the results in real-time.

How Tailwind Play Works:

  1. Live Code Editor:

    • Tailwind Play has a live editor where users can input HTML and Tailwind utility classes. The platform instantly renders the results in an embedded preview pane.
    • This is a custom-built environment where they use JavaScript to interpret the Tailwind classes you type and dynamically apply them in real-time.
  2. On-Demand TailwindCSS Generation:

    • As you type utility classes in Tailwind Play, it dynamically generates the CSS required for those classes.
    • TailwindCSS is not precompiled — it's generated on the fly using Tailwind's JIT (Just-In-Time) engine. This compiles the classes in real-time and injects them into the page.
  3. JIT Mode in Tailwind Play:

    • TailwindCSS JIT allows for real-time utility class compilation. As you add new classes like bg-red-500 or p-4, Tailwind Play regenerates the styles and immediately applies them.
  4. Custom JavaScript Handling:

    • The platform has custom JavaScript that binds the input from the editor to a TailwindCSS parser. As you add classes, Tailwind Play dynamically regenerates and applies the CSS without a rebuild step.
  5. Custom Integration:

    • Tailwind Play is an in-browser real-time sandbox, unlike a traditional Next.js setup. It behaves like a code playground or IDE, where you're directly manipulating the DOM with Tailwind classes in real-time.

Tailwind Play vs. Your Project

You're trying to mimic Tailwind Play, but your setup with Next.js + TailwindCSS differs because:

  • Tailwind Play dynamically generates and applies classes in real-time.
  • Your Next.js + TailwindCSS project builds the styles during the build or dev process, and you'll need to use JIT mode to mimic real-time CSS generation.

Option 1: Use TailwindCSS with JIT Mode

The closest way to mimic Tailwind Play in a production app like Next.js is by enabling Tailwind JIT mode. This compiles only the CSS for the classes you use and updates them as you modify your code.

How to Enable JIT Mode:

  1. Update tailwind.config.js: Add the following to enable JIT mode in your Tailwind config:

    // tailwind.config.js
    module.exports = {
      mode: 'jit',
      purge: ['./src/**/*.{js,ts,jsx,tsx}', './public/index.html'],
      theme: {
        extend: {},
      },
      plugins: [],
    };
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment